public BoardSpace(BoardSpace fromSpace) { position = fromSpace.position; piece = null; if (fromSpace.piece != null) { if (fromSpace.piece.GetType() == typeof(Pawn)) { piece = new Pawn(fromSpace.piece, this); } else if (fromSpace.piece.GetType() == typeof(Knight)) { piece = new Knight(fromSpace.piece, this); } else if (fromSpace.piece.GetType() == typeof(Bishop)) { piece = new Bishop(fromSpace.piece, this); } else if (fromSpace.piece.GetType() == typeof(Rook)) { piece = new Rook(fromSpace.piece, this); } else if (fromSpace.piece.GetType() == typeof(Queen)) { piece = new Queen(fromSpace.piece, this); } else if (fromSpace.piece.GetType() == typeof(King)) { piece = new King(fromSpace.piece, this); } } }
public Move(Chessman piece, BoardSpace fromSpace, BoardSpace toSpace, Board context, int score = 0) { contextBoard = context; this.toSpace = toSpace; this.fromSpace = fromSpace; this.score = score; // AI Weight }
public Knight(BoardSpace initPos, ChessmanColor color) { score = AIWeights.knightScore; position = initPos; this.color = color; whiteImage = Properties.Resources.WhiteKnight; blackImage = Properties.Resources.BlackKnight; }
public Bishop(BoardSpace initPos, ChessmanColor color) { score = AIWeights.bishopScore; position = initPos; this.color = color; whiteImage = Properties.Resources.WhiteBishop; blackImage = Properties.Resources.BlackBishop; }
public Pawn(BoardSpace initPos, ChessmanColor color) { score = AIWeights.pawnScore; position = initPos; this.color = color; whiteImage = Properties.Resources.WhitePawn; blackImage = Properties.Resources.BlackPawn; }
public Move(Move other, Board newBoard) // copy Using New Reference Board { contextBoard = newBoard; this.toSpace = new BoardSpace(other.toSpace); this.fromSpace = new BoardSpace(other.fromSpace); this.score = other.score; // AI Weight piece = other.toSpace.piece; }
public Move(Move other) // Copy using using Same Reference Board { contextBoard = other.contextBoard; this.toSpace = other.toSpace; this.fromSpace = other.fromSpace; this.score = other.score; // AI Weight piece = other.piece; }
public King(BoardSpace initPos, ChessmanColor color) { score = AIWeights.queenScore; position = initPos; this.color = color; whiteImage = Properties.Resources.WhiteKing; blackImage = Properties.Resources.BlackKing; }
public Rook(BoardSpace initPos, ChessmanColor color) { score = AIWeights.rookScore; position = initPos; this.color = color; whiteImage = Properties.Resources.WhiteRook; blackImage = Properties.Resources.BlackRook; }
public override bool Equals(object obj) { bool rv = false; if (obj != null || GetType() == obj.GetType()) { BoardSpace passedSpace = (BoardSpace)obj; if (position.Item1 == passedSpace.position.Item1 && position.Item2 == passedSpace.position.Item2) { rv = true; } } return(rv); }
public Move(String move, Board context, int score = 0, bool postMove = false) { if (Regex.IsMatch(move, "[A-Za-z][0-9][A-Za-z][0-9].*")) { contextBoard = context; toSpace = contextBoard.getSpace(move.Substring(0, 1).ToString().ToUpper()[0], int.Parse(move.Substring(1, 1))); fromSpace = contextBoard.getSpace(move.Substring(2, 1).ToString().ToUpper()[0], int.Parse(move.Substring(3, 1))); this.score = score; // AI Weight piece = (postMove) ? toSpace.piece : fromSpace.piece; } else { throw new Exception("Supplied Move is not UCI Formatted."); } }
public Queen(Chessman fromPiece, BoardSpace initPos) { if (fromPiece.GetType() == typeof(Queen)) { Queen from = (Queen)fromPiece; score = from.score; position = initPos; this.color = from.color; whiteImage = from.whiteImage; blackImage = from.blackImage; } else { throw new Exception($"Cant Create Copy of Piece becasue it is the wrong type"); } }
public BoardSpace getSpace(char col, int row) { BoardSpace rv = null; try { if (col >= 'A' && col <= 'H' && row >= 1 && row <= 8) { rv = boardState[BoardSpace.getIndexForBoardRow(row), BoardSpace.getIndexForBoardColumn(col)]; } } catch { } return(rv); }
private void updateValidSquaresForSelectedCB(MyCheckBox cb) { if (cb.Checked) { //cb.Image = ResizeImage(Properties.Resources.BlackKnight, cb.Size.Width/2, cb.Size.Height/2); cb.BackColor = MyCheckBox.chessBoardSelectedColor; } else { cb.BackColor = cb.checkerBoardColor; } //Disable All Invalid moves Game currGame = ((Game)gamesListBox.SelectedItem); Board board = currGame.gameBoard; BoardSpace boardPos = cb.getLinkedBoardSpace(); if (boardPos.piece != null && boardPos.piece.color == currGame.color) { List <Move> validMoves = boardPos.piece.getAvailableMoves(board); foreach (Move mv in validMoves) { // Highlight valid Moves bool found = false; for (int row = 1; row <= 8; row++) { for (Char col = 'A'; col <= 'H'; col++) { MyCheckBox itCB = chessBoardList[row - 1, col - 'A']; if (itCB.getLinkedBoardSpace().position.Item2 == mv.toSpace.position.Item2 && itCB.getLinkedBoardSpace().position.Item1 == mv.toSpace.position.Item1) { itCB.BackColor = MyCheckBox.chessBoardValidMoveColor; found = true; break;; } if (found) { break; } } } } } }
public override List <Move> getAvailableMoves(Board boardState) { List <Move> validMoveList = new List <Move>(); // Get Valid Moves BoardSpace toPos = null; // The King Has the Same Moveset whether it is in check or not // First Move for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (!(x == 0 && y == 0)) { toPos = boardState.getSpace(Convert.ToChar(position.position.Item1 + x), position.position.Item2 + y); if (toPos != null) { if (toPos.piece == null || toPos.piece.color != color) { Move newMove = new Move(this, position, toPos, boardState, (toPos.piece == null) ? 0 : toPos.piece.score); Board newBoardAfterMove = new Board(boardState); newBoardAfterMove.updateBoardForMove(newMove); if (!newBoardAfterMove.isKingInCheck(color)) // Check if New Move is in Check { validMoveList.Add(newMove); } } } } } } // Amend Scores for Pieces that Are now attacking this piece // Handle Castle return(validMoveList); }
public Board(Board fromBoard) { // Copy BoardState Array for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { boardState[row, col] = new BoardSpace(fromBoard.boardState[row, col]); } } // Recalculate moves on King updateAttacksOnKing(ChessmanColor.white); updateAttacksOnKing(ChessmanColor.black); // update attacks arrays whiteKingPos = this.getSpace(fromBoard.whiteKingPos.position.Item1, fromBoard.whiteKingPos.position.Item2); blackKingPos = this.getSpace(fromBoard.blackKingPos.position.Item1, fromBoard.blackKingPos.position.Item2); }
public override List <Move> getAvailableMoves(Board boardState) { List <Move> validMoveList = new List <Move>(); // Get Valid Moves BoardSpace toPos = null; for (int x = -2; x <= 2; x++) { for (int y = -2; y <= 2; y++) { if (x != 0 && y != 0 && (Math.Abs(x) != Math.Abs(y))) { toPos = boardState.getSpace(Convert.ToChar(position.position.Item1 + x), position.position.Item2 + y); if (toPos != null) { if (toPos.piece == null || toPos.piece.color != color) { Move newMove = new Move(this, position, toPos, boardState, (toPos.piece == null) ? 0 : toPos.piece.score); validMoveList.Add(newMove); } } } } } // Amend Scores for Pieces that Are now attacking this piece if (boardState.isKingInCheck(color)) { List <Move> afterCheckMoveList = getCheckBlockingMoves(boardState, validMoveList, color); validMoveList.Clear(); validMoveList.AddRange(afterCheckMoveList); } return(validMoveList); }
public Board(String fen = "") { // A1 is Bottom Left Corner, H8 is top Right Corner for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { Char tmp = (Char)('A' + col); int effectiveRow = 8 - row; boardState[row, col] = new BoardSpace(tmp, effectiveRow); if (fen != "") { // Process FEN boardState[row, col].generatePieceFromFen(fen); } else { if (effectiveRow == 2 || effectiveRow == 7) { boardState[row, col].piece = new Pawn(boardState[row, col], (effectiveRow == 2) ? ChessmanColor.white : ChessmanColor.black); } else if (effectiveRow == 1 || effectiveRow == 8) { // Setup White BackRow switch (tmp) { case 'A': boardState[row, col].piece = new Rook(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); break; case 'B': boardState[row, col].piece = new Knight(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); break; case 'C': boardState[row, col].piece = new Bishop(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); break; case 'D': boardState[row, col].piece = new Queen(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); break; case 'E': boardState[row, col].piece = new King(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); if (effectiveRow == 1) { whiteKingPos = boardState[row, col]; } else { blackKingPos = boardState[row, col]; } break; case 'F': boardState[row, col].piece = new Bishop(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); break; case 'G': boardState[row, col].piece = new Knight(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); break; case 'H': boardState[row, col].piece = new Rook(boardState[row, col], ((effectiveRow == 1) ? ChessmanColor.white : ChessmanColor.black)); break; } } } } } }
private Chessman movePieceOnBoard(BoardSpace fromSpace, BoardSpace toSpace, String extraArgs = "") { // This is kust updating the data model. Validation Should occur prior to this // This function Assumes that the moves are valid. Chessman fromPiece = fromSpace.piece; Chessman killedPiece = null; if (toSpace.piece != null) { // We killed piece remove it. killedPiece = toSpace.piece; } fromPiece.position = toSpace; toSpace.piece = fromPiece; fromSpace.piece = null; if (toSpace.piece.GetType() == typeof(King)) { King kp = (King)toSpace.piece; if (kp.canCastle) { kp.canCastle = false; } if (kp.color == ChessmanColor.white) { whiteKingPos = toSpace; } else { blackKingPos = toSpace; } } else if (toSpace.piece.GetType() == typeof(Rook)) { Rook rp = (Rook)toSpace.piece; if (rp.canCastle) { rp.canCastle = false; } } else if (toSpace.piece.GetType() == typeof(Pawn)) { ((Pawn)toSpace.piece).canMoveTwice = false; // Pawns Can only Move Twice on the first move if ((toSpace.piece.color == ChessmanColor.black && toSpace.position.Item2 == 1) || (toSpace.piece.color == ChessmanColor.white && toSpace.position.Item2 == 8)) { Char promotionType = 'q'; if (extraArgs.Length > 0) { promotionType = extraArgs.ToLower()[0]; } switch (promotionType) { case 'q': // Promote to Queen Queen q = new Queen(toSpace, toSpace.piece.color); toSpace.piece = q; break; case 'b': // Promote to Queen Bishop b = new Bishop(toSpace, toSpace.piece.color); toSpace.piece = b; break; case 'n': // Promote to Queen Knight kn = new Knight(toSpace, toSpace.piece.color); toSpace.piece = kn; break; case 'r': // Promote to Queen Rook r = new Rook(toSpace, toSpace.piece.color); toSpace.piece = r; break; } } } updateAttacksOnKing(toSpace.piece.color); return(killedPiece); }
public bool updateBoardForMove(String move) { bool moveValid = false; String fromPos = move.Substring(0, 2).ToUpper(); String toPos = move.Substring(2, 2).ToUpper(); String extraArgs = ""; if (move.Length > 4) { extraArgs = move.Substring(4, move.Length - 4); } try { BoardSpace fromSpace = getSpace(fromPos[0], int.Parse(new String(new char[] { fromPos[1] }))); BoardSpace toSpace = getSpace(toPos[0], int.Parse(new String(new char[] { toPos[1] }))); if (fromSpace.piece != null) { Chessman fromPiece = fromSpace.piece; if (true) // Do check if move is valid for piece type { if (fromPiece.GetType() == typeof(King) && ((King)fromPiece).canCastle && ((fromPiece.color == ChessmanColor.white && fromSpace.position.Item1 == 'E' && fromSpace.position.Item2 == 1 && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 1) || (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 1)) || (fromPiece.color == ChessmanColor.black && fromSpace.position.Item1 == 'E' && fromSpace.position.Item2 == 8 && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 8) || (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 8)))) { King kingPiece = ((King)fromPiece); //Attempting a castle VAlidate it BoardSpace rookSpace = null; if (kingPiece.color == ChessmanColor.white && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 1)) { // White King Side Castle rookSpace = getSpace('H', 1); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('F', 1).piece == null && getSpace('G', 1).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E1(Check),F1, G1) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('F', 1), extraArgs); // Move Rook moveValid = true; } } } } else if (kingPiece.color == ChessmanColor.white && (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 1)) { // White Queen Side Castle rookSpace = getSpace('A', 1); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('B', 1).piece == null && getSpace('C', 1).piece == null && getSpace('D', 1).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E1(Check), D1, C1) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('D', 1), extraArgs); // Move Rook moveValid = true; } } } } else if (kingPiece.color == ChessmanColor.black && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 8)) { // White King Side Castle rookSpace = getSpace('H', 8); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('F', 8).piece == null && getSpace('G', 8).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E8(Check),F8, G8) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('F', 8), extraArgs); // Move Rook moveValid = true; } } } } else if (kingPiece.color == ChessmanColor.black && (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 8)) { // White Queen Side Castle rookSpace = getSpace('A', 8); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('B', 8).piece == null && getSpace('C', 8).piece == null && getSpace('D', 8).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E1(Check), D1, C1) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('D', 8), extraArgs); // Move Rook moveValid = true; } } } } if (moveValid) { // Grab All King and Rooks for the color and disable castling foreach (BoardSpace s in boardState) { if (s.piece != null && s.piece.color == fromPiece.color) { if (s.piece.GetType() == typeof(Rook)) { ((Rook)s.piece).canCastle = false; } else if (s.piece.GetType() == typeof(King)) { ((King)s.piece).canCastle = false; } } } } } else { movePieceOnBoard(fromSpace, toSpace, extraArgs); moveValid = true; } } } else { // There should always be a piece in the from Space } } catch { } return(moveValid); }
public static List <Move> getMovesForBishop(Board boardState, ChessmanColor color, BoardSpace position) { List <Move> validMoveList = new List <Move>(); //Starting At Inital Position moves the 4 directions until the board is gone or a piece is reached char col = Convert.ToChar(position.position.Item1 + 1); int row = position.position.Item2 + 1; for (; row <= 8 && col <= 'H'; row++, col++) { // increasing Diagonal BoardSpace moveSpace = boardState.getSpace(col, row); if (moveSpace != null) { if (moveSpace.piece == null) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, 0)); } else if (moveSpace.piece.color != color) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, moveSpace.piece.score)); break; } else { break; } } } col = Convert.ToChar(position.position.Item1 - 1); row = position.position.Item2 - 1; for (; row >= 1 && col >= 'A'; row--, col--) { // increasing Diagonal BoardSpace moveSpace = boardState.getSpace(col, row); if (moveSpace != null) { if (moveSpace.piece == null) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, 0)); } else if (moveSpace.piece.color != color) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, moveSpace.piece.score)); break; } else { break; } } } col = Convert.ToChar(position.position.Item1 + 1); row = position.position.Item2 - 1; for (; row >= 1 && col <= 'H'; row--, col++) { // increasing Diagonal BoardSpace moveSpace = boardState.getSpace(col, row); if (moveSpace != null) { if (moveSpace.piece == null) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, 0)); } else if (moveSpace.piece.color != color) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, moveSpace.piece.score)); break; } else { break; } } } col = Convert.ToChar(position.position.Item1 - 1); row = position.position.Item2 + 1; for (; row <= 8 && col >= 'A'; row++, col--) { // increasing Diagonal BoardSpace moveSpace = boardState.getSpace(col, row); if (moveSpace != null) { if (moveSpace.piece == null) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, 0)); } else if (moveSpace.piece.color != color) { validMoveList.Add(new Move(position.piece, position, moveSpace, boardState, moveSpace.piece.score)); break; } else { break; } } } // Amend Scores for Pieces that Are now attacking this piece if (boardState.isKingInCheck(color)) { List <Move> afterCheckMoveList = getCheckBlockingMoves(boardState, validMoveList, color); validMoveList.Clear(); validMoveList.AddRange(afterCheckMoveList); } return(validMoveList); }
public override List <Move> getAvailableMoves(Board boardState) { List <Move> validMoveList = new List <Move>(); // Get Valid Moves int numMoves = (canMoveTwice) ? 2 : 1; BoardSpace toPos = null; // Normal Moves // First Move for (int i = 1; i <= numMoves; i++) { toPos = boardState.getSpace(Convert.ToChar(position.position.Item1), (color == ChessmanColor.white) ? position.position.Item2 + i : position.position.Item2 - i); if (toPos != null) { if (toPos.piece == null) { Move newMove = new Move(this, position, toPos, boardState, 0); // Is A Promoting Move if ((color == ChessmanColor.white) ? (toPos.position.Item2 == 8) : (toPos.position.Item2 == 1)) { newMove.score = AIWeights.queenScore; } validMoveList.Add(newMove); } else { break; } } else { continue; } } // Attacking Moves toPos = boardState.getSpace(Convert.ToChar(position.position.Item1 + 1), (color == ChessmanColor.white) ? position.position.Item2 + 1 : position.position.Item2 - 1); BoardSpace toPos2 = boardState.getSpace(Convert.ToChar(position.position.Item1 - 1), (color == ChessmanColor.white) ? position.position.Item2 + 1 : position.position.Item2 - 1); List <BoardSpace> possibleMoves = new List <BoardSpace>() { toPos, toPos2 }; foreach (BoardSpace space in possibleMoves) { if (space != null) { if (space != null && space.piece != null && space.piece.color == Helper.getOpponentColor(color)) { validMoveList.Add(new Move(this, position, space, boardState, ((space.piece.color == Helper.getOpponentColor(color)) ? space.piece.score : 0))); } } else { continue; } } // Amend Scores for Pieces that Are now attacking this piece if (boardState.isKingInCheck(color)) { List <Move> afterCheckMoveList = getCheckBlockingMoves(boardState, validMoveList, color); validMoveList.Clear(); validMoveList.AddRange(afterCheckMoveList); } return(validMoveList); }