import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
//Battleship

public class battleship_buttons extends Applet implements ActionListener
{
    //applet member data
    Button [] a; //1D array to simulate a 2D one
    int row = 5;
    int col = 8;
    int total = row * col;
    square ocean [] [] = new square [5] [8];
    /* ocean will hold values something like this:
	* * * * * D * S
	* C C C * D * S
	C * * * * D * *
	C * * S * D * *
	C * * S * * S S
     */


    public void init ()
    {
	JPanel p = new JPanel (new GridLayout (row, col, 0, 0));

	//declare a new array of buttons
	a = new Button [total];

	//initialize each of the buttons in the array
	//with an empty label
	for (int nNum = 0 ; nNum < total ; nNum++)
	{
	    a [nNum] = new Button ("");
	    p.add (a [nNum]);
	    //each button will have an action listener
	    a [nNum].addActionListener (this);
	    a [nNum].setBackground (Color.black);
	    //each button will send a message with its number
	    a [nNum].setActionCommand ("" + nNum);
	}

	add (p);
	initializeocean (ocean, row, col);
	printocean (ocean, row, col);
    }

    public void actionPerformed (ActionEvent e)
    { //To handle the button array - invoked when button is clicked

	//get the number of the button from getActionCommand
	//convert it to an int and store it in i
	int pos = Integer.parseInt (e.getActionCommand ());

	//find i and j
	int i = pos / col;
	int j = pos % col;

	//set button to have a new background colour
	if (a [pos].getBackground ().equals (Color.blue))
	    a [pos].setBackground (Color.yellow);
	else
	    a [pos].setBackground (Color.blue);

    }


    public void initializeocean (square ocean [] [], int sizex, int sizey)
    { //Pre: Ocean is sizex X sizey in dimensions, it has not be constructed
	//Post: All elements of ocean are not visible and have no ships (*) in them
	for (int i = 0 ; i < sizex ; i++)
	{
	    for (int j = 0 ; j < sizey ; j++)
	    {
		ocean [i] [j] = new square ();
		ocean [i] [j].inhere = "*";
		ocean [i] [j].visible = false;
	    }
	}
    }


    public void printocean (square ocean [] [], int sizex, int sizey)
    { //Pre: Ocean has values and is sizex X sizey in dimensions
	//Post: The ocean and the ships that have been found are printed to the screen

	int move = 0;
	for (int i = 0 ; i < sizex ; i++)
	{

	    for (int j = 0 ; j < sizey ; j++)
	    {
		if (ocean [i] [j].visible == false)
		{
		    a [move].setLabel ("~");
		    a [move].setBackground (Color.blue);
		    move++;
		}
		else
		{
		    a [move].setLabel (ocean [i] [j].inhere);
		    a [move].setBackground (Color.blue);
		    move++;
		}
	    }

	}
    }
}

public class square
{
    public String inhere;
    /* Note - possible values of inhere are:
	* = nothing is in that square
	S = submarine is in that square
	C = cruiser is in that square
	D = destroyer is in that square
    */
    public boolean visible;
    /* If the user has guessed that square, it becomes visible
       This means the value 'inhere' will be printed to the screen
    */
}



