|
56 | 56 | var gamePlay = false;
|
57 | 57 | var tileImages = [],
|
58 | 58 | tileArray = [],
|
59 |
| - tileFlippedOver = []; |
| 59 | + tfo = []; |
60 | 60 | var cardFlipped = -1;
|
61 | 61 | var timer;
|
62 | 62 | var sb = document.getElementById('start');
|
|
86 | 86 | html += '<div class="gameTile">';
|
87 | 87 | html += '<img id="cardz' + x +
|
88 | 88 | '" 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>'; |
91 | 90 | });
|
92 | 91 | gameBoard.innerHTML = html;
|
93 | 92 | }
|
94 | 93 |
|
95 | 94 | function pickCard(i, t) {
|
96 | 95 | 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 |
103 | 111 | } else {
|
104 |
| - message.innerHTML = "No Match"; |
105 |
| - timer = setInterval(hideCard, 1000); |
| 112 | + cardFlipped = i; |
| 113 | + cardFlip(i, t); |
106 | 114 | }
|
107 |
| - //check to see if its a match |
108 | 115 | } else {
|
109 |
| - cardFlipped = i; |
110 |
| - cardFlip(i, t); |
| 116 | + message.innerHTML = "Locked Out"; |
111 | 117 | }
|
112 | 118 | }
|
113 | 119 |
|
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 | + } |
115 | 134 |
|
116 | 135 | function checkSrc(a) {
|
117 | 136 | return document.getElementById(a).src;
|
118 | 137 | }
|
119 | 138 |
|
120 | 139 | function cardFlip(i, t) {
|
121 | 140 | t.src = "http://via.placeholder.com/250/ffffff/000000?text=" + tileArray[i];
|
122 |
| - tileFlippedOver.push(t.id); |
| 141 | + tfo.push(t.id); |
123 | 142 | }
|
124 | 143 |
|
125 | 144 | function buildArray() {
|
|
0 commit comments