Esempio n. 1
0
        //If a piece was saved in the saved data in controller, add the piece at its coordinate in the grid
        public void AddSavedPiece(PieceClass savedPiece)
        {
            PieceClass newSavedPiece = new PieceClass() { hasMoved = savedPiece.hasMoved, pieceType = savedPiece.pieceType,
                    position = savedPiece.position, team = savedPiece.team };
            savedPiece = null;
            grid[newSavedPiece.position.row, newSavedPiece.position.col] = newSavedPiece;

            if (newSavedPiece.team == Team.White)
            {
                whiteActive.Add(newSavedPiece);
            }
            else
            {
                blackActive.Add(newSavedPiece);
            }
            newSavedPiece = null;
        }
Esempio n. 2
0
        //Function used to simulate a move for the WillKingBeInCheck function
        private void SimulateMove(Coordinate source, Coordinate destination, Team player, ref PieceClass[,] copyOfGrid, List<PieceClass> whiteActiveCopy, List<PieceClass> blackActiveCopy)
        {
            if (copyOfGrid[destination.row, destination.col].team != Team.Blank)
            {
                //Remove attacked piece from respective active list
                if (player == Team.White)
                {
                    blackActiveCopy.Remove(blackActiveCopy.Find(x => (x.position.col == destination.col && x.position.row == destination.row)));
                }
                else
                {
                    whiteActiveCopy.Remove(whiteActiveCopy.Find(x => (x.position.col == destination.col && x.position.row == destination.row)));
                }
            }

            copyOfGrid[destination.row, destination.col] = copyOfGrid[source.row, source.col];
            copyOfGrid[destination.row, destination.col].position = new Coordinate { row = destination.row, col = destination.col, team = player };
            copyOfGrid[destination.row, destination.col].position = destination;
            copyOfGrid[source.row, source.col] = new PieceClass { pieceType = Piece.Blank, team = Team.Blank, position = source };
        }
Esempio n. 3
0
        private void InitializeGrid()
        {
            if (grid != null)
            {
                Array.Clear(grid, 0, grid.Length);
            }
            else
            {
                grid = new PieceClass[NUM_CELLS, NUM_CELLS];
            }

            grid[0, 0] = new PieceClass { pieceType = Piece.Rook, team = Team.Black, position = new Coordinate { row = 0, col = 0, team = Team.Black } };
            grid[0, 1] = new PieceClass { pieceType = Piece.Knight, team = Team.Black, position = new Coordinate { row = 0, col = 1, team = Team.Black } };
            grid[0, 2] = new PieceClass { pieceType = Piece.Bishop, team = Team.Black, position = new Coordinate { row = 0, col = 2, team = Team.Black } };
            grid[0, 3] = new PieceClass { pieceType = Piece.Queen, team = Team.Black, position = new Coordinate { row = 0, col = 3, team = Team.Black } };
            grid[0, 4] = new PieceClass { pieceType = Piece.King, team = Team.Black, position = new Coordinate { row = 0, col = 4, team = Team.Black } };
            grid[0, 5] = new PieceClass { pieceType = Piece.Bishop, team = Team.Black, position = new Coordinate { row = 0, col = 5, team = Team.Black } };
            grid[0, 6] = new PieceClass { pieceType = Piece.Knight, team = Team.Black, position = new Coordinate { row = 0, col = 6, team = Team.Black } };
            grid[0, 7] = new PieceClass { pieceType = Piece.Rook, team = Team.Black, position = new Coordinate { row = 0, col = 7, team = Team.Black } };

            grid[7, 0] = new PieceClass { pieceType = Piece.Rook, team = Team.White, position = new Coordinate { row = 7, col = 0, team = Team.White } };
            grid[7, 1] = new PieceClass { pieceType = Piece.Knight, team = Team.White, position = new Coordinate { row = 7, col = 1, team = Team.White } };
            grid[7, 2] = new PieceClass { pieceType = Piece.Bishop, team = Team.White, position = new Coordinate { row = 7, col = 2, team = Team.White } };
            grid[7, 3] = new PieceClass { pieceType = Piece.Queen, team = Team.White, position = new Coordinate { row = 7, col = 3, team = Team.White } };
            grid[7, 4] = new PieceClass { pieceType = Piece.King, team = Team.White, position = new Coordinate { row = 7, col = 4, team = Team.White } };
            grid[7, 5] = new PieceClass { pieceType = Piece.Bishop, team = Team.White, position = new Coordinate { row = 7, col = 5, team = Team.White } };
            grid[7, 6] = new PieceClass { pieceType = Piece.Knight, team = Team.White, position = new Coordinate { row = 7, col = 6, team = Team.White } };
            grid[7, 7] = new PieceClass { pieceType = Piece.Rook, team = Team.White, position = new Coordinate { row = 7, col = 7, team = Team.White } };

            for (int i = 0; i < 8; i++)
            {
                grid[1, i] = new PieceClass { pieceType = Piece.Pawn, team = Team.Black, position = new Coordinate { row = 1, col = i, team = Team.Black }, hasMoved = false };
                grid[6, i] = new PieceClass { pieceType = Piece.Pawn, team = Team.White, position = new Coordinate { row = 6, col = i, team = Team.White }, hasMoved = false };
            }

            for (int R = 2; R < 6; R++)
            {
                for (int C = 0; C < 8; C++)
                {
                    grid[R, C] = new PieceClass { pieceType = Piece.Blank, team = Team.Blank, position = new Coordinate { row = R, col = C, team = Team.Blank } };
                }
            }

            for (int i = 0; i < 8; i++)
            {
                whiteActive.Add(grid[6, i]);
                whiteActive.Add(grid[7, i]);
                blackActive.Add(grid[0, i]);
                blackActive.Add(grid[1, i]);
            }
        }
