private static Constant.GameState CheckColumns(CellManager cm, IMarker m, Constant.GameState currentGameState) { Constant.GameState result = currentGameState; if (result == Constant.GameState.Win) { return(result); } for (int column = 0; column < Constant.GridSize; column++) { int cnt = 0; for (int row = 0; row < Constant.GridSize; row++) { if (cm.Grid[row, column].Marker != null && cm.Grid[row, column].Marker.Equals(m)) { cnt++; } } if (cnt == Constant.GridSize) { return(Constant.GameState.Win); } } return(result); }
Constant.GameState IYatttgFacade.MakeMove(Player player, int position) { // To match up with internal array range. position--; // Update the grid cm_.SetCellAtIndex(position, player.Marker); currentGameState_ = GameLogic.CheckGridForWinner(cm_, player.Marker, currentGameState_); return currentGameState_; }
Constant.GameState IYatttgFacade.MakeMove(Player player, int position) { // To match up with internal array range. position--; // Update the grid cm_.SetCellAtIndex(position, player.Marker); currentGameState_ = GameLogic.CheckGridForWinner(cm_, player.Marker, currentGameState_); return(currentGameState_); }
Constant.GameState IYatttgFacade.InitGame(Player p1, Player p2) { p1_ = p1; p2_ = p2; // Flip a coin on who should start first. p1Turn = RandomFirstTurn(); // Init grid cm_.InitGrid(); // Game is now running. currentGameState_ = Constant.GameState.InProgress; return currentGameState_; }
public YatttgModel() { // Initialize default values. p1_ = null; p2_ = null; // Give Nort and Cross default values. Nort = new Nort(); Cross = new Cross(); cm_ = new CellManager(); // Initial state is to get player information. currentGameState_ = Constant.GameState.PlayerInfo; }
Constant.GameState IYatttgFacade.InitGame(Player p1, Player p2) { p1_ = p1; p2_ = p2; // Flip a coin on who should start first. p1Turn = RandomFirstTurn(); // Init grid cm_.InitGrid(); // Game is now running. currentGameState_ = Constant.GameState.InProgress; return(currentGameState_); }
private static Constant.GameState CheckDiagonals(CellManager cm, IMarker m, Constant.GameState currentGameState) { Constant.GameState result = currentGameState; if (result == Constant.GameState.Win) { return(result); } int cnt = 0; // Check leading diagonal. for (int i = 0; i < Constant.GridSize; i++) { if (cm.Grid[i, i].Marker != null && cm.Grid[i, i].Marker.Equals(m)) { cnt++; } } if (cnt == Constant.GridSize) { return(Constant.GameState.Win); } else { // Reset the counter, check antidiagonal. cnt = 0; for (int i = 0; i < Constant.GridSize; i++) { if (cm.Grid[i, Constant.GridSize - 1 - i].Marker != null && cm.Grid[i, Constant.GridSize - 1 - i].Marker.Equals(m)) { cnt++; } } if (cnt == Constant.GridSize) { return(Constant.GameState.Win); } } // No result was found on the diagonals. return(result); }
internal static Constant.GameState CheckGridForWinner(CellManager cm, IMarker m, Constant.GameState currentGameState) { Constant.GameState result = currentGameState; // Check winning conditions for the newly created move if (cm.AllocCount == Constant.MaxOptions) { result = Constant.GameState.Tie; } else { // We only need to check the grid for the marker that has // just been placed down. result = CheckRows(cm, m, result); result = CheckColumns(cm, m, result); result = CheckDiagonals(cm, m, result); } return(result); }