Ejemplo n.º 1
0
        void AddOneShipRandom(Board board, Ship ship)
        {
            do
            {
                NewRandomPosition(ship, board.Size);
            }while (!board.possibleShip(ship));

            board.AddShip(ship);
        }
Ejemplo n.º 2
0
        public void PrintBoard(Board board, Ship ship)
        {
            (int xmin, int xmax, int ymin, int ymax)shipCords = GenerateTuple(ship.OriginPoint.x, ship.OriginPoint.y, ship.Length, ship.InstanceDirection);
            Clear();
            Square[,] boardToPrint = board.ocean;
            Console.WriteLine("    A B C D E F G H I J ");
            for (int i = 0; i < boardToPrint.GetLength(0); i++)
            {
                if (i >= board.Size - 1)
                {
                    Console.Write($" {i + 1} ");
                }
                else
                {
                    Console.Write($"  {i + 1} ");
                }

                for (int j = 0; j < boardToPrint.GetLength(1); j++)
                {
                    if ((j >= shipCords.xmin && j <= shipCords.xmax) && (i >= shipCords.ymin && i <= shipCords.ymax))
                    {
                        if (board.possibleShip(ship))
                        {
                            Print("X ", ConsoleColor.Green);
                        }
                        else
                        {
                            Print("X ", ConsoleColor.DarkRed);
                        }
                    }
                    else
                    {
                        Print($"{boardToPrint[j, i].GetCharacter()} ", boardToPrint[j, i].GetColore());
                    }
                }
                Console.WriteLine();
            }
        }
Ejemplo n.º 3
0
        void AddOneShipManual(Board board, Ship ship)
        {
            int x    = 0;
            int y    = 0;
            int size = board.Size;

            Ship.Direction dir = Ship.Direction.horizontal;
            ConsoleKey     key;
            bool           CordsNotSet          = true;
            bool           wrongPositionMessage = false;

            do
            {
                ship.OriginPoint       = (x, y);
                ship.InstanceDirection = dir;
                _display.PrintBoard(board, ship);
                if (wrongPositionMessage)
                {
                    _display.PrintMessage("Invalid Ship Position", ConsoleColor.Red);
                    wrongPositionMessage = false;
                }
                key = _input.ReadKey();

                switch (key)
                {
                case ConsoleKey.UpArrow:
                    if (y == 0 && dir == Ship.Direction.vertical)
                    {
                        y = size - ship.Length;
                    }
                    else if (y == 0 && dir == Ship.Direction.horizontal)
                    {
                        y = size - 1;
                    }
                    else
                    {
                        y--;
                    }
                    break;

                case ConsoleKey.DownArrow:
                    if ((y == size - ship.Length && dir == Ship.Direction.vertical) || (y == size - 1))
                    {
                        y = 0;
                    }
                    else
                    {
                        y++;
                    }
                    break;

                case ConsoleKey.LeftArrow:
                    if (x == 0 && dir == Ship.Direction.horizontal)
                    {
                        x = size - ship.Length;
                    }
                    else if (x == 0 && dir == Ship.Direction.vertical)
                    {
                        x = size - 1;
                    }
                    else
                    {
                        x--;
                    }
                    break;

                case ConsoleKey.RightArrow:
                    if ((x == size - ship.Length && dir == Ship.Direction.horizontal) || (x == size - 1))
                    {
                        x = 0;
                    }
                    else
                    {
                        x++;
                    }
                    break;

                case ConsoleKey.Spacebar:
                    if (dir == Ship.Direction.horizontal)
                    {
                        dir = Ship.Direction.vertical;
                        if (y >= size - ship.Length)
                        {
                            y = size - ship.Length;
                        }
                    }
                    else
                    {
                        dir = Ship.Direction.horizontal;
                        if (x >= size - ship.Length)
                        {
                            x = size - ship.Length;
                        }
                    }
                    break;

                case ConsoleKey.Enter:
                    if (board.possibleShip(ship))
                    {
                        board.AddShip(ship);
                        CordsNotSet = false;
                    }
                    else
                    {
                        wrongPositionMessage = true;
                    }
                    break;

                default:
                    break;
                }
            } while (CordsNotSet);
        }