def valueOfMove(self, board, move):
     board = self.game.preRotate(move, board)
     testGame = GameState()
     testGame.setBoard(board)
     testGame.setBoard(testGame.copyArr())
     value = 0
     
     # store previous information
     oldScore = testGame.getScore()
     oldMaxTile = testGame.getMaxTile()
     
     testGame.executeMove(Move.down)
     newScore = testGame.getScore()
     value += newScore - oldScore
     
     # check if the largest tile is in a corner after the move
     newMaxTile = testGame.getMaxTile()
     for corner in self.fourCorners:
         if testGame.gameArray[corner[0]][corner[1]] == newMaxTile:
             value *= self.maxInCornerMultiplier
             value += self.cornerBonusScaledByMax * newMaxTile
             value += self.enterCorner
     
     # penalty for moving down
     if move == Move.down:
         value -= self.moveDownPenalty * newMaxTile
     
     board = self.game.postRotate(move, board)
     return value
Ejemplo n.º 2
0
 def estimateValue(self, depth, board, move):
     ev2 = [[0 for x in range(4)] for x in range(4)]
     ev4 = [[0 for x in range(4)] for x in range(4)]
     ev = 0
     numOptions = 0
     
     # go through all options
     if(self.game.isValid(move)): 
         baseGameUp = GameState()
         baseGameUp.setBoard(board)
         baseGameUp.executeMove(move)
         
         for x in range (0, 4):
             for y in range (0, 4):
                 if(baseGameUp.gameArray[x][y] == 0):
                     numOptions += 2
                     
                     # per cell ev expecting 4
                     pretendBoard2 = baseGameUp.copyArr()
                     pretendGame2 = GameState()
                     pretendBoard2[x][y] = 2
                     pretendGame2.setBoard(pretendBoard2)
                     searchEv = self.startSearch(pretendGame2.copyArr(), depth - 1)
                     ev2[x][y] = searchEv[1]
                     pretendGame4 = None
                 
                     # per cell ev expecting 4
                     pretendBoard4 = baseGameUp.copyArr()
                     pretendGame4 = GameState()
                     pretendBoard4[x][y] = 4
                     pretendGame4.setBoard(pretendBoard4)
                     searchEv = self.startSearch(pretendGame4.copyArr(), depth - 1)
                     ev4[x][y] = searchEv[1]
                     pretendGame4 = None
                 
                     pass
     return ev
