コード例 #1
0
ファイル: Player.cs プロジェクト: e-lajeunesse/Battleship
        // Method for player to fire on opponents board
        public string Fire(Board playerBoard)
        {
            bool   validFireGuess = false;
            string result         = "";

            while (!validFireGuess)
            {
                Console.WriteLine($"\n {Name} Enter the location you want to fire at: ");
                string playerGuess = Console.ReadLine();

                if (!ChoiceOnBoard(playerGuess))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("INVALID LOCATION");
                }
                else
                {
                    List <int>            coordinates  = ConvertChoiceToCoordinate(playerGuess);
                    ValueTuple <int, int> shipLocation = (coordinates[0], coordinates[1]);
                    if (playerBoard.board[coordinates[0], coordinates[1]].HasShip)
                    {
                        playerBoard.board[coordinates[0], coordinates[1]].DisplayString = " H ";
                        playerBoard.board[coordinates[0], coordinates[1]].HasShip       = false;
                        Ship hitShip = playerBoard.ShipTracker[shipLocation];
                        if (hitShip.IsSunk(playerBoard))
                        {
                            result = $"You've sunk the enemies {hitShip.Name}!";
                            Score++;
                        }
                        else
                        {
                            result = $"You've hit the enemies {hitShip.Name}!";
                        }
                    }
                    else
                    {
                        result = "You Missed!";
                        if (!(playerBoard.board[coordinates[0], coordinates[1]].DisplayString == " H "))
                        {
                            playerBoard.board[coordinates[0], coordinates[1]].DisplayString = " M ";
                        }
                    }
                    validFireGuess = true;
                }
            }
            return(result);
        }
コード例 #2
0
        public PlayerAction PlayTurn()
        {
            PlayerAction action = null;

            while (action == null)
            {
                int x = Rand.Next(0, GameEngine.GridSize);
                int y = Rand.Next(0, GameEngine.GridSize);

                if (AttackingPlayer.FiringGrid.IsEmpty(x, y))
                {
                    action = new PlayerAction(this.AttackingPlayer.Name, x, y);

                    // Take the shot
                    Shot shot = new Shot();

                    Ship ship = DefendingPlayer.GetShip(x, y);
                    if (ship != null)
                    {
                        ship.Hit(x, y);
                        shot.ShipClassification = ship.Classification;

                        action.Hit = true;
                        action.ShipClassification = ship.Classification;
                        action.Sunk = ship.IsSunk();
                    }

                    AttackingPlayer.FiringGrid.Map[x, y] = shot;
                }
            }

            this.AttackingPlayer.Actions.Add(action);

            if (this.DefendingPlayer.AreAllShipsSunk())
            {
                this.GameOver = true;
            }
            else
            {
                // Flip the attacking and defending players..
                var temp = AttackingPlayer;
                AttackingPlayer = DefendingPlayer;
                DefendingPlayer = temp;
            }

            return(action);
        }