/// <summary> /// Starts the game. /// </summary> /// <param name="game"> Game object, needed to pass players and board objects on. </param> /// <returns> Returns True if user wants to quit, False if user wants to restart. </returns> public static bool Start(Game game) { if (game is null) { throw new ArgumentNullException(nameof(game)); } bool gameFinished = false; Game.MarkType lastSetMark = Game.MarkType.O; int turnsCounter = 0; while (true) { ConsoleUI.NewTurn(game, ref turnsCounter, ref lastSetMark); Update(game.Board); if (game.Board.IsThereWin()) { gameFinished = true; Console.WriteLine($"{lastSetMark.ToString()} Won!"); } else if (turnsCounter == 9) { gameFinished = true; Console.WriteLine(xo.Properties.Resources.Tie); } if (gameFinished) { Console.WriteLine(xo.Properties.Resources.AfterGameOptions); return(ShouldGameQuit()); } } }
/// <summary> /// Tries to mark field. /// </summary> /// <param name="mark"> Mark type. </param> /// <returns> Marks field and returns true, if field was empty; otherwise returns false. </returns> public bool TrySetMark(Game.MarkType mark) { if (Mark != null) { return(false); } else { Mark = mark; return(true); } }
/// <summary> /// Performs a new turn, updates turns counter and last set mark. /// </summary> /// <param name="game"> Game object, needed to pass players and board objects on. </param> /// <param name="turnsCounter"> Turns counter, needed to set to 0 before game started. </param> /// <param name="lastSetMark"> Last set mark, needed to set to MarkType.O before game started. </param> public static void NewTurn(Game game, ref int turnsCounter, ref Game.MarkType lastSetMark) { if (game is null) { throw new ArgumentNullException(nameof(game)); } if (lastSetMark == game.Player1.Mark) { while (true) { var coords = ConsoleUI.GetCoords(game.Player2, game.Board); if (game.Player2.TryMakeTurn(game.Board, coords)) { break; } } lastSetMark = game.Player2.Mark; } else { while (true) { var coords = ConsoleUI.GetCoords(game.Player1, game.Board); if (game.Player1.TryMakeTurn(game.Board, coords)) { break; } } lastSetMark = game.Player1.Mark; } ++turnsCounter; }
/// <summary> /// Creates new computer player instance and assigns it certain mark. /// </summary> /// <param name="mark"> Mark that should be assigned to this player. </param> public Computer(Game.MarkType mark) : base(mark) { }
/// <summary> /// Creates new human player instance and assigns it certain mark. /// </summary> /// <param name="mark"> Mark that should be assigned to this player. </param> public User(Game.MarkType mark) : base(mark) { }
/// <summary> /// Creates new player and assigns it a mark. /// </summary> /// <param name="mark"> Mark that should be assigned to this player. </param> public Player(Game.MarkType mark) { Mark = mark; }