Ejemplo n.º 3
0
 def searchDirection(self, board, depth, move):
     testGame = GameState()
     testGame.setBoard(board)
     testGame.setBoard(testGame.copyArr())
     
     # if the move isn't valid, don't consider it
     if (not testGame.isValid(move)):
         return -1
     
     # determine the value for making the move at this level
     ourValue = self.valueOfMove(testGame.gameArray, move)
     
     # if we have reached bottom depth, stop searching and return the heuristic value
     if depth == 1:
         return ourValue
     
     # using that as the starting board, check a lot of possibilities
     afterMove = testGame.executeMove(move)
     testGame.setBoard(afterMove)
     ev2 = [[0 for x in range(4)] for x in range(4)]
     ev4 = [[0 for x in range(4)] for x in range(4)]
     
     options = 0
     searchValue = 0
     
     # determine the value of each cell
     for x in range (0, 4):
         for y in range (0, 4):
             trialBoard = testGame.copyArr()
             if (trialBoard[x][y] == 0):
                 options += 1
                 
                 trialBoard[x][y] = 2
                 ev2[x][y] = self.search(trialBoard, depth - 1)[1]
                 
                 trialBoard[x][y] = 4
                 ev4[x][y] = self.search(trialBoard, depth - 1)[1]
                 trialBoard[x][y] = 0
     
     
     # adjust those cells for their likelihood
     for x in range (0, 4):
         for y in range (0, 4):
             searchValue += (ev2[x][y] * 0.9) / options
             searchValue += (ev4[x][y] * 0.1) / options
     
     return ourValue + searchValue
 def searchDirection(self, board, depth, move):
     testGame = GameState()
     testGame.setBoard(board)
     testGame.setBoard(testGame.copyArr())
     
     # if the move isn't valid, don't consider it
     if (not testGame.isValid(move)):
         return -1
     
     # determine the value for making the move at this level
     ourValue = self.valueOfMove(testGame.gameArray, move)
     
     # using that as the starting board, check the child's options
     afterMove = testGame.executeMove(move)
     searchValue = self.search(afterMove, depth - 1)[1]
     
     return ourValue + searchValue
 def searchDirection(self, board, likelihood, move, depth):
     testGame = GameState()
     testGame.setBoard(board)
     testGame.setBoard(testGame.copyArr())
     
     # if the move isn't valid, don't consider it
     if (not testGame.isValid(move)):
         return -1
     
     #determine the value for making the move at this level
     ourValue = self.valueOfMove(testGame.gameArray, move)
     
     #'using that as the starting board, check a lot of possibilities'
     afterMove = testGame.executeMove(move)
     testGame.setBoard(afterMove)
     ev2 = [[0 for x in range(4)] for x in range(4)]
     ev4 = [[0 for x in range(4)] for x in range(4)]
     
     options = 0
     searchValue = 0
     
     # determine which cells can have a new tile
     trialBoard = testGame.copyArr()
     for x in range (0, 4):
         for y in range (0, 4):
             if (trialBoard[x][y] == 0):
                 options += 1
     
     # determine the value of each cell
     for x in range (0, 4):
         for y in range (0, 4):
             trialBoard = testGame.copyArr()
             if (trialBoard[x][y] == 0):
                 cellChance = likelihood / options
                 
                 trialBoard[x][y] = 2
                 ev2[x][y] = cellChance * 0.9 * self.search(trialBoard, likelihood * cellChance * 0.9, depth)[1]
                 trialBoard[x][y] = 0
                 
                 trialBoard[x][y] = 4
                 ev4[x][y] = cellChance * 0.9 * self.search(trialBoard, likelihood * cellChance * 0.1, depth)[1]
                 trialBoard[x][y] = 0
     
     return ourValue + searchValue
Ejemplo n.º 6
0
    def searchDirection(self, board, depth, move):
        testGame = GameState()
        testGame.setBoard(board)
        testGame.setBoard(testGame.copyArr())

        # if the move isn't valid, don't consider it
        if not testGame.isValid(move):
            return -1

        # determine the value for making the move at this level
        ourValue = self.valueOfMove(testGame.gameArray, move)

        #'using that as the starting board, check a lot of possibilities'
        afterMove = testGame.executeMove(move)
        testGame.setBoard(afterMove)

        options = 0
        searchValue = 0

        # arrays
        ev2 = [[0 for x in range(2)] for x in range(2)]
        ev4 = [[0 for x in range(2)] for x in range(2)]
        optionsInRegion = [[0 for x in range(2)] for x in range(2)]

        # determine value of the region
        for x in range(0, 2):  # location of region
            for y in range(0, 2):
                trialBoard = testGame.copyArr()
                validLocs = []
                for rx in range(0, 2):  # location within region
                    for ry in range(0, 2):
                        locX = (x * 2) + rx
                        locY = (y * 2) + ry

                        # take all 0s into account for likelihood
                        if trialBoard[locX][locY] == 0:
                            optionsInRegion[x][y] += 1
                            options += 1
                            validLocs += [(locX, locY)]

                # find a cell in the region to test
                possibilities = optionsInRegion[x][y]
                for loc in validLocs:
                    rand = randrange(0, 100)

                    # randomly select one cell in the region to care about
                    if rand < ((1 / possibilities) * 100):  # test whether or not this is the cell we care about
                        trialBoard[loc[0]][loc[1]] = 2
                        ev2[x][y] = self.search(trialBoard, depth - 1)[1]

                        trialBoard[loc[0]][loc[1]] = 4
                        ev4[x][y] = self.search(trialBoard, depth - 1)[1]
                        trialBoard[loc[0]][loc[1]] = 0
                        break
                    possibilities -= 1

                # reset our locations for the next region
                validLocs.clear()

        # adjust those region evs for their likelihood
        for x in range(0, 2):
            for y in range(0, 2):
                searchValue += (ev2[x][y] * 0.9) * (optionsInRegion[x][y] / options)
                searchValue += (ev4[x][y] * 0.1) * (optionsInRegion[x][y] / options)

        return ourValue + searchValue