-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgame.js
More file actions
64 lines (44 loc) · 1.37 KB
/
Copy pathgame.js
File metadata and controls
64 lines (44 loc) · 1.37 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
'use strict'
var cards = require('./cards');
var hands = require('./hands');
function getPlayerWithBestHand(){
bestPlayer = null;
bestHand = null;
for(var i = 0; i < players.length;i++){
var playerCards = players[i].pocketCards.slice(0);
playerCards.push.apply(playerCards, commonCards);
var hand = hands.getBestHand(playerCards);
if(!bestPlayer || !bestHand){
bestPlayer = players[i];
bestHand = hand;
continue;
}
if(hand.value > bestHand.value){
bestPlayer = players[i];
bestHand = hand;
continue;
}
}
return bestPlayer;
}
function echoBestPlayer(round){
getPlayerWithBestHand();
console.log(`After the ${round}, ${bestPlayer.name} is winning with a ${hands.getName(bestHand.value)}`);
}
var players = [{name: "John"}, {name: "Paul"}, {name: "George"}, {name: "Ringo"}];
var bestPlayer = null;
var bestHand = null;
var deck = cards.shuffle();
// deal
for (var i = 0; i < players.length; i++) {
players[i].pocketCards = deck.splice(0, 2);
}
var commonCards = [];
echoBestPlayer('deal');
// now the flop
commonCards = deck.splice(0, 3);
echoBestPlayer('flop');
commonCards.push.apply(commonCards, deck.splice(0, 1));
echoBestPlayer('turn');
commonCards.push.apply(commonCards, deck.splice(0, 1));
echoBestPlayer('river');