// Starting program for the game of NIM // written by Jarek Rossignac boolean firstGame=true; color red = color(250,30,30); color gr = color(30,250,60); int s=40; int p[] = new int[3]; int A=int(random(3,15.9)); int B=int(random(3,15.9)); int C=int(random(3,12.9)); void setup() { size(600, 120); fill (gr); ellipseMode(CENTER); stroke (2); p[0]=A;p[1]=B;p[2]=C; for (int i=0; i<3; i++) {println("p["+i+"] = "+p[i]); }; println("You play first."); } void draw() { background(254); for (int i=1; i<3; i++) { line (0,i*s,width,i*s); }; for (int i=1; i<15; i++) { line (i*s,0,i*s, height); }; for (int j=0; j<3; j++) {for (int i=0; i<15; i++) { if (p[j]>i) { ellipse(s/2+s*i,s/2+s*j,20,20); }; }; }; }; void mousePressed() { int i=mouseX/s,j=mouseY/s; println("i="+i+", j="+j); if (p[j]>i) {p[j]=i; }; if(p[0]+p[1]+p[2]==0) { if (firstGame) {firstGame=false; p[0]=A; p[1]=B; p[2]=C; println("You won the first game.Let's play again.");} else { println("You won the second game. Congratulations!");}; }; } void mouseReleased() { scheme(); if(p[0]+p[1]+p[2]==0) { if (firstGame) {firstGame=false; p[0]=A; p[1]=B; p[2]=C; scheme(); println("I won the first game. Let's play again. I played first");} else { println("I won the second game. Good try man!");}; }; } void scheme() { int m=0; if (p[1]>p[m]) { m=1;}; if (p[2]>p[m]) {m=2;}; p[m]--; /* Put your code here for scheme S1. Then next week replace it with the code for your own winning scheme. */ }