public void DoComputerMove(Player i_Player) { int numberOfMoves = m_listOfPossibleMoves.Count; int newMove = m_RandomMoveForPC.Next(numberOfMoves); drawPossibleMove(m_listOfPossibleMoves[newMove], i_Player); UpdatePlayerScore(i_Player); clearLastRoundPossibleMove(m_listOfPossibleMoves); }
public eSystemReply CheckIfUserCanPlay(Player i_Player) { eSystemReply canPlay = eSystemReply.PlayerHasNoMoves; List<PossibleMove?> listOfPossibleMovesForPlayer = GetPossibleMoves(i_Player); // if there are possible moves then the user can play. if (listOfPossibleMovesForPlayer.Count > 0) { canPlay = eSystemReply.PlayerCanMove; m_listOfPossibleMoves = listOfPossibleMovesForPlayer; } return canPlay; }
// initializes the board and players. public void InitGame() { m_BlackPlayer = new Player(Color.Black); m_WhitePlayer = new Player(Color.White); m_CurrentPlayer = m_BlackPlayer; m_BlackGamesScore = m_WhiteGamesScore = 0; FormGameSettings formSettings = new FormGameSettings(); formSettings.ShowDialog(); if (formSettings.DialogResult == DialogResult.OK) { if (formSettings.IsAgainstComputer) { m_WhitePlayer.IsComputer = true; } m_CurrentGame = new GameEngine(formSettings.BoardSize); m_GameUI = new GameForm(formSettings.BoardSize, this); m_CurrentGame.InitNewGame(); startGame(); } }
private void switchPlayer(Player i_Player) { if (i_Player == m_BlackPlayer) { m_CurrentPlayer = m_WhitePlayer; } else { m_CurrentPlayer = m_BlackPlayer; } }
private void anotherRound() { m_CurrentGame.RestartGame(); m_CurrentPlayer = m_BlackPlayer; List<PossibleMove?> possibleMoves = m_CurrentGame.GetPossibleMoves(m_CurrentPlayer); m_CurrentGame.UpdateCellsForPossibleMoves(possibleMoves); }
// Get the list of the possible moves in the board for a player public List<PossibleMove?> GetPossibleMoves(Player i_Player) { List<int[]> playerCells = getPlayerCellsInBoard(i_Player); List<eDirection> listOfDirectionsForPossibleMoves = new List<eDirection>(); List<PossibleMove?> listOfPossibleMoves = new List<PossibleMove?>(); bool searchInDirection = false; foreach (int[] xyCoord in playerCells) { // Now we need to check all the directions with opponent sign in them foreach (string str in Enum.GetNames(typeof(eDirection))) { eDirection direction = (eDirection)System.Enum.Parse(typeof(eDirection), str); searchInDirection = checkCellInDirection(xyCoord[k_XCoord], xyCoord[k_YCoord], (eDirection)direction, i_Player.GetOpponentSign()); if (searchInDirection) { listOfDirectionsForPossibleMoves.Add(direction); } } // For each direction with opponent we need to check if there is a possible move in the direction foreach (eDirection direction in listOfDirectionsForPossibleMoves) { PossibleMove? newMove = getPossibleCellFromDirection(xyCoord[k_XCoord], xyCoord[k_YCoord], direction, i_Player); addLocationToCell(newMove, ref listOfPossibleMoves); } // every new position we check we clear the list listOfDirectionsForPossibleMoves.Clear(); } m_listOfPossibleMoves = listOfPossibleMoves; return listOfPossibleMoves; }
// Get all empty cells in the board private List<int[]> getPlayerCellsInBoard(Player i_Player) { List<int[]> allPlayerCellsInBoard = new List<int[]>(); int[] cellCoordinates; int boardSize = m_Board.Size; for (int row = 0; row < boardSize; row++) { for (int colomn = 0; colomn < boardSize; colomn++) { if (m_Board[row, colomn].Color == i_Player.Color) { cellCoordinates = new int[k_XYCoord]; cellCoordinates[k_XCoord] = row; cellCoordinates[k_YCoord] = colomn; allPlayerCellsInBoard.Add(cellCoordinates); } } } return allPlayerCellsInBoard; }
// Get a possible move from cell to direction private PossibleMove? getPossibleCellFromDirection(int i_XCoord, int i_YCoord, eDirection i_Direction, Player i_Player) { int boardRow = 0, boardCol = 0; PossibleMove? possibleMove = null; bool foundSpaceToPlaceNewMove = false; bool inBoundsOfBoard = true; getDirectionRowAndColumn(ref boardRow, ref boardCol, i_Direction); // we want to search for an empty spot because we know theres already an opponent sign near us while (!foundSpaceToPlaceNewMove && inBoundsOfBoard) { i_XCoord += boardRow; i_YCoord += boardCol; // Checks if coordinates still in bound of the board inBoundsOfBoard = m_Board.IsCoordinatesInBounds(i_XCoord, i_YCoord); if (!inBoundsOfBoard) { break; } else if (m_Board[i_XCoord, i_YCoord].Color == i_Player.Color) { break; } else if (m_Board[i_XCoord, i_YCoord].Color == Color.Empty || m_Board[i_XCoord, i_YCoord].Color == Color.Gray) { foundSpaceToPlaceNewMove = true; } } // if we found a new place, create a new coordinate array and return it if (foundSpaceToPlaceNewMove) { possibleMove = new PossibleMove(i_XCoord, i_YCoord, getOppositeDirection(i_Direction)); } return possibleMove; }
// Draw a move of a player private void drawPossibleMove(PossibleMove? i_PossibleMove, Player i_Player) { if (i_PossibleMove != null) { // Drawing line for each direction in the possible move and update the board string[] directionsStringArray = i_PossibleMove.Value.Direction.ToString().Split(new[] { ", " }, StringSplitOptions.None); foreach (string directionStr in directionsStringArray) { eDirection direction = (eDirection)System.Enum.Parse(typeof(eDirection), directionStr); drawLineFromCellInDirection(i_PossibleMove.Value.X, i_PossibleMove.Value.Y, direction, i_Player); } } }
private void drawLineFromCellInDirection(int i_X, int i_Y, eDirection i_Direction, Player i_Player) { int rowDirection = 0, columnDirection = 0; m_Board.UpdateCell(i_X, i_Y, i_Player.Color); getDirectionRowAndColumn(ref rowDirection, ref columnDirection, i_Direction); do { i_X += rowDirection; i_Y += columnDirection; if (m_Board.IsCoordinatesInBounds(i_X + rowDirection, i_Y + columnDirection)) { m_Board.UpdateCell(i_X, i_Y, i_Player.Color); } } while (m_Board.IsCoordinatesInBounds(i_X + rowDirection, i_Y + columnDirection) && m_Board[i_X + rowDirection, i_Y + columnDirection].Color != i_Player.Color); }
private eSystemReply doPlayerMoveWithXY(int i_X, int i_Y, Player i_Player) { eSystemReply checkMoveReply = eSystemReply.ValidMove; bool playerCanMove = false; // Checks if the input is in the possible move list foreach (PossibleMove? move in m_listOfPossibleMoves) { // If it's in the list, it's a valid move if (move.Value.isEqualToPoisition(i_X, i_Y)) { playerCanMove = true; drawPossibleMove(move, i_Player); } } if (playerCanMove == false) { checkMoveReply = eSystemReply.ErrorBadLogicInput; } return checkMoveReply; }
public void UpdatePlayerScore(Player i_Player) { List<int[]> playerCellsInBoard = getPlayerCellsInBoard(i_Player); i_Player.Score = playerCellsInBoard.Count; }
// Holds the move of the player public eSystemReply Move(int i_X, int i_Y, Player i_Player) { if (i_Player.IsComputer) { DoComputerMove(i_Player); } eSystemReply moveReply = eSystemReply.ValidMove; moveReply = doPlayerMoveWithXY(i_X, i_Y, i_Player); if (moveReply == eSystemReply.ValidMove) { UpdatePlayerScore(i_Player); clearLastRoundPossibleMove(m_listOfPossibleMoves); } return moveReply; }