Ejemplo n.º 1
0
 public void HitShip()
 {
     if (--Size == 0)
     {
         UI.AddComment($"Ship {Type} has turned into space debris...\n");
         IsAlive = false;
     }
 }
        public void HandleActions()
        {
            while (!Player.IsLost && !AIOpponent.IsLost)
            {
                UI.PrintTwoBoards(Player, AIOpponent);
                UI.PrintComments();
                UI.PrintMessage("\nWhich field do you want to shoot?");

                bool  correctCoordinates = false;
                bool  isShip             = true;
                int[] coordinates;
                char  charRepresentation;

                while (!correctCoordinates && isShip && !AIOpponent.IsLost)
                {
                    coordinates        = UI.GetPairCoordinates();
                    correctCoordinates = AIOpponent.IsAlreadyUsed(coordinates);
                    if (correctCoordinates)
                    {
                        AIOpponent.HandleShooting(coordinates);
                        isShip             = AIOpponent.Board.CheckIfShip(coordinates);
                        correctCoordinates = false;
                        AIOpponent.CheckIfLost();
                        if (isShip)
                        {
                            UI.PrintTwoBoards(AIOpponent, AIOpponent);
                            UI.PrintComments();
                        }
                    }
                }

                isShip             = true;
                correctCoordinates = false;

                while (!correctCoordinates && isShip && !AIOpponent.IsLost && !Player.IsLost)
                {
                    coordinates = Handler.GetRandomCoordinates();
                    while (Player.alreadySelected.Any(x => x[0] == coordinates[0] && x[1] == coordinates[1]))
                    {
                        coordinates = Handler.GetRandomCoordinates();
                    }
                    charRepresentation = Convert.ToChar(('A' + coordinates[1]));
                    UI.AddComment($"\nAI shoots at: {charRepresentation}{coordinates[0] + 1}\n");
                    Player.alreadySelected.Add(coordinates);
                    correctCoordinates = Player.IsAlreadyUsed(coordinates);
                    if (correctCoordinates)
                    {
                        Player.HandleShooting(coordinates);
                        isShip             = Player.Board.CheckIfShip(coordinates);
                        correctCoordinates = false;
                        Player.CheckIfLost();
                    }
                }
            }
        }
        public void HandleShotOnSquare(int[] coordinates)
        {
            Square square = board[coordinates[0]][coordinates[1]];

            if (square.IsShip)
            {
                UI.AddComment("A ship was hit! Extra shoot!\n");
                // UI.PrintMessage("A ship was hit! You can shoot again :)");
            }
            else
            {
                UI.AddComment("Miss!\n");
            }
            square.IsHit = true;
            square.updateVisualRepresentation();
        }