import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

// -----------------------------------------------------------------

class XYButton extends Button {
  public int x, y;
  XYButton(String s, int x, int y) {
    super(s);
    this.x = x;
    this.y = y;
  }
}

class Ship {
  int [] xs;
  int [] ys;

  // Creates a ship of a given length, on a max by max grid.
  // Orientation is determined randomly
  public Ship(int length, int max) {
    xs = new int [length];
    ys = new int [length];

    boolean across = (Math.random() > 0.5);
    int pos1 = (int) (Math.random() * (max - length)); 
    int pos2 = (int) (Math.random() * max);
    for(int i=0; i<length; i++) {
      if (across) {
        xs[i] = pos1+i;
        ys[i] = pos2;
      } else {
        xs[i] = pos2;
        ys[i] = pos1+i;
      }
    }
  }
  
  public boolean hit(int x, int y) {
    for(int i=0; i<xs.length; i++) {
      if ((xs[i] == x) && (ys[i] == y)) {
        return true;
      }
    }
    return false;
  }
}


public class BattleShip extends Applet implements ActionListener {
  Vector ships = new Vector();
  static final int SIZE = 10;
  XYButton [] [] buttons;

  public void init () {
    addShip(2);  // 2 units long/tall
    addShip(3);  // 3 units long/tall

    setBackground(Color.white);
    setLayout(new BorderLayout());

    addButtonPanel();
    addOtherWidgets();
  }

  public void addButtonPanel() {
    Panel p = new Panel();
    p.setLayout(new GridLayout(SIZE, SIZE));

    buttons = new XYButton [SIZE][SIZE];  // How many buttons are created here?
              
    for(int j=0; j<SIZE; j++) {    // j for rows
      for(int i=0; i<SIZE; i++) {  // i for columns
        XYButton b = new XYButton("Ocean", i, j);
        b.setForeground(Color.blue); 
        b.addActionListener(this);
        b.setFont(new Font("SansSerif", Font.PLAIN, 10));
        buttons[i][j] = b;
        p.add(b);
      }
    }
    add(p, BorderLayout.CENTER);
  }

  public void addOtherWidgets() {
    // A container Panel to help with placement
    Panel southPanel = new Panel();
    southPanel.setLayout(new GridLayout(1, 2));

    // A textfield
    final TextField tfield = new TextField("", 30);
    tfield.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        String text = tfield.getText();
        text = text.toUpperCase();
        int x = ((int) text.charAt(0)) - ((int) 'A');
        int y = Integer.parseInt(text.substring(1));
        changeText(buttons[x][y]);
        tfield.setText("");
      }
    });
    southPanel.add(tfield);

    // A quit button
    Panel p = new Panel();
    Button b = new Button("Quit");
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        cheat();
      }
    });
    p.add(b);
    p.setLayout(new FlowLayout(FlowLayout.RIGHT));
    southPanel.add(p);

    // Add the textfield and button
    add(southPanel, BorderLayout.SOUTH);
  }

  public void cheat() {
    for(int i=0; i<ships.size(); i++) {
      Ship s = (Ship) ships.elementAt(i);
      for(int j=0; j<s.xs.length; j++) {
        int x = s.xs[j];
        int y = s.ys[j];
        changeText(buttons[x][y]);
      }
    }
  }


  public void addShip(int length) {
    ships.addElement(new Ship(length, SIZE));
  }

  public boolean isHit(int x, int y) {
    Enumeration e = ships.elements();
    while (e.hasMoreElements()) {
      Ship s = (Ship) e.nextElement();
      if (s.hit(x, y)) {
        return true;
      }
    }
    return false;
  }

  public void changeText(XYButton b) {
    int x = b.x;
    int y = b.y;
    boolean hit = isHit(x,y);
    if (hit) {
      b.setBackground(Color.red);
      b.setLabel("Hit");
    } else {
      b.setBackground(Color.yellow);
      b.setLabel("Miss");
    }
  }

  public void actionPerformed(ActionEvent ae) {
    XYButton b = (XYButton) ae.getSource();
    changeText(b);
  }
  
}
