コード例 #1
0
ファイル: ChessBoard.cs プロジェクト: banfstory/Chess-with-AI
        private void TestStaleMate() // Move white queen to E3 to perform stalemate (Used for testing purposes)
        {
            PvP         = true;
            br1.Visible = false; bkn1.Visible = false; bb1.Visible = false; bq.Visible = false; bk.Visible = false; bb2.Visible = false; bkn2.Visible = false; br2.Visible = false;
            bp1.Visible = false; bp2.Visible = false; bp3.Visible = false; bp4.Visible = false; bp5.Visible = false; bp6.Visible = false; bp7.Visible = false; bp8.Visible = false;
            wr1.Visible = false; wkn1.Visible = false; wb1.Visible = false; wq.Visible = false; wk.Visible = false; wb2.Visible = false; wkn2.Visible = false; wr2.Visible = false;
            wp1.Visible = false; wp2.Visible = false; wp3.Visible = false; wp4.Visible = false; wp5.Visible = false; wp6.Visible = false; wp7.Visible = false; wp8.Visible = false;

            board = new PictureBox[8][];
            for (int i = 0; i < board.Length; i++)
            {
                board[i] = new PictureBox[8];
            }
            board[0][2] = wq; board[0][5] = bb1; board[0][6] = bkn1; board[0][7] = br1; board[1][4] = bp1; board[1][6] = bp2; board[1][7] = bq; board[2][5] = bp3;
            board[2][6] = bk; board[2][7] = br2; board[3][7] = bp4; board[7][0] = wk; board[6][0] = wp1; board[6][1] = wp2; board[7][2] = wr1; board[4][7] = wp3;
            for (int y = 0; y < board.Length; y++)
            {
                for (int x = 0; x < board[y].Length; x++)
                {
                    if (board[y][x] != null)
                    {
                        board[y][x].Visible = true;
                        PieceDetails.movePiece(y, x, board[y][x]);
                    }
                }
            }
        }
コード例 #2
0
ファイル: ChessBoard.cs プロジェクト: banfstory/Chess-with-AI
 public void ResetGame() // reset chess game
 {
     resetHighlightedPieces();
     History             = new History();
     currentState        = gameState.Normal;
     turn                = true;
     selectedpiece       = null;
     alreadySelected     = false;
     PossiblePieceToTake = new List <PictureBox>();
     PvE          = false;
     PvP          = false;
     AIColor      = false;
     AIComplexity = 2;
     board        = new PictureBox[8][];
     for (int i = 0; i < board.Length; i++)
     {
         board[i] = new PictureBox[8];
     }
     board[0][0] = br1; board[0][1] = bkn1; board[0][2] = bb1; board[0][3] = bq; board[0][4] = bk; board[0][5] = bb2; board[0][6] = bkn2; board[0][7] = br2;
     board[1][0] = bp1; board[1][1] = bp2; board[1][2] = bp3; board[1][3] = bp4; board[1][4] = bp5; board[1][5] = bp6; board[1][6] = bp7; board[1][7] = bp8;
     board[7][0] = wr1; board[7][1] = wkn1; board[7][2] = wb1; board[7][3] = wq; board[7][4] = wk; board[7][5] = wb2; board[7][6] = wkn2; board[7][7] = wr2;
     board[6][0] = wp1; board[6][1] = wp2; board[6][2] = wp3; board[6][3] = wp4; board[6][4] = wp5; board[6][5] = wp6; board[6][6] = wp7; board[6][7] = wp8;
     for (int y = 0; y < board.Length; y++)
     {
         if ((y >= 0 && y <= 1) || y >= 6)
         {
             for (int x = 0; x < board.Length; x++)
             {
                 board[y][x].Visible = true;
                 PieceDetails.movePiece(y, x, board[y][x]);
             }
         }
     }
 }
