コード例 #1
0
        //Attack which reads in the dropBomb() coordinates //input validation is in other classes
        public void Attack(int x, int y, Ship gridship)
        {
            //create a position for comparing
            position = ship.Place(new Ship.Position(x, y));

            //Compare ship from grid to the different ship types, search for that position in the positionList and remove, and print messages
            if (gridship == battleship && battleship.positionList.Count() != 0)
            {
                for (int i = 0; i < battleship.positionList.Count(); i++)
                {
                    if (battleship.positionList[i].xPos == position.xPos && battleship.positionList[i].yPos == position.yPos)
                    {
                        battleship.positionList.RemoveAt(i);
                        Console.WriteLine("Hit!");
                    }
                }
                if (battleship.positionList.Count() == 0)
                {
                    Console.WriteLine("You sunk this ship!");
                    battleship.Sunk = true;
                }
            }
            else if (gridship == carrier && carrier.positionList.Count() != 0)
            {
                for (int i = 0; i < carrier.positionList.Count(); i++)
                {
                    if (carrier.positionList[i].xPos == position.xPos && carrier.positionList[i].xPos == position.xPos)
                    {
                        carrier.positionList.RemoveAt(i);
                        Console.WriteLine("Hit!");
                    }
                }
                if (carrier.positionList.Count() == 0)
                {
                    Console.WriteLine("You sunk this ship!");
                    carrier.Sunk = true;
                }
            }

            else if (gridship == cruiser && cruiser.positionList.Count() != 0)
            {
                for (int i = 0; i < cruiser.positionList.Count(); i++)
                {
                    if (cruiser.positionList[i].xPos == position.xPos && cruiser.positionList[i].xPos == position.xPos)
                    {
                        cruiser.positionList.RemoveAt(i);
                        Console.WriteLine("Hit!");
                    }
                }
                if (cruiser.positionList.Count() == 0)
                {
                    Console.WriteLine("You sunk this ship!");
                    cruiser.Sunk = true;
                }
            }
            else if (gridship == destroyer && destroyer.positionList.Count() != 0)
            {
                for (int i = 0; i < destroyer.positionList.Count(); i++)
                {
                    if (destroyer.positionList[i].xPos == position.xPos && destroyer.positionList[i].xPos == position.xPos)
                    {
                        destroyer.positionList.RemoveAt(i);
                        Console.WriteLine("Hit!");
                    }
                }
                if (destroyer.positionList.Count() == 0)
                {
                    Console.WriteLine("You sunk this ship!");
                    destroyer.Sunk = true;
                }
            }
            else if (gridship == submarine && submarine.positionList.Count() != 0)
            {
                for (int i = 0; i < submarine.positionList.Count(); i++)
                {
                    if (submarine.positionList[i].xPos == position.xPos && submarine.positionList[i].xPos == position.xPos)
                    {
                        submarine.positionList.RemoveAt(i);
                        Console.WriteLine("Hit!");
                    }
                }

                if (submarine.positionList.Count() == 0)
                {
                    Console.WriteLine("You sunk this ship!");
                    submarine.Sunk = true;
                }
            }
            else if (gridship == blankShip)
            {
                Console.WriteLine("Miss!");
            }


            System.Threading.Thread.Sleep(1000);
        }
コード例 #2
0
        internal void placeShips()
        {
            //create random class variable
            Random rand = new Random();

            theirShips = new Ships();

            //variable for boat length
            int boatLength = 0;

            //variable to exit loop when stack is out of boats
            bool emptyStack = false;

            //loop to place ships until gone
            while (!emptyStack)
            {
                //counter to verify that there are no collisions
                int goodSpace = 0;

                //assigns boatLength variable based on ship in the Stack
                boatLength = theirShips.shipsStack.Peek().shipLength;

                //generates random cooridinates
                int x = rand.Next(1, 10);
                int y = rand.Next(1, 10);

                //generates a random number for use in the assignDirection() method
                int directionNumber = rand.Next(1, 10);

                ship.direction = new Ship.ShipDirection();
                ship.direction = ship.assignDirection(directionNumber);

                //assigns the ship at the top of the stack a direction
                theirShips.shipsStack.Peek().direction = new Ship.ShipDirection();
                theirShips.shipsStack.Peek().direction = ship.direction;

                //checks for direction and valid starting coordinate
                if (gameGrid[y, x] == blankShip && theirShips.shipsStack.Peek().direction == Ship.ShipDirection.Horizontal)
                {
                    //check to make sure variables stay within scope
                    if (y + boatLength < 11)
                    {
                        //checks for more valid spaces
                        for (int i = 0; i < boatLength; i++)
                        {
                            if (gameGrid[y + i, x] == blankShip)
                            {
                                goodSpace++;
                            }
                        }
                    }

                    //if no collisions places boat in grid, adds to the ship's position array, and pops it out of the stack
                    if (goodSpace == boatLength)
                    {
                        for (int i = 0; i < boatLength; i++)
                        {
                            //Creates a new position variable
                            position = ship.Place(new Ship.Position(y + i, x));

                            //checks for the ship type to place in proper position list
                            switch (theirShips.shipsStack.Peek().symbol)
                            {
                            case " D ":
                                theirShips.destroyer.insertPosition(position);
                                break;

                            case " S ":
                                theirShips.submarine.insertPosition(position);
                                break;

                            case " B ":
                                theirShips.battleship.insertPosition(position);
                                break;

                            case " A ":
                                theirShips.carrier.insertPosition(position);
                                break;

                            case " C ":
                                theirShips.cruiser.insertPosition(position);
                                break;
                            }

                            gameGrid[y + i, x] = theirShips.shipsStack.Peek();
                        }
                        theirShips.shipsStack.Pop();
                    }
                }

                //same as the last block of code, but for different direction
                else if (gameGrid[y, x] == blankShip && theirShips.shipsStack.Peek().direction == Ship.ShipDirection.Vertical)
                {
                    if (x + boatLength < 11)
                    {
                        for (int i = 0; i < boatLength; i++)
                        {
                            if (gameGrid[y, x + i] == blankShip)
                            {
                                goodSpace++;
                            }
                        }
                    }

                    if (goodSpace == boatLength)
                    {
                        for (int i = 0; i < boatLength; i++)
                        {
                            //Creates a new position variable
                            position = ship.Place(new Ship.Position(y, x + i));

                            //checks for the ship type to place in proper position list
                            switch (theirShips.shipsStack.Peek().symbol)
                            {
                            case " D ":
                                theirShips.destroyer.position = position;
                                theirShips.destroyer.insertPosition(theirShips.destroyer.position);
                                break;

                            case " S ":
                                theirShips.submarine.position = position;
                                theirShips.submarine.insertPosition(theirShips.submarine.position);
                                break;

                            case " B ":
                                theirShips.battleship.position = position;
                                theirShips.battleship.insertPosition(theirShips.battleship.position);
                                break;

                            case " A ":
                                theirShips.carrier.position = position;
                                theirShips.carrier.insertPosition(theirShips.carrier.position);
                                break;

                            case " C ":
                                theirShips.cruiser.position = position;
                                theirShips.cruiser.insertPosition(theirShips.cruiser.position);
                                break;
                            }

                            gameGrid[y, x + i] = theirShips.shipsStack.Peek();
                        }
                        theirShips.shipsStack.Pop();
                    }
                }


                //check for empty stack to exit the loop
                if (theirShips.shipsStack.Count() == 0)
                {
                    emptyStack = true;
                }
            }
        }