예제 #1
0
        public void shotResult(Position position, ShotStatus status)
        {
            Shots.Add(position, status);

            ConsoleGraphics.showShootedField(Shots, 90);

            string str = null;

            switch (status)
            {
            case ShotStatus.HIT:
                str = "Попадание";
                break;

            case ShotStatus.MISS:
                str = "Промах";
                break;

            case ShotStatus.SUNK:
                str = "Потопил";
                break;
            }
            Console.WriteLine($"{position.getX()},{position.getY()} {str}");
            Console.ReadLine();
        }
예제 #2
0
        public Position chooseShot()
        {
            ConsoleGraphics.showShootedField(Shots, 90);

            Console.WriteLine($"Игрок {Name}, введите кординаты выстрела");

            Console.WriteLine("Введите X:");
            string answ = Console.ReadLine();

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

            int x, y;

            try
            {
                x = int.Parse(answ);
            }
            catch
            {
                Console.WriteLine("Введено неверное значение");
                return(chooseShot());
            }

            Console.WriteLine("Введите Y:");
            answ = Console.ReadLine();
            if (answ == "пауза")
            {
                throw new PauseException();
            }
            try
            {
                y = int.Parse(answ);
            }
            catch
            {
                Console.WriteLine("Введено неверное значение");
                return(chooseShot());
            }

            Position position = null;

            try
            {
                position = new Position(y, x);
            }
            catch
            {
                Console.WriteLine("Введены неверные кординаты, введите кординаты заново");
                return(chooseShot());
            }

            return(position);
        }
예제 #3
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));
                }
            }
        }
예제 #4
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);
        }