Ejemplo n.º 1
0
        public static Board BoardCreated(string PlayerName)
        {
            Board toReturn = new Board();

            for (ShipType s = ShipType.Carrier; s >= ShipType.Destroyer; s--)
            {
                bool IsShipSpotValid = false;
                do
                {
                    PlaceShipRequest request = new PlaceShipRequest();
                    request.Coordinate = ConsoleInput.GetCoordinate(PlayerName);
                    request.Direction  = ConsoleInput.GetDirection(PlayerName, s);
                    request.ShipType   = s;

                    var result = toReturn.PlaceShip(request);

                    if (result == ShipPlacement.NotEnoughSpace)
                    {
                        ConsoleOutput.TooFar();
                    }
                    if (result == ShipPlacement.Overlap)
                    {
                        ConsoleOutput.Overlap();
                    }
                    if (result == ShipPlacement.Ok)
                    {
                        ConsoleOutput.PlaceSuccess();
                        IsShipSpotValid = true;
                    }
                }while (!IsShipSpotValid);
            }
            return(toReturn);
        }
Ejemplo n.º 2
0
        public static bool Attack(IPlayer player, IPlayer opponent)
        {
            bool             isVictory;
            bool             targetAcquired;
            FireShotResponse response;
            Coordinate       radar;

            do
            {
                Console.Clear();
                ConsoleOutput.DisplayBoard(opponent, true);
                Console.WriteLine($"\n{player.Name}, fire munitions at {opponent.Name}!");

                radar          = ConsoleInput.GetCoordinate();
                response       = opponent.Board.FireShot(radar);
                targetAcquired = ConsoleOutput.IsValidShot(response);
            } while (!targetAcquired);

            Console.Clear();
            ConsoleOutput.DisplayBoard(opponent, true);
            isVictory = ConsoleOutput.OpponentFleetSunk(response, player);

            if (isVictory)
            {
                Continue();
                Console.Clear();
                ConsoleOutput.DisplayBoard(opponent, true);
                return(true);
            }

            Continue();
            return(false);
        }
Ejemplo n.º 3
0
        //each player place their ships
        public static void PlaceShips(Player player, int i)
        {
            PlaceShipRequest placeShipRequest = new PlaceShipRequest();
            string           shipName;
            ShipPlacement    placement;

            shipName = player.PlayerBoard.Ships[i].ShipName;
            do
            {
                ConsoleOutput.ShowBoard(player.PlayerBoard.DisplayBoard);

                Console.WriteLine($"{player.Name}, enter a starting coordinate for your {shipName}: (Ex. A2) ");

                placeShipRequest.Coordinate = ConsoleInput.GetCoordinate();
                placeShipRequest.Direction  = ConsoleInput.GetDirection();
                placeShipRequest.ShipType   = player.PlayerBoard.Ships[i].ShipType;
                placement = player.PlayerBoard.PlaceShip(placeShipRequest);
                if (placement != ShipPlacement.Ok)
                {
                    Console.WriteLine($"{placement}.\nPlease press enter to try a new coordinate for your {shipName}.");
                    Console.ReadKey();
                    Console.Clear();
                }
            } while (placement != ShipPlacement.Ok);

            Console.WriteLine($"Your {shipName} has been placed. Press enter to continue.");
            Console.ReadKey();
        }
Ejemplo n.º 4
0
        private Board BuildBoard(string name)
        {
            Board gameBoard = new Board();

            for (ShipType s = ShipType.Carrier; s >= ShipType.Destroyer; s--)
            {
                PlaceShipRequest shipRequest = new PlaceShipRequest();
                shipRequest.ShipType = s;
                ConsoleOutput.ShipToPlace(shipRequest.ShipType);
                shipRequest.Coordinate = ConsoleInput.GetCoordinate();
                shipRequest.Direction  = ConsoleInput.GetDirection();
                ShipPlacement shipPlaced = gameBoard.PlaceShip(shipRequest);

                switch (shipPlaced)
                {
                case ShipPlacement.Overlap:
                    ConsoleOutput.ShipOverlappedAlert(shipRequest.ShipType);
                    s++;
                    break;

                case ShipPlacement.NotEnoughSpace:
                    ConsoleOutput.ShipNoSpaceAlert(shipRequest.ShipType);
                    s++;
                    break;
                }
            }
            Console.Clear();
            return(gameBoard);
            //ConsoleInput.GetCoordinate();
        }
Ejemplo n.º 5
0
        public void IsCoordinateValid()
        {
            string     userCoordinate = "";
            Coordinate actual         = ConsoleInput.GetCoordinate(userCoordinate);
            Coordinate coordinate     = null;

            Assert.AreEqual(coordinate, actual);
        }