Esempio n. 4
0
        //Get pawn's simulated moves from the copy of the grid
        private List<Coordinate> GetPawnMoves(Coordinate baseCoord, PieceClass[,] copyOfGrid)
        {
            List<Coordinate> possibleMoves = new List<Coordinate>();

            possibleMoves.Add(new Coordinate { row = baseCoord.row + 1, col = baseCoord.col });

            if (!copyOfGrid[baseCoord.row, baseCoord.col].hasMoved)
            {
                possibleMoves.Add(new Coordinate { row = baseCoord.row + 2, col = baseCoord.col });
            }

            if (copyOfGrid[baseCoord.row + 1, baseCoord.col - 1].team != copyOfGrid[baseCoord.row, baseCoord.col].team &&
                copyOfGrid[baseCoord.row + 1, baseCoord.col - 1].team != Team.Blank)
            {
                possibleMoves.Add(new Coordinate { row = baseCoord.row + 1, col = baseCoord.col - 1 });
            }

            if (copyOfGrid[baseCoord.row + 1, baseCoord.col + 1].team != copyOfGrid[baseCoord.row, baseCoord.col].team &&
                copyOfGrid[baseCoord.row + 1, baseCoord.col + 1].team != Team.Blank)
            {
                possibleMoves.Add(new Coordinate { row = baseCoord.row + 1, col = baseCoord.col + 1 });
            }

            return possibleMoves;
        }
Esempio n. 5
0
        //Check for check with a copy of the grid
        private void CheckForCheck(Team player, ref PieceClass[,] copyOfGrid, List<PieceClass> whiteActiveCopy, List<PieceClass> blackActiveCopy)
        {
            List<Coordinate> masterList = new List<Coordinate>();

            //get teams possible moves
            if (player == Team.White)
            {
                foreach (PieceClass piece in whiteActiveCopy)
                {
                    masterList.AddRange(PossibleMoves(piece.position, player, ref copyOfGrid));
                }

                //lamba expression to determine if the black king's coordinate is in the master list of possible move coordinates
                if (masterList.Exists(x => x == blackActiveCopy.Find(z => z.pieceType == Piece.King).position))
                {
                    isBlackInCheck = true;
                }
                else
                {
                    isBlackInCheck = false;
                }
            }
            else
            {
                foreach (PieceClass piece in blackActiveCopy)
                {
                    masterList.AddRange(PossibleMoves(piece.position, player, ref copyOfGrid));
                }

                //lamba expression to determine if the whiate king's coordinate is in the master list of possible move coordinates
                if (masterList.Exists(x => x == whiteActiveCopy.Find(z => z.pieceType == Piece.King).position))
                {
                    isWhiteInCheck = true;
                }
                else
                {
                    isWhiteInCheck = false;
                }
            }
        }
Esempio n. 6
0
 public void UpdateActiveListFromSaveState(PieceClass piece)
 {
     if (piece.team == Team.White)
     {
         whiteActive.Add(piece);
     }
     else
     {
         blackActive.Add(piece);
     }
     piece = null;
 }
Esempio n. 7
0
 public void SetEmptyGridCellsBlank()
 {
     for (int rowCount = 0; rowCount < 8; rowCount++)
     {
         for (int colCount = 0; colCount < 8; colCount++)
         {
             if (grid[rowCount,colCount] == null)
             {
                 grid[rowCount, colCount] = new PieceClass { pieceType = Piece.Blank, team = Team.Blank, position = new Coordinate { row = rowCount, col = colCount, team = Team.Blank } };
             }
         }
     }
 }
Esempio n. 8
0
        //Simulation of possible moves
        public List<Coordinate> PossibleMoves(Coordinate coord, Team player, ref PieceClass[,] copyOfGrid)
        {
            List<Coordinate> possibleMoves = new List<Coordinate>();

            //Call appropriate piece function
            switch (copyOfGrid[coord.row, coord.col].pieceType)
            {
                case Piece.King: possibleMoves = GetKingMoves(coord);
                    break;
                case Piece.Queen: possibleMoves = GetQueenMoves(coord);
                    break;
                case Piece.Bishop: possibleMoves = GetBishopMoves(coord);
                    break;
                case Piece.Knight: possibleMoves = GetKnightMoves(coord);
                    break;
                case Piece.Rook: possibleMoves = GetRookMoves(coord);
                    break;
                case Piece.Pawn: possibleMoves = GetPawnMoves(coord);
                    break;
            }

            foreach (Coordinate c in possibleMoves.ToList())
            {
                if (c.row < 0 || c.row > 7 || c.col < 0 || c.col > 7)
                {
                    possibleMoves.Remove(c);
                }
                else if (copyOfGrid[c.row, c.col].team == player)
                {
                    possibleMoves.Remove(c);
                }
            }

            return possibleMoves;
        }
Esempio n. 9
0
        //remove the king's moves that would put it in check
        private List<Coordinate> ValidateKingMoves(List<Coordinate> coordList, Team player, ref PieceClass[,] copyOfGrid)
        {
            List<Coordinate> masterList = new List<Coordinate>();

            if (player == Team.White)
            {
                foreach (PieceClass piece in whiteActive)
                {
                    masterList.AddRange(PossibleMoves(piece.position, player, ref copyOfGrid));
                }
            }
            else
            {
                foreach (PieceClass piece in blackActive)
                {
                    masterList.AddRange(PossibleMoves(piece.position, player, ref copyOfGrid));
                }
            }

            foreach (Coordinate c in coordList)
            {
                if (masterList.Exists(x => x == c))
                {
                    coordList.Remove(coordList.Find(x => (x.col == c.col && x.row == c.row)));
                }
            }

            return coordList;
        }