JSFiddle - React, Tailwind, and code Playground
HTML
<table align="center" border="0">
<tr>
<td width="430" rowspan="3">
<div class="container">
<div id="a1" class="card"></div>
<div id="a2" class="card"></div>
<div id="a3" class="card"></div>
<div id="a4" class="card"></div>
<div id="b1" class="card"></div>
<div id="b2" class="card"></div>
<div id="b3" class="card"></div>
<div id="b4" class="card"></div>
<div id="c1" class="card"></div>
<div id="c2" class="card"></div>
<div id="c3" class="card"></div>
<div id="c4" class="card"></div>
<div id="d1" class="card"></div>
<div id="d2" class="card"></div>
<div id="d3" class="card"></div>
<div id="d4" class="card"></div>
</div>
</td>
<td width="161" height="95" align="center" valign="middle"></td>
</tr>
<tr>
<td height="148" valign="top">
<div class=scoreboard></div>
</td>
</tr>
<tr>
<td height="148" valign="bottom"><input id="clicky" type="button" value="Restart"/></td>
</tr>
<tr>
<td height="59"> </td>
<td align="center" valign="middle">
</td>
</tr>
</table>
CSS
body{
margin:0;
padding:0;
}
#clicky {
width: 100px;
height: 40px;
background-color: #F4F4F4;
color: #666;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
font-weight: bold;
margin-bottom: 20px;
}
.clicky {
width: 100px;
height: 40px;
background-color: #F4F4F4;
color: #666;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
font-weight: bold;
margin-bottom: 20px;
}
.container{
width:412px;
height:412px;
}
.scoreboard{
background-color: grey;
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
color: #FFF;
font-family: Arial;
font-weight: bold;
height:80px;
width:160px;
padding-left: 12px;
padding-top: 17px;
}
.card{
width:100px;
height:100px;
float:left;
background-color:#ccc;
margin-bottom:3px;
margin-right:3px;
position: relative;
}
.card:after {
content:"";
display:block;
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
background: lightgrey;
}
.card:hover:after {
width:94px;
height:94px;
border:3px solid black;
}
.show.card:after{
display:none;
border: 0;
}
.show:hover{
width:94px;
height:94px;
border:3px solid black;
}
JavaScript
var highscore = 0;
var rounds = 0;
var score = 0;
var pairs = 0;
function divs(){
var colors = [
"orange","orange","pink","pink","red","red","purple","purple",
"blue","blue","green","green","brown","brown","yellow","yellow"];
var divs = document.getElementsByClassName("card");
while (divs.length > 0) {
var i = Math.floor(Math.random() * colors.length);
divs[0].style.backgroundColor = colors[i];
colors.splice(i, 1);
divs = [].slice.call(divs, 1);
}
}
function restart(){
if(score>highscore){
highscore=score;
}
score = 0;
rounds++
$("div").removeClass("show");
$('.scoreboard').html("Top score: " + highscore
+ "<br>Current score: " + score
+ "<br>Games played: " + rounds);
divs();
}
divs();
$('.scoreboard').html("Top score: " + highscore
+ "<br>Current score: " + score
+ "<br>Games played: " + rounds);
var pick1 = false;
var pick2 = false;
var id1;
var id2;
$('.card').click(function(){
if(pick1) {
pick2 = $(this).css('background-color');
id2 = this.id;
$(this).addClass("show")
if(pick1 == pick2 && id1!=id2){
alert('Correct');
score += 100;
pairs++
if (pairs == 8){
alert("You Win!");
restart();
}
}
else {
setTimeout(function (){
console.log('Removed.');
$('#' + id1 + ', #' + id2).removeClass("show")
},3000);
score -= 10;
}
$('.scoreboard').html("Top score: " + highscore
+ "<br>Current score: " + score
+ "<br>Games played: " + rounds
+ "<br>Pairs matched: " + pairs);
pick1 = false;
pick2 =...