private ChessSquare[] randomSquares(byte length)
 // Generates an array with the specified length such that no square has the same
 // coordinates as another. length must not exceed 64.
 {
     if (length > 64)
     {
         throw new ArgumentException("length > 64");
     }
     ChessSquare[] squares = new ChessSquare[length];
     for (int i = 0; i < length; ++i)
     {
         bool cont = true;
         while (cont)
         {
             squares[i] = SQUARES[RANDOM.Next(8), RANDOM.Next(8)];
             cont       = false;
             for (int j = 0; j < i; ++j)
             {
                 if (squares[i] == squares[j])
                 {
                     cont = true;
                     break;
                 }
             }
         }
     }
     return(squares);
 }
 public ChessLeaper(ChessSquare square) : base(square)
 {
     if (POSSIBLE_RELATIVE_MOVES.GetUpperBound(1) != 1) // If not a [_,2] array
     {
         throw new Exception(
                   "POSSIBLE_RELATIVE_MOVES's 2nd dimension does not have length 2");
     }
 }
 public override void FindMoves(ChessSquare[,] squares)
 {
     this.Moves.Clear();
     for (int p = MAX_ATTACKS - 1; p >= 0; --p)
     {
         int c = this.Square.COL + POSSIBLE_RELATIVE_MOVES[p, 0];
         int r = this.Square.ROW + POSSIBLE_RELATIVE_MOVES[p, 1];
         if (c >= 0 && r >= 0 && c <= 7 && r <= 7)
         {
             ChessSquare square = squares[c, r];
             if (square.Piece == null || square.Piece.WHITE != this.WHITE)
             {
                 this.Moves.Add(square);
             }
         }
     }
 }
