-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (107 loc) · 3.38 KB
/
script.js
File metadata and controls
142 lines (107 loc) · 3.38 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
123
124
125
126
127
128
129
130
131
132
// Deal 26 Cards to each Player from a Deck of 52 cards.
// Iterate through the turns where each Player plays a Card.
// The Player who played the higher card is awarded a point.
// -Ties result in zero points for both Players.
// After all cards have been played, display the score and declare the winner.
// Four suits to represent the appearance (user interface - ui) for your cards
let cardSuits = ["Spades 🗡️", "Hearts ❤️", "Diamonds 💎", "Clubs 🍀"];
//console.log("Card Suits Example:", cardSuits);
class Deck {
constructor() {
this.deck = [];
this.ranks = [
"Ace",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"king",
];
this.Suits = ["Spades 🗡️", "Hearts ❤️", "Diamonds 💎", "Clubs 🍀"];
}
// A method that creates a deck... iterates over ranks & suits
// push a new card... (is an object) into our constructors this.deck
createDeck() {
for (let i = 0; i < this.Suits.length; i++) {
for(let j =0; j < this.ranks.length; j++) {
let card = {
name: `${this.ranks[j]} of ${this.Suits[i]}`,
value: j + 1
}
this.deck.push(card)
//console.log(card)
}
}
}
shuffleDeck() {
for (let i = this.deck.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[this.deck[i], this.deck[j]] = [this.deck[j], this.deck[i]];
}
}
}
class Game {
constructor() {
this.player1= {
name: 'Player 1',
score: 0,
hand: []
}
this.player2 = {
name: 'Player 2',
score: 0,
hand: []
}
}
playGame() {
const deck = new Deck
deck.createDeck()
deck.shuffleDeck()
while (deck.deck.length !==0) {
this.player1.hand.push(deck.deck.shift())
this.player2.hand.push(deck.deck.shift())
}
for (let i = 0; i < this.player1.hand.length; i++) {
if (this.player1.hand[i].value > this.player2.hand[i].value) {
this.player1.score ++
console.log(`
P1 Card: ${this.player1.hand[i].name}
P2 Card: ${this.player2.hand[i].name}
Player 1 wins a point!
Current Score: p1: ${this.player1.score}, p2: ${this.player2.score}
`)
} else if (this.player2.hand[i].value > this.player1.hand[i].value) {
this.player2.score ++
console.log(`
P1 Card: ${this.player1.hand[i].name}
P2 Card: ${this.player2.hand[i].name}
Player 2 wins a point!
Current Score: p1: ${this.player1.score}, p2: ${this.player2.score}
`)
} else {
console.log(`
P1 Card: ${this.player1.hand[i].name}
P2 Card: ${this.player2.hand[i].name}
Tie: No Points awarded
Current Score: p1: ${this.player1.score}, p2: ${this.player2.score}
`)
}
}
if (this.player1.score > this.player2.score) {
console.log('Player 1 wins')
} else if (this.player2.score > this.player1.score) {
console.log('Player 2 wins')
} else {
console.log('Tie')
}
}
}
const game = new Game
game.playGame()