Esempio n. 1
0
        public Ship(KindOfShip kindOfShip, int[] startCoords, Direction direction)
        {
            switch (kindOfShip)
            {
            case KindOfShip.Two:
            {
                BuildShipKindTwo(direction, startCoords);
                break;
            }

            case KindOfShip.Three:
            {
                BuildShipKindThree(direction, startCoords);
                break;
            }

            case KindOfShip.Four:
            {
                BuildShipKindFour(direction, startCoords);
                break;
            }

            case KindOfShip.Six:
            {
                BuildShipKindSix(direction, startCoords);
                break;
            }

            default:
                break;
            }
        }
Esempio n. 2
0
        internal CellProperty[,] PlaceShipOnBoard(CellProperty[,] board, string placement, string direction)
        {
            InputParser check = new InputParser();

            Coords = check.ChangeCordsToIndexes(placement);

            if (Coords[0] == -1 || Coords[1] == -1)
            {
                Console.WriteLine("Wrong coordinates!\n\n");
                return(board);
            }

            direction = direction.ToLower();
            Direction userChoice;

            switch (direction)
            {
            case "up":
                userChoice = Direction.Up;
                break;

            case "right":
                userChoice = Direction.Right;
                break;

            case "down":
                userChoice = Direction.Down;
                break;

            case "left":
                userChoice = Direction.Left;
                break;

            default:
                Console.WriteLine("Wrong direction!\n\n");
                return(board);
            }

            switch (CounterOfShipsPlaced)
            {
            case 0:
            {
                KindOfShip lengthOfShip = KindOfShip.Six;
                FillTheBoard(board, userChoice, lengthOfShip);
                break;
            }

            case 1:
            case 2:
            {
                KindOfShip lengthOfShip = KindOfShip.Four;
                FillTheBoard(board, userChoice, lengthOfShip);
                break;
            }

            case 3:
            case 4:
            {
                KindOfShip lengthOfShip = KindOfShip.Three;
                FillTheBoard(board, userChoice, lengthOfShip);
                break;
            }

            case 5:
            case 6:
            {
                KindOfShip lengthOfShip = KindOfShip.Two;
                FillTheBoard(board, userChoice, lengthOfShip);
                break;
            }
            }


            CounterOfShipsPlaced++;


            return(board);
        }
Esempio n. 3
0
        private void FillTheBoard(CellProperty[,] board, Direction userChoice, KindOfShip lengthOfShip)
        {
            if (CounterOfShipsPlaced > 0)
            {
                lengthOfShip = KindOfShip.Four;
                if (CounterOfShipsPlaced > 2)
                {
                    lengthOfShip = KindOfShip.Three;
                    if (CounterOfShipsPlaced > 4)
                    {
                        lengthOfShip = KindOfShip.Two;
                    }
                }
            }

            Ship ship = new Ship(lengthOfShip, Coords, userChoice);

            if (NextPlayer == false)
            {
                PlayerOneShips.Add(ship);
            }
            else
            {
                PlayerTwoShips.Add(ship);
            }

            int[] coordsToChange = { -1, -1 };

            int  canNotBePlaced   = 0;
            bool validCoordinates = false;

            Scanner scan = new Scanner();

            for (int i = 0; i < ship.Coords.Count; i++)
            {
                coordsToChange = ship.Coords[i];

                validCoordinates = scan.CheckCoordinatesCorrectness(coordsToChange, board);

                if (validCoordinates == true)
                {
                    for (int row = -1; row <= 1; row++)
                    {
                        for (int column = -1; column <= 1; column++)
                        {
                            try
                            {
                                if (board[coordsToChange[0] + row, coordsToChange[1] + column] != CellProperty.Empty)
                                {
                                    canNotBePlaced++;
                                }
                            }
                            catch
                            {
                                continue;
                            }
                        }
                    }
                }
            }

            if (canNotBePlaced <= 0 && validCoordinates == true)
            {
                for (int j = 0; j < ship.Coords.Count; j++)
                {
                    coordsToChange = ship.Coords[j];

                    try
                    {
                        board[coordsToChange[0], coordsToChange[1]] = CellProperty.Occupied;
                    }
                    catch
                    {
                        Console.WriteLine("Ship couldn't be build. It was either: near another ship, on another ship or out of board range.");

                        CounterOfShipsPlaced--;

                        break;
                    }
                }
            }
            else
            {
                Console.WriteLine("Ship couldn't be build. It was either: near another ship, on another ship or out board range.\n\n");

                CounterOfShipsPlaced--;
            }
        }