/// <summary> /// This function is used to remove all captured pieces /// </summary> public void removeCapturedPieces() { //If it is white's turn, remove all captured black pieces if (turnColor == PieceColor.White) { for (int index = 0; index < blackGamePieces.Count(); index++) { if (blackGamePieces[index].getCaptureStatus() == true) { gameBoard.getTileAt(blackGamePieces[index].position).unOccupy(); blackGamePieces.RemoveAt(index); index--; } } } //else remove all captured white pieces else { for (int index = 0; index < whiteGamePieces.Count(); index++) { if (whiteGamePieces[index].getCaptureStatus() == true) { gameBoard.getTileAt(whiteGamePieces[index].position).unOccupy(); whiteGamePieces.RemoveAt(index); index--; } } } }
/// <summary> /// Constructor for a state. /// A new game state is initialized by passing in the color the player is playing as. /// </summary> /// <param name="playerClr">The color for the player</param> public CheckersGameState(PieceColor playerClr) { gameBoard = new CheckersBoard(); whiteGamePieces = new List <CheckersPiece>(); blackGamePieces = new List <CheckersPiece>(); turnColor = PieceColor.None; winner = PieceColor.None; jumpExists = false; //If a player color is passed in, the blow will initialize all the white and black pieces. if (playerClr != PieceColor.None) { Vector2 blackPosition; Vector2 whitePosition; for (int index = 0; index < MAX_PIECES; index++) { //calculate the position for the black piece and the white piece if (index < 3) { blackPosition = new Vector2(index * 2f, 5 - (index / 3)); whitePosition = new Vector2(index * 2f + 1, index / 3); } else { blackPosition = new Vector2((index * 2f + 1) - 6, 5 - (index / 3)); whitePosition = new Vector2((index * 2f) - 6, index / 3); } //Create the pieces with the calculated positions and add them the the list CheckersPiece blackPiece = new CheckersPiece(new Vector2(blackPosition.X, blackPosition.Y), PieceColor.Black); CheckersPiece whitePiece = new CheckersPiece(new Vector2(whitePosition.X, whitePosition.Y), PieceColor.White); blackGamePieces.Add(blackPiece); whiteGamePieces.Add(whitePiece); //Set the tiles to be occupied gameBoard.getTileAt(blackPosition).occupy(PieceColor.Black); gameBoard.getTileAt(whitePosition).occupy(PieceColor.White); } //Set the player and ai colors if (playerClr == PieceColor.Black) { playerColor = PieceColor.Black; aiColor = PieceColor.White; } else { playerColor = PieceColor.White; aiColor = PieceColor.Black; } } }
/// <summary> /// Function that will determine all possible moves for the piece /// </summary> /// <param name="gameBoard">The gameboard</param> /// <param name="gameState">The current state of the game</param> /// <param name="noShow">Whether or not to mark the board</param> /// <returns>Type of move the piece has (NONE, MOVE, JUMP)</returns> public TileStatus determineMoves(CheckersBoard gameBoard, CheckersGameState gameState, bool noShow = false) { TileStatus foundMove = TileStatus.NONE; possibleMoves.Clear(); Vector2 tilePosition = new Vector2(position.X, position.Y); List <GameMove> jumps = getJumps(tilePosition, gameBoard, gameState); //Look for any jumps by the piece if (jumps.Count != 0) { foundMove = TileStatus.JUMP; possibleMoves = jumps; gameState.jumpExists = true; //Reset that the piece was just jumped over //Necessary for multi-jumps if (color == PieceColor.Black) { foreach (CheckersPiece piece in gameState.whiteGamePieces) { piece.justJumpedOver = false; } } else if (color == PieceColor.White) { foreach (CheckersPiece piece in gameState.blackGamePieces) { piece.justJumpedOver = false; } } } //If no jump was found in the context of the turn find possible regular moves for the piece else if (gameState.jumpExists == false) { List <GameMove> regularMoves = getRegularMoves(tilePosition, gameBoard, gameState); if (regularMoves.Count != 0) { foundMove = TileStatus.MOVE; possibleMoves = regularMoves; } } if (noShow == false) { gameBoard.clearMarkings(); if (foundMove != TileStatus.NONE) { foreach (GameMove move in possibleMoves) { gameBoard.getTileAt(move.destinationPosition).setStatus(foundMove); } } } return(foundMove); }
/// <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> /// 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); }