def movementPossible(self, playScreen): fields = playScreen.playFields for row in self._pieces: for piece in row: if piece is not None: step = piece.steps + 1 index = Utils.getFieldIndex(piece, fields) if index is not None: # Piece has died, apparently y = index[0] x = index[1] # Up for up in range(y, y + step if y + step <= len(fields) else len(fields)): if Utils.isLegalMove(fields[y][x], fields[up][x], fields): if ( fields[up][x].piece is None or fields[up][x].piece is not None and fields[up][x].piece.owner is not piece.owner ): return True # Right for right in range(x, x + step if x + step <= len(fields) else len(fields)): if Utils.isLegalMove(fields[y][x], fields[y][right], fields): if ( fields[y][right].piece is None or fields[y][right].piece is not None and fields[y][right].piece.owner is not piece.owner ): return True # Left for left in range(x - step if x - step > 0 else 0, x): if Utils.isLegalMove(fields[y][x], fields[y][left], fields): if ( fields[y][left].piece is None or fields[y][left].piece is not None and fields[y][left].piece.owner is not piece.owner ): return True # Down for down in range(y - step if y - step > 0 else 0, y): if Utils.isLegalMove(fields[y][x], fields[down][x], fields): if ( fields[down][x].piece is None or fields[down][x].piece is not None and fields[down][x].piece.owner is not piece.owner ): return True return False
def handleClick(self, field): #if the currentPlayer is a computer, ignore all of this. Also ignore if lockdown is true if self.currentPlayer.isComputer or self.lockdown: return #Firstclick: check if no piece has been selected before and if there is a piece there, and if this piece is his. If all of this is true, then check if he clicked a piece which can move. if self.firstSelected is None and field.piece is not None: if field.piece.steps > 0 and field.piece.owner is self.currentPlayer: self.firstSelected = field elif self.firstSelected is not None: if field is self.firstSelected: self.firstSelected = None elif field.piece is not None and field.piece.owner is self.currentPlayer: pass elif Utils.isLegalMove(self.firstSelected, field, self.playFields): #if the move is legal, execute it self.executeMove(self.firstSelected, field)
def executeMove(self,source, target): #check if its a legal move if not Utils.isLegalMove(source, target, self.playFields) or self.lockdown: return False #If the target is an empty field, do it instantly if target.piece is None: target.piece = source.piece source.piece = None #If its an AI, delay the game for a bit if self.currentPlayer.isComputer: self.AIDelayTimer = threading.Timer(self.aiDelay,self._changePlayerTurn) self.AIDelayTimer.start() else: self._changePlayerTurn() else: target.piece.hidden = False source.piece.hidden = False self.lockdown = True self.lockdownTimer = threading.Timer(self.lockdownTime, self._onLockDownFinish,[source,target]) self.lockdownTimer.start() return True
def play(self, playScreen): self.isPlaying = True fields = playScreen.playFields # Choose own move playableMoves = {} key = 0 for row in self._pieces: for piece in row: if piece is not None: step = piece.steps + 1 index = Utils.getFieldIndex(piece, fields) if index is not None: # Piece has died, apparently y = index[0] x = index[1] # Up for up in range(y, y + step if y + step <= len(fields) else len(fields)): if Utils.isLegalMove(fields[y][x], fields[up][x], fields): if ( fields[up][x].piece is None or fields[up][x].piece is not None and fields[up][x].piece.owner is not piece.owner ): playableMoves[key] = moveDTO(x, y, x, up) key += 1 # Right for right in range(x, x + step if x + step <= len(fields) else len(fields)): if Utils.isLegalMove(fields[y][x], fields[y][right], fields): if ( fields[y][right].piece is None or fields[y][right].piece is not None and fields[y][right].piece.owner is not piece.owner ): playableMoves[key] = moveDTO(x, y, right, y) key += 1 # Left for left in range(x - step if x - step > 0 else 0, x): if Utils.isLegalMove(fields[y][x], fields[y][left], fields): if ( fields[y][left].piece is None or fields[y][left].piece is not None and fields[y][left].piece.owner is not piece.owner ): playableMoves[key] = moveDTO(x, y, left, y) key += 1 # Down for down in range(y - step if y - step > 0 else 0, y): if Utils.isLegalMove(fields[y][x], fields[down][x], fields): if ( fields[down][x].piece is None or fields[down][x].piece is not None and fields[down][x].piece.owner is not piece.owner ): playableMoves[key] = moveDTO(x, y, x, down) key += 1 if len(playableMoves) > 0: # -1 = Don't do anything # 0 = Random # 1 = Enemy in range # 2 = Known enemy in range # 3 = Weaker known enemy in range movesScore = {} for move in playableMoves.keys(): movesScore[move] = 0 sourceY = playableMoves[move].sourceY sourceX = playableMoves[move].sourceX targetY = playableMoves[move].targetY targetX = playableMoves[move].targetX sourcePiece = fields[sourceY][sourceX].piece targetPiece = fields[targetY][targetX].piece if targetPiece is not None: movesScore[move] += 1 # 1 if targetPiece in self.knownPieces: print "------------------- Target is a bomb -------------------" movesScore[move] += 1 # 2 # If target is a bomb if targetPiece.type is "B": print "------------------- Target is a bomb -------------------" if sourcePiece.type is 3: print str(sourcePiece.type) + " is dismantling bomb" movesScore[move] += 2 # 4 else: print str(sourcePiece.type) + " is not attacking bomb" movesScore[move] = -1 # If target is not a bomb else: print "------------------- Target is not a bomb -------------------" # If source is stronger than target, set highest priority if targetPiece.type < sourcePiece.type: print str(sourcePiece.type) + " is stronger than " + str(targetPiece.type) movesScore[move] += 1 # 3 # If source is a spy and target is a marshall, set highest priority elif sourcePiece.type is 1 and targetPiece.type is 10: print str(sourcePiece.type) + " is capturing " + str(targetPiece.type) movesScore[move] += 3 # 5 # Else do nothing else: print str(sourcePiece.type) + " is not doing anything" movesScore[move] = 1 print "" highestScore = max(movesScore.iteritems(), key=operator.itemgetter(1))[1] highestMoves = [] for score in movesScore: if movesScore[score] is highestScore: highestMoves.append(score) highestMove = playableMoves[choice(highestMoves)] playScreen.executeMove( fields[highestMove.sourceY][highestMove.sourceX], fields[highestMove.targetY][highestMove.targetX] ) attackedField = fields[highestMove.targetY][highestMove.targetX] if attackedField is not None: self.knownPieces.append(attackedField.piece) # Draw movement line glLineWidth(5.0) glBegin(GL_LINES) glColor3f(1, 0, 1) glVertex2f( fields[highestMove.sourceY][highestMove.sourceX].x, fields[highestMove.sourceY][highestMove.sourceX].y ) glVertex2f( fields[highestMove.targetY][highestMove.targetX].x, fields[highestMove.targetY][highestMove.targetX].y ) glEnd() glLineWidth(1.0) return