Ejemplo n.º 1
0
        public PlayerInterface play()
        {
            Console.WriteLine("Вы можете в любую минуту поставить игру на паузу.\nДля этого введите \"пауза\" в командную строку\n");
            if (!OnPause)
            {
                Console.Clear();
                if (!Player1Ready)
                {
                    Console.WriteLine($"{player1.ToString()}:");
                    foreach (KeyValuePair <string, ShipInterface> kvp in Ships)
                    {
                        Console.WriteLine($"Выберите расположение для \"{kvp.Key}\"");

                        Ship ship = new Ship(kvp.Value.getSize());
                        try
                        {
                            Placement pl = player1.choosePlacement(ship, board1.Clone() as BoardInterface);
                            board1.placeShip(ship, pl.getPosition(), pl.isVerticalValue());
                        }
                        catch (PauseException e)
                        {
                            Console.Clear();
                            Console.WriteLine("Игра остановлена на этапе растановки кораблей, расстановка текущего игрока не будет запомнена");
                            return(null);
                        }
                    }
                    Player1Ready = true;
                }

                Console.Clear();
                if (!Player2Ready && !(player2 is ComputerPlayer))
                {
                    Console.WriteLine($"{player2.ToString()}:");
                    foreach (KeyValuePair <string, ShipInterface> kvp in Ships)
                    {
                        Console.WriteLine($"Выберите расположение для \"{kvp.Key}\"");

                        Ship ship = new Ship(kvp.Value.getSize());
                        try
                        {
                            Placement pl = player2.choosePlacement(ship, board2.Clone() as BoardInterface);
                            board2.placeShip(ship, pl.getPosition(), pl.isVerticalValue());
                        }
                        catch (PauseException e)
                        {
                            Console.Clear();
                            Console.WriteLine("Игра остановлена на этапе растановки кораблей, расстановка текущего игрока не будет запомнена");
                            return(null);
                        }
                    }
                    Player2Ready = true;
                }
                else if (player2 is ComputerPlayer)
                {
                    foreach (KeyValuePair <string, ShipInterface> kvp in Ships)
                    {
                        Ship      ship      = new Ship(kvp.Value.getSize());
                        Placement placement = player2.choosePlacement(ship, board2.Clone() as BoardInterface);
                        board2.placeShip(ship, placement.getPosition(), placement.isVerticalValue());
                    }
                }
            }

            Console.Clear();
            while (!(board1.allSunk() || board2.allSunk()))
            {
                try
                {
                    if (!board2.allSunk())
                    {
                        ConsoleGraphics.showField(board1, 60);
                        Position position1 = player1.chooseShot();
                        try
                        {
                            board2.shoot(position1);
                        }
                        catch (InvalidPositionException e) { }

                        checkStatus(position1, player1, board2);
                        Console.Clear();
                        player2.opponentShot(position1);
                    }

                    if (!board1.allSunk() && !(player2 is ComputerPlayer))
                    {
                        ConsoleGraphics.showField(board2, 60);
                        Position position2 = player2.chooseShot();
                        try
                        {
                            board1.shoot(position2);
                        }
                        catch (InvalidPositionException e) { }

                        checkStatus(position2, player2, board1);
                        Console.Clear();
                        player1.opponentShot(position2);
                    }

                    if (!board1.allSunk() && player2 is ComputerPlayer)
                    {
                        Position position3 = player2.chooseShot();
                        try
                        {
                            board1.shoot(position3);
                        }
                        catch (InvalidPositionException e) { }

                        player1.opponentShot(position3);
                    }
                }
                catch (PauseException e)
                {
                    Console.WriteLine("Игра остановлена");
                    OnPause = true;
                    return(null);
                }
            }

            if (board1.allSunk())
            {
                return(player2);
            }

            if (board2.allSunk())
            {
                return(player1);
            }

            return(null);
        }