Ejemplo n.º 1
0
        public void Start()
        {
            int r     = 0;
            int c     = 0;
            int m     = 0;
            int count = 0;


            do
            {
                //user inputing number of rows, columns and mines found in the board
                Console.Write("Rows: ");
                r = Convert.ToInt32(Console.ReadLine());
                Console.Write("Columns: ");
                c = Convert.ToInt32(Console.ReadLine());


                if (r != 0 && c != 0)
                {
                    b = new Board(r, c);
                    Console.WriteLine("Would you like to set up the board manually? (Y/N)");
                    string ans = Console.ReadLine();

                    //filling up the grid manually
                    if (ans.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
                    {
                        bool userInputValid = false;
                        do
                        {
                            userInputValid = b.SetUpBoardManually();
                        } while (userInputValid != true);
                        board = b.GetBoard();
                    }

                    //filling up the grid automatically
                    else if (ans.Equals("N", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Console.WriteLine("Setting up the board automatically.");
                        Console.Write("Enter max number of mines: ");
                        m = Convert.ToInt32(Console.ReadLine());
                        b.SetUpBoardAutomatically(m);
                        board = b.GetBoard();
                        b.PrintBoard();
                    }

                    //array is traversed to count the number of neighbouring mines for each element
                    for (int i = 0; i < r; i++)
                    {
                        for (int j = 0; j < c; j++)
                        {
                            if (board[i, j] != '*')
                            {
                                int mineCount = CountNeighbourMines(i, j);
                                board[i, j] = (char)(mineCount + 48);
                            }
                        }
                    }

                    boards.Add(board); //filling up list with boards that contain information about number of neighbouring mines
                }
                Console.WriteLine();
            } while (r != 0 && c != 0);

            //commencing printing of results - each array in the list is sent to be printed
            for (count = 0; count < boards.Count; count++)
            {
                Console.WriteLine("Field #" + (count + 1));
                PrintMineCountBoard(boards[count]);
                Console.WriteLine();
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }