import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Puzzle extends Applet
{
static Label stat;
static Panel controlPanel;
static Button easy,medium,hard;
static Panel buttonBar;
static cmdButton[][] ca = new cmdButton[4][4];
static Random rand = new Random();
static int difficulty = 0;
static void toggle(int x,int y)
{
if( difficulty >= 2 )
{// wrap button effects
ca[y & 0x3][x & 0x3].tog();
}
else {// no wrap
if( 0 <= x && x <= 3 && 0 <= y && y <= 3 )
ca[y][x].tog();
}
}
static void checkwin()
{
boolean win = true;
for(int y = 0; y < 4; y++)
for(int x = 0; x < 4; x++)
{
if( ca[y][x].isRed )
{
win = false;
break;
}
}
if( win )
switch( rand.nextInt() & 0x3 )
{
case 0: stat.setText("Wow!"); break;
case 1: stat.setText("You win!"); break;
case 2: stat.setText("Woo hoo!"); break;
case 3: stat.setText("Yowza!"); break;
}
}
class cmdButton extends Button implements ActionListener
{
char op;
int x,y;
boolean isRed;
cmdButton(int px, int py)
{
x = px;
y = py;
this.addActionListener(this);
isRed = false;
setBackground(Color.green);
}
public void setLabel(String label)
{
super.setLabel(label);
op = label.charAt(0);
}
public void setGreen()
{
isRed = false;
setBackground(Color.green);
}
public void tog()
{
isRed = !isRed;
if( isRed )
setBackground(Color.red);
else setBackground(Color.green);
}
// puzzle button pushed
public void actionPerformed(ActionEvent e)
{
switch( op )
{
default:
break;
case '+':
toggle(x,y);
toggle(x+1,y);
toggle(x-1,y);
toggle(x,y+1);
toggle(x,y-1);
break;
case '|':
toggle(x,y);
toggle(x,y+1);
toggle(x,y-1);
break;
case 'x':
toggle(x,y);
toggle(x-1,y-1);
toggle(x+1,y-1);
toggle(x-1,y+1);
toggle(x+1,y+1);
break;
case '-':
toggle(x,y);
toggle(x+1,y);
toggle(x-1,y);
break;
}
if( e != null )
checkwin();
}
}
public void init()
{
easy = new Button("Easy");
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ difficulty = 0; start(); }
});
medium = new Button("Medium");
medium.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ difficulty = 1; start(); }
});
hard = new Button("Hard");
hard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ difficulty = 2; start(); }
});
buttonBar = new Panel();
buttonBar.setLayout( new GridLayout(1,3) );
buttonBar.add(easy);
buttonBar.add(medium);
buttonBar.add(hard);
controlPanel = new Panel();
controlPanel.setLayout( new GridLayout(4,4) );
for(int y = 0; y < 4; y++)
for(int x = 0; x < 4; x++)
controlPanel.add(ca[y][x] = new cmdButton(x,y));
setLayout(new BorderLayout());
add(controlPanel);
add("North",buttonBar);
add("South",stat = new Label());
}
public void start()
{
stat.setText("Make all buttons green.");
// randomize button labels
for(int y = 0; y < 4; y++)
for(int x = 0; x < 4; x++)
{
switch( rand.nextInt() & 0x3 )
{
default: ca[y][x].setLabel("+"); break;
case 1: ca[y][x].setLabel("-"); break;
case 2: ca[y][x].setLabel("|"); break;
case 3: ca[y][x].setLabel("x"); break;
}
ca[y][x].setGreen();
}
// push some buttons to mix it up
int pushes = difficulty==0 ? 3 : 8;
for(int i = 0; i < pushes; i++)
{
int x = rand.nextInt() & 0x3;
int y = rand.nextInt() & 0x3;
ca[y][x].actionPerformed(null);
}
}
}