private void PossibleHorizontalDiagonalJumps(List <string> validMoves, int direction) { PlayerCoordinate playerCoordinate = GetCurrentPlayerCoodinate(); PlayerCoordinate opponentCoord = GetCurrentOpponentCoordinate(); if (playerCoordinate.Row + 1 < 17 && playerCoordinate.Col + 2 * direction < 17 && playerCoordinate.Col + 2 * direction > -1 && board[playerCoordinate.Row + 1, playerCoordinate.Col + 2 * direction] != WALL) { StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (opponentCoord.Col / 2))); sb.Append(value: 9 - (opponentCoord.Row / 2) - 1 < 1 ? 1 : 9 - (opponentCoord.Row / 2) - 1); validMoves.Add(sb.ToString()); } if (playerCoordinate.Row - 1 > -1 && playerCoordinate.Col + 2 * direction < 17 && playerCoordinate.Col + 2 * direction > -1 && board[playerCoordinate.Row - 1, playerCoordinate.Col + 2 * direction] != WALL) { StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (opponentCoord.Col / 2))); sb.Append(value: 9 - (opponentCoord.Row / 2) + 1 > 9 ? 9 : 9 - (opponentCoord.Row / 2) + 1); validMoves.Add(sb.ToString()); } }
public WallCoordinate(string str) { if (str.Length != 3 || !(str[2] == 'v' || str[2] == 'h')) { throw new Exception("Invalid input format"); } if (str[2] == 'v') { Orientation = WallOrientation.Vertical; } else { Orientation = WallOrientation.Horizontal; } StandardNotationString = str; PlayerCoordinate referenceCoordinate = new PlayerCoordinate(StandardNotationString.Substring(0, 2)); if (Orientation == WallOrientation.Vertical) { StartRow = referenceCoordinate.Row; StartCol = referenceCoordinate.Col + 1; EndRow = StartRow - 2; EndCol = StartCol; } else { StartRow = referenceCoordinate.Row - 1; StartCol = referenceCoordinate.Col; EndRow = StartRow; EndCol = StartCol + 2; } }
private void PossibleVerticalDiagonalJumps(List <string> validMoves, int direction) { PlayerCoordinate playerCoordinate = GetCurrentPlayerCoodinate(); PlayerCoordinate opponentCoord = GetCurrentOpponentCoordinate(); if (playerCoordinate.Col + 1 < 17 && playerCoordinate.Row + 2 * direction < 17 && playerCoordinate.Row + 2 * direction > -1 && board[playerCoordinate.Row + 2 * direction, playerCoordinate.Col + 1] != WALL) { StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(value: 97 + (opponentCoord.Col / 2) + 1 > 105 ? 105 : 97 + (opponentCoord.Col / 2) + 1)); sb.Append(9 - (opponentCoord.Row / 2)); validMoves.Add(sb.ToString()); } if (playerCoordinate.Col - 1 > -1 && playerCoordinate.Row + 2 * direction < 17 && playerCoordinate.Row + 2 * direction > -1 && board[playerCoordinate.Row + 2 * direction, playerCoordinate.Col - 1] != WALL) { StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(value: 97 + (opponentCoord.Col / 2) - 1 < 97 ? 97 : 97 + (opponentCoord.Col / 2) - 1)); sb.Append(9 - (opponentCoord.Row / 2)); validMoves.Add(sb.ToString()); } }
public GameBoard(GameBoard boardState) { gameOver = false; playerOneWin = false; playerTwoWin = false; playerOneLocation = new PlayerCoordinate(boardState.playerOneLocation.Row, boardState.playerOneLocation.Col); playerTwoLocation = new PlayerCoordinate(boardState.playerTwoLocation.Row, boardState.playerTwoLocation.Col); board = new char[TOTAL_ROWS, TOTAL_COLS]; for (int r = 0; r < TOTAL_ROWS; ++r) { for (int c = 0; c < TOTAL_COLS; ++c) { if ((r % 2 == 0) && (c % 2 == 0)) { board[r, c] = PLAYER_SPACE; } else { if (boardState.board[r, c].Equals(WALL)) { board[r, c] = WALL; } else { board[r, c] = WALL_SPACE; } } } } }
private void PossibleVerticalJumps(List <string> validMoves, int direction) { PlayerCoordinate playerCoordinate = GetCurrentPlayerCoodinate(); PlayerCoordinate opponentCoord = GetCurrentOpponentCoordinate(); if (playerCoordinate.Row + (3 * direction) < 17 && playerCoordinate.Row + (3 * direction) > -1) { if (board[playerCoordinate.Row + (3 * direction), playerCoordinate.Col] != WALL) { StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (opponentCoord.Col / 2))); sb.Append(value: 9 - (opponentCoord.Row / 2) - (1 * direction) > 9 ? 9 : 9 - (opponentCoord.Row / 2) - (1 * direction) < 1 ? 1 : 9 - (opponentCoord.Row / 2) - (1 * direction)); validMoves.Add(sb.ToString()); } else { PossibleVerticalDiagonalJumps(validMoves, direction); } } else { PossibleVerticalDiagonalJumps(validMoves, direction); } }
private bool IsDestinationAdjacent(PlayerCoordinate start, PlayerCoordinate destination) { bool verticalMove = (Math.Abs(destination.Row - start.Row) == 2) && (Math.Abs(destination.Col - start.Col) == 0); bool horizontalMove = (Math.Abs(destination.Col - start.Col) == 2) && (Math.Abs(destination.Row - start.Row) == 0); return(verticalMove ^ horizontalMove); // Only north south east west are considered adjacent }
private bool IsMoveBlocked(PlayerCoordinate start, PlayerCoordinate destination) { bool blocked = false; if (start.Row == destination.Row) { if (start.Col < destination.Col) { blocked = (board[start.Row, start.Col + 1] == WALL) || (board[destination.Row, destination.Col - 1] == WALL); } else { blocked = (board[start.Row, start.Col - 1] == WALL) || (board[destination.Row, destination.Col + 1] == WALL); } } else if (start.Col == destination.Col) { if (start.Row < destination.Row) { blocked = (board[start.Row + 1, start.Col] == WALL) || (board[destination.Row - 1, destination.Col] == WALL); } else { blocked = (board[start.Row - 1, start.Col] == WALL) || (board[destination.Row + 1, destination.Col] == WALL); } } return(blocked); }
private void PossibleHorizontalJumps(List <string> validMoves, int direction) { PlayerCoordinate playerCoordinate = GetCurrentPlayerCoodinate(); PlayerCoordinate opponentCoord = GetCurrentOpponentCoordinate(); if (playerCoordinate.Col + (3 * direction) < 17 && playerCoordinate.Col + (3 * direction) > -1) { if (board[playerCoordinate.Row, playerCoordinate.Col + (3 * direction)] != WALL) { StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (opponentCoord.Col / 2) + (1 * direction) > 105 ? 105 : 97 + (opponentCoord.Col / 2) + (1 * direction) < 97 ? 97 : 97 + (opponentCoord.Col / 2) + (1 * direction))); sb.Append(9 - (opponentCoord.Row / 2)); validMoves.Add(sb.ToString()); } else { PossibleHorizontalDiagonalJumps(validMoves, direction); } } else { PossibleHorizontalDiagonalJumps(validMoves, direction); } }
private void InitializeBoard(string playerOneStart, string playerTwoStart) { gameOver = false; playerOneWin = false; playerTwoWin = false; lastMove = new List <string>(); lastMove.Add(playerOneStart); lastMove.Add(playerTwoStart); lastStart = new List <string>(); lastStart.Add(playerOneStart); lastStart.Add(playerTwoStart); possibleWalls = new List <string>(); playerOneLocation = new PlayerCoordinate(playerOneStart); playerTwoLocation = new PlayerCoordinate(playerTwoStart); walls = new List <WallCoordinate>(); possibleMoves = new List <List <string> >(); // Init gameboard board = new char[TOTAL_ROWS, TOTAL_COLS]; for (int r = 0; r < TOTAL_ROWS; ++r) { for (int c = 0; c < TOTAL_COLS; ++c) { if ((r % 2 == 0) && (c % 2 == 0)) { board[r, c] = PLAYER_SPACE; } else { board[r, c] = WALL_SPACE; } } } // Build list of possible walls for (int r = 0; r < 8; ++r) { for (int c = 0; c < 8; ++c) { possibleWalls.Add((Convert.ToChar('a' + c)).ToString() + (Convert.ToChar('1' + r)) + 'h'.ToString()); possibleWalls.Add((Convert.ToChar('a' + c)).ToString() + (Convert.ToChar('1' + r)) + 'v'.ToString()); } } PlayerEnum actualStartingPlayer = whoseTurn; whoseTurn = PlayerEnum.ONE; possibleMoves.Add(PossibleMovesFromPosition()); whoseTurn = PlayerEnum.TWO; possibleMoves.Add(PossibleMovesFromPosition()); whoseTurn = actualStartingPlayer; }
private static string RandomNearbyRowMove(PlayerCoordinate player) { StringBuilder sb = new StringBuilder(); int col = random.Next(minValue: player.Col == 0 ? 97 : (97 + (player.Col / 2) - 1), maxValue: player.Col == 16 ? 106 : 97 + (player.Col / 2) + 2); sb.Append(Convert.ToChar(col)); sb.Append(9 - (player.Row / 2)); return(sb.ToString()); }
private bool PlayersAreAdjacent() { PlayerCoordinate playerCoordinate = (whoseTurn == 0 ? (playerOneLocation) : (playerTwoLocation)); PlayerCoordinate opponentCoord = (whoseTurn == 0 ? (playerTwoLocation) : (playerOneLocation)); return(((whoseTurn == 0 ? playerOneLocation : playerTwoLocation).Row == (whoseTurn == 0 ? playerTwoLocation : playerOneLocation).Row && (whoseTurn == 0 ? playerOneLocation : playerTwoLocation).Col + 2 == (whoseTurn == 0 ? playerTwoLocation : playerOneLocation).Col && board[(whoseTurn == 0 ? playerOneLocation : playerTwoLocation).Row, (whoseTurn == 0 ? playerOneLocation : playerTwoLocation).Col + 1] != WALL) || (playerCoordinate.Row == opponentCoord.Row && playerCoordinate.Col - 2 == opponentCoord.Col && board[playerCoordinate.Row, (playerCoordinate.Col - 1)] != WALL) || (playerCoordinate.Row + 2 == opponentCoord.Row && playerCoordinate.Col == opponentCoord.Col && board[playerCoordinate.Row + 1, (playerCoordinate.Col)] != WALL) || (playerCoordinate.Row - 2 == opponentCoord.Row && playerCoordinate.Col == opponentCoord.Col && board[playerCoordinate.Row - 1, playerCoordinate.Col] != WALL)); }
private static string RandomNearbyColMove(PlayerCoordinate player) { StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (player.Col / 2))); int row = random.Next(minValue: player.Row == 16 ? 1 : (9 - (player.Row / 2) - 1), maxValue: player.Row == 0 ? 10 : (9 - (player.Row / 2) + 2)); sb.Append(row); return(sb.ToString()); }
public static string GetRandomNearbyPlayerPieceMoveWeighted(PlayerCoordinate player, int weight) { StringBuilder sb = new StringBuilder(); int col = random.Next((player.Col == 0 ? 97 : ((97 + (player.Col / 2) - 1)) < 97 ? 97 : 97 + (player.Col / 2) - 1), (player.Col == 16 ? 106 : 97 + (player.Col / 2) + 1) > 105 ? 106 : 97 + (player.Col / 2) + 2); sb.Append(Convert.ToChar(col)); int row = random.Next(player.Row == 16 ? 1 : (9 - (player.Row / 2) - 1 < 1 ? 1 : 9 - (player.Row / 2) - 1), player.Row == 0 ? 10 : (9 - (player.Row / 2) + 1 > 9 ? 9 : 9 - (player.Row / 2) + 2)); sb.Append(row); return(sb.ToString()); }
public static string GetRandomNearbyPlayerPieceMove(PlayerCoordinate player) { string move; if (random.Next(0, 2) == 0) { // Retrieves a move from the same column move = RandomNearbyColMove(player); } else { // Retrieves a move from the same row move = RandomNearbyRowMove(player); } return(move); }
public PlayerCoordinate GetPlayerCoordinate(PlayerEnum player) { PlayerCoordinate playerCoordinate = null; switch (player) { case PlayerEnum.ONE: playerCoordinate = playerOneLocation; break; case PlayerEnum.TWO: playerCoordinate = playerTwoLocation; break; } return(playerCoordinate); }
private bool IsMoveOnOpenSpace(PlayerEnum player, PlayerCoordinate destination) { bool onPlayerSpace = destination.Row % 2 == 0 && // odd rows are walls destination.Col % 2 == 0; // odd cols are walls bool isSpaceEmpty; if (player == PlayerEnum.ONE) { isSpaceEmpty = !(destination.Row == playerTwoLocation.Row && destination.Col == playerTwoLocation.Col); } else { isSpaceEmpty = !(destination.Row == playerOneLocation.Row && destination.Col == playerOneLocation.Col); } return(onPlayerSpace && isSpaceEmpty); }
public bool IsValidPlayerMove(PlayerEnum player, PlayerCoordinate start, PlayerCoordinate destination) { if (gameOver || !IsMoveInBounds(destination.Row, destination.Col)) { return(false); } bool onPlayerSpace = IsMoveOnOpenSpace(player, destination); bool blocked = IsMoveBlocked(start, destination); bool canReach = IsDestinationAdjacent(start, destination); if (!canReach) { canReach = IsValidJump(player, start, destination); } return(onPlayerSpace && !blocked && canReach); }
public static bool IsMoveAdjacentToPosition(string move, PlayerCoordinate position) { string positionAsString = PlayerCoordinateToString(position); if (Convert.ToChar(positionAsString[1] + 1) == move[1] && positionAsString[0] == move[0]) { return(true); } if (Convert.ToChar(positionAsString[0] + 1) == move[0] && positionAsString[1] == move[1]) { return(true); } if (Convert.ToChar(positionAsString[1] - 1) == move[1] && positionAsString[0] == move[0]) { return(true); } if (Convert.ToChar(positionAsString[0] - 1) == move[0] && positionAsString[1] == move[1]) { return(true); } return(false); }
public WallCoordinate(int x, int y, char c) { char orientation = char.ToLower(c); if (x < 0 || x >= GameBoard.TOTAL_ROWS || y < 0 || y >= GameBoard.TOTAL_COLS || !(orientation == 'h' || orientation == 'v')) { throw new Exception("Invalid input format"); } if (orientation == 'h') { Orientation = WallOrientation.Horizontal; } else if (orientation == 'v') { Orientation = WallOrientation.Vertical; } StandardNotationString = x.ToString() + y.ToString() + c; PlayerCoordinate referenceCoordinate = new PlayerCoordinate(x, y); if (Orientation == WallOrientation.Vertical) { StartRow = referenceCoordinate.Row; StartCol = referenceCoordinate.Col + 1; EndRow = StartRow - 2; EndCol = StartCol; } else { StartRow = referenceCoordinate.Row - 1; StartCol = referenceCoordinate.Col; EndRow = StartRow; EndCol = StartCol + 2; } }
private Tuple <int, int> FindMidpoint(PlayerCoordinate start, PlayerCoordinate destination) { return(new Tuple <int, int>((start.Row + destination.Row) / 2, (start.Col + destination.Col) / 2)); }
private bool IsValidJump(PlayerEnum player, PlayerCoordinate start, PlayerCoordinate destination) { // Jumping over? Tuple <int, int> midpoint = FindMidpoint(start, destination); int midRow = midpoint.Item1; int midCol = midpoint.Item2; int opponentRow, opponentCol; if (player == PlayerEnum.ONE) { opponentRow = playerTwoLocation.Row; opponentCol = playerTwoLocation.Col; } else { opponentRow = playerOneLocation.Row; opponentCol = playerOneLocation.Col; } bool overJump = midRow == opponentRow && midCol == opponentCol && (Math.Abs(destination.Row - start.Row) == 4 || Math.Abs(destination.Col - start.Col) == 4); // Diagonal jump? bool diagonalJump = false; PlayerCoordinate opponent; if (player == PlayerEnum.ONE) { opponent = new PlayerCoordinate(playerTwoLocation.Row, playerTwoLocation.Col); } else { opponent = new PlayerCoordinate(playerOneLocation.Row, playerTwoLocation.Col); } if (start.Row != destination.Row && start.Col != destination.Col) { int targetOppRow, targetOppoCol; if (destination.Row == start.Row - 2 && destination.Col == start.Col + 2) // NE { targetOppRow = start.Row - 2; targetOppoCol = start.Col + 2; diagonalJump = ((opponent.Row == targetOppRow && opponent.Col == start.Col) || (opponent.Row == start.Row && opponent.Col == targetOppoCol)) && ((start.Row - 3 == -1 || start.Col + 3 == 17) || (board[start.Row - 3, start.Col] == WALL || board[start.Row, start.Col + 3] == WALL)); } else if (destination.Row == start.Row - 2 && destination.Col == start.Col - 2) // NW { targetOppRow = start.Row - 2; targetOppoCol = start.Col - 2; diagonalJump = ((opponent.Row == targetOppRow && opponent.Col == start.Col) || (opponent.Row == start.Row && opponent.Col == targetOppoCol)) && ((start.Row - 3 == -1 || start.Col - 3 == -1) || (board[start.Row - 3, start.Col] == WALL || board[start.Row, start.Col - 3] == WALL)); } else if (destination.Row == start.Row + 2 && destination.Col == start.Col - 2) // SW { targetOppRow = start.Row + 2; targetOppoCol = start.Col - 2; diagonalJump = ((opponent.Row == targetOppRow && opponent.Col == start.Col) || (opponent.Row == start.Row && opponent.Col == targetOppoCol)) && ((start.Row + 3 == 17 || start.Col - 3 == -1) || (board[start.Row + 3, start.Col] == WALL || board[start.Row, start.Col - 3] == WALL)); } else if (destination.Row == start.Row + 2 && destination.Col == start.Col + 2) // SE { targetOppRow = start.Row + 2; targetOppoCol = start.Col + 2; diagonalJump = ((opponent.Row == targetOppRow && opponent.Col == start.Col) || (opponent.Row == start.Row && opponent.Col == targetOppoCol)) && ((start.Row + 3 == 17 || start.Col + 3 == 17) || (board[start.Row + 3, start.Col] == WALL || board[start.Row, start.Col + 3] == WALL)); } } return(overJump || diagonalJump); }
private List <string> PossibleMovesFromPosition() { List <string> validMoves = new List <string>(); PlayerCoordinate playerCoordinate = GetCurrentPlayerCoodinate(); PlayerCoordinate opponentCoord = GetCurrentOpponentCoordinate(); if (PlayersAreAdjacent()) { if (playerCoordinate.Row == opponentCoord.Row) { if (playerCoordinate.Col < opponentCoord.Col) { PossibleHorizontalJumps(validMoves, 1); } else { PossibleHorizontalJumps(validMoves, -1); } } else { if (playerCoordinate.Row < opponentCoord.Row) { PossibleVerticalJumps(validMoves, 1); } else { PossibleVerticalJumps(validMoves, -1); } } } if (playerCoordinate.Row + 1 < 17 && board[playerCoordinate.Row + 1, playerCoordinate.Col] != WALL && (playerCoordinate.Row + 2 != opponentCoord.Row || playerCoordinate.Col != opponentCoord.Col)) { //South StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (playerCoordinate.Col / 2))); sb.Append(9 - (playerCoordinate.Row / 2) - 1 < 1 ? 1 : 9 - (playerCoordinate.Row / 2) - 1); validMoves.Add(sb.ToString()); } if (playerCoordinate.Row - 1 > -1 && board[playerCoordinate.Row - 1, playerCoordinate.Col] != WALL && (playerCoordinate.Row - 2 != opponentCoord.Row || playerCoordinate.Col != opponentCoord.Col)) { //North StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (playerCoordinate.Col / 2))); sb.Append(9 - (playerCoordinate.Row / 2) + 1 > 9 ? 9 : 9 - (playerCoordinate.Row / 2) + 1); validMoves.Add(sb.ToString()); } if (playerCoordinate.Col + 1 < 17 && board[playerCoordinate.Row, playerCoordinate.Col + 1] != WALL && (playerCoordinate.Row != opponentCoord.Row || playerCoordinate.Col + 2 != opponentCoord.Col)) { //East StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (playerCoordinate.Col / 2) + 1)); sb.Append(9 - (playerCoordinate.Row / 2)); validMoves.Add(sb.ToString()); } if (playerCoordinate.Col - 1 > -1 && board[playerCoordinate.Row, playerCoordinate.Col - 1] != WALL && (playerCoordinate.Row != opponentCoord.Row || playerCoordinate.Col - 2 != opponentCoord.Col)) { //West StringBuilder sb = new StringBuilder(); sb.Append(Convert.ToChar(97 + (playerCoordinate.Col / 2) - 1)); sb.Append(9 - (playerCoordinate.Row / 2)); validMoves.Add(sb.ToString()); } validMoves.Sort(delegate(string lValue, string rValue) { if (lValue == rValue) { return(0); } else { return(lValue.CompareTo(rValue)); } }); return(validMoves); }
public static string PlayerCoordinateToString(PlayerCoordinate location) { return(Convert.ToChar(97 + (location.Col / 2)).ToString() + (9 - (location.Row / 2))); }
public bool MovePiece(PlayerEnum player, PlayerCoordinate destinationCoordinate) { if (gameOver || player != whoseTurn) { return(false); } bool retValue = false; PlayerCoordinate startCoordinate = null; switch (player) { case PlayerEnum.ONE: startCoordinate = playerOneLocation; break; case PlayerEnum.TWO: startCoordinate = playerTwoLocation; break; } string move = Convert.ToChar(97 + (destinationCoordinate.Col / 2)).ToString() + (9 - destinationCoordinate.Row / 2).ToString(); if (possibleMoves[whoseTurn == 0 ? 0 : 1].Contains(move) /*IsValidPlayerMove(player, startCoordinate, destinationCoordinate)*/) { board[startCoordinate.Row, startCoordinate.Col] = PLAYER_SPACE; switch (player) { case PlayerEnum.ONE: playerOneLocation.Row = destinationCoordinate.Row; playerOneLocation.Col = destinationCoordinate.Col; lastStart[0] = lastMove[0]; if (BoardUtil.IsMoveAdjacentToPosition(move, playerOneLocation)) { lastMove[0] = BoardUtil.PlayerCoordinateToString(playerTwoLocation); } else { lastMove[0] = BoardUtil.PlayerCoordinateToString(playerOneLocation); } possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition(); whoseTurn = PlayerEnum.TWO; possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition(); break; case PlayerEnum.TWO: playerTwoLocation.Row = destinationCoordinate.Row; playerTwoLocation.Col = destinationCoordinate.Col; lastStart[1] = lastMove[1]; if (BoardUtil.IsMoveAdjacentToPosition(move, playerTwoLocation)) { lastMove[1] = BoardUtil.PlayerCoordinateToString(playerOneLocation); } else { lastMove[1] = BoardUtil.PlayerCoordinateToString(playerTwoLocation); } possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition(); whoseTurn = PlayerEnum.ONE; possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition(); break; } retValue = true; } // check for win if (playerOneLocation.Row == 0) { playerOneWin = true; } if (playerTwoLocation.Row == (TOTAL_ROWS - 1)) { playerTwoWin = true; } gameOver = playerOneWin || playerTwoWin; return(retValue); }