Ejemplo n.º 6
0
 public PlaceShipRequest GetPlaceShipRequest(ShipType vessel)
 {
     return(new PlaceShipRequest()
     {
         Coordinate = ConsoleInput.GetCoordinate(),
         Direction = ConsoleInput.GetShipDirection(),
         ShipType = vessel
     });
 }
Ejemplo n.º 7
0
        public void InputToCoordinate()
        {
            string     userCoordinate = "a10";
            Coordinate actual         = ConsoleInput.GetCoordinate(userCoordinate);
            Coordinate coordinate     = new Coordinate(1, 10);


            Assert.AreEqual(coordinate, actual);
        }
Ejemplo n.º 8
0
        private ShotStatus FireShot(GameState state)
        {
            // ShipType ship = ShipType.Battleship;
            Board            toFire    = state.IsPlayer1Turn ? state.P2.PlayerBoard : state.P1.PlayerBoard;
            ShotStatus       isVictory = ShotStatus.Miss;
            Coordinate       FireAt    = ConsoleInput.GetCoordinate();
            FireShotResponse response  = toFire.FireShot(FireAt);

            switch (response.ShotStatus)
            {
            case ShotStatus.Invalid:
                ConsoleOutput.IvalidResponse();
                isVictory = ShotStatus.Invalid;
                return(isVictory);

            case ShotStatus.Duplicate:
                ConsoleOutput.DuplicateResponse();
                isVictory = ShotStatus.Duplicate;
                return(isVictory);

            case ShotStatus.Hit:
                ConsoleOutput.HitResponse();
                isVictory = ShotStatus.Hit;
                return(isVictory);

            case ShotStatus.HitAndSunk:
                ConsoleOutput.HitAndSunkResponse(response.ShipImpacted);
                isVictory = ShotStatus.HitAndSunk;
                return(isVictory);

            case ShotStatus.Miss:
                ConsoleOutput.MissResponse();
                isVictory = ShotStatus.Miss;
                return(isVictory);

            case ShotStatus.Victory:
                ConsoleOutput.VictoryResponse();
                isVictory = ShotStatus.Victory;
                return(isVictory);
            }
            return(isVictory);
        }
Ejemplo n.º 9
0
        public void RunGame()
        {
            Setup gameSetup = new Setup();

            Player PlayerOne = gameSetup.Player1;
            Player PlayerTwo = gameSetup.Player2;

            Board playerOneBoard = PlayerOne.PlayerBoard;
            Board playerTwoBoard = PlayerTwo.PlayerBoard;

            bool determineFirst = gameSetup.Player1Turn;
            bool IsVictory      = false;

            while (!IsVictory)
            {
                if (determineFirst)
                {
                    ConsoleOutput.GetBoard(playerTwoBoard); //playerone board parameter
                    var fireShotResponse = playerTwoBoard.FireShot(ConsoleInput.GetCoordinate(PlayerOne.PlayerName));
                    Console.WriteLine("This is the shot status : {0}", fireShotResponse.ShotStatus);
                    Console.WriteLine("This is the ship impacted status : {0}", fireShotResponse.ShipImpacted);


                    IsVictory = fireShotResponse.ShotStatus == ShotStatus.Victory;
                }
                else
                {
                    ConsoleOutput.GetBoard(playerOneBoard);
                    var fireShotResponse = playerOneBoard.FireShot(ConsoleInput.GetCoordinate(PlayerTwo.PlayerName));
                    Console.WriteLine("This is the shot status : {0}", fireShotResponse.ShotStatus);
                    Console.WriteLine("This is the ship impacted status : {0}", fireShotResponse.ShipImpacted);
                    IsVictory = fireShotResponse.ShotStatus == ShotStatus.Victory;
                }
                determineFirst = !determineFirst;
            }


            // GameState thisGame = new GameState(PlayerOne, PlayerTwo, determineFirst);
        }
