import java.applet.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

/**
 * BattleShip class defines an applet which consist of game board 
 * of 100 buttons and button which starts a new game and a status
 * bar which is used to tell the situation in game.
 */
public class BattleShip extends Applet implements ActionListener
{
  
  private MyButton buttons[][];
  private int buttonStatus[][];
  private int shipTable[][];
  private int destroyedShips;
  private Button restart;
  private TextField status;
  
  private static final int EMPTY=0, NO_SHIP=1, SHIP=2;
  private static final int NumberOfShips = 20;

  /** BattleShip contructor */
  public BattleShip() {
    super();
    buttons = new MyButton[10][10];
    buttonStatus = new int[10][10];
    shipTable = new int[10][10];
  }


  /** Check the status of the game and update it */
  void Status(int x, int y) {
    buttonStatus[x][y] = shipTable[x][y];
    if (buttonStatus[x][y] == SHIP)
      destroyedShips++;
    showStatus();
  }
  
  /** Method that checks if the bom hit the ship or missed a ship */
  String Bom(int x, int y) {
    Status(x, y);
    if (shipTable[x][y] == SHIP)
      return new String("X");
    else
      return new String("O");
  }
 
  /** Set the status of the game to the status bar */
  private void showStatus() {
    if (destroyedShips == NumberOfShips)
      status.setText("You won!");
    else
      status.setText(NumberOfShips-destroyedShips + " ships to go.");
  }
  
  /** Applet initialization */
  public void init() {

    GridBagConstraints c = new GridBagConstraints();    
    c.fill = GridBagConstraints.BOTH;  // fill entire display area
    c.weightx = 1.0;
    c.weighty = 1.0;

    // Set layout to applet 
    GridBagLayout g = new GridBagLayout();
    setLayout(g);
    g.setConstraints(this, c);

    // Add Main panel
    Panel MainPanel = new Panel(g);
    g.setConstraints(MainPanel, c);
    add(MainPanel);

    // Add Button panel for start button and status bar
    c.fill = GridBagConstraints.VERTICAL;
    Panel ButtonPanel = new Panel(g);
    g.setConstraints(ButtonPanel, c);
    MainPanel.add(ButtonPanel);

    // Add Grid panel for 100 game board buttons
    c.fill = GridBagConstraints.BOTH;
    Panel GridPanel = new Panel(g);
    g.setConstraints(GridPanel, c);
    MainPanel.add(GridPanel);
    
    // Create the hundred buttons
    for (int y = 0; y < 10; y++) {
      c.gridwidth = 1;
      for (int x = 0; x < 10; x++) {
    	buttonStatus[x][y] = EMPTY;
    	buttons[x][y] = new MyButton(this, x, y);
    	if (x == 9)
    	  c.gridwidth = GridBagConstraints.REMAINDER; //end row
    	g.setConstraints(buttons[x][y], c);
    	GridPanel.add(buttons[x][y]);
      }
    }

    // Creates ships to shipTable
    int l = 0; 
    int x, y;
    while (l < NumberOfShips) {
      x = (int)Math.round(Math.random() * 9.0);
      y = (int)Math.round(Math.random() * 9.0);
      if (shipTable[x][y] == EMPTY) {
	shipTable[x][y] = SHIP;
	l++;
      }
    }

    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;

    // Add status bar to Button panel
    status = new TextField();
    showStatus();
    status.setEditable(false);
    g.setConstraints(status, c);
    ButtonPanel.add(status);

    c.gridwidth = GridBagConstraints.REMAINDER; //end row

    // Add start button to Button panel
    restart = new Button("Start Game");
    restart.addActionListener(this);
    restart.setActionCommand("restart");
    g.setConstraints(restart, c);
    ButtonPanel.add(restart);  

  }
  
  /** Start or resume execution */
  public void start()
  {
  }

  /** Responds to user actions */
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    String command = e.getActionCommand();
    
    if (source instanceof Button) { 
      
      if (command == "restart") {
	for (int y = 0; y < 10; y++)
	  for (int x = 0; x < 10; x++) {
	    buttonStatus[x][y] = EMPTY;
	    buttons[x][y].setLabel("");
	  }
	destroyedShips = 0;
	showStatus();
      }
    }
  }
  
  /** Suspends execution */
  public void stop()
  {
  }
  
  /** Perform shutdown activities */
  public void destroy()
  {
  }
  
  /** Returns information on applet */
  public String getAppletInfo()
  {
    return new String("Java-ohjelmoinnin perusteet - BattleShip Game");
  }

  /** Paints widgets */
  public void paint(Graphics g) {
    for (int y = 0; y < 10; y++)
      for (int x = 0; x < 10; x++) {
    	buttons[x][y].paint(g);
      }
    status.paint(g);
    restart.paint(g);
  }
}













