-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager_3.py
executable file
·147 lines (113 loc) · 4.1 KB
/
GameManager_3.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
from Grid_3 import Grid
from ComputerAI_3 import ComputerAI
from PlayerAI_3 import PlayerAI
# working minimax with ab-pruning backup
# from Minimax_ab_pruning_3 import PlayerAI
# working minimax backup
# from Minimax_3 import PlayerAI
from Displayer_3 import Displayer
from random import randint
import time
defaultInitialTiles = 2
defaultProbability = 0.9
actionDic = {
0: "UP",
1: "DOWN",
2: "LEFT",
3: "RIGHT"
}
(PLAYER_TURN, COMPUTER_TURN) = (0, 1)
# Time Limit Before Losing
timeLimit = 0.2
allowance = 0.05
class GameManager:
def __init__(self, size = 4):
self.grid = Grid(size)
self.possibleNewTiles = [2, 4]
self.probability = defaultProbability
self.initTiles = defaultInitialTiles
self.computerAI = None
self.playerAI = None
self.displayer = None
self.over = False
def setComputerAI(self, computerAI):
self.computerAI = computerAI
def setPlayerAI(self, playerAI):
self.playerAI = playerAI
def setDisplayer(self, displayer):
self.displayer = displayer
def updateAlarm(self, currTime):
if currTime - self.prevTime > timeLimit + allowance:
print("TIME RUN OUT")
self.over = True
else:
while time.clock() - self.prevTime < timeLimit + allowance:
pass
self.prevTime = time.clock()
def start(self):
for i in range(self.initTiles):
self.insertRandonTile()
self.displayer.display(self.grid)
# Player AI Goes First
turn = PLAYER_TURN
maxTile = 0
self.prevTime = time.clock()
while not self.isGameOver() and not self.over:
# Copy to Ensure AI Cannot Change the Real Grid to Cheat
gridCopy = self.grid.clone()
move = None
if turn == PLAYER_TURN:
print("Player's Turn:", end="")
move = self.playerAI.getMove(gridCopy)
print(actionDic[move])
# Validate Move
if move != None and move >= 0 and move < 4:
if self.grid.canMove([move]):
self.grid.move(move)
# Update maxTile
maxTile = self.grid.getMaxTile()
else:
print("Invalid PlayerAI Move")
self.over = True
else:
print("Invalid PlayerAI Move - 1")
self.over = True
else:
print("Computer's turn:")
move = self.computerAI.getMove(gridCopy)
# Validate Move
if move and self.grid.canInsert(move):
self.grid.setCellValue(move, self.getNewTileValue())
else:
print("Invalid Computer AI Move")
self.over = True
if not self.over:
self.displayer.display(self.grid)
# Exceeding the Time Allotted for Any Turn Terminates the Game
self.updateAlarm(time.clock())
turn = 1 - turn
print(maxTile)
self.playerAI._logger.info("Max Depth={}, Avg move time={}, Max Tile={}".format(self.playerAI._max_move_depth,self.playerAI._avg_move_time/self.playerAI._no_of_moves,maxTile))
def isGameOver(self):
return not self.grid.canMove()
def getNewTileValue(self):
if randint(0,99) < 100 * self.probability:
return self.possibleNewTiles[0]
else:
return self.possibleNewTiles[1];
def insertRandonTile(self):
tileValue = self.getNewTileValue()
cells = self.grid.getAvailableCells()
cell = cells[randint(0, len(cells) - 1)]
self.grid.setCellValue(cell, tileValue)
def main():
gameManager = GameManager()
playerAI = PlayerAI()
computerAI = ComputerAI()
displayer = Displayer()
gameManager.setDisplayer(displayer)
gameManager.setPlayerAI(playerAI)
gameManager.setComputerAI(computerAI)
gameManager.start()
if __name__ == '__main__':
main()