Ejemplo n.º 1
0
        public Placement choosePlacement(ShipInterface ship, BoardInterface board)
        {
            ConsoleGraphics.showField(board, 70);

            bool isVertical = checkIsVertical();

            Console.WriteLine("Введите кординату X(1 - 10):");
            string answ = Console.ReadLine();

            if (answ == "пауза")
            {
                throw new PauseException();
            }
            int x = int.Parse(answ);

            Console.WriteLine("Введите кординату Y(1 - 10):");
            answ = Console.ReadLine();
            if (answ == "пауза")
            {
                throw new PauseException();
            }
            int y = int.Parse(answ);

            Position       position    = null;
            BoardInterface boardToShow = board.Clone() as BoardInterface;

            try
            {
                position = new Position(x, y);
                boardToShow.placeShip(ship, position, isVertical);
            }
            catch
            {
                Console.WriteLine("данные введены неправильно, введите заново");
                return(choosePlacement(ship, board));
            }

            Console.Clear();
            ConsoleGraphics.showField(boardToShow, 70);

            while (true)
            {
                Console.WriteLine("Вы уверены в своём выборе?");
                answ = Console.ReadLine();
                if (answ == "да")
                {
                    return(new Placement(position, isVertical));
                }
                else if (answ == "нет")
                {
                    return(choosePlacement(ship, board));
                }
            }
        }
Ejemplo n.º 2
0
        public Placement choosePlacement(ShipInterface ship, BoardInterface board)
        {
            Random rnd = new Random();

            while (true)
            {
                try
                {
                    Position       position   = new Position(rnd.Next(1, 10), rnd.Next(1, 10));
                    bool           isVertical = rnd.Next(0, 1) == 1;
                    BoardInterface tempBoard  = board.Clone() as BoardInterface;
                    tempBoard.placeShip(ship, position, isVertical);
                    return(new Placement(position, isVertical));
                }
                catch { }
            }
        }
Ejemplo n.º 3
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);
        }