Пример #1
0
 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]);
             }
         }
     }
 }
Пример #2
0
        private bool IsValidMove(pieceName sourcePieceType, int destinationY, int destinationX) // determine if the piece is able to move to its target square
        {
            int sourceY = PieceDetails.FindCoordinate(selectedpiece.Location.Y);
            int sourceX = PieceDetails.FindCoordinate(selectedpiece.Location.X);

            switch (sourcePieceType)
            {
            case pieceName.Pawn:
                Pawn pawn = new Pawn(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(pawn.Move(board));

            case pieceName.Rook:
                Rook rook = new Rook(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(rook.Move(board));

            case pieceName.Knight:
                Knight knight = new Knight(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(knight.Move(board));

            case pieceName.Bishop:
                Bishop bishop = new Bishop(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(bishop.Move(board));

            case pieceName.Queen:
                Queen queen = new Queen(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(queen.Move(board));

            default:
                King king = new King(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(king.Move(board));
            }
        }
Пример #3
0
 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
 private bool QueenCheck(PictureBox[][] board, int y, int x, int targetY, int targetX)
 {
     bool[] pieceDirection = new bool[8]; // this array represents north, east, south, west, northeast, southeast, southwest, northwest and will reduce the processing time
     for (int i = 1; i < 8; i++)
     {
         // queen cannot move anymore as it is either out of bound or there is another piece blocking it from its target
         if (PieceDetails.checkedAllDirections(pieceDirection))
         {
             break;
         }
         for (int j = 0; j < PieceDetails.QueenDirection.Length; j++)
         {
             if (pieceDirection[j])
             {
                 continue;
             }
             int Y = y + i * PieceDetails.QueenDirection[j][0];
             int X = x + i * PieceDetails.QueenDirection[j][1];
             if (Y == targetY && X == targetX)
             {
                 return(true);
             }
             else if (Y < 0 || Y > 7 || X < 0 || X > 7 || board[Y][X] != null) // queen cannot move anymore in this direction as it is either out of bound or there is another piece blocking it from its target
             {
                 pieceDirection[j] = true;
             }
         }
     }
     return(false);
 }
Пример #5
0
        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]);
                    }
                }
            }
        }
Пример #6
0
 private void BishopCheck(PictureBox[][] board, int y, int x, Dictionary <int, HashSet <int> > targets) // bishop check king
 {
     bool[] pieceDirection = new bool[4];                                                               // this array represents northeast, southeast, southwest, northwest and will reduce the processing time
     for (int i = 1; i < 8; i++)
     {
         // bishop cannot move anymore as it is either out of bound or there is another piece blocking it from its target
         if (PieceDetails.checkedAllDirections(pieceDirection))
         {
             break;
         }
         for (int j = 0; j < PieceDetails.BishopDirection.Length; j++)
         {
             if (pieceDirection[j])
             {
                 continue;
             }
             int Y = y + i * PieceDetails.BishopDirection[j][0];
             int X = x + i * PieceDetails.BishopDirection[j][1];
             if (targets.ContainsKey(Y) && targets[Y].Contains(X))
             {
                 RemoveTarget(Y, X, targets);
             }
             if (Y < 0 || Y > 7 || X < 0 || X > 7 || board[Y][X] != null) // bishop cannot move anymore in this direction as it is either out of bound or there is another piece blocking it from its target
             {
                 pieceDirection[j] = true;
             }
         }
     }
 }
Пример #7
0
 private bool QueenBlock(PictureBox[][] board, int y, int x, Dictionary <int, HashSet <int> > targets)
 {
     bool[] pieceDirection = new bool[8]; // this array represents north, east, south, west, northeast, southeast, southwest, northwest and will reduce the processing time
     for (int i = 1; i <= 7; i++)
     {
         // queen cannot move anymore as it is either out of bound or there is another piece blocking it from its target
         if (PieceDetails.checkedAllDirections(pieceDirection))
         {
             break;
         }
         for (int j = 0; j < PieceDetails.QueenDirection.Length; j++)
         {
             if (pieceDirection[j])
             {
                 continue;
             }
             int Y = y + i * PieceDetails.QueenDirection[j][0];
             int X = x + i * PieceDetails.QueenDirection[j][1];
             if (targets.ContainsKey(Y) && targets[Y].Contains(X)) // determine if queen can block the piece checking the king
             {
                 return(true);
             }
             else if (Y < 0 || Y > 7 || X < 0 || X > 7 || board[Y][X] != null) // queen cannot move anymore in this direction as it is either out of bound or there is another piece blocking it from its target
             {
                 pieceDirection[j] = true;
             }
         }
     }
     return(false);
 }
