-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatchinggame.pde
More file actions
144 lines (126 loc) · 3.63 KB
/
matchinggame.pde
File metadata and controls
144 lines (126 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
int x = 20; // x position of reset button
int y = 55; // y position of reset button
int w = 50; // cwidth of reset button
int h = 20; // height of reset button
int cx = 20; // position x of tiles
int cy = 85; // position y of tiles
int cw = 800; // cwidth of all tiles
int ch = 100; // height of tiles
int cwidth = 50; // width of single tile
int[] deck = {};
boolean[] exposed = {}; // currently exposed cards
int N = 16; // length of deck
//int numExposed = 0; // determine win
int turns; // number of turns
int state; // track num cards revealed
int card1;
int card2;
String message;
void setup() {
size(840, 200);
resetButtonColor = color(221, 160, 221);
myFont = createFont("Sans-Serif", 16);
textFont(myFont);
initializeDeck();
newGame();
}
void draw() {
background(255);
// reset button
fill(resetButtonColor);
stroke(255);
rect(x, y, w, h);
fill(color(0));
textAlign(LEFT, CENTER);
text(message, x, y / 2);
textAlign(CENTER, CENTER);
text("Reset", x + (w / 2), y + (h / 2));
textAlign(RIGHT, CENTER);
text("Turns = " + turns, width - 25, y + (h / 2));
// cards
fill(cardsColor);
rect(cx, cy, cw, ch);
// display exposed cards only
fill(color(255));
textAlign(CENTER, CENTER);
for(int i = 0; i < N; i++){
if(exposed[i]==true){
text(deck[i], (cx + (cwidth*i) + (cwidth/2)), cy + (ch / 2));
}
line((cx + (cwidth*i) + (cwidth)), cy, (cx + (cwidth*i) + (cwidth)), cy + ch);
}
}
void mousePressed() {
// reset clicked
if(mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h) {
newGame();
}
// cards clicked
if(mouseX >= cx && mouseX <= cx+cw && mouseY >= cy && mouseY <= cy+ch) {
playGame();
}
}
void newGame() {
state = 0;
turns = 0;
//numExposed = 0;
shuffle();
// reset exposed cards
for (int e = 0; e < N; e++){
exposed[e] = false;
}
message = "Welcome! Test your memory by clicking on two cards. Try to match all cards in the lowest number of turns."
}
void initializeDeck() {
// create deck with matchable cards
cardsColor = color(147, 112, 219);
for (int j = 0; j < 2; j++){
for (int i = 0; i < (N/2); i++){
append(deck, i);
}
}
// for use in iteration of deck
N = deck.length;
}
void shuffle(){
// randomize deck
for (int i = 0; i < N; i++){
int r = i + (int) (Math.random() * (N-i));
String t = deck[r];
deck[r] = deck[i];
deck[i] = t;
}
}
void playGame(){
int clickIndex;
// find card selected as index in deck
clickIndex = floor((mouseX - cx) / cwidth);
if(state == 0){
state = 1;
exposed[clickIndex] = true;
card1 = clickIndex;
} else if(state == 1){
if(exposed[clickIndex] != true){
state = 2;
exposed[clickIndex] = true;
card2 = clickIndex;
turns++;
}
} else if(state==2){
if(exposed[clickIndex] != true){
if(deck[card1]!=deck[card2]){
exposed[card1] = false;
exposed[card2] = false;
}
//else if(deck[card1]==deck[card2]){
// numExposed+= 2;
// if(numExposed==16){
// message = "Congratulations, you win! Click 'Reset' to play again.";
// }
//}
state = 1;
exposed[clickIndex] = true;
card1 = clickIndex;
}
}
}