/// <summary> /// Copy constructor for the BoardTile class /// This constructor is used for the generation of temporary states /// </summary> /// <param name="tile">The tile to copy</param> public BoardTile(BoardTile tile) { position = new Vector2(tile.position.X, tile.position.Y); status = tile.status; isOccupied = tile.isOccupied; tileArea = tile.tileArea; }
/// <summary> /// Copy constructor for the checkers board. /// This constructor is used for creating temporary states used in the AI. /// </summary> /// <param name="board">A board to copy</param> public CheckersBoard(CheckersBoard board) { tiles = new BoardTile[6, 6]; for (int x = 0; x < MAX_HORIZONTAL_TILES; x++) { for (int y = 0; y < MAX_VERTICAL_TILES; y++) { BoardTile copyTile = board.tiles[x, y]; tiles[x, y] = new BoardTile(copyTile); } } }
/// <summary> /// Default constructor for the checkers board /// </summary> public CheckersBoard() { position = Vector2.Zero; tiles = new BoardTile[6, 6]; for (int x = 0; x < MAX_HORIZONTAL_TILES; x++) { for (int y = 0; y < MAX_VERTICAL_TILES; y++) { Vector2 tilePosition = new Vector2(x, y); tiles[x, y] = new BoardTile(tilePosition); } } }
/// <summary> /// This function is for handling any tile actions. /// Function takes a tile as a parameter. If the tile is marked as a possible move, /// then the function will apply the logic necessary for the move. /// </summary> /// <param name="tile">A tile to do an action on</param> /// <returns>Whether or not the tile was valid for move</returns> public bool doTileAction(BoardTile tile) { bool actionDone = false; List <GameMove> availableMoves = new List <GameMove>(); //Apply logic only if the tile is marked for a possible action (i.e. MOVE, JUMP) if (tile.getStatus() != TileStatus.NONE) { //Get the moves for the active piece availableMoves = activeGamePiece.getPossibleMoves(); //The below loop finds the move that the tile correlates to foreach (GameMove move in availableMoves) { //Below is the logic for when the move is found if (tile.position == move.destinationPosition) { //If the tile is marked for jump, also capture the pieces // specified in the move and remove them if (tile.getStatus() == TileStatus.JUMP) { //Capture the pieces specified in the move foreach (CheckersPiece capturedPiece in move.capturedPieces) { var piece = getPiece(capturedPiece.getColor(), capturedPiece.position); activeGamePiece.Capture(piece, gameBoard.getTileAt(capturedPiece.position)); } //Remove captured pieces from the game removeCapturedPieces(); } //Move the active piece to the destination tile activeGamePiece.moveTo(tile); //Unoccupy the tile the active piece came from gameBoard.getTileAt((int)(activeGamePiece.position.X), (int)(activeGamePiece.position.Y)).unOccupy(); //Remove all marking of tiles gameBoard.clearMarkings(); actionDone = true; break; } } } return(actionDone); }
/// <summary> /// Function that will find all regular moves for this piece /// A regular move is a non-jump move /// </summary> /// <param name="tilePosition">The position of the tile to check jumps from</param> /// <param name="gameBoard">The gameboard</param> /// <param name="gameState">The current gamestate</param> /// <returns>A list of possible regular moves</returns> public List <GameMove> getRegularMoves(Vector2 tilePosition, CheckersBoard gameBoard, CheckersGameState gameState) { List <GameMove> regularMoves = new List <GameMove>(); //Below is the logic for if the checkers piece is black if (color == PieceColor.Black) { //Get the two possible tiles a black piece could move to BoardTile topLeftTile = gameBoard.getTileAt((int)tilePosition.X - 1, (int)tilePosition.Y - 1); BoardTile topRightTile = gameBoard.getTileAt((int)tilePosition.X + 1, (int)tilePosition.Y - 1); //If the tile exists on the board, add that tile as a possible move if (topLeftTile != null && topLeftTile.getOccupiedStatus() == PieceColor.None) { regularMoves.Add(new GameMove(this, topLeftTile.position)); } if (topRightTile != null && topRightTile.getOccupiedStatus() == PieceColor.None) { regularMoves.Add(new GameMove(this, topRightTile.position)); } } //Below is the logic for if the checkers piece is white if (color == PieceColor.White) { //Get the two possible tiles a black piece could move to BoardTile bottomLeftTile = gameBoard.getTileAt((int)tilePosition.X - 1, (int)tilePosition.Y + 1); BoardTile bottomRightTile = gameBoard.getTileAt((int)tilePosition.X + 1, (int)tilePosition.Y + 1); //If the tile exists on the board, add that tile as a possible move if (bottomLeftTile != null && bottomLeftTile.getOccupiedStatus() == PieceColor.None) { regularMoves.Add(new GameMove(this, bottomLeftTile.position)); } if (bottomRightTile != null && bottomRightTile.getOccupiedStatus() == PieceColor.None) { regularMoves.Add(new GameMove(this, bottomRightTile.position)); } } return(regularMoves); }
/// <summary> /// This method will move this (CheckersPiece) to the tile that as passed in /// </summary> /// <param name="tile">The tile to move the piece to</param> public void moveTo(BoardTile tile) { destination = tile.position; tile.occupy(color); }
/// <summary> /// Function that will recursively find all the jump sequences possible by this piece /// </summary> /// <param name="tilePosition">The position of the tile to check jumps from</param> /// <param name="gameBoard">The gameboard</param> /// <param name="gameState">The current gamestate</param> /// <returns>A list of possible jumps</returns> public List <GameMove> getJumps(Vector2 tilePosition, CheckersBoard gameBoard, CheckersGameState gameState) { List <GameMove> jumps = new List <GameMove>(); //Get all the possible tiles that could result with a jump BoardTile topLeftTile = gameBoard.getTileAt((int)tilePosition.X - 1, (int)tilePosition.Y - 1); BoardTile topRightTile = gameBoard.getTileAt((int)tilePosition.X + 1, (int)tilePosition.Y - 1); BoardTile bottomLeftTile = gameBoard.getTileAt((int)tilePosition.X - 1, (int)tilePosition.Y + 1); BoardTile bottomRightTile = gameBoard.getTileAt((int)tilePosition.X + 1, (int)tilePosition.Y + 1); BoardTile topLeftJumpTile = gameBoard.getTileAt((int)tilePosition.X - 2, (int)tilePosition.Y - 2); BoardTile topRightJumpTile = gameBoard.getTileAt((int)tilePosition.X + 2, (int)tilePosition.Y - 2); BoardTile bottomLeftJumpTile = gameBoard.getTileAt((int)tilePosition.X - 2, (int)tilePosition.Y + 2); BoardTile bottomRightJumpTile = gameBoard.getTileAt((int)tilePosition.X + 2, (int)tilePosition.Y + 2); //Set the enemy color PieceColor enemyColor; if (this.color == PieceColor.Black) { enemyColor = PieceColor.White; } else { enemyColor = PieceColor.Black; } //The below four if-elses will basically determine if there was a piece of the enemy color was jumped over //by moving to a tile 2 diagonal tiles away. If there was a jump, it will mark the piece that it just jumped over //and then recursively look for any other following jumps from that tile. if (topLeftTile != null && topLeftTile.getOccupiedStatus() == enemyColor) { //Get the piece that was jumped over CheckersPiece adjacentPiece = gameState.getPiece(enemyColor, topLeftTile.position); if (topLeftJumpTile != null && topLeftJumpTile.getOccupiedStatus() == PieceColor.None && adjacentPiece.justJumpedOver == false) { //mark the piece as jumped over adjacentPiece.justJumpedOver = true; //Find any jumps from the new tile position List <GameMove> sequenceJumps = getJumps(topLeftJumpTile.position, gameBoard, gameState); //If there was no following jump then add the jump to the list of jump if (sequenceJumps.Count == 0) { GameMove newJump = (new GameMove(this, topLeftJumpTile.position)); newJump.capturedPieces.Add(adjacentPiece); jumps.Add(newJump); } //Else for each following jump, add the piece the first jump captured to the list of captured pieces else { foreach (GameMove jump in sequenceJumps) { //GameMove newJump = (new GameMove(this, jump.destinationPosition)); //jump.capturedPieces = jump.capturedPieces; jump.capturedPieces.Add(adjacentPiece); jumps.Add(jump); } } } } if (topRightTile != null && topRightTile.getOccupiedStatus() == enemyColor) { CheckersPiece adjacentPiece = gameState.getPiece(enemyColor, topRightTile.position); if (topRightJumpTile != null && topRightJumpTile.getOccupiedStatus() == PieceColor.None && adjacentPiece.justJumpedOver == false) { //get any jumps from that tile adjacentPiece.justJumpedOver = true; List <GameMove> sequenceJumps = getJumps(topRightJumpTile.position, gameBoard, gameState); if (sequenceJumps.Count == 0) { GameMove newJump = (new GameMove(this, topRightJumpTile.position)); newJump.capturedPieces.Add(adjacentPiece); jumps.Add(newJump); } else { foreach (GameMove jump in sequenceJumps) { GameMove newJump = (new GameMove(this, jump.destinationPosition)); newJump.capturedPieces = jump.capturedPieces; newJump.capturedPieces.Add(adjacentPiece); jumps.Add(newJump); } } } } if (bottomLeftTile != null && bottomLeftTile.getOccupiedStatus() == enemyColor) { CheckersPiece adjacentPiece = gameState.getPiece(enemyColor, bottomLeftTile.position); if (bottomLeftJumpTile != null && bottomLeftJumpTile.getOccupiedStatus() == PieceColor.None && adjacentPiece.justJumpedOver == false) { //get any jumps from that tile adjacentPiece.justJumpedOver = true; List <GameMove> sequenceJumps = getJumps(bottomLeftJumpTile.position, gameBoard, gameState); if (sequenceJumps.Count == 0) { GameMove newJump = (new GameMove(this, bottomLeftJumpTile.position)); newJump.capturedPieces.Add(adjacentPiece); jumps.Add(newJump); } else { foreach (GameMove jump in sequenceJumps) { GameMove newJump = (new GameMove(this, jump.destinationPosition)); newJump.capturedPieces = jump.capturedPieces; newJump.capturedPieces.Add(adjacentPiece); jumps.Add(newJump); } } } } if (bottomRightTile != null && bottomRightTile.getOccupiedStatus() == enemyColor) { CheckersPiece adjacentPiece = gameState.getPiece(enemyColor, bottomRightTile.position); if (bottomRightJumpTile != null && bottomRightJumpTile.getOccupiedStatus() == PieceColor.None && adjacentPiece.justJumpedOver == false) { //get any jumps from that tile adjacentPiece.justJumpedOver = true; List <GameMove> sequenceJumps = getJumps(bottomRightJumpTile.position, gameBoard, gameState); if (sequenceJumps.Count == 0) { GameMove newJump = (new GameMove(this, bottomRightJumpTile.position)); newJump.capturedPieces.Add(adjacentPiece); jumps.Add(newJump); } else { foreach (GameMove jump in sequenceJumps) { GameMove newJump = (new GameMove(this, jump.destinationPosition)); newJump.capturedPieces = jump.capturedPieces; newJump.capturedPieces.Add(adjacentPiece); jumps.Add(newJump); } } } } return(jumps); }
/// <summary> /// Function to capture a checkerpiece given the piece, and its tile position /// </summary> /// <param name="piece">The piece to be captured</param> /// <param name="tile">The tile the piece is on</param> public void Capture(CheckersPiece piece, BoardTile tile) { tile.unOccupy(); piece.isCaptured = true; }