-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.py
128 lines (105 loc) · 4.2 KB
/
GameController.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
import GameViewer,AIPlayer,HumanPlayer, time,random
# get color from each player
# start game
# bool move is valid
GREEN=(0,255,0)
WHITE=(255,255,255)
BLACK=(0,0,0)
class GameController:
def __init__ (self,size):
self.rows = size
self.cols = size
self.playingField = []
self.viewer = None
self.whitePlayer=None
self.blackPlayer=None
self.NewGame()
def NewGame(self):
GameOver=False
self.playingField = self.createMatrix()
self.viewer = GameViewer.GameViewer(self.rows, self.cols, 600, self, self.playingField)
self.viewer.setup()
humanColor=self.viewer.startScreen()
if humanColor=="W":
self.whitePlayer=HumanPlayer.HumanPlayer("W",self)
self.blackPlayer=AIPlayer.AIPlayer("B",self)
else:
self.blackPlayer=HumanPlayer.HumanPlayer("B",self)
self.whitePlayer=AIPlayer.AIPlayer("W",self)
self.blackPlayer.setupFirstTwoTiles(self.rows)
self.whitePlayer.setupFirstTwoTiles(self.cols)
turn = self.blackPlayer
self.updateViewer()
while(not GameOver):
validMoves=turn.myMove(self.playingField)
if validMoves:
moveToMake=None
self.viewer.showPossibleMoves(validMoves)
if isinstance(turn,HumanPlayer.HumanPlayer):
while(moveToMake==None):
clickedTilePos=self.viewer.run()
for move in validMoves:
if clickedTilePos==move.getPos():
moveToMake=move
break
elif isinstance(turn,AIPlayer.AIPlayer) :
#time.sleep(2)
moveToMake=turn.calcBestMove(self.playingField,validMoves)
turn.makeMove(moveToMake.getPos())
self.flipTiles(moveToMake.getTilesToFlip(),moveToMake.getColor())
#self.calculatePoints()
self.updateViewer()
if turn==self.whitePlayer:
turn=self.blackPlayer
else:
turn=self.whitePlayer
else:
GameOver=True
winner=None
if self.whitePlayer.getPoints()>self.blackPlayer.getPoints():
winner=self.whitePlayer.getName()
elif self.whitePlayer.getPoints()<self.blackPlayer.getPoints():
winner=self.blackPlayer.getName()
self.viewer.drawGameOver(winner)
def createMatrix(self):
tile_matrix = []
for i in range(self.rows):
row = []
for j in range(self.cols):
row.append(0)
tile_matrix.append(row)
return tile_matrix
def updateViewer(self):
self.calculatePoints()
self.viewer.draw(self.playingField,self.whitePlayer.getName(),self.blackPlayer.getName(),self. whitePlayer.getPoints(), self.blackPlayer.getPoints())
# def handleClick(self, position):
#self.players["HumanPlayer"].handleIncomingMove(position)
#lägg till i placeTile
def getAvailableTiles(self, position, playerColor):
availableLiles = []
def flipTiles(self,tilesToFlip,color):
for position in tilesToFlip:
self.playingField[position[0]][position[1]]=color
def calculatePoints(self):
whitepoints=0
blackpoints=0
for row in self.playingField:
for tile in row:
if tile=="W":
whitepoints+=1
if tile=="B":
blackpoints+=1
self.whitePlayer.setPoints(whitepoints)
self.blackPlayer.setPoints(blackpoints)
def moveIsValid(self, position, playerColor):
if self.playingField[position[0]][position[1]] == 0:
return True
else:
return False
def placeTile(self,position,playerColor):
if self.moveIsValid(position, playerColor):
self.playingField[position[0]][position[1]] = playerColor
return True
else:
return False
GameController(8)