-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
165 lines (126 loc) · 4.87 KB
/
game.py
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
#!/usr/local/bin/python3
# Game
from random import randint
from NPC import NPC
from human import Human
from RESTPlayer import RESTPlayer
suits = ['HEARTS', 'DIAMONDS', 'SPADES', 'CLUBS']
ranks = ['ACE', 'KING', 'QUEEN', 'JACK', '10', '9', '8', '7', '6', '5', '4', '3', '2']
deck = [ (i,j) for j in suits for i in ranks ]
class Game:
def __init__(self):
# self.players = [Human('player-%d' % (i+1)) for i in range(1)] + [NPC('cpu-%d' % i) for i in range(3)]
self.players = [RESTPlayer('http://127.0.0.1:8000')] + [NPC('cpu-%d' % i) for i in range(3)]
self.starting = 0
self.names = [p.name for p in self.players]
self.estimates = [0,0,0,0]
self.won = [0,0,0,0]
self.points = [0,0,0,0]
def playGame(self):
cards = 10
while (cards <= 13):
self.playHand(numCards=cards)
cards += 1
self.starting = (self.starting + 1) % 4
self.printScores()
def playHand(self, numCards):
# input('Press Enter to continue')
trumpSuit = suits[randint(0,3)]
self.deal(numCards)
self.getEstimates(numCards, trumpSuit)
self.won = [0,0,0,0]
for i in range(numCards):
self.playRound(trumpSuit)
self.tallyScores(numCards)
self.starting = (numCards - 3) % 4
def deal(self, numCards):
current_deck = self.shuffle(deck)
player_idx = self.starting
counter = numCards * 4
for card in current_deck:
p = self.players[player_idx]
p.giveCard(card)
player_idx = (player_idx + 1) % 4
counter -= 1
if counter <= 0:
break
def shuffle(self, deck):
master = [i for i in range(len(deck))]
shuffled = []
total = len(master)
while (total > 0):
idx = randint(0, total-1)
shuffled.append(deck[master[idx]])
del master[idx]
total = len(master)
return shuffled
def getEstimates(self, numCards, trumpSuit):
invalid = -1
total = 0
for i in range(4):
curr_player_idx = (self.starting + i) % 4
player = self.players[curr_player_idx]
if i == 3 and total <= numCards:
invalid = numCards - total
est = player.getEstimate(trumpSuit, invalid)
total += est
self.estimates[curr_player_idx] = est
def playRound(self, trumpSuit):
print('First player is %s' % self.players[self.starting].name)
# input('Press Enter to continue')
turns = []
for i in range(4):
curr_player = self.players[(self.starting + i) % 4]
played_card = curr_player.getCard(turns, trumpSuit)
played_turn = (curr_player.name, *played_card)
self.printTurn(played_turn)
turns.append(played_turn)
self.findWinner(turns, trumpSuit)
def printTurn(self, turn):
print('%s played the %s of %s' % turn)
def findWinner(self, turns, trumpSuit):
highest = None
lead_suit = None
# print('Finding winner in', turns, 'with trump suit', trumpSuit)
for turn in turns:
if highest is None:
highest = turn
lead_suit = turn[2]
# print('First turn was', turn)
continue
if turn[2] == trumpSuit and highest[2] != trumpSuit:
highest = turn
# print('Trumped by', turn)
continue
if turn[2] != trumpSuit:
if turn[2] != lead_suit:
continue
if highest[2] == trumpSuit:
continue
if ranks.index(turn[1]) < ranks.index(highest[1]): # higher cards come first in the list
# print('Higher card in', turn)
highest = turn
continue
# print('\033c')
# for card in turns:
# print('%s played: %s of %s' % card)
print('%s won with the %s of %s' % highest)
winning_player = self.names.index(highest[0])
# print(winning_player)
self.won[winning_player] += 1
self.players[winning_player].roundWon()
self.starting = winning_player
def tallyScores(self, numCards):
for i,est in enumerate(self.estimates):
won = self.won[i]
if won == est:
print('%s estimated %d, won.' % (self.players[i].name, est) )
self.points[i] += (est + numCards)
else:
print('%s estimated %d, won %d rounds' % (self.players[i].name, est, won) )
self.points[i] -= abs(est - won)
self.printScores()
def printScores(self):
print('Scores are:')
for i,points in enumerate(self.points):
print('%s: %d points' % (self.players[i].name, points) )