def createMapPoints(self, oBitmap, oCoordinates): height = len(oBitmap) width = len(oBitmap[0]) iFollowingPoints = 0 iMenuEnd, iMenuStart = self.detectMenuPosition(width, height, oBitmap) iStartPosition = (iMenuStart / 2, 20 + (height / 2)) height, oMenuBitmap, width = self.separateMenu(height, oBitmap, iMenuEnd, iMenuStart) iHorizontalAverage = self.calculateHorizontalAverages( height, width, oMenuBitmap) iPlayerButtonsStart, iPlayerButtonsEnd = self.findPlayerButtons( height, iHorizontalAverage) (iXLeftSeat, iYLeftSeat) = (len(oMenuBitmap[0]) / 3 + iMenuStart, iPlayerButtonsStart + 2) strColor = getColor( ColorGrabber.getPixelColor(iXLeftSeat, iYLeftSeat, True, oCoordinates)) iLeftSeat = None iRightSeat = None if (strColor == "EmptySeat" or strColor == "NonEmptySeat"): iLeftSeat = (iXLeftSeat, iYLeftSeat) (iXRightSeat, iYRightSeat) = ((len(oMenuBitmap[0]) / 3) * 2 + iMenuStart, iPlayerButtonsStart + 2) strColor = getColor( ColorGrabber.getPixelColor(iXRightSeat, iYRightSeat, True, oCoordinates)) if (strColor == "EmptySeat" or strColor == "NonEmptySeat"): iRightSeat = (iXRightSeat, iYRightSeat) for i in range(0, width): (iXLeftPlayerColor, iYLeftPlayerColor) = (iMenuStart + i, iPlayerButtonsStart - 4) strColor = getColor( ColorGrabber.getPixelColor(iXLeftPlayerColor, iYLeftPlayerColor, True, oCoordinates)) if ("NonEmptySeat" == strColor or "Player1" == strColor): iLeftPlayerColor = (iXLeftPlayerColor, iYLeftPlayerColor) break i += 4 print("Left color found!") for i in range(0, width): (iXRightPlayerColor, iYRightPlayerColor) = ((len(oMenuBitmap[0]) / 2) + iMenuStart + i, iPlayerButtonsStart - 4) strColor = getColor( ColorGrabber.getPixelColor(iXRightPlayerColor, iYRightPlayerColor, True, oCoordinates)) if ("NonEmptySeat" == strColor or "Player1" == strColor): iRightPlayerColor = (iXRightPlayerColor, iYRightPlayerColor) break i += 4 print("Right color found!") return Menu(iLeftSeat, iRightSeat, iStartPosition, iLeftPlayerColor, iRightPlayerColor, True, oCoordinates)
def checkGameStatus(oMap, tiColors): iColorIterator = 0 if isinstance(oMap, Map): for y in oMap.oMap: for element in y: oColor = getColor(tiColors[iColorIterator]) if(oColor == Colors.RedMenu.name): return True iColorIterator+=1 return False
def updateMap(oMap, tiColors, iPlayer): iColorIterator = 0 bChanged = False if isinstance(oMap, Map): for y in oMap.oMap: for element in y: oColor = getColor(treshold(tiColors[iColorIterator])) if(oColor == Colors.NonEmptySeat.name): if(element.iPlayerToken != 1): element.iPlayerToken = 1 bChanged = True elif(oColor == Colors.Black.name): if(element.iPlayerToken != 2): element.iPlayerToken = 2 bChanged = True elif(oColor == Colors.LastMove.name): print("Enemy placed token",swapPlayer(iPlayer),"at",element.iXPosition,element.iYPosition) element.iPlayerToken = swapPlayer(iPlayer) bChanged = True else: element.iPlayerToken = 0 iColorIterator+=1 return bChanged
def process(self): oMapGenerator = Settings["mapGenerator"].value self.oMap = oMapGenerator.createMap(self.oBitmap, self.oCoordinates) oMenuGenerator = Settings["menuGenerator"].value oMenuPoints = oMenuGenerator.createMapPoints(self.oBitmap, self.oCoordinates) self.iSelectedSeat = self.selectSeat(oMenuPoints) self.iPlayer = None bStarted = False self.turn = 2 bSomethingChanged = False bFirstTurn = True bSkipCheck = False botLogics = Settings["botLogics"].value while (True): if (bStarted == False and Colors.NonEmptySeat.name == getColor( getPixelColor(oMenuPoints["startPosition"][0], oMenuPoints["startPosition"][1]))): self.oMouseClicker.tryClick(oMenuPoints["startPosition"][0], oMenuPoints["startPosition"][1]) time.sleep(5) print("WAITING FOR START") elif (bStarted == False and Colors.NonEmptySeat.name == getColor( getPixelColor(oMenuPoints["startPosition"][0], oMenuPoints["startPosition"][1] + 10))): self.oMouseClicker.tryClick( oMenuPoints["startPosition"][0], oMenuPoints["startPosition"][1] + 10) time.sleep(5) print("WAITING FOR START") elif (bStarted == False): print("Game started!") bStarted = True if (1 == self.iSelectedSeat): oPlayerColor = getColor( getPixelColor(oMenuPoints["player1Color"][0], oMenuPoints["player1Color"][1])) if (oPlayerColor == Colors.Player1.name): self.iPlayer = 2 #black else: self.iPlayer = 1 #white elif (2 == self.iSelectedSeat): oPlayerColor = getColor( getPixelColor(oMenuPoints["player2Color"][0], oMenuPoints["player2Color"][1])) if (oPlayerColor == Colors.NonEmptySeat.name): self.iPlayer = 1 #black else: self.iPlayer = 2 #white else: return False print("SELECTED SIDE:", self.iPlayer) if (bStarted): print("Turn", self.turn, "Player", self.iPlayer, end='\r') self.oRefreshMap = getMapColors(self.oMap.get2DArray()) if (checkGameStatus(self.oMap, self.oRefreshMap)): bStarted = False if (botLogics.bWinningCondition): self.iWon += 1 else: self.iLost += 1 continue if (bSkipCheck): updateMap(self.oMap, self.oRefreshMap, self.iPlayer) bSkipCheck = False else: bSomethingChanged = updateMap(self.oMap, self.oRefreshMap, self.iPlayer) if (bSomethingChanged == True and self.turn != self.iPlayer): self.turn = swapPlayer(self.turn) bFirstTurn = False elif (self.turn == self.iPlayer): if (bFirstTurn): (iXClick, iYClick) = randomPosition(self.oMap) else: (iXClick, iYClick) = botLogics.getPoint(self.oMap, self.iPlayer) self.oMouseClicker.tryClick(iXClick, iYClick) self.turn = swapPlayer(self.iPlayer) bFirstTurn = False bSomethingChanged = False bSkipCheck = True if (self.isReadyToExit): break
def clickSeat(self, oClickPoint): if (Colors.EmptySeat.name == getColor( getPixelColor(oClickPoint[0], oClickPoint[1]))): self.oMouseClicker.tryClick(oClickPoint[0], oClickPoint[1]) return True return False