<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class JQuinto extends Applet implements ActionListener, WindowListener {

  private int gridx=5;
  private int gridy=5;
  private int moves=0;

  private JQuinto.Piece[][] pieces;

  public static void main(String[] args) {
    //simplest is best for now;  things can be added like "help", "info",
    //yadda yadda yadda... maybe options to change the dimension of the board,
    //pick colors, whatever...
    Frame f = new Frame();
    JQuinto j = new JQuinto();
    j.init();
    f.add(j);
    f.pack();
    f.setLocation(50,50);
    f.addWindowListener(j);
    f.setVisible(true);
  }

  public void init() {
    //make a board of appropriate size
    GridLayout gl = new GridLayout(gridx,gridy);
    setLayout(gl);
    pieces= new JQuinto.Piece[gridx][gridy];
    for (int y=0;y&lt;gridy;y++) for (int x=0;x&lt;gridx;x++) {
      pieces[x][y] = new JQuinto.Piece(x,y);
      pieces[x][y].addActionListener(this);
      //if random, call p.toggle() randomly here :) [LATER]
      add(pieces[x][y]);
    }
  }

  public void actionPerformed(ActionEvent a) {
    moves++;
    Point p = ((JQuinto.Piece)a.getSource()).getPosition();
    //left
    if (p.x &gt; 0) pieces[p.x-1][p.y].toggle();
    //right
    if (p.x &lt; gridx - 1) pieces[p.x+1][p.y].toggle();
    //up
    if (p.y &gt; 0) pieces[p.x][p.y-1].toggle();
    //down
    if (p.y &lt; gridy - 1) pieces[p.x][p.y+1].toggle();
    //check for win
    for (int y=0;y&lt;gridy;y++) for (int x=0;x&lt;gridx;x++)
      if (!pieces[x][y].isSet()) return;
    //if we made it here, there's a win 
    System.out.println("congratulations: "+moves+" moves.");
    System.exit(-1);
  }

  //for being a WindowListener
  public void windowOpened(WindowEvent e) { }
  public void windowClosing(WindowEvent e) { System.exit(0); }
  public void windowClosed(WindowEvent e) { }
  public void windowIconified(WindowEvent e) { }
  public void windowDeiconified(WindowEvent e) { }
  public void windowActivated(WindowEvent e) { }
  public void windowDeactivated(WindowEvent e) { }

private class Piece extends Component implements MouseListener {
  private boolean set=false;
  private int x,y;
  private ActionListener al;
  private Dimension mysize=new Dimension(100,100);

  public Piece(int x,int y) { this.x=x; this.y=y; addMouseListener(this);}

  //for acting like a semi-intelligent piece
  public Point getPosition() { return new Point(x,y); }
  public void toggle() { set=!set; repaint(); }
  public boolean isSet() { return set; }

  //for acting as a component
  public Dimension getPreferredSize() { return mysize; }
  public Dimension getMinimumSize()   { return mysize; }
  public Dimension getMaximumSize()   { return mysize; }

  public void paint(Graphics g) {
    g.setColor((set)?Color.white:Color.black);
    g.fillRect(0,0,mysize.width,mysize.height);
    g.setColor((!set)?Color.white:Color.black);
    g.drawRect(0,0,mysize.width,mysize.height);
  }

  //MouseListener
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mouseClicked(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) {
    set=!set;
    al.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"Click"));
  }

  //for dealing with action listeners
  public void addActionListener(ActionListener al) { 
    this.al=AWTEventMulticaster.add(this.al,al);
  }

  public void removeActionListener(ActionListener al) {
    this.al=AWTEventMulticaster.remove(this.al,al);
  }

}

}
</pre></body></html>