Exemplo n.º 1
0
        private ChessPiece CheckDetection(King king)
        {
            //Checking from other perspective - If king can't see pieces , pieces can't see king -> Big Brain

            List <Tuple <Type, Position[]> > universalPieceTurn = new List <Tuple <Type, Position[]> >()
            {
                new Tuple <Type, Position[]>(typeof(Rook), PossibleMovesWithCollission(new Rook(king.Position, king.Color))),
                new Tuple <Type, Position[]>(typeof(Queen), PossibleMovesWithCollission(new Queen(king.Position, king.Color))),
                new Tuple <Type, Position[]>(typeof(Bishop), PossibleMovesWithCollission(new Bishop(king.Position, king.Color))),
                new Tuple <Type, Position[]>(typeof(King), PossibleMovesWithCollission(new King(king.Position, king.Color))),
                new Tuple <Type, Position[]>(typeof(Horse), PossibleMovesWithCollission(new Horse(king.Position, king.Color))),
                new Tuple <Type, Position[]>(typeof(Pawn), PossibleMovesWithCollission(new Pawn(king.Position, king.Color))),
            };

            foreach (Tuple <Type, Position[]> possibleTurnsOfPiece in universalPieceTurn)
            {
                foreach (Position pos in possibleTurnsOfPiece.Item2)
                {
                    ChessPiece pieceOnPos = this[pos];
                    if (pieceOnPos != null && pieceOnPos.GetType() == possibleTurnsOfPiece.Item1 && pieceOnPos.Color != king.Color)
                    {
                        //Found the piece which triggers check
                        OnCheck(king, pieceOnPos);
                        return(pieceOnPos);
                    }
                }
            }
            //Found nothing
            return(null);
        }
Exemplo n.º 2
0
        public bool MakeMove(int pieceRow, int pieceCol, int targetRow, int targetCol)
        {
            ChessPiece activePiece = matchBoard.Spaces[pieceRow, pieceCol];

            if (activePiece != null && activePiece.Color == currentPlayer)
            {
                //if the square is a valid move for the piece, moves the piece
                if (IsValidMove(activePiece, targetRow, targetCol))
                {
                    activePiece.MoveCount++;
                    ChessPiece targetPiece = matchBoard.Spaces[targetRow, targetCol];
                    if (targetPiece != null)
                    {
                        matchBoard.GetPlayer(currentPlayer).CapturePiece(targetPiece);
                        matchBoard.GetPlayer(!currentPlayer).PieceCaptured(targetPiece);
                    }
                    if (activePiece.GetType() == typeof(Pawn))
                    {
                        if (targetRow == 0 || targetRow == 7)
                        {
                            activePiece = new Queen(currentPlayer);
                        }
                    }
                    matchBoard.Spaces[targetRow, targetCol] = activePiece;
                    matchBoard.Spaces[pieceRow, pieceCol]   = null;
                    currentPlayer = !currentPlayer;
                    FindPossibleMoves();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 3
0
 public void FindPossibleMoves()
 {
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             ChessPiece cp = matchBoard.Spaces[i, j];
             if (cp != null)
             {
                 cp.CurrentMoves.Clear();
                 if (cp.GetType() == typeof(Pawn))
                 {
                     if (cp.MoveCount == 0)
                     {
                         for (int cm = 0; cm < 2; cm++)
                         {
                             int[] arr = cp.Moveset.ElementAt(cm);
                             int   x   = i + arr[0];
                             int   y   = j + arr[1];
                             if ((x >= 0 && x < 8) && (y >= 0 && y < 8))
                             {
                                 ChessPiece target = matchBoard.Spaces[x, y];
                                 if (target == null)
                                 {
                                     cp.CurrentMoves.Add(new int[] { x, y });
                                 }
                                 else
                                 {
                                     break;
                                 }
                             }
                         }
                     }
                     else
                     {
                         int[] arr = cp.Moveset.ElementAt(0);
                         int   x   = i + arr[0];
                         int   y   = j + arr[1];
                         if ((x >= 0 && x < 8) && (y >= 0 && y < 8))
                         {
                             ChessPiece target = matchBoard.Spaces[x, y];
                             if (target == null)
                             {
                                 cp.CurrentMoves.Add(new int[] { x, y });
                             }
                         }
                     }
                     for (int cm = 2; cm < cp.Moveset.Count; cm++)
                     {
                         int[] arr = cp.Moveset.ElementAt(cm);
                         int   x   = i + arr[0];
                         int   y   = j + arr[1];
                         if ((x >= 0 && x < 8) && (y >= 0 && y < 8))
                         {
                             ChessPiece target = matchBoard.Spaces[x, y];
                             if (target != null && target.Color != cp.Color)
                             {
                                 cp.CurrentMoves.Add(new int[] { x, y });
                             }
                         }
                     }
                 } //pawns are special
                 else
                 {
                     for (int cm = 0; cm < cp.Moveset.Count; cm++)
                     {
                         int[] arr = cp.Moveset.ElementAt(cm);
                         int   x   = i + arr[0];
                         int   y   = j + arr[1];
                         if ((x >= 0 && x < 8) && (y >= 0 && y < 8))
                         {
                             ChessPiece target = matchBoard.Spaces[x, y];
                             if (target == null)
                             {
                                 cp.CurrentMoves.Add(new int[] { x, y });
                             }
                             else if (target.Color != cp.Color)
                             {
                                 cp.CurrentMoves.Add(new int[] { x, y });
                                 if (cp.VariableDistance)
                                 {
                                     cm = (cm + 7) - ((cm + 7) % 7) - 1;
                                 }
                             }
                             else if (cp.VariableDistance)
                             {
                                 //if the piece's path is blocked and piece is capable of
                                 //moving beyond that space, skips forward in the
                                 //possible moves array to the next block of possible moves.
                                 cm = (cm + 7) - ((cm + 7) % 7) - 1;
                             }
                         }
                     }
                 }
             }
         }
     }
 }