-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.js
120 lines (95 loc) · 3.45 KB
/
Script.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
let deck = [];
let player1 = [];
let player2 = [];
let box1 = document.querySelector(".box1");
let box2 = document.querySelector(".box2");
function createDeck() {
const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "0", "J", "Q", "K", "A"];
const suits = ["H", "C", "D", "S"]
for (let i=0; i<4; i++) {
for (let x=0; x<13; x++) {
deck.push({rank: ranks[x],
suits: suits[i],
value: x+1,
img: `https://deckofcardsapi.com/static/img/${ranks[x]}${suits[i]}.png`
});
}
}
return deck;
}
function shuffleDeck(deck) {
for(let c=0; c<deck.length; c++) {
const tempCard = deck[c];
const randomIndex = Math.floor(Math.random() * 52);
let x = deck[c];
deck[c] = deck[randomIndex];
deck[randomIndex] = x
deck[randomIndex] = tempCard;
}
}
const newDeck = createDeck();
shuffleDeck(newDeck);
function splitDecks(){
const half = Math.ceil(newDeck.length / 2);
player1 = (newDeck.slice(0, half))
player2 = (newDeck.slice(-half))
}
splitDecks();
console.log(player1);
console.log(player2);
function playTurn() {
scoresCheck();
box1.innerHTML = `<img src="${player1[0].img}" class="cardsImg">`
box2.innerHTML = `<img src="${player2[0].img}" class="cardsImg">`
if(player1[0].value>player2[0].value) {
document.querySelector('.points1').innerHTML = parseInt(document.querySelector('.points1').innerHTML)+1;
} else {
document.querySelector('.points2').innerHTML = parseInt(document.querySelector('.points2').innerHTML)+1;
}
if(player1[0].value)
player1.shift();
player2.shift();
}
const shuffleButton = document.querySelector("#shuffle-button")
shuffleButton.addEventListener("click", playTurn);
function startOver() {
location.reload();
}
const scoreChange = document.querySelector("#whoWin")
const resetButton = document.querySelector("#reset-button")
resetButton.addEventListener("click", startOver)
function scoresCheck(whoWin) {
if(player1.length<1 || player2.length<1) {
gameOver();
playerFinalScore();
}
if (player1[0].value>player2[0].value) {
scoreChange.innerText = "Player1 Win!";
} else if (player1[0].value<player2[0].value) {
scoreChange.innerText = "Player2 Win!";
} else {
scoreChange.innerText = "It's Draw!";
}
}
function gameOver() {
const myTimeout = setTimeout(function() {
scoreChange.innerText = "Game Over!";
}, 1000);
}
const gameOverMsg = document.querySelector(".full-screen")
function playerFinalScore() {
const playerWinMsg = setTimeout(function() {
if (document.querySelector('.points1').innerHTML>document.querySelector('.points2').innerHTML) {
scoreChange.innerText = "Player1 has more score!";
} else if (document.querySelector('.points1').innerHTML<document.querySelector('.points2').innerHTML) {
scoreChange.innerText = "Player2 has more score!";
} else {
scoreChange.innerText = "It's Draw!";
}
}, 5000);
}
//check to see who won! if else statements
// if player1 has the most point end of the game, message "player1 wins"!
//settimeout starts then it scoreboard message changes to an another message.
//if player2 wins has the most, message player2 wins!
// disable the shuffle button after the game