コード例 #3
0
ファイル: Pawn.cs プロジェクト: banfstory/Chess-with-AI
 public override bool Move(PictureBox[][] board)                 // move pawn
 {
     if ((turn && diffY == -1) || (!turn && diffY == 1))         // pawn can only move forward and not backwards
     {
         if (destination != null && (diffX == 1 || diffX == -1)) // if moving diagonally pawn must not land on an empty square
         {
             if (GameState(board))                               // determine if piece can be moved without their king being checked
             {
                 destination.Visible = false;
                 return(PieceDetails.movePiece(destinationY, destinationX, source));
             }
         }
         else if (destination == null && sourceX == destinationX) // if moving forward but only one square forward
         {
             if (GameState(board))                                // determine if piece can be moved without their king being checked
             {
                 return(PieceDetails.movePiece(destinationY, destinationX, source));
             }
         }
     }
     else if (destination == null && sourceX == destinationX && ((turn && diffY == -2 && board[sourceY - 1][sourceX] == null && sourceY == 6) || (!turn && diffY == 2 && board[sourceY + 1][sourceX] == null && sourceY == 1)))
     {
         // moving pawn forward two blocks must land on an empty square and must not be moved before
         if (GameState(board)) // determine if piece can be moved without their king being checked
         {
             return(PieceDetails.movePiece(destinationY, destinationX, source));
         }
     }
     return(false);
 }
コード例 #4
0
ファイル: ChessBoard.cs プロジェクト: banfstory/Chess-with-AI
 private void redo()
 {
     History      = History.Next;
     currentState = History.State;
     turn         = !History.Turn;
     board[History.DestinationY][History.DestinationX] = History.Source;
     PieceDetails.movePiece(History.DestinationY, History.DestinationX, History.Source);
     board[History.SourceY][History.SourceX] = null;
     if (History.Destination != null)
     {
         History.Destination.Visible = false;
     }
 }
