forked from penkar/Speed-Card-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed-logic.js
305 lines (274 loc) · 9.21 KB
/
speed-logic.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
var mainDeck, upperCounter, lowerCounter;
var lowerDeck, upperDeck, drawLeft, drawRight, playLeft, playRight, lowerHand, upperHand, stats;
var start, end, elapsed;
var name;
var drawInd = false;
var gameOver = false;
//the following are referred to herein as the 'play variables' since they are the ones we will be using to play the game.
//IIFE that makes sure there is a fresh deck when the page loads. This also sets all other play variables to null.
//Card values being at Ace being 1, and continue to to D on a base-14 scale. A = 10, B = Jack, C = Queen, D = King.
var resetDeck = function () {
mainDeck = [
'1 C', '2 C', '3 C', '4 C', '5 C', '6 C', '7 C', '8 C', '9 C', 'A C', 'B C', 'C C', 'D C',
'1 D', '2 D', '3 D', '4 D', '5 D', '6 D', '7 D', '8 D', '9 D', 'A D', 'B D', 'C D', 'D D',
'1 H', '2 H', '3 H', '4 H', '5 H', '6 H', '7 H', '8 H', '9 H', 'A H', 'B H', 'C H', 'D H',
'1 S', '2 S', '3 S', '4 S', '5 S', '6 S', '7 S', '8 S', '9 S', 'A S', 'B S', 'C S', 'D S'
];
upperCounter = null;
lowerCounter = null;
lowerDeck = null;
uppperDeck = null;
lowerHand = null;
upperHand = null;
drawLeft = null;
drawRight = null;
playLeft = null;
playRight = null;
};
resetDeck();
var $document = $(document)
$(document).on('resetGame', function() {
resetDeck();
updateCounters();
shuffleDeck();
$('.lowerDeck').text(lowerCounter);
$('.upperDeck').text(upperCounter);
console.log('Deck reset, cards shuffled. Ready to deal.');
})
$(document).on('newGame', function() {
start = Date.now();
dealCards();
updateCounters();
$('.lowerDeck').text(lowerCounter);
$('.upperDeck').text(upperCounter);
console.log('Go!');
})
$(document).on('playAttempt', function (e, playObject) {
attemptPlay(window[playObject.hand], playObject.cardIndex, window[playObject.playCard]);
});
$(document).on('lowerDraw', function () {
drawCard(lowerHand);
showCard();
});
$(document).on('noMoves', function () {
drawInd = true;
drawLeftAndRight();
showCard();
drawInd = false;
})
$(document).on('playMade', function() {
showCard();
updateCounters();
$('.lowerDeck').text(lowerCounter);
$('.upperDeck').text(upperCounter);
console.log('play made');
setWin();
})
//Randomly arranges the values in mainDeck to simulate shuffling. Even if this method is called during play, the mainDeck will be empty and it should not affect any other deck.
var shuffleDeck = function() {
for (var j, x, i = mainDeck.length; i; j = Math.floor(Math.random() * i), x = mainDeck[--i], mainDeck[i] = mainDeck[j], mainDeck[j] = x);
return mainDeck;
};
// function shuffleDeck (array, random) {
// var i = mainDeck.length, j, swap;
// while (--i) {
// j = (random ? random() : Math.random()) * (i + 1) | 0;
// swap = mainDeck[i];
// mainDeck[i] = mainDeck[j];
// mainDeck[j] = swap;
// }
// return mainDeck;
// }
//First, it calls the shuffleDeck function to make sure that the cards are not dealt out in sequential order.
//It then deals out cards by removing them from the mainDeck and placing them into the play variables.
var dealCards = function() {
shuffleDeck();
lowerDeck = mainDeck.splice(0, 15);
lowerHand = mainDeck.splice(0, 5);
upperDeck = mainDeck.splice(0, 15);
upperHand = mainDeck.splice(0, 5);
drawLeft = mainDeck.splice(0, 5);
playLeft = mainDeck.splice(0, 1);
playRight = mainDeck.splice(0, 1);
drawRight = mainDeck.splice(0, 5);
upperCounter = upperDeck.length + upperHand.length;
lowerCounter = lowerDeck.length + lowerHand.length;
showCard()
};
//Takes 3 arguments: the variable name of the hand being playing from (upperHand/lowerHand),
//the index of the card being attempted in that hand, and the card in play being utilized (playLeft/playRight).
//The method parses an integer from the string representing the card, using a base 14 radix. The if clause in the
//conditional makes sure Aces are able to be played on either Kings or Twos; the else if clause makes sure Kings can
//be played on Aces or Queens; and the else clause establishes that all other cards can be played on values either one
//value higher or one value lower than itself. The makePlay method is called when the move is legal, and that method
//takes the same arguments given to attemptMove.
var attemptPlay = function(hand, cardIndex, playCard) {
attemptCard = parseInt(hand[cardIndex], 14);
compareCard = parseInt(playCard[0], 14);
if (attemptCard == 1) {
switch (compareCard) {
case 13:
makePlay(hand, cardIndex, playCard);
break;
case 2:
makePlay(hand, cardIndex, playCard);
break;
default:
console.log("Invalid Move");
break;
}
}
else if (attemptCard == 13) {
switch (compareCard) {
case 1:
makePlay(hand, cardIndex, playCard);
break;
case 12:
makePlay(hand, cardIndex, playCard);
break;
default:
console.log("Invalid Move");
break;
}
}
else {
switch (compareCard) {
case (attemptCard - 1):
makePlay(hand, cardIndex, playCard);
break;
case (attemptCard + 1):
makePlay(hand, cardIndex, playCard);
break;
default:
console.log("Invalid Move");
break;
}
}
};
//This function will always be called from the attemptMove function, and is given those same arguments. This function
//removes the value from the playCard (center card being played on) and replaces it with the value of the player's card.
//The card is then removed from the player's hand.
var makePlay = function(hand, cardIndex, playCard) {
console.log(hand,cardIndex,playCard)
playCard.splice(0, 1, hand[cardIndex]);
hand.splice(cardIndex, 1);
$(document).trigger('playMade');
};
//This function checks that the player's hand has fewer than five cards in it. If so, it then checks which hand is attempting
//the draw, and makes sure that there is at least one card in the corresponding deck. If all conditions check out, a card
//is spliced from the first index of that player's deck array and pushed into player's hand.
var drawCard = function(hand) {
if (hand.length < 5) {
if (hand == lowerHand && lowerDeck.length > 0) {
hand.push(lowerDeck.splice(0, 1)[0]);
}
else if (hand == upperHand && upperDeck.length > 0) {
hand.push(upperDeck.splice(0, 1)[0]);
}
else {
console.log("Invalid input.");
}
}
else {
console.log("Hand size at maximum.");
}
showCard();
updateCounters();/////////////////////////////////////////////////////////////////////
};
var drawLeftAndRight = function () {
if(drawInd){
updateCounters();
if (drawLeft === 0) {
if (upperCounter > lowerCounter) {
alert("Stalemate. Upper Hand wins by default.");
alert("...losers.");
}
else if (lowerCounter > upperCouter) {
alert("Stalemate. Player 1 wins by default.");
alert("But you both suck.");
}
}
else if (drawLeft.length > 0) {
playLeft.splice(0, 1, drawLeft.splice(0, 1)[0]);
playRight.splice(0, 1, drawRight.splice(0, 1)[0]);
}
drawInd=false;
updateCounters();
};
};
var updateCounters = function() {
upperCounter = upperHand.length + upperDeck.length;
lowerCounter = lowerHand.length + lowerDeck.length;
};
var stats = function() {
console.log(mainDeck);
console.log(upperDeck);
console.log(upperHand);
console.log(lowerDeck);
console.log(lowerHand);
console.log(drawLeft);
console.log(playLeft);
console.log(playRight);
console.log(drawRight);
};
var translate = function(card){
f = card[0];
l = card[2];
if(f==='1'){
f = 'A'
} else if (f === 'A'){
f = "10"
} else if (f === 'B'){
f = "J"
} else if (f === 'C'){
f = "Q"
} else if (f === 'D'){
f = "K"
}
if(l==='D'){
l = '♦'
} else if(l === 'C'){
l = '♣'
} else if(l === 'H'){
l = '♥'
} else if(l === 'S'){
l = '♠'
}
return (f+' '+l)
}
// var a = function(){for(var i = 0; i < 5; i++){$('.uhand div:nth-child(' + i + ')').text('Hello')}}
// updateHandNames(upperHand,uhand)
var updateHandNames = function(hand,handClass){
console.log(hand,handClass)
var handArray = ['','','','',''];
for(var i = 0; i < hand.length; i++){
handArray[i]=(translate(hand[i])); //do fewer of these
}
//handArray.each (translate); DO MORE OF THESE
handArray;
for(var j = 0; j < 5; j++){
var k = j+1
$('.'+handClass+' div:nth-child( '+k+')').contents().remove();
$('.'+handClass+' div:nth-child( '+k+')').html(handArray[j]);
}
}
var showCard=function(){
updateHandNames(upperHand,'uhand');
updateHandNames(lowerHand,'dhand');
updateHandNames([playLeft[0],playRight[0]],'onField');
}
var setWin= function(){
if (upperHand.length === 0 && upperDeck.length === 0){
end = Date.now();
elapsed = (end - start)/1000;
name = prompt('Player Two has the Upper Hand.\nWinning time: '+elapsed+' seconds.\nEnter name for scoreboard:');
gameOver=true;
return true;
} else if (lowerHand.length === 0 && lowerDeck.length === 0){
end = Date.now();
elapsed = (end - start)/1000;
name = prompt('Player 1 is victorious.\nWinning time: '+elapsed+' seconds.\nEnter name for scoreboard:');
gameOver=true;
return true;
}
}