Example #1
0
        public void StartGame(Game game)
        {
            // Player One first
            Player currentPlayer = game.PlayerOne;

            // Keep asking the matrix until it reaches the maximum number of tries or all ships are found.
            do
            {
                Attack attack = null;

                // Change color depends on which player is playing
                Console.ForegroundColor = currentPlayer.Color;

                Console.WriteLine("Player {0}\n", currentPlayer.Number);

                do
                {
                    // Column position Input
                    Console.WriteLine("Input column position number (minimum 1 and maximum {0}):", game.Columns);
                    var column = Console.ReadLine();

                    Console.WriteLine("Input line position number (minimum 1 and maximum {0}):", game.Lines);
                    var line = Console.ReadLine();

                    attack = _gameService.ValidateAttack(game, currentPlayer, column, line);
                    if (!attack.Valid)
                    {
                        Console.WriteLine(attack.Message);
                    }
                    else
                    {
                        // Set the position of the attack
                        currentPlayer.BoardLastState = currentPlayer.Board[attack.Column - 1, attack.Line - 1];
                    }

                    // Keep asking a valid entry
                } while (!attack.Valid);


                // Process and print the result of the attack to the player
                if (currentPlayer.BoardLastState == PositionState.Floating)
                {
                    // Subtract a ship when hit
                    currentPlayer.NumberOfShipsLeft--;

                    // Mark the spot where the ship was hit so it is not marked anymore
                    _playerService.ChangePlayerBoardState(currentPlayer, attack, PositionState.Sunk);

                    Console.ForegroundColor = ConsoleColor.Blue;// color of success
                    Console.WriteLine("\n------You've hit it!------\n------There are {0} ships left------\n", currentPlayer.NumberOfShipsLeft);
                }
                else
                {
                    // Mark the spot where the ship was hit so it is not marked again
                    _playerService.ChangePlayerBoardState(currentPlayer, attack, PositionState.Attacked);

                    Console.WriteLine("\n------You've missed------\n------You still have {0} ships left------\n", currentPlayer.NumberOfShipsLeft);
                }

                // Set who is the next player
                currentPlayer = currentPlayer.Number == PlayerNumber.One ? game.PlayerTwo : game.PlayerOne;

                // The game finishes either when you reach the maximum number ships sunk
            } while (currentPlayer.NumberOfShipsLeft > 0);


            // Game finished and show the result of the game
            Console.ForegroundColor = ConsoleColor.Blue;
            if (game.PlayerOne.NumberOfShipsLeft == game.PlayerTwo.NumberOfShipsLeft)
            {
                Console.WriteLine("-------------------------Game Tied!------------------------\n\n", currentPlayer.Number);
            }
            else
            {
                Console.WriteLine("-------------------------Well done Player {0}!------------------------\n\n", currentPlayer.Number);
            }
            Console.Read();
        }