Пример #8
0
 public bool PawnMove(PictureBox[][] board, int y, int x, bool turn, int targetY, int targetX) // pawn move
 {
     if (turn)
     {
         // if white pawn can move one square forward (does not need to check if it can two squares forward as we just need to find if it can move without being checked)
         if (y - 1 >= 0)
         {
             if (board[y - 1][x] == null)                                                         // white pawn move forward one square and must be null
             {
                 if (IsMovable.IsAbleToMovePiece(board, y, x, y - 1, x, !turn, targetY, targetX)) // determine if white pawn can move without being checked
                 {
                     return(true);
                 }
             }
             if (x - 1 >= 0 && board[y - 1][x - 1] != null && !PieceDetails.IsPieceBlackorWhite(board[y - 1][x - 1].Name)) // white pawn eat another piece at north-west
             {
                 if (IsMovable.IsAbleToMovePiece(board, y, x, y - 1, x - 1, !turn, targetY, targetX))
                 {
                     return(true);
                 }
             }
             if (x + 1 < 8 && board[y - 1][x + 1] != null && !PieceDetails.IsPieceBlackorWhite(board[y - 1][x + 1].Name)) // white pawn eat another piece at north-east
             {
                 if (IsMovable.IsAbleToMovePiece(board, y, x, y - 1, x + 1, !turn, targetY, targetX))
                 {
                     return(true);
                 }
             }
         }
     }
     else if (!turn)
     {
         // if black pawn can move one square forward (does not need to check if it can two squares forward as we just need to find if it can move without being checked)
         if (y + 1 < 8)
         {
             if (board[y + 1][x] == null)                                                         // black pawn move forward one square and must be null
             {
                 if (IsMovable.IsAbleToMovePiece(board, y, x, y + 1, x, !turn, targetY, targetX)) // determine if white pawn can move without being checked
                 {
                     return(true);
                 }
             }
             else if (x - 1 >= 0 && board[y + 1][x - 1] != null && PieceDetails.IsPieceBlackorWhite(board[y + 1][x - 1].Name)) // black pawn eat another piece at south-west
             {
                 if (IsMovable.IsAbleToMovePiece(board, y, x, y + 1, x - 1, !turn, targetY, targetX))
                 {
                     return(true);
                 }
             }
             else if (x + 1 < 8 && board[y + 1][x + 1] != null && PieceDetails.IsPieceBlackorWhite(board[y + 1][x + 1].Name)) // black pawn eat another piece at south-west
             {
                 if (IsMovable.IsAbleToMovePiece(board, y, x, y + 1, x + 1, !turn, targetY, targetX))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Пример #9
0
        private bool MovePiece(PictureBox destination) // moving a piece to eat another piece
        {
            pieceName sourcePieceType = turn ? PieceDetails.selectedWhitePiece(selectedpiece.Name) : PieceDetails.selectedBlackPiece(selectedpiece.Name);
            int       destinationY    = PieceDetails.FindCoordinate(destination.Location.Y);
            int       destinationX    = PieceDetails.FindCoordinate(destination.Location.X);

            return(IsValidMove(sourcePieceType, destinationY, destinationX));
        }
Пример #10
0
 // determine if move been made by the AI is illegal or not
 private bool IsValidMove(PictureBox[][] board, bool turn)
 {
     if (movesCount == 0)
     {
         Check check     = new Check();
         int[] kingCoord = PieceDetails.FindKing(board, turn);
         if (check.IsChecked(board, kingCoord[0], kingCoord[1], !turn)) // determine if this is an illegal move by check
         {
             return(true);
         }
     }
     return(false);
 }
Пример #11
0
 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;
     }
 }
Пример #12
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);
        }
 private void PawnPossibleMoves(int Y, int X, PictureBox[][] board, List <PictureBox> PossiblePieceToTake, bool turn, ChessBoard.pieceName sourcePieceType)
 {
     if (turn)
     {
         if (Y - 1 >= 0)
         {
             if (board[Y - 1][X] == null)
             {
                 gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X), PieceDetails.ToCoordinate(Y - 1), sizeOfBox, sizeOfBox);
             }
             if (X - 1 >= 0 && board[Y - 1][X - 1] != null && !PieceDetails.IsPieceBlackorWhite(board[Y - 1][X - 1].Name))
             {
                 PossiblePieceToTake.Add(board[Y - 1][X - 1]);
                 board[Y - 1][X - 1].BackColor = backcolor;
             }
             if (X + 1 < 8 && board[Y - 1][X + 1] != null && !PieceDetails.IsPieceBlackorWhite(board[Y - 1][X + 1].Name))
             {
                 PossiblePieceToTake.Add(board[Y - 1][X + 1]);
                 board[Y - 1][X + 1].BackColor = backcolor;
             }
         }
         if (Y == 6 && Y - 2 >= 0 && board[Y - 2][X] == null && board[Y - 1][X] == null)
         {
             gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X), PieceDetails.ToCoordinate(Y - 2), sizeOfBox, sizeOfBox);
         }
     }
     else
     {
         if (Y + 1 < 8)
         {
             if (board[Y + 1][X] == null)
             {
                 gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X), PieceDetails.ToCoordinate(Y + 1), sizeOfBox, sizeOfBox);
             }
             if (X - 1 >= 0 && board[Y + 1][X - 1] != null && PieceDetails.IsPieceBlackorWhite(board[Y + 1][X - 1].Name))
             {
                 PossiblePieceToTake.Add(board[Y + 1][X - 1]);
                 board[Y + 1][X - 1].BackColor = backcolor;
             }
             if (X + 1 < 8 && board[Y + 1][X + 1] != null && PieceDetails.IsPieceBlackorWhite(board[Y + 1][X + 1].Name))
             {
                 PossiblePieceToTake.Add(board[Y + 1][X + 1]);
                 board[Y + 1][X + 1].BackColor = backcolor;
             }
         }
         if (Y == 1 && Y + 2 < 8 && board[Y + 2][X] == null && board[Y + 1][X] == null)
         {
             gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X), PieceDetails.ToCoordinate(Y + 2), sizeOfBox, sizeOfBox);
         }
     }
 }
