-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
322 lines (292 loc) · 8.16 KB
/
code.js
File metadata and controls
322 lines (292 loc) · 8.16 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
var screenWidth = 320;
var screenHeight = 450;
var dividerY = 285;
var maxRowY = dividerY;
var numRows = 10;
var rowHeightLarge = 60;
var rowHeightSmall = 0;
var elementPrefix = ["hint", "slot"];
var rowInfo = {};
rowInfo.hint = [];
rowInfo.slot = [];
var currentRow = 0;
var pegEmpty = "assets/peg_empty.png";
var pegRed = "assets/peg_red.png";
var pegBlue = "assets/peg_blue.png";
var pegGreen = "assets/peg_green.png";
var pegYellow = "assets/peg_yellow.png";
var pegWhite = "assets/peg_white.png";
var pegBlack = "assets/peg_black.png";
var currentColor = pegEmpty;
var okButtonBackground = "#eab208";
var okButtonGreyBackground = "#e2e2e2";
var okButtonGreyedOut = true;
var answer = [];
setup();
resetBoard();
function setup() {
var rowIndex;
createCanvas("background");
line(0, dividerY, screenWidth, dividerY);
rememberRowLayout();
// Create all the other rows
for(rowIndex = 1; rowIndex < numRows; rowIndex++) {
createRow(rowIndex);
}
// Set the event listener for the OK button.
onEvent("ok", "click", onOkClick);
// Set the event listeners for all the color buttons.
onEvent("white", "click", function () { currentColor = pegWhite; });
onEvent("black", "click", function () { currentColor = pegBlack; });
onEvent("red", "click", function () { currentColor = pegRed; });
onEvent("blue", "click", function () { currentColor = pegBlue; });
onEvent("green", "click", function () { currentColor = pegGreen; });
onEvent("yellow", "click", function () { currentColor = pegYellow; });
onEvent("play_again_button", "click", function () { resetBoard(); });
// Set the event listener for all the slots.
for(rowIndex = 0; rowIndex < numRows; rowIndex++) {
for(var slotIndex = 0; slotIndex < 4; slotIndex++) {
onEvent(slotId(slotIndex, rowIndex),
"click",
createSlotClickCallback(slotIndex, rowIndex));
}
}
console.log("setup complete");
}
function onOkClick() {
if (okButtonGreyedOut) {
return;
}
if (scoreRow(currentRow)) {
winGame();
} else if (currentRow + 1 >= numRows) {
loseGame();
} else {
setRow(currentRow + 1);
greyOutOkButton(true);
}
}
function winGame() {
showElement("game_over_background");
showElement("play_again_button");
showElement("you_win_text_1");
showElement("you_win_text_2");
showElement("you_win_image");
}
function loseGame() {
showElement("game_over_background");
showElement("play_again_button");
showElement("you_lose_text");
showElement("you_lose_image");
}
function scoreRow(rowIndex) {
var nextHint = 0;
var i, j;
var copyAnswer = [];
var copyGuess = [];
for (i = 0; i < 4; i++) {
appendItem(copyAnswer, answer[i]);
}
for (i = 0; i < 4; i++) {
appendItem(copyGuess, getImageURL(slotId(i, rowIndex)));
}
for (i = 0; i < 4; i++) {
if (copyAnswer[i] == copyGuess[i]) {
setImageURL(hintId(nextHint, rowIndex), pegRed);
nextHint++;
copyAnswer[i] = pegEmpty;
copyGuess[i] = pegEmpty;
}
}
if (nextHint == 4) {
return true;
}
// Look at each of the answer slots
for (i = 0; i < 4; i++) {
if (copyAnswer[i] == pegEmpty) {
continue;
}
// Look at each of the guess slots
for (j = 0; j < 4; j++) {
if (i == j) {
continue;
}
if (copyGuess[j] == pegEmpty) {
continue;
}
if (copyAnswer[i] == copyGuess[j]) {
setImageURL(hintId(nextHint, rowIndex), pegWhite);
nextHint++;
copyAnswer[i] = pegEmpty;
copyGuess[j] = pegEmpty;
break;
}
}
}
return false;
}
function createSlotClickCallback(slotIndex, rowIndex) {
return function () {
slotClick(slotIndex, rowIndex);
};
}
function slotClick(clickedSlot, clickedRow) {
// If this row isn't the selected row, don't do anything.
if (clickedRow != currentRow) {
return;
}
// Set this slot to the currently selected color.
setImageURL(slotId(clickedSlot, clickedRow), currentColor);
currentColor = pegEmpty;
// Check to see if all the slots in this row are filled in.
var filledIn = true;
for (var slotIndex = 0; slotIndex < 4; slotIndex++) {
if (getImageURL(slotId(slotIndex, currentRow)) == pegEmpty) {
filledIn = false;
break;
}
}
greyOutOkButton(!filledIn);
}
function rememberRowLayout() {
var minY = screenHeight;
var i,j,prefix,id,bottom;
maxRowY = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < elementPrefix.length; j++) {
prefix = elementPrefix[j];
id = pegId(prefix, i, 0);
var info = {};
info.x = getXPosition(id);
info.y = getYPosition(id);
info.width = getProperty(id, "width");
info.height = getProperty(id, "height");
rowInfo[prefix][i] = info;
if (info.y < minY) {
minY = info.y;
}
bottom = info.y + info.height;
if (bottom > maxRowY) {
maxRowY = bottom;
}
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < elementPrefix.length; j++) {
prefix = elementPrefix[j];
id = pegId(prefix, i, 0);
rowInfo[prefix][i].y -= minY;
}
}
// Update the y extents to be correct
rowHeightLarge = maxRowY - minY;
rowHeightSmall = (maxRowY - rowHeightLarge) / (numRows - 1);
}
function resetBoard() {
// Make sure all the game-over stuff is hidden
hideElement("game_over_background");
hideElement("play_again_button");
hideElement("you_win_text_1");
hideElement("you_win_text_2");
hideElement("you_win_image");
hideElement("you_lose_text");
hideElement("you_lose_image");
for (var rowIndex = 0; rowIndex < numRows; rowIndex++) {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < elementPrefix.length; j++) {
setImageURL(pegId(elementPrefix[j], i, rowIndex), pegEmpty);
}
}
}
setRow(0);
greyOutOkButton(true);
pickAnswer();
}
function pickAnswer() {
answer = [];
var allPegs = [pegRed, pegBlue, pegGreen, pegYellow, pegWhite, pegBlack];
var peg, i;
for (i = 0; i < 4; i++) {
peg = randomNumber(0, allPegs.length - 1);
appendItem(answer, allPegs[peg]);
removeItem(allPegs, peg);
}
console.log("the answer is");
for (i = 0; i < 4; i++) {
console.log(answer[i]);
}
}
function greyOutOkButton(value) {
if (value) {
setProperty("ok", "background-color", okButtonGreyBackground);
} else {
setProperty("ok", "background-color", okButtonBackground);
}
okButtonGreyedOut = value;
}
function setRow(index) {
currentRow = index;
var y = maxRowY;
for (var rowIndex = 0; rowIndex < numRows; rowIndex++) {
if (rowIndex == currentRow) {
y -= rowHeightLarge;
setRowPositionAndSize(rowIndex, y, rowHeightLarge);
} else {
y -= rowHeightSmall;
setRowPositionAndSize(rowIndex, y, rowHeightSmall);
}
}
}
function setRowPositionAndSize(rowIndex, y, rowHeight) {
var scale = rowHeight / rowHeightLarge;
var x = screenWidth / 2 * (1 - scale);
for (var i = 0; i < 4; i++) {
for (var j = 0; j < elementPrefix.length; j++) {
var prefix = elementPrefix[j];
var info = rowInfo[prefix][i];
var id = pegId(prefix, i, rowIndex);
setPosition(
id,
x + info.x * scale,
y + info.y * scale,
info.width * scale,
info.height * scale);
}
}
}
function createRow(rowIndex) {
for(var i = 0; i < 4; i++) {
for (var j = 0; j < elementPrefix.length; j++) {
var prefix = elementPrefix[j];
var originalId = pegId(prefix, i, 0);
var id = pegId(prefix, i, rowIndex);
copyPeg(originalId, id);
}
}
}
function hintId(index, rowIndex) {
return pegId("hint", index, rowIndex);
}
function slotId(index, rowIndex) {
return pegId("slot", index, rowIndex);
}
function pegId(prefix, index, rowIndex) {
return prefix + index + "_row" + rowIndex;
}
function copyPeg(pegId, newId) {
var properties = [
"border-width",
"border-color",
"border-radius",
];
image(newId, getImageURL(pegId));
setPosition(
newId,
getXPosition(pegId), getYPosition(pegId),
getProperty(pegId, "width"), getProperty(pegId, "height"));
for (var i = 0; i < properties.length; i++) {
var property = properties[i];
var value = getProperty(pegId, property);
setProperty(newId, property, value);
}
}