forked from tmsteck/tmsteck.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_game.html
221 lines (203 loc) · 8.52 KB
/
card_game.html
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Card Game Challenge</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#game-container {
text-align: center;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#player-hand, #computer-hand, #play-area {
margin: 20px 0;
min-height: 100px;
}
.card {
display: inline-block;
width: 50px;
height: 70px;
background-color: white;
border: 1px solid black;
margin: 0 5px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
}
.card.played {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#result {
font-weight: bold;
margin-top: 10px;
}
#play-area {
display: flex;
justify-content: center;
align-items: center;
}
.card.played {
position: relative;
transform: none;
margin: 0 10px;
}
</style>
</head>
<body>
<div id="game-container">
<h1 id="message">Beat the game to access the invitation!</h1>
<div id="game-area" style="display: none;">
<div id="computer-hand"></div>
<div id="play-area"></div>
<div id="player-hand"></div>
<p id="score">Computer Score: 0</p>
<p id="result"></p>
</div>
<button id="deal-button">Deal Cards</button>
<button id="give-up-button">I give up</button>
</div>
<script>
const gameContainer = document.getElementById('game-container');
const message = document.getElementById('message');
const gameArea = document.getElementById('game-area');
const playerHand = document.getElementById('player-hand');
const computerHand = document.getElementById('computer-hand');
const playArea = document.getElementById('play-area');
const scoreDisplay = document.getElementById('score');
const resultDisplay = document.getElementById('result');
const dealButton = document.getElementById('deal-button');
const giveUpButton = document.getElementById('give-up-button');
let computerScore = 0;
let playerCards = [];
let computerCards = [];
const suits = ['♠', '♥', '♦', '♣'];
const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
function createDeck() {
let deck = [];
for (let suit of suits) {
for (let value of values) {
deck.push({ suit, value });
}
}
return deck;
}
function shuffleDeck(deck) {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
return deck;
}
function dealCards() {
let deck = shuffleDeck(createDeck());
playerCards = deck.slice(0, 5);
computerCards = deck.slice(5, 10);
displayCards();
gameArea.style.display = 'block';
dealButton.textContent = 'Play Again';
resultDisplay.textContent = '';
playArea.innerHTML = '';
}
function displayCards() {
playerHand.innerHTML = '';
computerHand.innerHTML = '';
playerCards.forEach(card => {
const cardElement = document.createElement('div');
cardElement.className = 'card';
cardElement.textContent = `${card.value}${card.suit}`;
cardElement.onclick = () => playCard(card);
playerHand.appendChild(cardElement);
});
computerCards.forEach(() => {
const cardElement = document.createElement('div');
cardElement.className = 'card';
cardElement.textContent = '?';
computerHand.appendChild(cardElement);
});
}
function playCard(playerCard) {
const computerCard = computerCards[Math.floor(Math.random() * computerCards.length)];
// Animate player's card
//const playerCardElement = document.querySelector(`.card:contains('${playerCard.value}${playerCard.suit}')`);
const cards = Array.from(document.querySelectorAll('.card'));
const playerCardElement = cards.filter(card => card.textContent.includes(`${playerCard.value}${playerCard.suit}`))[0];
playerCardElement.classList.add('played');
playArea.appendChild(playerCardElement);
// Animate computer's card
setTimeout(() => {
const computerCardElement = document.createElement('div');
computerCardElement.className = 'card played';
computerCardElement.textContent = `${computerCard.value}${computerCard.suit}`;
playArea.appendChild(computerCardElement);
// Compare cards and update score
const result = compareCards(playerCard, computerCard);
resultDisplay.textContent = result;
computerScore += 1;
scoreDisplay.textContent = `Computer Score: ${computerScore}`;
// Remove cards from hands
playerCards = playerCards.filter(c => c !== playerCard);
computerCards = computerCards.filter(c => c !== computerCard);
// Clear play area and update display after a delay
setTimeout(() => {
playArea.innerHTML = '';
displayCards();
if (playerCards.length === 0) {
endGame();
}
}, 1500);
}, 500);
}
function compareCards(playerCard, computerCard) {
const cardOrder = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
const playerValue = cardOrder.indexOf(playerCard.value);
const computerValue = cardOrder.indexOf(computerCard.value);
const playerSuit = suits.indexOf(playerCard.suit);
const computerSuit = suits.indexOf(computerCard.suit);
const suit_order = ['♠', '♥', '♦', '♣'];
//if player value is smaller, computer wins. Then, clubs beats diamonds beats hearts beats spades
//Get the index of the player and the computer suit:
const playerSuitIndex = suit_order.indexOf(playerCard.suit);
const computerSuitIndex = suit_order.indexOf(computerCard.suit);
if (playerValue < computerValue) {
return "High card wins. Computer wins 1 point";
} else if (playerSuitIndex < computerSuitIndex) {
// Convert from python: return "{} beats {}. Computer earns 1 point".format(computerSuit, playerSuit)
return `${computerCard.suit} beats ${playerCard.suit}. Computer wins 1 point`;
} else {
return "Compter wins. Earns 1 point";
}
}
function endGame() {
message.textContent = 'You lost!';
gameArea.style.display = 'none';
dealButton.textContent = 'Play Again';
computerScore = 0;
}
function giveUp() {
message.innerHTML = "Quitter! Anyways, we have a new house! We'd love for you to join us for a housewarming party, which will probably look suspiciously like a normal game night, at 5pm on August 3rd. Hope you can join! Feel free to invite friends! As always, an RSVP is helpful but not required. \n The new address is 6723 44th Ave, University Park, MD 20782. Street parking is available.";
gameArea.style.display = 'none';
dealButton.style.display = 'none';
giveUpButton.style.display = 'none';
}
dealButton.onclick = dealCards;
giveUpButton.onclick = giveUp;
</script>
</body>
</html>