Пример #14
0
 private void PawnSquaresTaken(int Y, int X, PictureBox[][] board, bool turn)
 {
     if (turn)
     {
         if (Y - 1 >= 0)
         {
             if (board[Y - 1][X] == null)
             {
                 capturedIncrDecr(turn);
             }
             if (X - 1 >= 0 && board[Y - 1][X - 1] != null && !PieceDetails.IsPieceBlackorWhite(board[Y - 1][X - 1].Name))
             {
                 capturedIncrDecr(turn);
             }
             if (X + 1 < 8 && board[Y - 1][X + 1] != null && !PieceDetails.IsPieceBlackorWhite(board[Y - 1][X + 1].Name))
             {
                 capturedIncrDecr(turn);
             }
         }
         if (Y == 6 && Y - 2 >= 0 && board[Y - 2][X] == null && board[Y - 1][X] == null)
         {
             capturedIncrDecr(turn);
         }
     }
     else
     {
         if (Y + 1 < 8)
         {
             if (board[Y + 1][X] == null)
             {
                 capturedIncrDecr(turn);
             }
             if (X - 1 >= 0 && board[Y + 1][X - 1] != null && PieceDetails.IsPieceBlackorWhite(board[Y + 1][X - 1].Name))
             {
                 capturedIncrDecr(turn);
             }
             if (X + 1 < 8 && board[Y + 1][X + 1] != null && PieceDetails.IsPieceBlackorWhite(board[Y + 1][X + 1].Name))
             {
                 capturedIncrDecr(turn);
             }
         }
         if (Y == 1 && Y + 2 < 8 && board[Y + 2][X] == null && board[Y + 1][X] == null)
         {
             capturedIncrDecr(turn);
         }
     }
 }
