-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
143 lines (109 loc) · 3.58 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
import random, poker
from deck import Deck
from pokerPlayers import pokerPlayerFactory
from poker import Poker
class Game:
def __init__(self,playerArr):
# STARTING GAME
self.pokerRules = Poker()
# CREATING PLAYERS
playerFactory = pokerPlayerFactory()
self.players = []
for player in playerArr:
playerName = player[0]
playerType = player[1]
newPlayer = playerFactory.newPokerPlayer(playerName,playerType)
self.players.append(newPlayer)
random.shuffle(self.players,random.random)
# PLAYER ORDER
playerOrder = []
for player in self.players:
playerOrder.append(player.name)
def startGame(self):
# ACTUAL GAMEPLAY
self.roundNum = 0
while self.playersLeftForGame():
startingPlayerIndex = self.roundNum % len(self.players)
self.playSingleRound(startingPlayerIndex)
self.roundNum += 1
# FINDING THE WINNER
for player in self.players:
if player.cardsLeft > 0:
return player.name
def playersLeftForGame(self):
playersWithCards = 0
for player in self.players:
if player.cardsLeft > 0:
playersWithCards += 1
return playersWithCards > 1
def playSingleRound(self,startingPlayerIndex):
# ROUND INIT
self.dealCards()
for player in self.players:
player.calibrateStrategy(self)
self.currentAnnouncedHand=[]
self.announcingPlayerIndex = startingPlayerIndex
stillAnnouncing = True
# ROUND ACTUAL PLAY
while stillAnnouncing:
#Get the next player that is still in the game to announce
while self.players[self.announcingPlayerIndex].cardsLeft == 0:
self.announcingPlayerIndex += 1
self.announcingPlayerIndex %= len(self.players)
#ANNOUNCEMENT
self.currentAnnouncedHand = self.players[self.announcingPlayerIndex].announce(self)
self.firstChallenger = True
for increment in xrange(1,len(self.players)):
challengingPlayerIndex = (self.announcingPlayerIndex + increment) % len(self.players)
if self.players[challengingPlayerIndex].cardsLeft == 0:
#This player is not in the game anymore
continue
if self.currentAnnouncedHand == [(14,4)]:
# Highest available hand
stillAnnouncing = False
break
if self.players[challengingPlayerIndex].challenge(self):
#Challenge occured
stillAnnouncing = False
break
self.firstChallenger = False
#Get the index for the next player
if stillAnnouncing:
self.announcingPlayerIndex = (self.announcingPlayerIndex + 1) % len(self.players)
# STANDOFF
goodChallenge = self.pokerRules.standOff(self.cardsOnTable,self.currentAnnouncedHand)
# PLAYERS LEARN AND RECALIBERATE
for player in self.players:
player.learn(self,goodChallenge)
player.resetSettings()
# UPDATE PLAYERS CARD QUOTA AFTER STANDOFF
if goodChallenge:
self.players[self.announcingPlayerIndex].cardsLeft -= 1
else:
self.players[challengingPlayerIndex].cardsLeft -= 1
def dealCards(self):
theDeck = Deck()
# DEALING CARDS
counter = 0
for player in self.players:
player.cardsInHand = []
player.playersHand = []
for i in xrange(player.cardsLeft):
player.recieveCard(theDeck.dealCard())
counter += 1
# INITING TABLE VARIABLES
cardsOnTable = []
self.numCardsOnTable = 0
for player in self.players:
for cardFromPlayer in player.playersHand:
self.numCardsOnTable += cardFromPlayer[1]
existOnTable = False
for cardOnTable in cardsOnTable:
if cardFromPlayer[0] == cardOnTable[0]:
cardOnTable[1] += cardFromPlayer[1]
existOnTable = True
if not existOnTable:
cardsOnTable.append(cardFromPlayer)
self.cardsOnTable = cardsOnTable
def getNumCardsOnTable(self):
return self.numCardsOnTable