-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
184 lines (160 loc) · 6.33 KB
/
main.py
File metadata and controls
184 lines (160 loc) · 6.33 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
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
from copy import deepcopy
from random import randint
import pickle
from game import Game
from completegame import CompleteGame
from timeit import default_timer as timer
# Returns the result of playing a given card as a oneOff
# Returns array of possible results (in case card can be played multiple ways)
def playCardAsOneOff(originalGame, pIndex, cIndex):
res = []
game = deepcopy(originalGame)
card = game.players[pIndex].hand[cIndex]
# Move all points to scrap
if card.rank == 1:
game.scrap += game.players[0].points
game.scrap += game.players[1].points
game.players[0].points = []
game.players[1].points = []
game.log.append("Player %s destroys all points with the %s" %(pIndex, card.name()))
res.append(game)
elif card.rank == 2:
pass
elif card.rank == 3:
pass
elif card.rank == 4:
pass
# Draw 2 cards
elif card.rank == 5:
if len(game.deck) > 0:
game.scrap.append(game.players[pIndex].hand.pop(cIndex))
game.players[pIndex].hand.append(game.deck.pop(-1))
if len(game.deck) > 0:
game.players[pIndex].hand.append(game.deck.pop(-1))
game.log.append("Player %s draws two cards with the %s" %(pIndex, card.name()))
else:
game.log.append("Player %s draws ONE card with the %s" %s(pIndex, card.name()))
res.append(game)
elif card.rank == 6:
game.scrap += game.players[0].faceCards
game.scrap += game.players[1].faceCards
game.players[0].faceCards = []
game.players[1].faceCards = []
game.log.append("Player %s destroys all face cards with the %s" %(pIndex, card.name()))
res.append(game)
pass
elif card.rank == 7:
pass
elif card.rank == 9:
pass
return res
# Returns a list of all gamestates resulting from all possible moves
# that a given player could make this turn
def findPossibleMoves(originalGame, pIndex):
res = [] # List of gamestates resulting from possible moves
game = deepcopy(originalGame) #Copy original game to avoid overwriting it
# Draw
if len(game.deck) > 0:
game.players[pIndex].hand.append(game.deck.pop(-1))
game.log.append("Player %s draws" %pIndex)
res.append(deepcopy(game))
game = deepcopy(originalGame)
# Loop through each card in hand and add all legal moves makeable with that card
for i, card in enumerate(game.players[pIndex].hand):
if card.rank < 11:
# Play points
game = deepcopy(originalGame)
name = card.name()
lenHand = len(game.players[pIndex].hand)
game.players[pIndex].points.append(game.players[pIndex].hand.pop(i))
game.log.append("Player %s played the %s for points" %(pIndex, card.name()))
res.append(deepcopy(game))
# Scuttle
# For each opponent's point card, if scuttle is legal, add it to list of possible moves
for j, pointCard in enumerate(game.players[(pIndex + 1)%2].points):
if card.rank > pointCard.rank or (card.rank == pointCard.rank and card.suit > pointCard.suit):
scuttleResult = deepcopy(originalGame)
scuttleResult.scrap = scuttleResult.scrap + pointCard.jacks #Move jacks to scrap pile
pointCard.jacks = []
scuttleResult.scrap.append(scuttleResult.players[(pIndex + 1) % 2].points.pop(j)) # Move destroyed point card to scrap
scuttleResult.scrap.append(scuttleResult.players[pIndex].hand.pop(i)) # Move played card to scrap
scuttleResult.log.append("Player %s scuttled the %s with the %s" %(pIndex, pointCard.name(), card.name()))
res.append(deepcopy(scuttleResult))
# Play as oneOff
res += playCardAsOneOff(originalGame, pIndex, i)
# Play king or queen
elif card.rank > 11:
game = deepcopy(originalGame)
game.players[pIndex].faceCards.append(game.players[pIndex].hand.pop(i))
game.log.append("Player %s played the %s" %(pIndex, card.name()))
res.append(deepcopy(game))
# Play jack
elif card.rank == 11 and game.players[(pIndex + 1) % 2].queenCount() == 0:
game = deepcopy(originalGame)
# Loop through all possible jack targets and add move for each one
for j, pointCard in enumerate(game.players[(pIndex + 1) % 2].points):
jackResult = deepcopy(originalGame)
jackResult.players[(pIndex + 1) % 2].points[j].jacks.append(jackResult.players[pIndex].hand.pop(i)) # Move jack from hand to the jacks of targeted point card
jackResult.players[pIndex].points.append(jackResult.players[(pIndex + 1) % 2].points.pop(j)) # Move stolen point card to this player's points
jackResult.log.append("Player %s stole the %s with the %s" %(pIndex, pointCard.name(), card.name()))
res.append(deepcopy(jackResult))
return res
def chooseRandomMove(moves):
return moves[randint(0, len(moves) - 1)]
# Plays two turns of a game (each player plays once)
# Modifies the gameHistory param to append new gamestates
def playRound(gameHistory):
chosenMove = gameHistory.gameStates[-1] # Gamestate before round
moves = findPossibleMoves(gameHistory.gameStates[-1], 0)
stalemate = False
if len(moves) > 0:
chosenMove = chooseRandomMove(moves)
gameHistory.gameStates.append(chosenMove)
else:
stalemate = True
if gameHistory.gameStates[-1].winner() == None:
moves = findPossibleMoves(chosenMove, 1)
if len(moves) > 0:
chosenMove = chooseRandomMove(moves)
gameHistory.gameStates.append(chosenMove)
elif stalemate:
gameHistory.result = "Stalemate"
return gameHistory
# Play full game until there is a winner
# Returns complete game object with full game history
def playGame():
gameHistory = CompleteGame()
# Play until there is a winner or staelemate
while gameHistory.winner() == None and gameHistory.result == None:
playRound(gameHistory)
# If no stalemate, set the result to index of winner
if gameHistory.result == None:
gameHistory.result = gameHistory.winner()
return gameHistory
# Plays n games and returns list of CompleteGames
def playNGames(n):
res = []
for i in range(n):
res.append(playGame())
return res
gameList = []
##############################
# Loading and saving results #
##############################
# Read old list of complete games
with open('./game.pkl', 'rb') as f:
gameList = pickle.load(f)
# Add new games
start = timer()
n = 100
gameList += playNGames(588) #This param sets how many games are added to list
end = timer()
print("Ran %s games in %s" %(n, end-start))
print("Saved a total of %s games" %len(gameList))
# for game in gameList:
# print("\n\n===================================================")
# game.print()
gameList[-1].print()
# Save results to disk
with open('./game.pkl', 'wb') as f:
pickle.dump(gameList, f)