#59 Simple memory card game
Alternative names: Mix-n-match, Concentration, Memory, Matching cards,…
Code
int w = 4, h = 4, scl = 150, buffer=5;
card c[][] = new card[w][h];
int s1 = -1, s2 = -1, state = 0;
void setup() {
size(600, 600);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
c[i][j] = new card(i, j, floor((i+j*w)/2));
}
shuffleBoard(1000);
}
void draw() {
background(0);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
c[i][j].display( (s1==i+j*w || s2==i+j*w), (s1 == i+j*w? #00FF00 : (s2 == i+j*w? #0000FF : #FFFFFF)) );
}
}
void mousePressed() {
int p = -1;
for (int i = 0; i < w && i >= 0; i++) for (int j = 0; j < h && i >= 0; j++) {
if (c[i][j].pressed(mouseX, mouseY)) {
p = i+j*w;
i = -2;
}
}
if (state == 0 && p>=0) {
s1 = p;
s2 = -1;
state++;
} else if (state == 1) {
if (p != s1) {
s2 = p;
state++;
}
} else if (state == 2) {
state = 0;
if (c[s1%w][floor(s1/w)].value == c[s2%w][floor(s2/w)].value) {
c[s1%w][floor(s1/w)].active = false;
c[s2%w][floor(s2/w)].active = false;
} else {
s1 = -1;
s2 = -1;
}
}
}
void shuffleBoard(int times) {
int temp;
for (int i = 0; i < times; i++) {
int x1=(int)random(w), y1=(int)random(h), x2=(int)random(w), y2=(int)random(h);
temp = c[x1][y1].value;
c[x1][y1].value = c[x2][y2].value;
c[x2][y2].value = temp;
}
}
class card {
int x, y, value;
boolean active;
card(int X, int Y, int Value) {
x = X;
y = Y;
value = Value;
active = true;
}
void display(boolean show, color cl) {
if (active) {
fill(cl);
square(x*scl+buffer, y*scl+buffer, scl-buffer*2);
if (show) {
fill(0);
text(value, (x+0.5)*scl, (y+0.5)*scl);
}
}
}
boolean pressed(int mx, int my) {
return(active && mx>x*scl+buffer && my>y*scl+buffer&&mx<(x+1)*scl-buffer&&my<(y+1)*scl-buffer);
}
}
VERSION 2
Summary
int w = 4, h = 4, scl = 150, buffer=5;
card c[][] = new card[w][h];
int s1 = -1, s2 = -1, state = 0;
void setup() {
size(600, 600);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
c[i][j] = new card(i, j, floor((i+j*w)/2));
}
colorMode(HSB);
textSize(scl*0.5);
textAlign(3,3);
shuffleBoard(1000);
}
void draw() {
background(0);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
c[i][j].display( (s1==i+j*w || s2==i+j*w));
}
}
void mousePressed() {
int p = -1;
for (int i = 0; i < w && i >= 0; i++) for (int j = 0; j < h && i >= 0; j++) {
if (c[i][j].pressed(mouseX, mouseY)) {
p = i+j*w;
i = -2;
}
}
if (state == 0 && p>=0) {
s1 = p;
s2 = -1;
state++;
} else if (state == 1) {
if (p != s1 && p != -1) {
s2 = p;
state++;
}
} else if (state == 2) {
state = 0;
if (c[s1%w][floor(s1/w)].value == c[s2%w][floor(s2/w)].value) {
c[s1%w][floor(s1/w)].active = false;
c[s2%w][floor(s2/w)].active = false;
} else {
s1 = -1;
s2 = -1;
}
}
}
void shuffleBoard(int times) {
int temp;
for (int i = 0; i < times; i++) {
int x1=(int)random(w), y1=(int)random(h), x2=(int)random(w), y2=(int)random(h);
temp = c[x1][y1].value;
c[x1][y1].value = c[x2][y2].value;
c[x2][y2].value = temp;
}
}
class card {
int x, y, value;
boolean active;
card(int X, int Y, int Value) {
x = X;
y = Y;
value = Value;
active = true;
}
void display(boolean show) {
if (active) {
fill((show? color(getColor(value,w*h*0.5),255,255) : #FFFFFF));
square(x*scl+buffer, y*scl+buffer, scl-buffer*2);
if (show) {
fill(0);
text(value, (x+0.5)*scl, (y+0.45)*scl);
}
}
}
boolean pressed(int mx, int my) {
return(active && mx>x*scl+buffer && my>y*scl+buffer&&mx<(x+1)*scl-buffer&&my<(y+1)*scl-buffer);
}
}
float getColor(int c, float max) {
return map(c,0,max,0,255);
}
void keyPressed() { //RESET
if (key == ' ') {
c = new card[w][h];
s1 = -1;
s2 = -1;
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
c[i][j] = new card(i, j, floor((i+j*w)/2));
}
shuffleBoard(1000);
}
}