final static short NUM = 100;
final static boolean[] toggles = new boolean[NUM];
final static PVector[] rules = new PVector[NUM];
final static boolean[] answers = new boolean[NUM];
boolean started, done;
final PFont pfb = createFont("Arial", 32);
final PFont pfs = createFont("Arial", 16);
void setup() {
size(220, 220);
textAlign(CENTER, TOP);
int i = 0;
while (i!=NUM) {
int a, b, c;
a=b=c=0;
while ( a == b | b == c | a == c |
a == i | b == i | c == i ) {
a = int(random(NUM));
b = int(random(NUM));
c = int(random(NUM));
}
rules[i] = new PVector(a, b, c);
toggles[i] = true;
answers[i++] = false;
}
for ( i=0; i
int r =
int(
random(NUM) );
toggle(r);
}
}
void
draw() {
if ( done || !started ) {
background(-1);
for (
int x= 0; x < 10; x++ ) {
fill(-1);
stroke(196);
rect(10+20*x, 10, 20, 20);
rect(10+20*x, 190, 20, 20);
rect(10, 10+20*x, 20, 20);
rect(190, 10+20*x, 20, 20);
stroke(0);
fill(done? #00FF00 : #FF0000);
rect(15+20*x, 15, 10, 10);
rect(15+20*x, 195, 10, 10);
rect(15, 15+20*x, 10, 10);
rect(195, 15+20*x, 10, 10);
}
if ( ! done ) {
translate(
width>>1,
height>>1);
fill(#FF0000);
textFont(pfb, 32);
text(
"INSANITY", 0, -80);
fill(0);
textFont( pfs, 16 );
String txt =
"The goal is to turn all the squares green." +
"\n\nClick to start.";
text(txt, -70, -40, 140, 200);
}
else {
translate(
width>>1,
height>>1);
fill(#00FF00);
textFont(pfb, 32);
text(
"YOU WIN!", 0, -80);
fill(0);
textFont( pfs, 16 );
String txt =
"I can't believe you actually beat it. " +
"Email me and let me know you did it. Codeword: Bonkers";
text(txt, -70, -40, 140, 200);
}
return;
}
if ( started && !done ) {
background(-1);
int i = 0;
for (
int y= 0; y < 10; y++ ) {
for (
int x= 0; x < 10; x++ ) {
fill(255);
stroke(196);
rect(10+20*x, 10+20*y, 20, 20);
stroke(0);
fill( toggles[i]?0:255, toggles[i]?255:0, 0 );
rect(15+20*x, 15+20*y, 10, 10);
i++;
}
}
}
}
void
mousePressed() {
redraw();
println(
"Clicked");
if (!started) {
started =
true;
noLoop();
return;
}
int mx = (
int) ( (
mouseX - 10) / 20 );
int my = (
int) ( (
mouseY - 10) / 20 );
int mi = 10 * my + mx;
if ( mi >= 0 && mi < 100 ) {
toggle(mi);
doneCheck();
}
}
void toggle(
int which) {
answers[which]=!answers[which];
toggles[which]=!toggles[which];
toggles[
int(rules[which].x)]=!toggles[
int(rules[which].x)];
toggles[
int(rules[which].y)]=!toggles[
int(rules[which].y)];
toggles[
int(rules[which].z)]=!toggles[
int(rules[which].z)];
}
void doneCheck() {
int falseCount = 0;
for (
int i=0; i < 100 && falseCount==0; i++ ) {
if ( !toggles[i] ) {
falseCount++;
}
}
if ( falseCount == 0 ) {
done =
true;
started =
false;
}
}