private void flipCells(List <Cell> i_ListOfCells, Cell.eType i_NewType) { foreach (Cell cell in i_ListOfCells) { changeCellType(cell, i_NewType); } }
private 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 void flipCells(List <Cell> i_ListOfCells, Cell.eType i_NewType) { foreach (Cell cell in i_ListOfCells) { cell.CellType = i_NewType; } }
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 updateScore(Player i_Player) { int counter = 0; Cell.eType playerCells = i_Player.PlayerID == Player.ePlayerID.Player1 ? Cell.eType.Player1 : Cell.eType.Player2; foreach (Cell cell in m_GameBoard.Matrix) { if (cell.CellType == playerCells) { counter++; } } i_Player.Score = counter; }
private eResponseCode checkValidCellsForPlayer(Cell.eType i_Type) { List <Cell> availableCells = new List <Cell>(); eResponseCode response = eResponseCode.NoValidCellsForPlayer; foreach (Cell cell in m_GameBoard.Matrix) { if (cell.CellType == Cell.eType.Empty) { availableCells = findCellsToFlip(cell.CellLocation, i_Type); if (availableCells.Count > 0) { response = eResponseCode.OK; break; } } } return(response); }
public List <Cell> GetValidCells() { List <Cell> validCells = new List <Cell>(); List <Cell> tempList = new List <Cell>(); Cell.eType currentType = m_CurrentPlayer == Player1 ? Cell.eType.Player1 : Cell.eType.Player2; foreach (Cell cell in m_GameBoard.Matrix) { if (cell.CellType == Cell.eType.Empty) { tempList = findCellsToFlip(cell.CellLocation, currentType); if (tempList.Count > 0) { validCells.Add(cell); } } } return(validCells); }
private char covertCellTypeToChar(Cell.eType i_Type) { char type; switch (i_Type) { case Cell.eType.Player1: type = 'O'; break; case Cell.eType.Player2: type = 'X'; break; default: type = ' '; break; } return(type); }
public eResponseCode CheckValidCellsForBothPlayers() { eResponseCode response; Cell.eType currentType = m_CurrentPlayer.PlayerID == Player.ePlayerID.Player1 ? Cell.eType.Player1 : Cell.eType.Player2; if (checkValidCellsForPlayer(Cell.eType.Player1) == eResponseCode.NoValidCellsForPlayer && checkValidCellsForPlayer(Cell.eType.Player2) == eResponseCode.NoValidCellsForPlayer) { response = eResponseCode.NoValidCellsForBothPlayers; } else if (checkValidCellsForPlayer(currentType) == eResponseCode.NoValidCellsForPlayer) { response = eResponseCode.NoValidCellsForPlayer; } else { response = eResponseCode.OK; } return(response); }
private void changeCellType(Cell i_Cell, Cell.eType i_NewType) { i_Cell.CellType = i_NewType; }
private Cell.eType getCurrentCellType(Player.ePlayerID i_PlayerID) { Cell.eType cellType = i_PlayerID == Player.ePlayerID.Player1 ? Cell.eType.Player1 : Cell.eType.Player2; // player2 can be also the computer return(cellType); }