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

== Software Development ==
 is hard, expensive, time consuming, but lucrative

We will DEVELOP a GAME
~~~~~~~~~~~~~~~~~~~~~~~~~
(1)  IDEA = Minesweeper Game

(2)  RESEARCH = investigate the problem, what exists
     learn the rules by playing the game
     If this is a non-computer game (for example Poker)
     you would play the normal game lots of times
     before trying to write a program.

(3)  PLAN - this is the HARDEST part

 DESIGN = Big Picture, what we WANT

   Window
   Reset button
   Menues = save game, new game, load game, exit
         options = change grid size, how many mines, difficulty
         instructions
   Buttons -
      lots, in a grid
      bombs
      pictures
      click on a button, if it's a mine, you lose
      otherwise there are "warnings" numbers

 HOW to do it = implementation, what we NEED
   How do the bombs work?
   How do the warnings work?

 PLAN
   -->  USER INTERFACE
   -->  DATA STORAGE and DATA STRUCTURES
   -->  TASKS and ALGORITHMS (methods)

START with a VERY SIMPLE VERSION, improve it later

HOW do we store the positions of the bombs?

Start with a SPECIFIC EXAMPLE

   O O O X O O
   X O O O X O
   O O X O O O
   O O O O O O
   X X X X X X
   O X O X O X

VARIABLES ?

  int bomb = 0;
  int bomb2 = 1; ??
  no, not that way, we need too many different names

ARRAY ?

  String[] bombs = new String[10];
  bombs[1] = "O";
  bombs[2] = "O";
  bombs[3] = "O";
  bombs[4] = "X";
  ...
  bombs[35] = "O"
  bombs[36] = "X"   okay, but confusing
  ---------- need a better idea

List of ARRAYS ?

   row1[1] = "O"
   row1[2] = "O"
   row1[3] = "O"
   row1[4] = "X"
   ...
   row6[5] = "O"
   row6[6] = "X"

This would work, but there is still a better way

2-D ARRAY ==>  This is the RIGHT WAY, as we will see

You can make a MATRIX (a 2-Dimensional Array)
like this:

  String[][] cells = new String[6][6];

  cells[0][0] = "O";
  cells[0][1] = "O";
  ...
  cells[0][3] = "X";
  ...
  cells[5][5] = "X";

Pressing a button is complicated
So we went back and played some games
to make sure we REALLY understand the game.

Now look at the SAMPLE PROGRAM below to see
how to make a 2-D ARRAY of BUTTONS.

(4) Programming = making the program

This is EASIER than making a PLAN (at least if you have a good plan),
but it is equally important.  Most novice programmers want to start here -
probably because it is closer to the end and more fun - but it is
usually a big waste of time to start programming without a clear plan.

Always start with a SIMPLE VERSION - e.g. a small board,
not too many bombs, bombs in a fixed place, no fancy graphics, etc.
After you have worked out some of the technical problems,
GO BACK TO THE PLANNING STAGE and plan a better version,
then expand your program.

Below is a VERY SIMPLE version.  It needs lots of improvements.
There are suggestions below.

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

import java.awt.*;

public class Game extends EasyApp
{  // MineSweeper
   
   Button[][] buttons = new Button[3][3];
      
   public static void main(String[] args)
   {
      new Game();  
   }
   
   public Game()
   {
      // making the buttons
      
      buttons[0][0] = addButton("",100,100,20,20,this);
      buttons[0][1] = addButton("",120,100,20,20,this);
      buttons[0][2] = addButton("",140,100,20,20,this);
      buttons[1][0] = addButton("",100,120,20,20,this);
      buttons[1][1] = addButton("",120,120,20,20,this);
      buttons[1][2] = addButton("",140,120,20,20,this);
      buttons[2][0] = addButton("",100,140,20,20,this);
      buttons[2][1] = addButton("",120,140,20,20,this);
      buttons[2][2] = addButton("",140,140,20,20,this);
   }
   
   public void actions(Object source,String command)
   {
      // checking which button was pressed
      
      if (source == buttons[0][0])
      {  process(0,0); }
      if (source == buttons[0][1])
      {  process(0,1); }
      if (source == buttons[0][2])
      {  process(0,2); }
      if (source == buttons[1][0])
      {  process(1,0); }
      if (source == buttons[1][1])
      {  process(1,1); }
      if (source == buttons[1][2])
      {  process(1,2); }
      if (source == buttons[2][0])
      {  process(2,0); }
      if (source == buttons[2][1])
      {  process(2,1); }
      if (source == buttons[2][2])
      {  process(2,2); }
      
   }
   
   public void process(int row, int col)
   {  // reacting to the pressed button
      
      if (row == col)
      {
         output("Boom!!!");
         System.exit(0);
      }
      else
      {  buttons[row][col].setLabel("#"); }
   }
}

/********************************
The demo shows how to make a 2-D array of Buttons.
This is okay for 9 buttons, but will be a big mess for 100 buttons.
You need to rewrite this using LOOPS instead of single commands.

(1) Rewrite the CONSTRUCTOR using NESTED LOOPS, like this:

    for (int row = 0; row < 3; row = row + 1)
    {  
       for (int pos = 0; pos < 3; pos = pos + 1)
       {
          buttons[row][pos] = addButton("", ?? , ?? , 20 , 20 , this);
       }
    }

    The numbers (??) need to be CALCULATED from row and pos.
    Something like this, but you need to put these commands
    in the CORRECT LOCATION in the program - don't put them
    all inside the loops.
       
       int x = 100;
       ...
       int y = 100;
       ...
       x = x + 20;
       ...
       y = y + 20;
       ...
       buttons[row][pos] = addButton("", x , y , 20 , 20 , this);
    
(2) Rewrite the actions() method using LOOPS instead of single commands.
    Something like this:

        for ( _____________________ )
        {
           for ( ______________________ )
           {
              if (source == buttons[row][pos])
              {  process(row,pos); }
           }
        }
        
(3) Now it should be RELATIVELY SIMPLE to make a 10 x 10 board,
    just by changing a few numbers.  Do this.

(4) Change the program so that if you click NEXT TO a bomb,
    it will say "You are close" .  First, think about the following:
    (a) Where are the bombs?  What positions?
    (b) What are the positions of all the places that are NEXT TO a bomb?
    (c) How can you EASILY write commands that will check these positions?

(5) Which part of the program needs to change so that
     bombs will be at random positions?
    Now it is time to GO BACK to PLANNING, and think carefully
     about how this should work.
     
**********************************/