Esempio n. 4
0
 public override void FindMoves(ChessSquare[,] squares)
 {
     this.Moves.Clear();
     for (int p = 0; p <= 7; ++p)
     {
         int c = this.Square.COL + DIRECTIONS[p, 0];
         int r = this.Square.ROW + DIRECTIONS[p, 1];
         if (c >= 0 && r >= 0 && c <= 7 && r <= 7)
         {
             ChessSquare square = squares[c, r];
             if (square.Piece == null || !square.Piece.WHITE)
             {
                 this.Moves.Add(square);
             }
         }
     }
 }
 public void Move(ChessSquare newSquare)
 // Removes this ChessPiece from its current ChessSquare and moves it to newSquare. If a
 // piece is on newSquare, that piece is replaced (captured) by this piece. The two
 // ChessSquares' images are automatically updated in that class through assignment.
 {
     if (this.Square != null)
     {
         this.Square.Piece = null;
     }
     if (newSquare.Piece != null)
     {
         newSquare.Piece.Square = null;
     }
     this.Square     = newSquare;
     newSquare.Piece = this;
     // The first 4 lines are just as important as the last 2. Omitting them caused errors.
 }
        public void TakeBlackTurn(out bool gameOver)
        // Takes Black's turn, moving its king. Checks for and handles checkmate and stalemate.
        // Handles draw for King vs King but not for any other situation such as King and Bishop
        // vs King. In checkmate, stalemate, or draw, sets gameOver to true; otherwise, sets it to
        // false. Does not enable/disable squares.
        {
            ++numMoves;
            AnalyzeAttacks();

            BlackKing.FindMoves(this.SQUARES);
            ChessSquare BlackKingChoice = BlackKing.ChooseMove();

            if (BlackKingChoice != null)
            {
                if (BlackKingChoice.Piece != null)
                {
                    White.Remove(BlackKingChoice.Piece);
                }
                BlackKing.Move(BlackKingChoice);

                if (White.Count == 1) // Draw by lack of material
                {
                    // This would be slightly better with a custom message box with a bigger font.
                    MessageBox.Show("Draw by lack of material 😞\nIn " + numMoves + " moves");
                    gameOver = true;
                }
                else
                {
                    AnalyzeAttacks();
                    gameOver = false;
                }
            }
            else // Black King cannot move, is in checkmate or stalemate
            {
                if (BlackKing.Square.WhiteAttacked)
                {
                    MessageBox.Show("Checkmate! 😁\nIn " + numMoves + " moves");
                }
                else
                {
                    MessageBox.Show("Stalemate 😞\nIn " + numMoves + " moves");
                }
                gameOver = true;
            }
        }
        protected void FindAttacksInDirection(ChessSquare[,] squares, byte dir)
        // Similar to FindMovesInDirection. Sets WhiteAttacked or BlackAttacked to true for all
        // attacked squares in direction dir. The line of attack is blocked by a piece of the same
        // color but, unlike the line of moves, continues beyond a piece of the opposite color.
        // This method is implemented almost exactly the same as FindMovesInDirection. Only the
        // middle of the body of the while loop is different.
        {
            if (dir < 0)
            {
                throw new ArgumentException("dir < 0");
            }
            if (dir > 7)
            {
                throw new ArgumentException("dir > 7");
            }

            // (dc,dr) = change in coordinates per square in line
            sbyte dc = DIRECTIONS[dir, 0];
            sbyte dr = DIRECTIONS[dir, 1];

            int c = this.Square.COL + dc;
            int r = this.Square.ROW + dr;

            while (c >= 0 && r >= 0 && c <= 7 && r <= 7)
            {
                ChessSquare square = squares[c, r];

                if (WHITE)
                {
                    square.WhiteAttacked = true;
                }
                else
                {
                    square.BlackAttacked = true;
                }

                if (square.Piece != null && (square.Piece.WHITE == this.WHITE))
                {
                    break;
                }

                c += dc;
                r += dr;
            }
        }
        protected void FindMovesInDirection(ChessSquare[,] squares, byte dir)
        // Adds to this.Moves (without clearing this.Moves first, unlike FindMoves()) by finding
        // the squares along an orthogonal or diagonal ray in direction dir that a piece moving
        // in that direction would be able to move to. In other words, finds the squares that a
        // queen moving in direction dir would be able to move to without being blocked or leaving
        // the board. This method is only applicable to queens, rooks, and bishops.
        // dir stores a value from 0 to 7. From square (3,3) in the center, dir 0 points to (7,3),
        // dir 1 points to (7,7), ..., dir 7 points to (7,0). In other words, dir d corresponds to
        // the vector represented by the dth pair in DIRECTIONS, where column comes before row in
        // the pair.
        {
            if (dir < 0)
            {
                throw new ArgumentException("dir < 0");
            }
            if (dir > 7)
            {
                throw new ArgumentException("dir > 7");
            }

            // (dc,dr) = change in coordinates per square in line
            sbyte dc = DIRECTIONS[dir, 0];
            sbyte dr = DIRECTIONS[dir, 1];

            int c = this.Square.COL + dc;
            int r = this.Square.ROW + dr;

            while (c >= 0 && r >= 0 && c <= 7 && r <= 7)
            {
                ChessSquare square = squares[c, r];
                if (square.Piece != null && (square.Piece.WHITE == this.WHITE))
                {
                    break;
                }
                this.Moves.Add(square);
                if (square.Piece != null)
                {
                    break;
                }
                c += dc;
                r += dr;
            }
        }
 public WhiteChampion(ChessSquare square) : base(square)
 {
 }
Esempio n. 10
0
 public WhiteBishop(ChessSquare square) : base(square)
 {
 }
Esempio n. 11
0
 public WhiteMann(ChessSquare square) : base(square)
 {
 }
 public WhiteKnight(ChessSquare square) : base(square)
 {
 }
Esempio n. 13
0
 public BlackKing(ChessSquare square) : base(square)
 {
     this.RANDOM = new Random();
 }
        // Finds what Squares this piece can move to. Sets this.Moves to a list of all and only
        // those squares.

        public ChessPiece(ChessSquare square)
        {
            this.Move(square);
            this.Moves = new List <ChessSquare>(capacity: MAX_ATTACKS);
        }
 public WhiteWazir(ChessSquare square) : base(square)
 {
 }
Esempio n. 16
0
 public WhiteKing(ChessSquare square) : base(square)
 {
 }
 public WhiteRook(ChessSquare square) : base(square)
 {
 }
Esempio n. 18
0
 public WhiteFerz(ChessSquare square) : base(square)
 {
 }
Esempio n. 19
0
 public WhiteWizard(ChessSquare square) : base(square)
 {
 }
 public WhiteQueen(ChessSquare square) : base(square)
 {
 }