Пример #15
0
 private void KnightSquaresTaken(int Y, int X, PictureBox[][] board, bool turn)
 {
     foreach (int[] dir in PieceDetails.KnightDirection)
     {
         if ((dir[0] < 0 ? Y + dir[0] >= 0 : Y + dir[0] < 8) && (dir[1] < 0 ? X + dir[1] >= 0 : X + dir[1] < 8))
         {
             if (board[Y + dir[0]][X + dir[1]] == null)
             {
                 capturedIncrDecr(turn);
             }
             else if ((turn && !PieceDetails.IsPieceBlackorWhite(board[Y + dir[0]][X + dir[1]].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y + dir[0]][X + dir[1]].Name)))
             {
                 capturedIncrDecr(turn);
             }
         }
     }
 }
 private void KnightPossibleMoves(int Y, int X, PictureBox[][] board, List <PictureBox> PossiblePieceToTake, bool turn, ChessBoard.pieceName sourcePieceType)
 {
     foreach (int[] dir in PieceDetails.KnightDirection)
     {
         if ((dir[0] < 0 ? Y + dir[0] >= 0 : Y + dir[0] < 8) && (dir[1] < 0 ? X + dir[1] >= 0 : X + dir[1] < 8))
         {
             if (board[Y + dir[0]][X + dir[1]] == null)
             {
                 gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X + dir[1]), PieceDetails.ToCoordinate(Y + dir[0]), sizeOfBox, sizeOfBox);
             }
             else if ((turn && !PieceDetails.IsPieceBlackorWhite(board[Y + dir[0]][X + dir[1]].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y + dir[0]][X + dir[1]].Name)))
             {
                 gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X + dir[1]), PieceDetails.ToCoordinate(Y + dir[0]), sizeOfBox, sizeOfBox);
                 PossiblePieceToTake.Add(board[Y + dir[0]][X + dir[1]]);
                 board[Y + dir[0]][X + dir[1]].BackColor = backcolor;
             }
         }
     }
 }
Пример #17
0
        public int CaptureCount(PictureBox[][] board)
        {
            for (int i = 0; i < board.Length; i++)
            {
                for (int j = 0; j < board[i].Length; j++)
                {
                    if (board[i][j] == null)
                    {
                        continue;
                    }
                    bool turn = PieceDetails.IsPieceBlackorWhite(board[i][j].Name);
                    ChessBoard.pieceName selected = PieceDetails.selectedPiece(board[i][j].Name);
                    switch (selected)
                    {
                    case ChessBoard.pieceName.Pawn:
                        PawnSquaresTaken(i, j, board, turn);
                        break;

                    case ChessBoard.pieceName.Rook:
                        RookSquaresTaken(i, j, board, turn);
                        break;

                    case ChessBoard.pieceName.Knight:
                        KnightSquaresTaken(i, j, board, turn);
                        break;

                    case ChessBoard.pieceName.Bishop:
                        BishopSquaresTaken(i, j, board, turn);
                        break;

                    case ChessBoard.pieceName.Queen:
                        QueenSquaresTaken(i, j, board, turn);
                        break;

                    default:
                        KingSquaresTaken(i, j, board, turn);
                        break;
                    }
                }
            }
            return(capturedBoard);
        }
Пример #18
0
 public bool KnightMove(PictureBox[][] board, int y, int x, bool turn, int targetY, int targetX) // knight move
 {
     foreach (int[] dir in PieceDetails.KnightDirection)
     {
         int Y = y + dir[0];
         int X = x + dir[1];
         if (Y < 0 || X < 0 || Y > 7 || X > 7)
         {
             continue;                                   // knight cannot move out of bounds
         }
         if (board[Y][X] == null || (turn && !PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)))
         {
             if (IsMovable.IsAbleToMovePiece(board, y, x, Y, X, !turn, targetY, targetX)) // determine if knight can move without being checked
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Пример #19
0
 private void KingSquaresTaken(int Y, int X, PictureBox[][] board, bool turn)
 {
     for (int i = -1; i <= 1; i++)
     {
         for (int j = -1; j <= 1; j++)
         {
             if ((i == 0 && j == 0) || Y + i >= 8 || Y + i < 0 || X + j >= 8 || X + j < 0)
             {
                 continue;
             }
             if (board[Y + i][X + j] == null)
             {
                 capturedIncrDecr(turn);
             }
             else if ((turn && !PieceDetails.IsPieceBlackorWhite(board[Y + i][X + j].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y + i][X + j].Name)))
             {
                 capturedIncrDecr(turn);
             }
         }
     }
 }
Пример #20
0
 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
 }
