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

== Maxi Sweeper ==

This version of our game is finally SCALABLE (easily expanded)
thanks to the use of arrays, loops, and SETTINGS.
Settings are values declared at the top of the program.
Then we use the NAMES of the settings throughout the
program instead of writing many copies of the same LITERAL VALUE.
For example, rather than writing 3 everywhere to tell the
size of the array, we declare a variable named BOARD_SIZE at the
top of the program, then write BOARD_SIZE everywhere.  That makes
the program more flexible, because only one number must
be changed to change the entire program.

Once we have introduced settings variables, we want to ensure
that all portions of the program use these values, rather
than literal values.  All the numbers 20 have been replaced
by BUTTON_SIZE.

# 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.
    
========================================================

-- Warnings --
In MineSweeper, you get warnings when you click on empty spots.
The warning is a number telling you how many bombs there are
in neighboring squares.  For example, consider this board

   0 0 0 1
   0 0 1 1
   1 1 0 0
   0 1 1 0
   
Then the following WARNING values would be displayed:

   Location    Warning Value
   ---------   -------------
    [0][0]      0
    [0][1]      1
    [0][2]      3
    [0][3]      BOOM!
    
    [0][0]      
    [0][1]      
    [0][2]      
    [0][3]      

    [0][0]      
    [0][1]      
    [0][2]      
    [0][3]      
    
    [0][0]      
    [0][1]      
    [0][2]      BOOM!
    [0][3]      1

# Fill in the rest of the warning values
    
The WARNING value should be displayed by the process() method.
Rather than putting '#' on the button, it can put the
warning number on the button.  This requires looking at
the NEIGHBORING positions.  For example:

   warning = 0;
   if ( bombs[row][pos+1] == 1 )   // right
   {  warning = warning + 1; }
   if ( bombs[row+1][pos+1] == 1)  // down-right
   {  warning = warning + 1; }
   ...
   ...
   buttons[row][pos].setLabel(warning + "");
   
You need to add more if.. commands to check all the neighboring
positions.  There are 8 neighboring positions :
  left  up  right  down  up-left  up-right  down-left  down-right

Unfortunately, if you ALWAYS check 8 neighbors, you will get
ERRORS at the edges.  At the edges (and in the corners) you
cannot check the directions where the neighbors don't exist.
So your if commands must be a bit more complicated, more like this:

   if (row != BOARD_SIZE - 1)
   {  if ( bombs[row][pos+1] == 1 )
      {  warning = warning + 1; }
   }   

You might want to do the "simple" version first -
the one for the interior squares - then expand it to
work correctly on the edges.

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

import java.awt.*;

public class MaxiSweeper extends EasyApp
{
   //--- Settings ---
   
   int BOARD_SIZE = 5;
   int BUTTON_SIZE = 30;
      
   //--- GUI components ---
   
   Label title = addLabel("MaxiSweeper - by FIS IB Comp Sci 07",
                            10,10,600,80,this);
   Button[][] buttons = new Button[BOARD_SIZE][BOARD_SIZE];
   
   Button bCheat = addButton("Cheat",10,100,50,20,this);
   
   //--- Global Variables ---
   
   int[][] bombs = new int[BOARD_SIZE][BOARD_SIZE];
   
   //--- Program ---
   
   public static void main(String[] args)
   {  
      new MaxiSweeper();  
   }
   
   public MaxiSweeper()
   {
      // making the bombs
      
      for (int row = 0; row < BOARD_SIZE; row = row + 1)
      {  
         for (int pos = 0; pos < BOARD_SIZE ; pos = pos + 1)
         {
            int num = (int) Math.floor( Math.random()*2 );
            bombs[row][pos] = num;
         }
      }
      
      title.setFont(new Font("Arial",0,30));
      
      // making the buttons
      
      int x = 100;
      int y = 100;
      
      for (int row = 0; row < BOARD_SIZE ; row = row + 1)
      {
         for (int pos = 0; pos < BOARD_SIZE; pos = pos + 1)
         {            
            buttons[row][pos] = addButton("",x,y,BUTTON_SIZE,BUTTON_SIZE,this);
            x = x + BUTTON_SIZE;
         }
         y = y + BUTTON_SIZE;
         x = 100;
      }
   }
   
   public void actions(Object source,String command)
   {
      // checking which button was pressed
      
      if (source == bCheat)
      {  cheat(); }
      for (int row = 0; row < BOARD_SIZE; row = row + 1)
      {
         for (int pos = 0; pos < BOARD_SIZE; pos = pos + 1)
         {
            if (source == buttons[row][pos])
            {  process(row,pos); }
         }
      }
   }
   
   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("#"); }
   }
   
   public void cheat()
   {
      // print out entire bombs array - for debugging
      
      for (int row = 0; row < BOARD_SIZE; row = row + 1)
      {
         for (int pos = 0; pos < BOARD_SIZE; pos = pos + 1)
         {
            System.out.print( bombs[row][pos] );
         }
         System.out.println();
      }
   }
}