public eResponseCode ExecuteMove(Cell.Location i_Location) { eResponseCode moveResult; List <Cell> cellsToFlip = getListOfCellsToFlip(i_Location); if (m_GameBoard.Matrix[i_Location.X, i_Location.Y].CellType == Cell.eType.Empty) { Cell.eType cellType = getCurrentCellType(m_CurrentPlayer.PlayerID); // player2 can be also the computer if (cellsToFlip.Count == 0) { moveResult = eResponseCode.InvalidMove; } else { flipCells(cellsToFlip, cellType); updateScore(m_Player1); updateScore(m_Player2); moveResult = eResponseCode.OK; } } else { moveResult = eResponseCode.NotEmpty; } return(moveResult); }
private List <Cell> getListOfCellsToFlip(Cell.Location i_Location) { List <Cell> cellsToFlip = new List <Cell>(); Cell.eType cellType = getCurrentCellType(m_CurrentPlayer.PlayerID); cellsToFlip = findCellsToFlip(i_Location, cellType); return(cellsToFlip); }
private List <Cell> findCellsToFlip(Cell.Location i_Cell, Cell.eType i_CellType) { List <Cell> cellsToFlip = new List <Cell>(); Cell.eType otherCellType = i_CellType == Cell.eType.Player1 ? Cell.eType.Player2 : Cell.eType.Player1; int[,] directionArr = new int[8, 2] { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { -1, 0 }, { -1, 1 } }; for (int i = 0; i < directionArr.GetLength(0); i++) { int x = i_Cell.X; int y = i_Cell.Y; int xDirection = directionArr[i, 0]; int yDirection = directionArr[i, 1]; x += xDirection; y += yDirection; if (isOnMatrix(x, y) && m_GameBoard.Matrix[x, y].CellType == otherCellType) { x += xDirection; y += yDirection; if (!isOnMatrix(x, y)) { continue; } while (m_GameBoard.Matrix[x, y].CellType == otherCellType) { x += xDirection; y += yDirection; if (!isOnMatrix(x, y)) { break; } } if (!isOnMatrix(x, y)) { continue; } if (m_GameBoard.Matrix[x, y].CellType == i_CellType) { // there are cells to flip - go in reverse to find them while (x != i_Cell.X || y != i_Cell.Y) { x -= xDirection; y -= yDirection; addToList <Cell>(ref cellsToFlip, m_GameBoard.Matrix[x, y]); } } } } return(cellsToFlip); }
private void playTurns() { while (m_GameManager.Winner == null) { eResponseCode availableCells = m_GameManager.CheckValidCellsForBothPlayers(); if (availableCells == eResponseCode.NoValidCellsForBothPlayers) { Player looser = m_GameManager.CalculateWinnerAndLooser(); PrintErrorMessege(availableCells); GameOverMessege(m_GameManager.Winner, looser, m_GameManager.Player1.Score, m_GameManager.Player2.Score); FinishGame(); } else if (availableCells == eResponseCode.NoValidCellsForPlayer) { PrintErrorMessege(availableCells); } else { if (m_GameManager.CurrentPlayer.PlayerID == Player.ePlayerID.Computer) { m_GameManager.MakeAiMove(); } else { string inputLocation = GetMoveFromUser(m_GameManager.CurrentPlayer.PlayerID); Cell.Location location = m_GameManager.ParseLocation(inputLocation); eResponseCode moveResponse = m_GameManager.ExecuteMove(location); while (moveResponse != eResponseCode.OK) { PrintErrorMessege(moveResponse); inputLocation = GetInputFromUser(); location = m_GameManager.ParseLocation(inputLocation); moveResponse = m_GameManager.ExecuteMove(location); } } PrintTheBoardAfterTurn(); } m_GameManager.ChangeCurrentPlayer(); } }
public Cell.Location ParseLocation(string i_InputLocation) { if (i_InputLocation == "Q") // Quit game { OnExit.Invoke(); } eResponseCode response = locationInputValidation(i_InputLocation); while (response != eResponseCode.OK) { OnPrintErrorMessage.Invoke(response); i_InputLocation = OnGetUserInput.Invoke(); response = locationInputValidation(i_InputLocation); } Cell.Location chosenLocation = new Cell.Location(); chosenLocation.Y = i_InputLocation[0] - 65; // convert letter to number chosenLocation.X = int.Parse((i_InputLocation[1]).ToString()) - 1; return(chosenLocation); }