-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
296 lines (268 loc) · 5.35 KB
/
game.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
(function(){
var _ = require("underscore");
var Q = require("q");
var util = require('util');
var inquirer = require("inquirer");
var _gamePlayed = 0;
var _testGameCount = 1000;
var _autoPlayer;
var bucketRange = _.range(10);
var discardedPileRange = _.range(6);
var currentCard;
var cards;
var buckets;
var discardedPile;
var skipUserTerm;
var gameEnded;
var winCount = 0;
var loseCount = 0;
function reset()
{
currentCard = null;
cards = _.shuffle(_.range(52));
buckets = [];
_.each(bucketRange, function(val){
buckets.push(null);
});
discardedPile = [];
skipUserTerm = false;
gameEnded = false;
}
reset();
function clearScreen()
{
util.print("\u001b[2J\u001b[0;0H");
}
function showBuckets()
{
var result = [];
_.each(bucketRange, function(val){
if (buckets[val] == null)
{
result.push("[ ]");
}
else
{
result.push("[" + buckets[val] + "]")
}
});
console.log("Buckets " + result.join(" "));
}
function showDiscardedPiles()
{
var result = [];
_.each(discardedPileRange, function(val){
if (discardedPile[val] == null)
{
result.push("[ ]");
}
else
{
result.push("[" + discardedPile[val] + "]")
}
});
console.log("Discarded pile " +result.join(" "));
}
function toCardNumber(val)
{
return val % 13 + 1;
}
function turnLogic()
{
if (_gamePlayed >= _testGameCount)
{
showWinLoseCount();
process.exit();
}
if (buckets.length == 10 && buckets.indexOf(null) == -1)
{
console.log("You win!");
winCount++;
gameEnded = true;
_gamePlayed++;
}
if (discardedPile.length == 6)
{
console.log("You lose.");
loseCount++;
gameEnded = true;
_gamePlayed++;
}
if (currentCard == null)
{
currentCard = cards.pop();
}
console.log("Current card is: " + toCardNumber(currentCard));
}
function findValidSpot(val)
{
//Find number/index that is next immediate smaller than val
var smaller;
var smallIndex = null;
var bigger;
var biggerIndex = null;
_.each(buckets, function(bVal, bIndex){
if (bVal == null)
return;
if (bVal < val)
{
smaller = bVal;
smallIndex = bIndex + 1;
}
if (bigger)
{
return;
}
if (bVal > val)
{
bigger = bVal;
biggerIndex = bIndex;
}
});
//Find number/index that is next immediate larger than val
//Return spots inbetween these with _.range
if (smallIndex && biggerIndex)
{
return _.range((smallIndex), (biggerIndex)).map(String);
}
else if (biggerIndex != null)
{
return _.range(0, biggerIndex).map(String);
}
else if (smallIndex != null)
{
return _.range(smallIndex, 10).map(String);
}
else
{
return _.range(0, 10).map(String);
}
}
function promptList(option, list, currentCardNum)
{
var deferred = Q.defer();
if (_autoPlayer)
{
setTimeout(deferred.resolve, 1, {
spot: _autoPlayer.pick(list, currentCardNum, buckets, discardedPile)
});
}
else
{
inquirer.prompt(option, function(answer)
{
deferred.resolve(answer);
});
}
return deferred.promise;
}
function promptConfirm(option)
{
var deferred = Q.defer();
if (_autoPlayer)
{
if (gameEnded)
{
setTimeout(deferred.resolve, 1, {confirm_reset:true});
}
else
{
setTimeout(deferred.resolve, 1, {confirm:true});
}
}
else
{
inquirer.prompt(option, function(answer)
{
deferred.resolve(answer);
})
}
return deferred.promise;
}
function getUserInput()
{
var currentCardNum = toCardNumber(currentCard);
var askUser;
if (gameEnded)
{
askUser = promptConfirm([{type: "confirm", name:"confirm_reset", message:"Play another game?"}]);
}
else if (buckets.indexOf(currentCardNum) != -1)
{
//Card already exist
askUser = promptConfirm([{type: "confirm", name:"confirm", message:"Card already exist. Continue"}]);
currentCard = null;
skipUserTerm = true;
}
else
{
var validSpots = findValidSpot(currentCardNum);
if (validSpots.length > 0)
{
askUser = promptList([{type: "list", name:"spot", message:"Which spot to place?", choices: validSpots}], validSpots, currentCardNum);
}
else
{
askUser = promptConfirm([{type: "confirm", name:"confirm", message:"No placement possible. Continue"}]);
discardedPile.push(currentCardNum)
currentCard = null;
skipUserTerm = true;
}
}
return askUser;
}
function processUserInput(val)
{
if (val.confirm_reset)
{
reset();
}
if (skipUserTerm)
{
skipUserTerm = false;
}
else
{
var userIndex = parseInt(val.spot);
var currentCardNum = toCardNumber(currentCard);
var validSpots = findValidSpot(currentCardNum);
//Place the card
buckets[userIndex] = currentCardNum;
currentCard = null;
}
loop();
}
function showWinLoseCount()
{
console.log("Win " + winCount + " - Lose " + loseCount);
}
function loop()
{
//console.log("Entering loop");
clearScreen();
showWinLoseCount();
showBuckets();
showDiscardedPiles();
turnLogic();
var inputPromise = getUserInput();
inputPromise
.then(processUserInput)
.fail(function(e){
console.trace(e);
})
//console.log("Choices");
}
function setAutoPlayer(val)
{
_autoPlayer = val;
}
function setTestGameCount(val)
{
_testGameCount = val;
}
module.exports = {
setTestGameCount: setTestGameCount,
setAutoPlayer:setAutoPlayer,
loop:loop
}
}());