def start(self, firstPlayer): self.GameState = 0 EventManager.notify("GameStarting") # move counter init self.moveCounter = 0 # generate fresh state self.HexBoard = HexBoard(self.size[0], self.size[1]) self.HexBoard.setReferenceToGame(self) # current player depending on decision self._currentPlayer = firstPlayer if self.mode == "ki": self.KI = MonteCarloTreeSearch(self, 2) # TODO: ki_player_id == 2? # self.KI = HexKI(self.size[0], self.size[1]) if self.mode == "inter" or self.mode == "machine": self.KI = [] self.KI.append(HexKI(self.size[0], self.size[1])) self.KI.append(HexKI(self.size[0], self.size[1])) self._currentPlayerType = "ki" # if random number wanted, generate one if firstPlayer == 0: self.chooseFirst() EventManager.notify("GameStarted")
def receiveMove(self, move): # first store the last move self.lastMove = move # get the vertex the move pointed on vertex = self.getVertex(move[1], move[0]) # mark it vertex.player = self.Game.currentPlayer() # first add it to a new group (later on merge them) vertex.group = self._groupCounter # increment the group counter to avoid conflicts self._groupCounter = self._groupCounter +1 # the following lines manipulate the groups of # vertices at the border of the gameboard # red: left 0, right -1 # blue: top 0, right -1 if self.Game.currentPlayer() == 1: if move[1] == 0: vertex.group = 0 if move[1] == self.size[0]-1: vertex.group = -1 else: if move[0] == 0: vertex.group = 0 if move[0] == self.size[1]-1: vertex.group = -1 # get the adjacent vertices to that one, which is marked adjVertices = self.getSurroundingVertices(move[1], move[0]) # any neightbours?: if len(adjVertices) > 0: # put the marked one to the list adjVertices.append(vertex) # only concentrate on the groups groups = [x.group for x in adjVertices] # WIN Condition # either within the gameboard # or the last vertex marked has been at the borders if (-1 in groups and 0 in groups): EventManager.notify("GameFinished") # get the minimum group minGroup = min(groups) # set all neighbours to the minimum of the group for key, value in self.Vertices.items(): if value.group in groups: value.group = minGroup
def receiveMove(self, move): # first store the last move self.lastMove = move # get the vertex the move pointed on vertex = self.getVertex(move[1], move[0]) # mark it vertex.player = self.Game.currentPlayer() # first add it to a new group (later on merge them) vertex.group = self._groupCounter # increment the group counter to avoid conflicts self._groupCounter = self._groupCounter + 1 # the following lines manipulate the groups of # vertices at the border of the gameboard # red: left 0, right -1 # blue: top 0, right -1 if self.Game.currentPlayer() == 1: if move[1] == 0: vertex.group = 0 if move[1] == self.size[0] - 1: vertex.group = -1 else: if move[0] == 0: vertex.group = 0 if move[0] == self.size[1] - 1: vertex.group = -1 # get the adjacent vertices to that one, which is marked adjVertices = self.getSurroundingVertices(move[1], move[0]) # any neightbours?: if len(adjVertices) > 0: # put the marked one to the list adjVertices.append(vertex) # only concentrate on the groups groups = [x.group for x in adjVertices] # WIN Condition # either within the gameboard # or the last vertex marked has been at the borders if (-1 in groups and 0 in groups): EventManager.notify("GameFinished") # get the minimum group minGroup = min(groups) # set all neighbours to the minimum of the group for key, value in self.Vertices.items(): if value.group in groups: value.group = minGroup
def onUITick(self): if not self.Game._pause: # notify subscripts EventManager.notify("UITick") # redo this step every nth seconds self.tkInstance.after(200, self.onUITick)
def openPage(self, page): if page == "menu": self._GUIMenu.show() self._GUIGameBoard.hide() elif page == "game": self._GUIMenu.hide() self._GUIGameBoard.show() EventManager.notify("GameUILoaded")
def makeMove(self, move): # Notify EventManager.notify("MoveBegan") # if already marked dont do anything if self.HexBoard.isMarked(move[1], move[0]): EventManager.notify("MoveDenied") else: # otherwise count the click self.moveCounter = self.moveCounter + 1 # notify Model self.HexBoard.receiveMove(move) # notify View self.changePlayer() EventManager.notify("PlayerChanged") if not self.isPlayerHuman(): move = self.KI.getMove() self.makeMove(move) EventManager.notify("MoveFinished")
def makeMove(self, move): # Notify EventManager.notify("MoveBegan") # if already marked dont do anything if self.HexBoard.isMarked(move[0], move[1]): EventManager.notify("MoveDenied") assert False else: # otherwise count the click self.moveCounter = self.moveCounter + 1 # notify Model self.HexBoard.receiveMove(move) if self.mode == "inter": self.KI[0].receiveMove(move) self.KI[1].receiveMove(move) elif self.mode == "ki": self.KI.receiveMove(move) # notify View self.changePlayer() EventManager.notify("PlayerChanged") EventManager.notify("MoveFinished")
def openPage(self, page): if page == "splash": self._GUIMenu.hide() self._GUIGameBoard.hide() self._GUISplashScreen.show() elif page == "menu": self._GUIMenu.show() self._GUIGameBoard.hide() self._GUISplashScreen.hide() elif page == "game": self._GUIMenu.hide() self._GUIGameBoard.show() self._GUISplashScreen.hide() EventManager.notify("GameUILoaded")
def start(self, firstPlayer): EventManager.notify("GameStarting") # move counter init self.moveCounter = 0 # generate fresh state self.HexBoard = HexBoard(self.size[0], self.size[1]) self.HexBoard.setReferenceToGame(self) # current player depending on decision self._currentPlayer = firstPlayer # if random number wanted, generate one if firstPlayer == 0: self.chooseFirst() EventManager.notify("GameStarted")
def onGameFinished(self): EventManager.notify("GameFinished") self._finished = True
def onGameFinished(self): if not self._eventsSuppressed: EventManager.notify("GameFinished") self._finished = True