/// <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> /// Copy constructor to make a new copy of this current state /// This constructor is used to create temporary states in the ai. /// </summary> /// <param name="gameState">The state to be copied</param> public CheckersGameState(CheckersGameState gameState) { whiteGamePieces = new List <CheckersPiece>(); blackGamePieces = new List <CheckersPiece>(); moveablePieces = new List <CheckersPiece>(); gameBoard = new CheckersBoard(gameState.gameBoard); foreach (CheckersPiece piece in gameState.blackGamePieces) { CheckersPiece newPiece = new CheckersPiece(piece); //find the active game piece if (gameState.activeGamePiece != null && newPiece.position == gameState.activeGamePiece.position) { activeGamePiece = newPiece; } blackGamePieces.Add(newPiece); } foreach (CheckersPiece piece in gameState.whiteGamePieces) { CheckersPiece newPiece = new CheckersPiece(piece); //find the active game piece if (gameState.activeGamePiece != null && newPiece.position == gameState.activeGamePiece.position) { activeGamePiece = newPiece; } whiteGamePieces.Add(newPiece); } foreach (CheckersPiece piece in gameState.moveablePieces) { CheckersPiece newPiece = new CheckersPiece(piece); moveablePieces.Add(newPiece); } playerColor = gameState.playerColor; aiColor = gameState.aiColor; turnColor = gameState.turnColor; jumpExists = gameState.jumpExists; winner = gameState.winner; numTurnsPassed = gameState.numTurnsPassed; }
/// <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); }