Example #1
0
        public FireShotResponse TryPlayerAttack(Player currentPlayer, Player enemyPlayer)
        {
            UserIO.WriteLine($"{currentPlayer.Name}, your turn!");
            UserIO.DrawBoard(enemyPlayer);
            UserIO.WriteLine("Where would you like to fire?");
            Coordinate aimingAt = UserIO.GetCoord();

            return(enemyPlayer.Board.FireShot(aimingAt));
        }
Example #2
0
        public void HandleGame(bool playerTwoFirst)
        {
            Player[] players = new Player[3] {
                Player1, Player2, Player1
            };
            int whoseTurn = playerTwoFirst ? 1 : 0;

            while (HandlePlayerTurn(players[whoseTurn], players[whoseTurn + 1]))
            {
                whoseTurn++;
                whoseTurn %= 2;
                UserIO.Continue();
            }
            UserIO.WriteLine($"Congratulations on your victory, {players[whoseTurn].Name}!");
        }
Example #3
0
        public void ShipSetup()
        {
            ResetBoard();
            UserIO.WriteLine($"Alright {Name}, let's setup your ships.");
            ShipType currentShip;

            for (int i = 0; i < 5; i++)
            {
                currentShip = (ShipType)i;
                UserIO.WriteLine($"Place your {currentShip.ToString()}.");

                PlaceShipRequest request = new PlaceShipRequest()
                {
                    Coordinate = UserIO.GetCoord(),
                    Direction  = UserIO.GetDirection(),
                    ShipType   = currentShip
                };

                ShipPlacement spotValidity = board.PlaceShip(request);
                switch (spotValidity)
                {
                case ShipPlacement.NotEnoughSpace:
                    i--;
                    UserIO.WriteLine("Not enough space to place a ship there!");
                    continue;

                case ShipPlacement.Overlap:
                    i--;
                    UserIO.WriteLine("This spot overlaps with another ship!");
                    break;

                case ShipPlacement.Ok:
                    UserIO.WriteLine("Ship placement works!");
                    break;

                default:
                    break;
                }
            }
            UserIO.Continue();
        }
Example #4
0
        public void Run()
        {
            Random rand      = new Random();
            bool   playAgain = true;

            UserIO.WhiteOutput();
            do
            {
                bool playerTwoFirst = (rand.Next(0, 2) == 1);
                HandleGame(playerTwoFirst);
                playAgain = UserIO.GetPlayAgain();
                if (playAgain)
                {
                    //Reset ships without asking for names again
                    Player1.ShipSetup();
                    Player2.ShipSetup();
                    continue;
                }
            } while (playAgain);
            UserIO.WriteLine("Thanks for playing! Goodbye!");
        }