-
Notifications
You must be signed in to change notification settings - Fork 1
/
pitch.js
324 lines (300 loc) · 7.69 KB
/
pitch.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const prompt = require('prompt');
const console = require('better-console');
const deck = require('./deck.json')
const dummyTeams = require('./dummy-teams.json')
prompt.start()
let trick = null;
let round = null;
class Match {
constructor() {
this.teams = dummyTeams;
}
start() {
console.log('match start');
round = new Round();
round.start();
}
nextRound() {
if (matchOver) {
console.log(`${matchOver.name} WINS!`)
} else {
let round = new Round();
round.start();
}
}
}
class Round extends Match {
constructor(teams) {
super(teams);
this.deck = shuffle(deck);
this.tricks = [];
this.playerOrder = null;
}
start() {
console.log('round start')
this.deal();
}
deal() {
for (let team of this.teams) {
for (const player of team.players) {
let hand = this.deck.splice(0, 7);
this.deck = this.deck.slice(0, 7);
player.hand = hand;
// console.log(player)
}
}
match.playerOrder = [this.teams[0].players[0], this.teams[1].players[0]];
//console.log(match.playerOrder)
this.bid();
}
bid() {
const players = match.playerOrder;
// console.log(match,match.playerOrder)
let bids = [];
// console.log(players)
turn(0);
function turn(index) {
if (index < players.length) {
const player = players[index];
showHand(player);
prompt.get(['bid'], (err, result) => {
let bid = result.bid;
if (bid.toUpperCase() === 'P') {
bid = 0;
}
for (let existingBid of bids) {
if (existingBid.bid > bid && bid != 0) {
bid = false;
}
}
if (bid === false) {
console.log(`INVALID BID. BID HIGHER, OR PASS`);
} else {
bids.push({player: player.name, bid: bid});
console.table(bids);
index++;
}
turn(index);
})
} else {
let highestBid = Math.max.apply(Math, bids.map(function(bid) {
return bid.bid;
}))
let winningBid = bids.filter(function(bid) {
return parseFloat(bid.bid) === highestBid
});
winningBid = winningBid[0];
setDealer(winningBid.player);
}
}
}
}
class Trick extends Round {
constructor(){
super();
this.cardsPlayed = [];
this.trumps = null;
}
start() {
this.cardsPlayed = [];
console.log('trick start')
if (this.tricks.length > 7) {
return true;
}
console.log(`Trick #${this.tricks.length + 1}:`)
const players = match.playerOrder;
//console.log(players)
turn(0);
function turn(index) {
if (index < players.length) {
const player = players[index];
showHand(player);
prompt.get([
'value', 'suit'
], (err, result) => {
if (playCard(player, result.value.toUpperCase(), result.suit.toUpperCase())) {
index++;
console.table(showTrickStatus(match))
} else {
console.log(`YOU DONT HAVE THAT CARD`)
}
turn(index)
})
} else {
determineTrickWinner(trick.cardsPlayed, trick.trumps)
}
}
}
}
const shuffle = (deck) => deck.sort(() => (Math.random() - 0.5));
const showHand = (player) => {
console.log(`${player.name}'s Turn:`);
console.table(player.hand)
}
const showTrickStatus = (match) => {
console.log(`Current Trick:`)
console.table(trick.cardsPlayed.map(function(card) {
return {card: card.unicode, 'played by': card.playedBy}
}))
}
const playCard = (player, value, suit) => {
const hand = player.hand;
let i;
let cardToBePlayed = false;
for (i = hand.length - 1; i >= 0; i -= 1) {
const card = hand[i];
if (card.value === value && card.suit === suit) {
cardToBePlayed = card;
// console.log(`Playing the ${cardToBePlayed.text}`)
cardToBePlayed.playedBy = player.name;
trick.cardsPlayed.push(cardToBePlayed);
hand.splice(i, 1);
break;
}
}
return cardToBePlayed;
}
const findIndexOfObjByParam = (array, param, value) => {
for (var i = 0; i < array.length; i += 1) {
if (array[i][param] === value) {
return i;
}
}
return -1;
}
const setDealer = (playerName) => {
const reorder = (data, index) => {
return data.slice(index).concat(data.slice(0, index))
};
console.log(`${playerName} leads!`)
match.playerOrder = reorder(match.playerOrder, findIndexOfObjByParam(match.playerOrder, 'name', playerName));
chooseTrumps(match.playerOrder[0])
}
const chooseTrumps = (player) => {
showHand(player)
console.log(`What is trumps?`)
prompt.get(['suit'], (err, result) => {
let trumps = result.suit.toUpperCase()
console.log(`${suitUnicode(trumps)} is trumps!`);
trick = new Trick();
trick.trumps = trumps;
trick.start()
})
}
const suitUnicode = (letter) => {
letter = letter.toUpperCase();
const unicode = {
S: '♠',
H: '♥',
D: '♦',
C: '♣'
}
return unicode[letter]
}
const cardValue = (value) => {
// console.log('cardValue',value)
const faceCards = [
{
value: 'A',
number: 14
}, {
value: 'K',
number: 13
}, {
value: 'Q',
number: 12
}, {
value: 'J',
number: 11
}
];
const numberCards = [2,3,4,5,6,7,8,9,10];
if(numberCards.includes(parseFloat(value))){
return value;
}
else{
var number = faceCards.filter(function(card) {
return card.value == value;
});
return number[0].number;
}
}
const showScore = (match) => {
return {team: team.name, score: team.score}
let score = match.teams.map(function(team) {})
console.table(score);
}
const matchOver = (match) => {
let winningPosition = false;
let teams = match.teams;
for (team of teams) {
if (team.score >= 11) {
winningPosition = true
}
}
if (winningPosition === true) {
if (abs(teams[0].score - teams[1].score) >= 2) {
let highestScore = Math.max.apply(Math, teams.map(function(team) {
return team.score;
}))
let winningTeam = teams.filter(function(team) {
return parseFloat(team.score) === highestScore
});
return winningTeam;
}
} else {
return false;
}
}
const determineTrickWinner = (trick, trumps) => {
console.log('Determining trick winner...')
const lead = trick[0];
// the suit of the card lead is boss, even if not trumps,
// until someone trumps the trick
let suitLead = lead.suit;
console.log(`${suitUnicode(trumps)} is trumps...`)
console.log(`${lead.unicode} was lead...`)
let trumped = null;
//if trumps was not lead, determine if trick was trumped
if(lead.suit !== trumps){
let trumped = false;
for(card of trick){
if(card.suit === trumps){
trumped = true;
break;
}
}
}
else{
trumped = true;
}
let trickFiltered = null;
if(trumped){
// filter for trump cards only
trickFiltered = trick.filter(function(card){
return card.suit = trumps
})
}
else{
//filter for cards of lead suit only
trickFiltered = trick.filter(function(card){
return card.suit = lead.suit
})
}
let highestCard = Math.max.apply(Math, trickFiltered.map(function(card) {
let value = cardValue(card.value);
return value;
}))
let winningCard = trickFiltered.filter(function(card) {
return parseFloat(cardValue(card.value)) === highestCard
});
// console.log(`Winning Card: ${winningCard.unicode}`);
let winningPlayer = winningCard[0].playedBy;
console.log(`${winningPlayer} takes the trick!`)
return winningPlayer;
}
const determineLow = () => {}
const determineHigh = () => {}
const countForGame = () => {}
let match = new Match();
match.start();