Ejemplo n.º 1
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);
 }
Ejemplo n.º 2
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;
             }
         }
     }
 }
Ejemplo n.º 3
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);
 }
 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;
             }
         }
     }
 }
Ejemplo n.º 5
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;
             }
         }
     }
 }
Ejemplo n.º 6
0
        private bool QueenAI(PictureBox[][] board, int y, int x, bool turn, int movesCount, int movesLimit, int currentBoardState, AIResult finalResult)
        {
            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++)          // represents the distance to be moved
            {
                // 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++) // represents the array to define the direction of the move
                {
                    if (pieceDirection[j])
                    {
                        continue;
                    }
                    int Y = y + i * PieceDetails.QueenDirection[j][0];
                    int X = x + i * PieceDetails.QueenDirection[j][1];
                    if (Y >= 0 && Y < 8 && X >= 0 && X < 8)
                    {
                        PictureBox[][] newBoard = new PictureBox[8][];
                        if (board[Y][X] == null)                      // if queen moves to an empty square
                        {
                            setNewBoard(board, newBoard, y, x, Y, X); // create new board object to be used as parameter to call minimax function
                            if (IsValidMove(newBoard, turn))
                            {
                                continue;
                            }
                            AI  newAI          = new AI(movesCount + 1);
                            int bestBoardState = newAI.MiniMax(newBoard, !turn, movesLimit, currentBoardState, finalResult);
                            if (movesCount == 0)
                            {
                                CapturedSquares capture = new CapturedSquares();
                                setResult(turn, bestBoardState, y, x, Y, X, capture.CaptureCount(newBoard));
                            }
                            else
                            {
                                setResult(turn, bestBoardState, y, x, Y, X, 0);
                            }
                        }
                        else
                        {
                            bool pieceColor = PieceDetails.IsPieceBlackorWhite(board[Y][X].Name);
                            int  value      = PieceValue(PieceDetails.selectedPiece(board[Y][X].Name));
                            if (turn && !pieceColor)    // if white queen eats a black piece
                            {
                                if (value == kingvalue) // if king is eatten prune this path completely
                                {
                                    return(true);
                                }
                                if (bestPath <= currentBoardState + value) // prune path that are smaller than the current board state
                                {
                                    setNewBoard(board, newBoard, y, x, Y, X);
                                    if (IsValidMove(newBoard, turn))
                                    {
                                        pieceDirection[j] = true;
                                        continue;
                                    }
                                    bestPath = currentBoardState + value;
                                    AI  newAI          = new AI(movesCount + 1);
                                    int bestBoardState = newAI.MiniMax(newBoard, !turn, movesLimit, currentBoardState + value, finalResult);
                                    if (movesCount == 0)
                                    {
                                        CapturedSquares capture = new CapturedSquares();
                                        setResult(turn, bestBoardState, y, x, Y, X, capture.CaptureCount(newBoard));
                                    }
                                    else
                                    {
                                        setResult(turn, bestBoardState, y, x, Y, X, 0);
                                    }
                                }
                                pieceDirection[j] = true;
                            }
                            else if (!turn && pieceColor) // if black queen eats a white piece
                            {
                                if (value == kingvalue)
                                {
                                    return(true);
                                }
                                if (bestPath >= currentBoardState - value) // prune path that are larger than the current board state
                                {
                                    setNewBoard(board, newBoard, y, x, Y, X);
                                    if (IsValidMove(newBoard, turn))
                                    {
                                        pieceDirection[j] = true;
                                        continue;
                                    }
                                    bestPath = currentBoardState - value;
                                    AI  newAI          = new AI(movesCount + 1);
                                    int bestBoardState = newAI.MiniMax(newBoard, !turn, movesLimit, currentBoardState - value, finalResult);
                                    if (movesCount == 0)
                                    {
                                        CapturedSquares capture = new CapturedSquares();
                                        setResult(turn, bestBoardState, y, x, Y, X, capture.CaptureCount(newBoard));
                                    }
                                    else
                                    {
                                        setResult(turn, bestBoardState, y, x, Y, X, 0);
                                    }
                                }
                                pieceDirection[j] = true;
                            }
                            else
                            {
                                pieceDirection[j] = true; // if bishop lands on the its same color piece
                            }
                        }
                    }
                    else
                    {
                        pieceDirection[j] = true; // if bishop lands out of bounds
                    }
                }
            }
            return(false);
        }