import java.awt.*;

public class MiniSweeper extends EasyApp
{
   Label title = addLabel("MiniSweeper - by FIS IB Comp Sci 07",
                            10,10,600,80,this);
   Button[][] buttons = new Button[3][3];
      
   public static void main(String[] args)
   {  
      new MiniSweeper();  
   }
   
   public MiniSweeper()
   {
      title.setFont(new Font("Arial",0,30));
      
      // making the buttons
      int x = 100;
      int y = 100;
      
      for (int row = 0; row < 3 ; row = row + 1)
      {
         for (int pos = 0; pos < 3; pos = pos + 1)
         {            
            buttons[row][pos] = addButton("",x,y,20,20,this);
            x = x + 20;
         }
         y = y + 20;
         x = 100;
      }
   }
   
   public void actions(Object source,String command)
   {
      // checking which button was pressed
      for (int row = 0; row < 3; row = row + 1)
      {
         for (int pos = 0; pos < 3; pos = pos + 1)
         {
            if (source == buttons[row][pos])
            {  process(row,pos); }
         }
      }
   }
   
   public void process(int row, int pos)
   {  
      // reacting to the pressed button
      if (row == pos)
      {
         output("Boom!!!");
         System.exit(0);
      }
      else
      {  buttons[row][pos].setLabel("#"); }
   }
}

/****************************************************
If you managed the tasks in the Game assignment, you got this far.
Now it's time to make random bombs.

First, we must come to grips with a relatively new and
powerful concept, called Model-View-Controller (MVC).
Here is a quick summary.

== MVC ==

MVC tells us there are three important concepts in programming,
and we should deal with these somewhat SEPARATELY, rather
than blurring them all together into one poorly defined mess.
You can read a brief explanation at : ootips.org/mvc-pattern.html

In our MiniSweeper game, the MVC concept is:

- Model = an ARRAY of bombs and free positions

- View = the BUTTONS that appear on the screen

- Controller = the USER ACTIONS - clicking a button - together with
                the PROCESS method reacting to the click

The MVC PARADIGM (concept) tells us we should treat these separately
if possible.  It is especially important to understand that
the User will SEE something quite different than the computer KNOWS.
The computer knows where the bombs are, but the user won't see this.
That means we cannot store the bomb location data in the buttons.
It needs to be stored somewhere else - in an ARRAY.  This was
already discussed in the previous GAME program.  There we thought
we could represent a bomb by an X and a free spot by O.
It will be a bit simpler to use 1 for a bomb and 0 (zero) for free.

     1 O O
     O 1 O
     1 1 O
     
We can store this data in an array as follows:

   int[][] bombs = {
                      {1 , 0 , 0} ,
                      {0 , 1 , 0} ,
                      {1 , 1 , 0}
                   } ;  

We notice that a 2-D array is actually an ARRAY of ARRAYS.
You can write this command at the beginning of the program.
In a "real" game, these number will be generated randomly,
during the constructor. But we start with a very simple version.

Now in the PROCESS method, we can check the number in the
array to see whether there is a bomb, rather than "calculating"
the result, like this:

   public void process(int row, int pos)
   {  
      // reacting to the pressed button
      if (bombs[row][pos] == 1)
      {
         output("Boom!!!");
         System.exit(0);
      }
      else
      {  buttons[row][pos].setLabel("#"); }
   }

If you add the bombs[][] array and modify the PROCESS method,
you have a good start on a real MiniSweeper game.  This
is a much better start than the original GAME, because
this version is SCALABLE.

== SCALABLE ==

The term SCALABLE means that the basic concepts in the solution
can be EASILY changed to make a BIGGER version.  In the original
GAME, a bigger board requires more and more commands.  In this
version, a bigger board only requires changing a few numbers.
Well, except for the BOMBS array.  It has been written LITERALLY -
that means we wrote out the contents exactly.  Just like the
rest of the program, the bombs array needs to be created by LOOPS,
not by writing the contents.

(1) Add the bombs[][] array to the program, and change the
    PROCESS method as shown above.  Test the program and debug it.
    
(2) Change the program so the bombs are created during
    the constructor, like this:
    
   for (int row = 0; row < 3; row = row + 1)
   {  
      for (int pos = 0; pos < 3 ; pos = pos + 1)
      {
         int num = (int) Math.floor( Math.random()*2 );
         bombs[row][pos] = num;
      }
   }
   
   Change the bombs declaration to say:
   
   int[][] bombs = new int[3][3];
   
(3) Testing a program like this is impossible, because
   the actual contents of the array are different every time
   and they are invisible.  Add a CHEAT button that prints
   out the array.  When the game is finished, you can
   easily remove the cheat button. It could work like this:
   
   for (int row = 0; row < 3; row = row + 1)
   {
      for (int pos = 0; pos < 3; pos = pos + 1)
      {
         System.out.print( bombs[row][pos] );
      }
      System.out.println();
   }   

(4) Now the basic game is pretty good.  Time to SCALE it up.
   Make all the changes necessary to change to a 5 x 5 board.
   In the program, write comments (notes) to keep track
   of all the changes you make, so it will be easy to make
   a 10x10 board later.
   
(5) Now add code to do some of the following:
   (a) At the beginning, tell the user how many bombs there are.
   (b) Keep score, so you get 10 points each time you click
       on an empty square.
   (c) Change the random command so it generates numbers 0,1,2,3.
       and only a 1 is a bomb.  This makes 25% of the squares
       bombs, so the game is easier.
   (d) Let the user choose their level of difficulty at the
       beginning - either 50% bombs or 25% bombs or 10% bombs.
   (e) Make a DEFUSE option.  If you turn on defusing, then
       when you click on a bomb you get 50 points, but if
       you click on an empty square, you lose 10 points.

Next time we move on to the really interesting stuff -
like counting the number of neighboring bombs around an empty square.

*****************************************************/