Пример #21
0
 public bool RookMove(PictureBox[][] board, int y, int x, bool turn, int targetY, int targetX) // rook move
 {
     for (int i = 0; i < PieceDetails.RookDirection.Length; i++)                               // looks at each directions rook can travel: north, east, south, west
     {
         // only one square for each direction needs to be checked as it only needs to determine rook can be moved
         int Y = y + 1 * PieceDetails.RookDirection[i][0];
         int X = x + 1 * PieceDetails.RookDirection[i][1];
         if (Y < 0 || Y > 7 || X < 0 || X > 7)
         {
             continue;
         }
         if (board[Y][X] == null || (turn && !PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)))
         {
             if (IsMovable.IsAbleToMovePiece(board, y, x, Y, X, !turn, targetY, targetX)) // determine if rook can move without being checked
             {
                 return(true);
             }
         }
     }
     return(false);
 }
 private void BishopPossibleMoves(int y, int x, PictureBox[][] board, List <PictureBox> PossiblePieceToTake, bool turn, ChessBoard.pieceName sourcePieceType)
 {
     bool[] pieceDirection = new bool[4]; // this array represents northeast, southeast, southwest, northwest and will reduce the processing time
     for (int i = 1; i < 8; i++)
     {
         // bishop cannot move anymore as it is either out of bound or there is another piece blocking it from its target
         if (PieceDetails.checkedAllDirections(pieceDirection))
         {
             break;
         }
         for (int j = 0; j < PieceDetails.BishopDirection.Length; j++)
         {
             if (pieceDirection[j])
             {
                 continue;
             }
             int Y = y + i * PieceDetails.BishopDirection[j][0];
             int X = x + i * PieceDetails.BishopDirection[j][1];
             if (Y < 0 || Y > 7 || X < 0 || X > 7)
             {
                 pieceDirection[j] = true;
             }
             else if (board[Y][X] == null)
             {
                 gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X), PieceDetails.ToCoordinate(Y), sizeOfBox, sizeOfBox);
             }
             else if ((turn && !PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)))
             {
                 PossiblePieceToTake.Add(board[Y][X]);
                 board[Y][X].BackColor = backcolor;
                 pieceDirection[j]     = true;
             }
             else
             {
                 pieceDirection[j] = true;
             }
         }
     }
 }
 private void KingPossibleMoves(int Y, int X, PictureBox[][] board, List <PictureBox> PossiblePieceToTake, bool turn, ChessBoard.pieceName sourcePieceType)
 {
     for (int i = -1; i <= 1; i++)
     {
         for (int j = -1; j <= 1; j++)
         {
             if ((i == 0 && j == 0) || Y + i >= 8 || Y + i < 0 || X + j >= 8 || X + j < 0)
             {
                 continue;
             }
             if (board[Y + i][X + j] == null)
             {
                 gObject.FillRectangle(brush, PieceDetails.ToCoordinate(X + j), PieceDetails.ToCoordinate(Y + i), sizeOfBox, sizeOfBox);
             }
             else if ((turn && !PieceDetails.IsPieceBlackorWhite(board[Y + i][X + j].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y + i][X + j].Name)))
             {
                 PossiblePieceToTake.Add(board[Y + i][X + j]);
                 board[Y + i][X + j].BackColor = backcolor;
             }
         }
     }
 }
Пример #24
0
 private void RookSquaresTaken(int y, int x, PictureBox[][] board, bool turn)
 {
     bool[] pieceDirection = new bool[4]; // this array represents north, east, south, west and will reduce the processing time
     for (int i = 1; i < 8; i++)
     {
         // rook cannot move anymore as it is either out of bound or there is another piece blocking it from its target
         if (PieceDetails.checkedAllDirections(pieceDirection))
         {
             break;
         }
         for (int j = 0; j < PieceDetails.RookDirection.Length; j++)
         {
             if (pieceDirection[j])
             {
                 continue;
             }
             int Y = y + i * PieceDetails.RookDirection[j][0];
             int X = x + i * PieceDetails.RookDirection[j][1];
             if (Y < 0 || Y > 7 || X < 0 || X > 7)
             {
                 pieceDirection[j] = true;
             }
             else if (board[Y][X] == null)
             {
                 capturedIncrDecr(turn);
             }
             else if ((turn && !PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)) || (!turn && PieceDetails.IsPieceBlackorWhite(board[Y][X].Name)))
             {
                 capturedIncrDecr(turn);
                 pieceDirection[j] = true;
             }
             else
             {
                 pieceDirection[j] = true;
             }
         }
     }
 }
