public void continueGame(int x, int y, playerSymbol player) { if (game.currentPlayer == player) { if ((x > 1) || (x < 4) || (y > 1) || (y < 4)) { if (game.AddPoint(x, y) == false) { gameHistory.SetMemento(game.SaveMemento()); OutPutField?.Invoke(game); if (game.CheckAll() == true) { Winning(); return; } } else { Winning(); } } } else { throw new Exception("Choose another player for this course!"); } }
internal bool Check(Field gameField, playerSymbol currentPlayer) { this.gameField = gameField; if (currentPlayer == playerSymbol.playerA) { symbol = "X"; if (CheckColumns() == true) { return(true); } if (CheckRows() == true) { return(true); } if (CheckMainDiagonal() == true) { return(true); } if (CheckSecondDiagonal() == true) { return(true); } return(false); } else { symbol = "0"; if (CheckColumns() == true) { return(true); } if (CheckRows() == true) { return(true); } if (CheckMainDiagonal() == true) { return(true); } if (CheckSecondDiagonal() == true) { return(true); } return(false); } }
internal GameMemento(Game game) { gameField = new Field(); gameField.field = new string[3, 3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { gameField.field[i, j] = game.gameField.field[i, j]; } } currentPlayer = game.currentPlayer; X = new List <int>(); Y = new List <int>(); for (int i = 0; i < game.X.Count; i++) { this.X.Add(game.X[i]); this.Y.Add(game.Y[i]); } }
public void startGame(int x, int y, playerSymbol player) { if (game.currentPlayer == player) { OutPutField?.Invoke(game); if ((x < 1) || (x > 4) || (y < 1) || (y > 4)) { game.RestoreState(gameHistory.GetLastMemento()); } else { game.AddPoint(x, y); gameHistory.SetMemento(game.SaveMemento()); OutPutField?.Invoke(game); } } else { throw new Exception("Choose another player for this course!"); } }
internal bool AddPoint(int x, int y) { playerSymbol player = currentPlayer; if (x > 0 && x < 4 && y < 4 && y > 0) { if (gameField.field[x - 1, y - 1] == null) { if (X.Count > 0) { if (X[X.Count - 1] != x && X[Y.Count - 1] != y) { X.Add(x); Y.Add(y); } } else { X.Add(x); Y.Add(y); } if (currentPlayer == playerSymbol.playerA) { gameField.field[x - 1, y - 1] = "X"; currentPlayer = playerSymbol.playerB; } else { gameField.field[x - 1, y - 1] = "0"; currentPlayer = playerSymbol.playerA; } } } else { new ArgumentOutOfRangeException("You came out from available range of values!"); } return(checker.Check(gameField, player)); }
internal void RestoreState(GameMemento memento) { this.gameField = memento.gameField; this.currentPlayer = memento.currentPlayer; for (int i = 0; i < this.X.Count; i++) { if (i < memento.X.Count) { this.X[i] = memento.X[i]; this.Y[i] = memento.Y[i]; } else { this.X.RemoveAt(i); this.Y.RemoveAt(i); } } if (memento.Y.Count == 0) { this.X.Clear(); this.Y.Clear(); } }