-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryGame.js
195 lines (161 loc) · 4.98 KB
/
MemoryGame.js
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
var MemoryGame = {
settings: {
rows: 2,
columns: 3,
images: 3, // Number of images
},
// Properties that indicate state
cards: [], // Array of MemoryGame.Card objects
attempts: 0, // How many pairs of cards were flipped before completing game
mistakes: 0, // How many pairs of cards were flipped before completing game
isGameOver: false,
initialize : function(rows, columns, images) {
var validOptions = true;
// Validate arguments
if (!(typeof columns === 'number' && (columns % 1) === 0 && columns > 1) ||
!(typeof rows === 'number' && (rows % 1) === 0) && rows > 1) {
validOptions = false;
throw {
name: "invalidInteger",
message: "Both rows and columns need to be integers greater than 1."
};
}
if ((columns * rows) % 2 !== 0) {
validOptions = false;
throw {
name: "oddNumber",
message: "Either rows or columns needs to be an even number."
};
}
if (validOptions) {
this.settings.rows = rows;
this.settings.columns = columns;
this.settings.images = images;
this.attempts = 0;
this.mistakes = 0;
this.isGameOver = false;
this.createCards().shuffleCards();
}
return this.cards;
},
createCards: function() {
var cards = [];
var values = [];
var count = 0;
var maxValue = (this.settings.columns * this.settings.rows) / 2;
while (count < maxValue) {
// Next random card value
var value = this.getRandomCardValue(values);
// Card A
cards[2 * count] = new this.Card(value);
// Card B (matching card)
cards[2 * count + 1] = new this.Card(value, true);
count++;
}
this.cards = cards;
return this;
},
getRandomCardValue: function(values) {
var valid = false;
var randomValue = 0;
while (!valid) {
randomValue = Math.floor(Math.random() * this.settings.images) + 1;
var found = false;
for (var index = 0; index < values.length; index++) {
if (randomValue === values[index]) {
found = true;
break;
}
}
if (!found) {
valid = true;
values.push(randomValue); // Purposely modify the array parameter.
}
}
return randomValue;
},
shuffleCards: function() {
var cards = this.cards;
var shuffledCards = [];
var randomIndex = 0;
// Shuffle cards
while (shuffledCards.length < cards.length) {
// Random value between 0 and cards.length
randomIndex = Math.floor(Math.random() * cards.length);
// If element isn't false, add element to shuffled deck
if(cards[randomIndex]) {
// Add new element to shuffle deck
shuffledCards.push(cards[randomIndex]);
// Set element to false to avoid being reused
cards[randomIndex] = false;
}
}
this.cards = shuffledCards;
return this;
},
play: (function() {
var cardSelection = [];
var revealedCards = 0;
var revealedValues = [];
return function(index) {
var status = {};
var value = this.cards[index].value;
if (!this.cards[index].isRevealed) {
this.cards[index].reveal();
cardSelection.push(index);
if (cardSelection.length == 2) {
this.attempts++;
if (this.cards[cardSelection[0]].value !=
this.cards[cardSelection[1]].value) {
// No match
this.cards[cardSelection[0]].conceal();
this.cards[cardSelection[1]].conceal();
var isMistake = false;
if (revealedValues.indexOf(this.cards[cardSelection[0]].value) === -1) {
revealedValues.push(this.cards[cardSelection[0]].value);
}
else {
isMistake = true;
}
if (revealedValues.indexOf(this.cards[cardSelection[1]].value) === -1) {
revealedValues.push(this.cards[cardSelection[1]].value);
}
if (isMistake) {
this.mistakes++;
}
revealedValues.push(this.cards[cardSelection[0]].value);
status.code = 3,
status.message = 'No Match. Conceal cards.';
status.args = cardSelection;
}
else {
revealedCards += 2;
if (revealedCards == this.cards.length) {
// Game over
this.isGameOver = true;
revealedCards = 0;
revealedValues = [];
status.code = 4,
status.message = 'GAME OVER! Attempts: ' + this.attempts +
', Mistakes: ' + this.mistakes;
}
else {
status.code = 2,
status.message = 'Match.';
}
}
cardSelection = [];
}
else {
status.code = 1,
status.message = 'Flip first card.';
}
}
else {
status.code = 0,
status.message = 'Card is already facing up.';
}
return status;
};
})()
};