コード例 #5
0
        override public bool Move(PictureBox[][] board) // move king - can only move one block in each direction
        {
            double diffYX = diffX != 0 ? ((double)diffY / (double)diffX) : 0;

            if (diffY == 0)                    // moving east or west
            {
                if (diffX != -1 && diffX != 1) // if king can't move east or west than the it is an invalid move
                {
                    return(false);
                }
            }
            else if (diffX == 0)               // moving north or south
            {
                if (diffY != -1 && diffY != 1) // if king can't move north or south than the it is an invalid move
                {
                    return(false);
                }
            }
            else if (diffYX == 1)                                                 // move south-east or north-west
            {
                if (!(diffY == 1 && diffX == 1) && !(diffY == -1 && diffX == -1)) // if king can't move south-east or north-westthan the it is an invalid move
                {
                    return(false);
                }
            }
            else if (diffYX == -1)                                                // move north-east or south-west
            {
                if (!(diffY == -1 && diffX == 1) && !(diffY == 1 && diffX == -1)) // if king can't move north-east or south-west than the it is an invalid move
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            if (GameState(board))        // determine if piece can be moved without their king being checked
            {
                if (destination != null) // if piece is eatting another piece
                {
                    destination.Visible = false;
                    return(PieceDetails.movePiece(destinationY, destinationX, source));
                }
                else // if piece is moving to an empty square
                {
                    return(PieceDetails.movePiece(destinationY, destinationX, source));
                }
            }
            return(false);
        }
コード例 #6
0
ファイル: ChessBoard.cs プロジェクト: banfstory/Chess-with-AI
 private void undo()
 {
     if (History.Prev != null) // the current game state should be represented two turns before
     {
         currentState = History.Prev.State;
     }
     else
     {
         currentState = gameState.Normal;
     }
     turn = History.Turn;
     board[History.SourceY][History.SourceX] = History.Source;
     PieceDetails.movePiece(History.SourceY, History.SourceX, History.Source);
     board[History.DestinationY][History.DestinationX] = History.Destination;
     if (History.Destination != null) // if destination was null than the picturebox object does not need to be modified
     {
         PieceDetails.movePiece(History.DestinationY, History.DestinationX, History.Destination);
         History.Destination.Visible = true;
     }
     History = History.Prev; // go to previous History object
 }
コード例 #7
0
 public override bool Move(PictureBox[][] board)         // move knight
 {
     foreach (int[] dir in PieceDetails.KnightDirection) // loop through all directions knight can make
     {
         if (diffY == dir[0] && diffX == dir[1])
         {
             if (GameState(board))        // determine if piece can be moved without their king being checked
             {
                 if (destination != null) // if piece is eatting another piece
                 {
                     destination.Visible = false;
                     return(PieceDetails.movePiece(destinationY, destinationX, source));
                 }
                 else // if piece is moving to an empty square
                 {
                     return(PieceDetails.movePiece(destinationY, destinationX, source));
                 }
             }
             return(false);
         }
     }
     return(false);
 }
コード例 #8
0
ファイル: ChessBoard.cs プロジェクト: banfstory/Chess-with-AI
        private void TestAICheckmate() // Move white queen to E3 to perform stalemate (Used for testing purposes)
        {
            ResetGame();
            PvE = true;
            for (int i = 0; i < board.Length; i++)
            {
                for (int j = 0; j < board[i].Length; j++)
                {
                    if (board[i][j] == null)
                    {
                        continue;
                    }
                    board[i][j].Visible = false;
                }
            }

            board = new PictureBox[8][];
            for (int i = 0; i < board.Length; i++)
            {
                board[i] = new PictureBox[8];
            }
            board[0][0] = br1; board[0][2] = bb1; board[0][4] = bq; board[0][6] = br2; board[1][0] = bp1; board[1][1] = bp2; board[1][2] = bp3; board[1][3] = bp4; board[1][7] = bp5;
            board[2][4] = bp6; board[2][6] = bk;
            board[7][5] = wq; board[3][4] = wp1; board[3][6] = wb1; board[5][2] = wp2; board[5][7] = wp3; board[6][0] = wp4; board[6][6] = wp5; board[7][0] = wr1; board[7][4] = wk;
            board[7][7] = wr2;
            for (int y = 0; y < board.Length; y++)
            {
                for (int x = 0; x < board[y].Length; x++)
                {
                    if (board[y][x] != null)
                    {
                        board[y][x].Visible = true;
                        PieceDetails.movePiece(y, x, board[y][x]);
                    }
                }
            }
        }
コード例 #9
0
ファイル: Bishop.cs プロジェクト: banfstory/Chess-with-AI
        public override bool Move(PictureBox[][] board)                       // move bishop
        {
            double diffYX = diffX != 0 ? ((double)diffY / (double)diffX) : 0; // diffYX determines if bishop is valid diagonal move where it must be -1 or 1

            if (diffYX == 1)                                                  // move bishop diagonally south-east or north-west
            {
                if (diffY > 0 && diffX > 0)                                   // moving bishop south-east
                {
                    for (int y = sourceY + 1, x = sourceX + 1; y < destinationY && x < destinationX; y++, x++)
                    {
                        if (board[y][x] != null)
                        {
                            return(false);
                        }
                    }
                }
                else // moving bishop north-west
                {
                    for (int y = sourceY - 1, x = sourceX - 1; y > destinationY && x > destinationX; y--, x--)
                    {
                        if (board[y][x] != null)
                        {
                            return(false);
                        }
                    }
                }
            }
            else if (diffYX == -1)          // move bishop diagonally north-east or south-west
            {
                if (diffY < 0 && diffX > 0) // moving bishop north-east
                {
                    for (int y = sourceY - 1, x = sourceX + 1; y > destinationY && x < destinationX; y--, x++)
                    {
                        if (board[y][x] != null)
                        {
                            return(false);
                        }
                    }
                }
                else // moving bishop south-west
                {
                    for (int y = sourceY + 1, x = sourceX - 1; y < destinationY && x > destinationX; y++, x--)
                    {
                        if (board[y][x] != null)
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                return(false);
            }
            if (GameState(board))        // determine if piece can be moved without their king being checked
            {
                if (destination != null) // if piece is eatting another piece
                {
                    destination.Visible = false;
                    return(PieceDetails.movePiece(destinationY, destinationX, source));
                }
                else // if piece is moving to an empty square
                {
                    return(PieceDetails.movePiece(destinationY, destinationX, source));
                }
            }
            return(false);
        }