package EDU.gac.max.flip; import java.awt.*; /** This class is the main class for an application program called Flip * that is a puzzle in which the user is presented with a 6x6 grid of * buttons such that clicking any one flips the color of that button * and its neighbors (if any) to the north, south, east, and west. * The goal is to get the buttons back to all one color. * *

Written 17 Aug 1996 by * Max Hailperin * <max@gac.edu> */ public class Flip extends Frame { public Flip() { setTitle("Flip"); state = new boolean[size*size]; Panel controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); controlPanel.add(new Button("Randomize")); add("North", controlPanel); Panel mainPanel = new Panel(); mainPanel.setLayout(new GridLayout(size, size)); for (int i = 0; i < size*size; i++) mainPanel.add(new Button("" + i)); add("Center", mainPanel); indicators = mainPanel.getComponents(); for(int i = 0; i < size*size; i++) recolor(i); resize(size * cellSize, size * cellSize + controlPanel.preferredSize().height); show(); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { if(arg.equals("Randomize")){ randomize(); return true; } else if (arg instanceof String) { int key = Integer.parseInt((String) arg); flip(key); if(key >= size) flip(key-size); if(key < size*size - size) flip(key+size); if(key % size > 0) flip(key-1); if(key % size < size - 1) flip(key+1); return true; } return super.action(evt, arg); } /** The flip method takes care of flipping one of the "keys" on the * board -- such as the one clicked-on or one of its neighbors. This * procedure is responsible both for updating the object's record of which * state the key is in and also for actually recoloring the displayed key * (using the recolor method). */ protected void flip(int key) { state[key] = !state[key]; recolor(key); } /** The recolor method updates the displayed color of one of the keys * on the board to reflect the current flippedness state of that key. */ protected void recolor(int n) { indicators[n].setBackground(state[n] ? Color.red : Color.green); } /** The randomize method independently chooses whether to flip each key * with a 50/50 chance for each. Note that for some board sizes this * kind of randomization might lead to board configurations that can * not be solved (to a single-color state) by any combination of * mouse clicks. If such sizes are to be used, it would be better to * have the randomize procedure act like the user interface does, flipping * a key and its neighbors rather than just the one key. */ protected void randomize() { for(int i = 0; i < size*size; i++){ if(Math.random() > .5){ flip(i); } } } public static void main(String[] args) { new Flip(); } // changing size to 3 makes the puzzle easier static final int size = 6; // how many buttons wide and high static final int cellSize = 65; // how big each is (unless resized) boolean[] state; Component[] indicators; }