コード例 #1
0
        public Ocean GuessEnemyPositionAKAFire(string position, Ocean enemysOcean)
        {
            int[] position1 = GetShipPositionInput(enemysOcean);

            /* PSEUDOCODE
             * if enemyOcean[position] == ship {
             *  myOcean[position].color - zaznacz na znaleziony statek
             * }
             * else {
             *  myOcean[position].color - zaznacz ze nie ma statku
             * }*/
            enemysOcean.Squares[position1[0]][position1[1]].VisibleForOpponent = true;
            if (enemysOcean.Squares[position1[0]][position1[1]].Fill == "X")
            {
                Printer.Print("hit");
                Thread.Sleep(2000);
            }
            else
            {
                Printer.Print("miss");
                Thread.Sleep(2000);
            }

            return(enemysOcean);
        }
コード例 #2
0
        public Ocean GetShipsPositionsAndPlaceShipsOnBoard(Ocean ocean)
        {
            // Ship names needed for printing and ship types list
            List <string> shipTypesNames = new List <string> {
                "Carrier (5",
                "Battleship (4",
                "Cruiser (3",
                "Submarine (3",
                "Destroyer (2"
            };

            Ship.ShipType[] shipTypes = new Ship.ShipType[5] {
                Ship.ShipType.CARRIER,
                Ship.ShipType.BATTLESHIP,
                Ship.ShipType.CRUISER,
                Ship.ShipType.SUBMARINE,
                Ship.ShipType.DESTROYER
            };

            // Get data from user where to place each type of the ship
            foreach (string type in shipTypesNames)
            {
                Printer.Print("\nPlease place " + type + " squares) on your ocean:");

                // Ask for ship position (top left Square of the ship) and layout (horizontal/vertical)
                // until the ship can be placed on the ocean (1. is inside board, 2. not overlap with
                // another ship, 3. do not touch another ship)
                bool shipPlacedInTheOcean = false;
                while (shipPlacedInTheOcean == false)
                {
                    int[]  position = GetShipPositionInput(ocean);
                    string layout   = GetShipLayoutInput();

                    // Create new ship object from the input
                    int  index = shipTypesNames.IndexOf(type);
                    Ship ship  = new Ship(shipTypes[index], layout);

                    // Check if ship can be placed in the ocean
                    shipPlacedInTheOcean = ocean.CanPlaceShip(ship, position[0], position[1]);
                    // TODO
                    // Here add check if ships not overlap
                    // Here add check if ships do not touch each other

                    if (shipPlacedInTheOcean == false)
                    {
                        Printer.Print("Not possible to place the ship in the ocean, try again");
                    }
                    else
                    {
                        ocean.PlaceShipAtTheOcean(ship, position[0], position[1]);
                        Printer.Print(ocean, Program.Status.SHIPS_POSITIONING);
                        // ocean.DisplayOcean(Program.Status.SHIPS_POSITIONING);
                    }
                }
            }

            return(ocean);
        }
コード例 #3
0
 public bool HasWon(Ocean enemysOcean)
 {
     for (int i = 0; i < enemysOcean.Dimension; i++)
     {
         for (int j = 0; j < enemysOcean.Dimension; j++)
         {
             if (enemysOcean.Squares[i][j].Fill == "X" && enemysOcean.Squares[i][j].VisibleForOpponent == false)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
コード例 #4
0
        public static void Print(Ocean ocean, Program.Status status)
        {
            // Display column names  A B C D E F .... J etc.
            // A-Z: ASCII values 65-90
            Console.Write(String.Format("{0,5}", ""));
            for (int i = 65; i < 65 + ocean.Dimension; i++)
            {
                Console.Write(" " + (char)i + " ");
            }
            Console.WriteLine();
            // Display horizontal line
            Console.Write(String.Format("{0,5}", ""));
            for (int i = 0; i < ocean.Dimension; i++)
            {
                Console.Write("---");
            }
            Console.WriteLine();

            // Display board of squares
            foreach (List <Square> sqList in ocean.Squares)
            {
                // First display row number + pipe symbol: e.g. 1 |     2 |     3 |     at the beginning of ech row
                string s = String.Format("{0,5}", (ocean.Squares.IndexOf(sqList) + 1) + " |");
                Console.Write(s);
                foreach (Square squareO in sqList)
                {
                    // For all game status except Fight print all Ocean content
                    if (status != Program.Status.FIGHT)
                    {
                        Console.Write(" " + squareO.Fill + " ");
                    }
                    // ONLY for game status = Fight print content only if Square is already visible
                    else
                    {
                        if (squareO.VisibleForOpponent is true)
                        {
                            Console.Write(" " + squareO.Fill + " ");
                        }
                        // otherwise print tylda - as unknown value
                        else
                        {
                            Console.Write(" " + "~" + " ");
                        }
                    }
                }
                Console.WriteLine();
            }
        }
コード例 #5
0
        private int[] GetShipPositionInput(Ocean ocean)
        {
            int positionX = -1;
            int positionY = -1;

            while (positionX == -1 || positionY == -1)
            {
                Printer.Print("Position:");
                string position = GetUserInput().ToUpper();

                if (position != null && IsLetter(position[0].ToString()))
                {
                    positionY = ConvertLetterToInt(position[0]);
                    if (positionY >= ocean.Dimension)
                    {
                        positionY = -1;
                        Printer.Print("Column index exceeded board dimension");
                    }
                    if (position.Substring(1) != "" && IsNumeric(position.Substring(1)))
                    {
                        positionX = Int32.Parse(position.Substring(1)) - 1;
                        if (positionX >= ocean.Dimension)
                        {
                            positionX = -1;
                            Printer.Print("Row index exceeded board dimension");
                        }
                    }
                    else
                    {
                        Printer.Print("Invalid row number");
                    }
                }
                else
                {
                    Printer.Print("First input character must be a letter indcating column");
                }
            }

            int[] positionInput = new int[2] {
                positionX, positionY
            };

            return(positionInput);
        }