I want to create a game with JavaScript that randomly selects 52 random cards (no jokers) and displays the cards one at a time while comparing the cards depending on the card being lower than the last card that was selected.
So, it's a high and low card game.
Here is my code:
CSS
body {
border: 2px solid #f45f;
}
div {
background:red;
}
#card {
font-size:40px;
background:red;
}
JavaScript
var cards = [
["♠","♣", "♦", "♥","♠","♣", "♦", "♥","♠","♣", "♦", "♥","♠","♣", "♦", "♥",
"♠","♣", "♦", "♥","♠","♣", "♦", "♥","♠","♣", "♦", "♥","♠","♣", "♦", "♥",
"♠","♣", "♦", "♥","♠","♣", "♦", "♥","♠","♣", "♦", "♥","♠","♣", "♦", "♥",
"♠","♣", "♦", "♥"],
[1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13]
];
var x=0;
var newCard=[],[];
function Start(){
document.getElementById("welcome").innerHTML ="<br>Welcome to the high and low card game ! :)";
document.getElementById("test").style.display = 'block';
document.getElementById("test2").style.display = 'block';
document.getElementById("test1").style.display = 'none';
}
function highOrLow(answer){
var answer=answer;
var rand=Math.floor(Math.random() * 50) + 1;
document.getElementById("res1").innerHTML =rand;
document.getElementById("res").innerHTML =x;
document.getElementById("question").innerHTML = "Is The next card higher or lower ?";
document.getElementById("name").innerHTML = answer;
document.getElementById("card").innerHTML = cards[1][rand]+cards[0][rand];
if (x < 51) {
return x++;
}
else if (x = 51) {
document.getElementById("welcome").innerHTML ="";
document.getElementById("res1").innerHTML ="";
document.getElementById("res").innerHTML ="";
document.getElementById("card").innerHTML ="";
document.getElementById("question").innerHTML = "";
document.getElementById("test").style.display = 'none';
document.getElementById("test2").style.display = 'none';
document.getElementById("test").style.display = 'none';
document.getElementById("test2").style.display = 'none';
document.getElementById("test1").style.display = 'block';
document.getElementById("name").innerHTML = "the game is over";
return x = 0;
}
}
function clbutton() {
document.getElementById("test").style.display = 'none';
document.getElementById("test2").style.display = 'none';
}
HTML
<body onload="clbutton()">
<div id="top">
<center><h1>High Low Card Game</h1></center>
</div>
<br>
<p>the rules are simple chose if the next card is high or is the card low and try to beat your own score !
♠
♣
♦
♥
</p>
<br>
<button id="test1" type="button"
onclick="setTimeout( Start(), 1000000 )">
Start Game</button>
<span id="welcome"></span>
<p id="name"></p>
<p id="res"></p>
<br>
<p id="res1"></p>
<br>
<span id="card"></span>
<br>
<span id="question"></span>
<button id="test" type="button"
onclick="highOrLow('high')" >
Higher</button>
<br>
<button id="test2" type="button"
onclick="highOrLow('low')">
Lower</button>
<br>
<span id="high"></span>
</body>