Пример #25
0
        protected bool GameState(PictureBox[][] board) // determine the game state whether its check, checkmate, stalemate or normal
        {
            Check check = new Check();

            board[sourceY][sourceX]           = null;
            board[destinationY][destinationX] = source;
            int[] kingCoord = PieceDetails.FindKing(board, turn);

            if (check.IsChecked(board, kingCoord[0], kingCoord[1], !turn)) // determine if this is an illegal move by check
            {
                board[sourceY][sourceX]           = source;
                board[destinationY][destinationX] = destination;
                return(false);
            }
            else
            {
                CurrentStatus        status = new CurrentStatus();
                ChessBoard.gameState state  = status.TurnResult(board, turn);

                if (state == ChessBoard.gameState.Check)
                {
                    setHistory(ChessBoard.gameState.Check);
                }
                else if (state == ChessBoard.gameState.Checkmate)
                {
                    setHistory(ChessBoard.gameState.Checkmate);
                }
                else if (state == ChessBoard.gameState.Stalemate)
                {
                    setHistory(ChessBoard.gameState.Stalemate);
                }
                else
                {
                    setHistory(ChessBoard.gameState.Normal);
                }
            }
            return(true);
        }
Пример #26
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);
 }
Пример #27
0
 private void board_Click(object sender, EventArgs e) // when selected piece move to empty square
 {
     if (currentState != gameState.Checkmate && currentState != gameState.Stalemate && (PvP || PvE))
     {
         if (alreadySelected)
         {
             Point position = Cursor.Position;
             position = panel.PointToClient(position);
             int destinationY = PieceDetails.FindCoordinate(position.Y);
             int destinationX = PieceDetails.FindCoordinate(position.X);
             if (MovePiece(destinationY, destinationX))
             {
                 resetHighlightedPieces();
                 completeTurn();
                 AIMove();
             }
             else
             {
                 MessageBox.Show("Invalid Move, please try again.");
             }
         }
     }
 }
Пример #28
0
        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]);
                    }
                }
            }
        }
Пример #29
0
 private void clickWhite(object sender, EventArgs e)
 {
     if (currentState != gameState.Checkmate && currentState != gameState.Stalemate && (PvP || PvE))
     {
         if (alreadySelected && !turn) // when black piece eats white black piece
         {
             if (MovePiece((PictureBox)sender))
             {
                 panel.Refresh();
                 selectedpiece.BackColor = Color.Transparent;
                 foreach (PictureBox p in PossiblePieceToTake)
                 {
                     p.BackColor = Color.Transparent;
                 }
                 PossiblePieceToTake = new List <PictureBox>();
                 completeTurn();
                 AIMove();
             }
             else
             {
                 MessageBox.Show("Invalid Move, please try again.");
             }
         }
         else if (turn && selectedpiece != (PictureBox)sender) // selecting white piece
         {
             resetHighlightedPieces();
             selectedpiece           = (PictureBox)sender;
             selectedpiece.BackColor = Color.FromArgb(220, 13, 86, 212);
             alreadySelected         = true;
             pieceName            sourcePieceType = PieceDetails.selectedWhitePiece(selectedpiece.Name);
             int                  Y   = PieceDetails.FindCoordinate(selectedpiece.Location.Y);
             int                  X   = PieceDetails.FindCoordinate(selectedpiece.Location.X);
             DisplayPossibleMoves dpm = new DisplayPossibleMoves(panel.CreateGraphics(), Color.FromArgb(125, 48, 118, 240));
             dpm.DisplayMoves(Y, X, board, PossiblePieceToTake, turn, sourcePieceType);
         }
     }
 }
Пример #30
0
 private void AIMove() // determine how the AI will move and swap back to players turn after AI makes its move
 {
     if (PvE && (currentState == gameState.Normal || currentState == gameState.Check) && ((turn && AIColor) || (!turn && !AIColor)))
     {
         int      currentBoardState = 0;
         AIResult aiResult          = new AIResult();
         AI       ai = new AI(0);
         for (int y = 0; y < board.Length; y++)
         {
             for (int x = 0; x < board[y].Length; x++)
             {
                 if (board[y][x] == null)
                 {
                     continue;
                 }
                 if (PieceDetails.IsPieceBlackorWhite(board[y][x].Name))
                 {
                     currentBoardState += ai.PieceValue(PieceDetails.selectedPiece(board[y][x].Name));
                 }
                 else
                 {
                     currentBoardState -= ai.PieceValue(PieceDetails.selectedPiece(board[y][x].Name));
                 }
             }
         }
         ai.MiniMax(board, turn, AIComplexity, currentBoardState, aiResult);
         selectedpiece = board[aiResult.SourceY][aiResult.SourceX];
         if (!IsValidMove(PieceDetails.selectedPiece(board[aiResult.SourceY][aiResult.SourceX].Name), aiResult.DestinationY, aiResult.DestinationX))
         {
             MessageBox.Show("AI ERROR");
             undo();
             return;
         }
         completeTurn();
     }
 }