def isBetterState(gameBoard1, gameBoard2, playerTurn): #return compare(gameBoard1, gameBoard2, playerTurn) isWinner, winner, pos, direction= glf.boardContainsWinner( gameBoard1, 4 ) if isWinner and winner != playerTurn: return -1, 0, 0 elif isWinner and winner == playerTurn: return 1, 100, 0 isWinner, winner, pos, direction= glf.boardContainsWinner( gameBoard2, 4 ) if isWinner and winner != playerTurn: return -1, 0, 0 elif isWinner and winner == playerTurn: return 1, 100, 0 return 0, 0, 0
def randomMovePlus(gameBoard): move= None res,winner,pos,direction= glf.boardContainsWinner(gameBoard,3) if res: move= aif.blockOpponent(gameBoard, pos, direction) else: move= randomMove(gameBoard) if not move: return randomMove(gameBoard) else: return move
def scoreTreeWithSeqCellsPlus( self, playerTurn ): #print "start scoring tree" opponent= getOpponent( playerTurn ) for boardNode in self.leafNodes: #print "start sequentialCellsPlus" sequentialCells= glf.getSequentialCellsPlus( boardNode.board, 4 ) #print "end sequentialCellsPlus" myCells= sequentialCells[playerTurn] oppCells= sequentialCells[ opponent ] #print "start boardContainswinner" winnerFound, winnerPlayerID, _, _= glf.boardContainsWinner( boardNode.board, 4 ) #print "end boardContainsWinner" if winnerFound and winnerPlayerID == opponent: score= len(myCells) - len(oppCells) - 1000.0 elif winnerFound and winnerPlayerID == playerTurn: score= len(myCells) - len(oppCells) + 100.0 else: score= len(myCells) - len(oppCells) boardNode.value= score #print "done scoring leaves" #recurse up the tree children= self.leafNodes parents= self.getPriorGen( children ) while parents != [None]: for parentNode in parents: childrenOfParent= self.structure[parentNode] player= childrenOfParent[0].playerTurn childrenValues= [] for child in childrenOfParent: childrenValues.append( child.value ) if player == playerTurn: #get maximum parentNode.value= max( childrenValues ) else: #get minimum parentNode.value= min( childrenValues ) children= parents parents= self.getPriorGen( children )