-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameManager_3.py
145 lines (112 loc) · 4.19 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
from Grid_3 import Grid
from ComputerAI_3 import ComputerAI
from PlayerAI_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##0.9
self.initTiles = defaultInitialTiles#2
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):
## print(currTime-self.prevTime)
if currTime - self.prevTime > timeLimit + allowance:
## print(currTime-self.prevTime)##0.2+0.05
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):##range(2),在开始的时候首先放进去几个值此时为2个
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)##查询AI要走那个方向
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())
print(self.over)
turn = 1 - turn
print(maxTile)
def isGameOver(self):
##print(self.grid.canMove())
return not self.grid.canMove()
def getNewTileValue(self):
if randint(0,99) < 100 * self.probability:
##放2,或者4的可能性有probability决定,此时为0.9放2,0.1放4
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()