-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
245 lines (188 loc) · 4.99 KB
/
game.js
File metadata and controls
245 lines (188 loc) · 4.99 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
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
// Game object
window.Game = {}
//List of players
Game.table = [];
Game.card = function(){
// Suit - string
this.suit;
// Face value - string or number.
//Put check here to see if it's a facecard.
this.faceValue;
}
Game.Player = function(type, username){
this.type = type;
this.username = username;
var totalMoney = 100;
// dealer limit
this.limit;
//var that = this;
var handValue;
this.hand = [];
if (this.type == "dealer"){
//Set dealer specific properties and methods here
this.limit = 16;
}
function getTotalMoney(){
return totalMoney;
}
this.placeBet = function (amt){
this.transact("subtract", amt);
return "Bet of $" + amt + " placed.";
}
this.transact = function(action, amt){
this.action = action;
this.amt = amt;
if(this.action == "add"){
totalMoney = totalMoney + amt;
} else{
totalMoney = totalMoney - amt;
}
}
this.getPlayerType = function(){
return this.type;
}
this.getTotalMoney = function(){
return totalMoney;
}
this.calculateTotalHand = function(){
var value;
return value;
}
this.init = function(){
this.addToGame();
}
//Initialize here
this.init();
}
Game.Player.prototype.getPlayerName = function(){
return this.username;
};
Game.Player.prototype.addToGame = function(){
var playername = this.getPlayerName();
Game.table.push(this);
};
Game.Player.prototype.calculateHandValue = function(){
var values = [];
var totalHand = 0;
for(var i = 0; i < this.hand.length; i++){
values[i] = Game.calculateCardValue(this.hand[i]);
totalHand = totalHand + values[i];
}
return totalHand;
}
Game.Deck = {
cards : [],
fullDeck : ['1-s', '1-c', '1-h', '1-d', '2-s', '2-c', '2-h', '2-d', '3-s', '3-c', '3-h', '3-d', '4-s', '4-c', '4-h', '4-d', '5-s', '5-c', '5-h', '5-d', '6-s', '6-c', '6-h', '6-d', '7-s', '7-c', '7-h', '7-d', '8-s', '8-c', '8-h', '8-d', '9-s', '9-c', '9-h', '9-d', '10-s', '10-c', '10-h', '10-d', '11-s', '11-c', '11-h', '11-d', '12-s', '12-c', '12-h', '12-d', '13-s', '13-c', '13-h', '13-d'],
hitPlayer : function(player){
this.player = player;
var deck = Game.Deck;
// Pop a card off the cards array and pass the value to the player
var numCards = deck.getNumCards() - 1;
var topCard = deck.cards[numCards];
this.player.receiveCard(topCard);
deck.cards.pop();
},
initialDeal : function(){
//Pop two cards off the stack and pass the value of each to the player.
for(var i = 0; i < Game.table.length; i++){
var deck = Game.Deck;
var currPlayer = Game.table[i];
var currPlayerName = currPlayer.getPlayerName();
var currPlayerType = currPlayer.getPlayerType();
for(var j = 0; j < 2; j++){
deck.hitPlayer(currPlayer);
}
var handValue = currPlayer.calculateHandValue();
console.log(currPlayer.limit);
if(currPlayerType == "dealer"){
console.log("in dealer if");
console.log(typeof(handValue));
console.log(typeof(currPlayer.limit));
/*while(handValue < currPlayer.limit){
deck.hitPlayer(currPlayer);
console.log("Hit dealer additionally");
}*/
}
}
console.log("cards are dealt");
},
fillDeck : function(){
Game.Deck.cards = Game.Deck.fullDeck;
return Game.Deck.cards;
},
shuffle : function(){
Game.Deck.fillDeck();
Game.Deck.cards.sort(function() { return 0.5 - Math.random() });
return "cards are shuffled";
},
getNumCards : function(){
return Game.Deck.cards.length;
},
init : function(){
Game.Deck.shuffle();
}
}
Game.calculateCardValue = function(cardId){
this.cardId = cardId;
var cardSplit = cardId.split("-");
var cardNum = parseInt(cardSplit[0]);
if(cardNum < 11){
return cardNum;
} else {
return 10;
}
}
Game.calculateWinner = function(){
var i,
j,
player,
playerName,
playerTotal;
this.playerTotals = [];
for(i = 0; i < Game.table.length; i++ ){
player = Game.table[i];
console.log(player);
playerName = player.getPlayerName();
console.log("Player: " + playerName);
playerTotal = player.calculateHandValue();
console.log("PlayerTotal: " + playerTotal);
if(playerTotal < 22){
this.playerTotals.push(playerTotal);
}
}
this.highestValue = Math.max.apply( Math, this.playerTotals );
for(j = 0; j < this.playerTotals.length; j++){
if(this.playerTotals[j] == this.highestValue){
return Game.table[j].getPlayerName() + " is the winner.";
}
}
Game.end();
//return Game.table[this.highestValue].getPlayerName() + " is the winner.";
}
Game.end = function(){
return "Game over.";
}
Game.Pot = {
value : 0,
add : function(amt){
Game.Pot.value += amt;
},
awardPlayer : function(username){
this.username = username;
this.username.transact("add", Game.Pot.value);
Game.Pot.value = 0;
},
getPotValue : function(){
return Game.Pot.value;
}
}
Game.Player.prototype.receiveCard = function(card){
this.card = card;
this.hand.push(this.card);
}
//Set up Players
var Dealer = new Game.Player('dealer', 'Dealer');
var Jim = new Game.Player('player', 'Jim');
//Initializers
Game.Deck.init();
Game.Deck.initialDeal();