-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwar-o.js
304 lines (235 loc) · 8.99 KB
/
war-o.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
WARO = (function (doc) {
var _numberOfPlayers = 0;
var _players = [];
var _kitty = null;
var createGame = function (numberOfRounds, players, customSignalEndOfGame) {
var STATE = {
INVALID: "Invalid",
IN_PROGRESS: "In progress",
FINISHED: "Finished"
};
var _state = STATE.INVALID;
var _players = players.slice(0);
var _signalEndOfGame = customSignalEndOfGame || function () {
console.log("End of Game! A Winner is You!");
};
// Create an array and initialize to 0
var _playerScores = Array.apply(null, new Array(_players.length)).map(Number.prototype.valueOf,0);
var _playerHands = [];
var _rounds = [];
var _currentRound = 0;
var _playersOK = (_players !== null) && (_players.length > 1);
console.log("Current player count: " + _players.length);
console.log("Number of rounds to play: " + numberOfRounds);
var isInvalid = function () {
return STATE.INVALID === _state;
};
var isInProgress = function () {
return STATE.IN_PROGRESS === _state;
};
var isFinished = function () {
return STATE.FINISHED === _state;
};
var getPlayerList = function () {
var playerNames = [];
for (var playerIndex = 0; playerIndex < _players.length; playerIndex++) {
playerNames.push(_players[playerIndex].getName());
}
return playerNames;
};
var acceptPlayerBid = function (playerNumber, bid) {
var roundObj = _rounds[_currentRound];
roundObj.acceptBid(playerNumber, bid);
var handIndex = playerNumber - 1;
var hand = _playerHands[handIndex];
hand = removeNumberFromHand(bid, hand);
_playerHands[handIndex] = hand;
if (roundObj.isFinished()) {
var winner = roundObj.getWinner();
var playerIndex = winner.getNumber() - 1;
_playerScores[playerIndex] += roundObj.getKittyValue();
_currentRound++;
if (_currentRound === _rounds.length) {
console.log("Game is FINISHED!");
console.log("Player scores: " + _playerScores);
_state = STATE.FINISHED;
// Signal end of game
_signalEndOfGame();
} else {
console.log("Initiate round " + _currentRound);
initiateRound();
}
}
};
var initiateRound = function (roundObject) {
var roundObj = _rounds[_currentRound];
var kittyValue = roundObj.getKittyValue();
var prevRound = null;
if (_currentRound >= 1) {
prevRound = _rounds[_currentRound - 1];
}
console.log("Kitty Value for Round " + _currentRound + " is " + kittyValue);
for (var playerIndex = 0; playerIndex < _players.length; playerIndex++) {
// Give players the next kitty value and ask for bids
var playerHand = _playerHands[playerIndex].slice(0);
_players[playerIndex].signalNextBid(kittyValue, acceptPlayerBid, playerHand, prevRound);
}
};
var startGame = function () {
console.log('Game has started!');
initiateRound();
};
var getScoreForPlayer = function (playerNum) {
return _playerScores[playerNum - 1];
};
if (_playersOK && (numberOfRounds > 0)) {
_players = players.slice(0);
_state = STATE.IN_PROGRESS;
var deckSize = numberOfRounds * (1 + _players.length);
var deck = createDeck(deckSize);
var shuffledDeck = shuffleDeck(deck);
var splits = splitDeck(shuffledDeck, numberOfRounds);
// Player hands are the first N splits of the split deck
_playerHands = splits.slice(1);
var kitty = splits[0];
for ( var roundIndex = 0; roundIndex < kitty.length; roundIndex++) {
_rounds.push(createRound(kitty[roundIndex], _players));
}
for (var playerNumber = 1; playerNumber <= _players.length; playerNumber++) {
_players[playerNumber - 1].setNumber(playerNumber);
}
} else {
_state = STATE.INVALID;
}
return {isInvalid: isInvalid,
isInProgress: isInProgress,
isFinished: isFinished,
getPlayerList: getPlayerList,
acceptPlayerBid: acceptPlayerBid,
startGame: startGame,
getScoreForPlayer: getScoreForPlayer
};
};
var createPlayer = function (name, customSignalNextBid) {
var _name = name;
var _hand = [];
var _number = -1;
var _currentKittyValue = -1;
var _game = null;
var getName = function () {
return name;
};
var getNumber = function () {
return _number;
};
var setNumber = function (number) {
_number = number;
};
var signalNextBid = customSignalNextBid || function (kittyValue, setNextBidFunction, hand) {
_hand = hand;
_currentKittyValue = kittyValue;
setNextBidFunction(_number, _hand.pop());
};
var setGame = function () {
_game = game;
};
return {getName: getName,
getNumber: getNumber,
setNumber: setNumber,
signalNextBid: signalNextBid,
setGame: setGame
};
};
var createDeck = function (cards) {
var deck = [];
for(var initIndex = 0; initIndex < cards; initIndex++) {
deck[initIndex] = initIndex + 1;
}
return deck;
};
var shuffleDeck = function (deck) {
// From: http://stackoverflow.com/a/6274398/418969
var shuffledDeck = deck.slice(0); // naive clone
var counter = shuffledDeck.length - 1;
var temp, index;
while (counter) {
index = (Math.random() * counter) | 0;
temp = shuffledDeck[counter];
shuffledDeck[counter] = shuffledDeck[index];
shuffledDeck[index] = temp;
counter--;
}
return shuffledDeck;
};
var splitDeck = function (deck, splitSize) {
var splits = [];
for (var deckIndex = 0, splitIndex = 0;
deckIndex < deck.length;
deckIndex += splitSize, splitIndex++) {
splits[splitIndex] = deck.slice(deckIndex, deckIndex + splitSize);
}
return splits;
};
var getPositionInHand = function (number, hand) {
for (var valPos = 0; valPos < hand.length; valPos++) {
if (hand[valPos] === number) {
return valPos;
}
}
return -1;
};
var removeNumberFromHand = function (number, hand) {
var valPos = getPositionInHand(number, hand);
var result = hand;
if (valPos >= 0) {
var half1 = hand.slice(0, valPos);
var half2 = hand.slice(valPos + 1);
result = half1.concat(half2);
}
return result;
};
var createRound = function (kittyValue, players) {
var _kittyValue = kittyValue;
var _players = players;
var _playerBids = [];
var getKittyValue = function() {
return _kittyValue;
};
var isFinished = function () {
return _players.length === _playerBids.length;
};
var getWinner = function () {
var winner = -1;
var bidValue = -1;
var currentHighest = -1;
if (isFinished()) {
for (var bidIndex = 0; bidIndex < _playerBids.length; bidIndex++) {
bidValue = _playerBids[bidIndex].bid;
if (bidValue > currentHighest) {
currentHighest = bidValue;
winner = _playerBids[bidIndex].player;
}
}
}
return winner;
};
var acceptBid = function (playerNumber, bidValue) {
var playerObj = _players[playerNumber - 1];
_playerBids.push({player: playerObj, bid: bidValue});
};
return {getKittyValue: getKittyValue,
isFinished: isFinished,
acceptBid: acceptBid,
getWinner: getWinner
};
};
return {createPlayer: createPlayer,
createDeck: createDeck,
shuffleDeck: shuffleDeck,
splitDeck: splitDeck,
createGame: createGame,
createRound: createRound,
getPositionInHand: getPositionInHand,
removeNumberFromHand: removeNumberFromHand
};
} (document) );