Ejemplo n.º 10
0
        public static void SetUpBoard(User player)
        {
            ShipPlacement response;

            var ships = new Dictionary <ShipType, int>()
            {
                { ShipType.Destroyer, 2 },
                { ShipType.Cruiser, 3 },
                { ShipType.Submarine, 3 },
                { ShipType.Battleship, 4 },
                { ShipType.Carrier, 5 }
            };

            foreach (KeyValuePair <ShipType, int> vessel in ships)
            {
                do
                {
                    Console.Clear();
                    ConsoleOutput.DisplayBoard(player, false);
                    ConsoleOutput.DisplayPlacementMessage(player, vessel.Key, vessel.Value);
                    var request = new PlaceShipRequest()
                    {
                        Coordinate = ConsoleInput.GetCoordinate(),
                        Direction  = ConsoleInput.GetShipDirection(),
                        ShipType   = vessel.Key
                    };

                    response = player.Board.PlaceShip(request);
                    Console.Clear();
                    ConsoleOutput.DisplayBoard(player, false);
                    ConsoleOutput.DisplayShipResponse(request, response);
                    ConsoleInput.Continue();
                } while (response != ShipPlacement.Ok);
            }
            Console.Clear();
            ConsoleOutput.DisplayBoard(player, false);
            ClearScreenPrompt();
        }
Ejemplo n.º 11
0
        public static bool Game(Board enemyPlayerBoard, Player player, Player otherPlayer)
        {
            bool checkVictory;

            ConsoleOutput.ShowBoard(player.GuessBoard.DisplayBoard);
            Console.WriteLine($"{player.Name}, enter a coordinate to fire a shot at enemy ships: (Ex. A2) ");
            Coordinate       shot             = ConsoleInput.GetCoordinate();
            FireShotResponse fireShotResponse = enemyPlayerBoard.FireShot(shot);

            HitOrMiss(shot, fireShotResponse, player);
            if (fireShotResponse.ShotStatus == ShotStatus.Invalid || fireShotResponse.ShotStatus == ShotStatus.Duplicate)
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus} entry! Press enter to clear the screen and try again.");
                Console.ReadKey();
                player.WhoseTurn      = false;
                otherPlayer.WhoseTurn = true;
            }
            else if (fireShotResponse.ShotStatus == ShotStatus.HitAndSunk)
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus} {fireShotResponse.ShipImpacted}! Press enter to end your turn.");
                Console.ReadKey();
            }
            else if (fireShotResponse.ShotStatus == ShotStatus.Victory)
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus}! {player.Name} sunk all enemy ships!  Press enter to end the game.");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus}! Press enter to end your turn.");
                Console.ReadKey();
            }
            checkVictory = Win(fireShotResponse);
            Console.Clear();
            return(checkVictory);
        }
Ejemplo n.º 12
0
        internal static void GamePlay(GameState state)
        {
            // Decide who is going to shoot first
            // Draw the board of the defender player

            Player attackinPlayer  = null;
            Player defendingPlayer = null;



            ShotStatus status = new ShotStatus();


            // Decide who goes first and alternate each shot.



            //ConsoleOut.DrawBoard(defendingPlayer.PlayerBoard);

            // var statuss = ShotStatus.Miss;
            //var gameStatus = ShotStatus.Victory;



            bool isVictory = false;

            while (!isVictory)
            {
                if (state.isPlayerOneTurn)
                {
                    attackinPlayer  = state.P1;
                    defendingPlayer = state.P2;
                }

                else
                {
                    defendingPlayer = state.P2;
                    attackinPlayer  = state.P1;
                }

                ConsoleOut.DrawBoard(defendingPlayer.PlayerBoard);

                var coord = ConsoleInput.GetCoordinate();
                //ConsoleOut.DrawBoard(attackinPlayer.PlayerBoard);

                //FireShotResponse response = new FireShotResponse();



                FireShotResponse response = defendingPlayer.PlayerBoard.FireShot(coord);



                switch (response.ShotStatus)
                {
                case ShotStatus.Hit:
                    Console.WriteLine("It is a hit!");
                    state.isPlayerOneTurn = !state.isPlayerOneTurn;
                    break;

                case ShotStatus.Duplicate:
                    Console.WriteLine("It is a duplicate!");
                    break;

                case ShotStatus.Invalid:
                    Console.WriteLine("It is invalid");
                    break;

                case ShotStatus.Miss:
                    Console.WriteLine("It is a miss");
                    state.isPlayerOneTurn = !state.isPlayerOneTurn;
                    break;

                case ShotStatus.Victory:
                    Console.WriteLine("Congratulation you win the game");
                    state.isPlayerOneTurn = !state.isPlayerOneTurn;
                    break;

                case ShotStatus.HitAndSunk:
                    Console.WriteLine("Congratulation you win the game");
                    break;
                }



                /* if (response.ShotStatus == ShotStatus.Victory)
                 * {
                 *   isVictory = true;
                 * }
                 * else
                 * {
                 *   isVictory = false;
                 *   Player temp = defendingPlayer;
                 *   defendingPlayer = attackinPlayer;
                 *   attackinPlayer = temp;
                 *
                 * }*/
            }
        }