-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.js
More file actions
122 lines (93 loc) · 3.6 KB
/
deck.js
File metadata and controls
122 lines (93 loc) · 3.6 KB
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
121
122
//Initialised required card arrays
const cardSuits = ['♤', '♥', '♦', '♧'];
const cardNumbers = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
const suitValues = { 'J': 11, 'Q': 12, 'K': 13, 'A': 14 };
// Event listeners
document.addEventListener("DOMContentLoaded", function () {
console.log("DOMContentLoaded");
let startBtn = document.getElementById("start");
let drawBtn = document.getElementById("draw");
// Start Game button event listening
startBtn.addEventListener("click", () => {
document.getElementById("uCard").innerText = `Cards shuffled Please draw`;
document.getElementById("cCard").innerText = `Cards shuffled Please draw`;
document.getElementById("uPoint").innerText = "0";
document.getElementById("cPoint").innerText = "0";
return shuffledCards = createDeck();
});
// Draw Game button event listening
drawBtn.addEventListener("click", function () {
// The below makes the draw event happen and gives points to
// players based on who got the highest value card
if (shuffledCards.length !== 0) {
const user = shuffledCards.pop();
console.log(`User Value: ${user.value}`);
document.getElementById("uCard").innerText = user.display;
const computer = shuffledCards.pop();
console.log(`Computer Value: ${computer.value}`);
document.getElementById("cCard").innerText = computer.display;
if (user.value > computer.value) {
let val1 = document.getElementById("uPoint").innerText;
val1 = parseInt(val1);
val1 = val1 + 1;
document.getElementById("uPoint").innerText = val1;
}
else {
let val2 = document.getElementById("cPoint").innerText;
val2 = parseInt(val2);
val2 = val2 + 1;
document.getElementById("cPoint").innerText = val2;
}
}
// If there are no more cards to draw, the below displays who won
else {
let finalUPoint = document.getElementById("uPoint").innerText;
let finalCPoint = document.getElementById("cPoint").innerText;
finalUPoint = parseInt(finalUPoint);
finalCPoint = parseInt(finalCPoint);
if (finalUPoint > finalCPoint) {
document.getElementById("uCard").innerText = `You Won!!!`;
}
else if (finalUPoint === finalCPoint) {
document.getElementById("uCard").innerText = `Game Draw!!!`;
document.getElementById("cCard").innerText = `Game Draw!!!`;
}
else {
document.getElementById("cCard").innerText = `Computer Won!!!`;
}
}
});
});
//creates deck of cards and shuffles them
function createDeck() {
let cards = new Deck(cardNumbers, cardSuits, suitValues);
console.log(cards.cards());
cards.cards();
console.log(cards.shuffle());
let shuffledCards = cards.shuffle();
return shuffledCards;
}
// Deck class defintion
class Deck {
constructor(numbers, suits, suitVal) {
this.numbers = numbers;
this.suits = suits;
this.suitVal = suitVal;
}
cards() {
const result = [];
for (let i = 0; i < this.numbers.length; i++) {
for (let j = 0; j < this.suits.length; j++) {
let value = isNaN(this.numbers[i]) ? this.suitVal[this.numbers[i]] : parseInt(this.numbers[i], 10);
result.push({ display: this.numbers[i] + this.suits[j], value });
}
};
this.deckOfCards = result;
return result;
}
shuffle() {
let shuffled = this.deckOfCards.sort(function () { return 0.5 - Math.random() });
return shuffled;
}
}
module.exports = Deck;