/********************************************************************** * Created by: Eric Gilbert * Date: 1/24/99 * Designed to mimic the game Deluxe Lights Out(r) by Tiger Electronics **********************************************************************/ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class FourByFour extends Applet implements ActionListener { int width = 4, height = 4; Button[][] lights = new Button[width][height]; Color mostlyRed = new Color(255, 50, 50); Color veryDarkGray = Color.gray; public void init() { setLayout(new GridLayout(width, height)); // Make the screen a grid double choice; Random generator = new Random(); for(int i = 0; i < width; i++) for(int j = 0; j < height; j++) { choice = generator.nextDouble(); lights[i][j] = new Button(""); lights[i][j].setBackground(choice < 0.5 ? veryDarkGray : mostlyRed); add(lights[i][j]); lights[i][j].addActionListener(this); } } public void changeColor(Button toChange) { if (toChange.getBackground() == mostlyRed) toChange.setBackground(veryDarkGray); else toChange.setBackground(mostlyRed); } public void actionPerformed(ActionEvent e) { int row = 0, column = 0; for(int i = 0; i < width; i++) for(int j = 0; j < height; j++) if (lights[i][j] == e.getSource()) { row = i; column = j; } changeColor(lights[row][column]); changeColor(lights[row][(column - 1 + width)%width]); changeColor(lights[(row + 1)%height][column]); changeColor(lights[row][(column + 1)%width]); changeColor(lights[(row - 1 + height)%height][column]); } }