示例#1
0
        private Board BuildBoard(string playerName)
        {
            Board board = new Board();

            for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++)
            {
                bool isValidPlacement = false;
                do
                {
                    PlaceShipRequest request = new PlaceShipRequest();
                    request.Coordinate = ConsoleInput.GetCoord(playerName, s);
                    request.Direction  = ConsoleInput.GetDir(playerName, s);
                    request.ShipType   = s;

                    var result = board.PlaceShip(request);
                    if (result == ShipPlacement.Overlap)
                    {
                        ConsoleOutput.ShipOverlap(playerName);
                    }
                    else if (result == ShipPlacement.NotEnoughSpace)
                    {
                        ConsoleOutput.NotEnoughSpace(playerName);
                    }
                    else
                    {
                        ConsoleOutput.ShipPlaceOk(playerName);
                        isValidPlacement = true;
                    }
                } while (!isValidPlacement);
            }
            return(board);
        }
示例#2
0
        private Board GameBoard(string Playername)
        {
            Board board = new Board();

            for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++)
            {
                bool IsPlacementValid = false;
                do
                {
                    PlaceShipRequest request = new PlaceShipRequest();
                    request.Coordinate = ConsoleInput.GetCoord(Playername);
                    request.Direction  = ConsoleInput.GetDirect(Playername);
                    request.ShipType   = s;

                    var result = board.PlaceShip(request);

                    if (result == ShipPlacement.NotEnoughSpace)
                    {
                        ConsoleOutput.Out();
                    }
                    else if (result == ShipPlacement.Overlap)
                    {
                        ConsoleOutput.Overlap();
                    }
                    else if (result == ShipPlacement.Ok)
                    {
                        ConsoleOutput.Ok();

                        IsPlacementValid = true;
                    }
                }while (!IsPlacementValid);
            }
            return(board);
        }
示例#3
0
        public static void Start(GameState state)
        {
            Player attackingPlayer = null;
            Player defendingPlayer = null;
            bool   isGameOver      = false;

            while (!isGameOver)
            {
                if (state.IsPlayerOneTurn)
                {
                    attackingPlayer = state.P1;
                    defendingPlayer = state.P2;
                }
                else
                {
                    attackingPlayer = state.P2;
                    defendingPlayer = state.P1;
                }

                ConsoleOutput.DrawBoard(defendingPlayer.Board);

                Coordinate attack = ConsoleInput.GetCoord(attackingPlayer.Name);

                FireShotResponse ShotFireStatus = defendingPlayer.Board.FireShot(attack);

                switch (ShotFireStatus.ShotStatus)
                {
                case ShotStatus.Hit:
                    Console.WriteLine("Hit confirmed! Next players turn.");
                    Console.ReadLine();
                    Console.Clear();
                    state.IsPlayerOneTurn = !state.IsPlayerOneTurn;
                    break;

                case ShotStatus.Miss:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Welp! You tried but that shot was a miss. Next players turn.");
                    Console.ReadLine();
                    Console.Clear();
                    state.IsPlayerOneTurn = !state.IsPlayerOneTurn;
                    break;

                case ShotStatus.Invalid:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("...can't shoot there, coordinates must be inside of the grid, try again.");
                    Console.ReadLine();
                    break;

                case ShotStatus.Duplicate:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Whoops! Didn't look at your board? Already shot there, try again.");
                    Console.ReadLine();
                    break;

                case ShotStatus.HitAndSunk:
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Yahtzee! You sunk a ship.");
                    Console.ReadLine();
                    Console.Clear();
                    state.IsPlayerOneTurn = !state.IsPlayerOneTurn;
                    break;

                case ShotStatus.Victory:
                    isGameOver = true;
                    ConsoleOutput.GameOver();
                    Console.Clear();
                    break;
                }
            }
            ConsoleOutput.GameOver();
        }