Skip to content

Commit 5b2be53

Browse files
committed
Helper Function Lockout
1 parent 3a814f5 commit 5b2be53

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

Intermediate_JavaScript/GamePlay_setup_Values.html

+35-16
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
var gamePlay = false;
5757
var tileImages = [],
5858
tileArray = [],
59-
tileFlippedOver = [];
59+
tfo = [];
6060
var cardFlipped = -1;
6161
var timer;
6262
var sb = document.getElementById('start');
@@ -86,40 +86,59 @@
8686
html += '<div class="gameTile">';
8787
html += '<img id="cardz' + x +
8888
'" src="http://via.placeholder.com/250/000000/ffffff?text=click" onclick="pickCard(' + (x -
89-
1) +
90-
',this)" class="flipImage"></div>';
89+
1) + ',this)" class="flipImage"></div>';
9190
});
9291
gameBoard.innerHTML = html;
9392
}
9493

9594
function pickCard(i, t) {
9695
console.log(event.target);
97-
if (cardFlipped >= 0) {
98-
cardFlip(i, t);
99-
if (checkSrc(tileFlippedOver[tileFlippedOver.length - 1]) == checkSrc(tileFlippedOver[tileFlippedOver
100-
.length -
101-
2])) {
102-
message.innerHTML = "Match Found";
96+
// not in array of flipped over
97+
// not locked out
98+
if (!playLockout && !isinArray(t.id, tfo)) {
99+
if (cardFlipped >= 0) {
100+
cardFlip(i, t);
101+
playLockout = true;
102+
if (checkSrc(tfo[tfo.length - 1]) == checkSrc(tfo[tfo.length - 2])) {
103+
message.innerHTML = "Match Found";
104+
cardFlipped = -1;
105+
playLockout = false;
106+
} else {
107+
message.innerHTML = "No Match";
108+
timer = setInterval(hideCard, 1000);
109+
}
110+
//check to see if its a match
103111
} else {
104-
message.innerHTML = "No Match";
105-
timer = setInterval(hideCard, 1000);
112+
cardFlipped = i;
113+
cardFlip(i, t);
106114
}
107-
//check to see if its a match
108115
} else {
109-
cardFlipped = i;
110-
cardFlip(i, t);
116+
message.innerHTML = "Locked Out";
111117
}
112118
}
113119

114-
function hideCard() {}
120+
function isinArray(v, array) {
121+
return array.indexOf(v) > -1;
122+
}
123+
124+
function hideCard() {
125+
for (var x = 0; x < 2; x++) {
126+
var vid = tfo.pop();
127+
document.getElementById(vid).src = 'http://via.placeholder.com/250/000000/ffffff?text=click';
128+
}
129+
clearInterval(timer);
130+
cardFlipped = -1;
131+
playLockout = false;
132+
message.innerHTML = "Select Again";
133+
}
115134

116135
function checkSrc(a) {
117136
return document.getElementById(a).src;
118137
}
119138

120139
function cardFlip(i, t) {
121140
t.src = "http://via.placeholder.com/250/ffffff/000000?text=" + tileArray[i];
122-
tileFlippedOver.push(t.id);
141+
tfo.push(t.id);
123142
}
124143

125144
function buildArray() {

0 commit comments

Comments
 (0)