class Environment(object): def __init__(self): self.S = TwentyFortyEight() self.S.make_tables() self.score = 0 self.S.new_tile() print("New Episode") def reset(self): self.S.score = 0 self.S.cells = 0 self.score = 0 self.S.new_tile() return self.S.vectorize_state() def step(self, action): score_prev = self.S.score1() cells = self.S.cells self.S.move(action + 1) r = self.S.score1() - score_prev self.score = self.S.score1() if not cells == self.S.cells: self.S.new_tile() if not self.S.canMove(): return self.S.vectorize_state(), r, True return self.S.vectorize_state(), r, False def seed(self, a): return
def monte_play(game_state): ans=0.0 numRuns = 10 if(game_state.maxValue() >= 16): numRuns = 20 elif(game_state.maxValue() >= 32): numRuns = 50 elif(game_state.maxValue() >= 256): numRuns = 75 elif(game_state.maxValue() >= 512): numRuns = 120 for i in range(numRuns): # print(i) tmp=TwentyFortyEight() tmp.cells=game_state.cells tmp.score=game_state.score count = 0 while(tmp.canMove()): # count += 1 # print(count) dir=random.choice([1,2,3,4]) tmp.move(dir) tmp.new_tile() #print(game_state.cells) ans+=tmp.score1() ans /= numRuns return ans
def run_2048_move_test(): """ Checks if the grid has been redrawn correctly after a move command """ # suite = TestSuite() o_game_board = TwentyFortyEight(4, 6) o_game_board.set_tile(0, 0, 2) o_game_board.set_tile(1, 0, 2) o_game_board.set_tile(1, 3, 2) o_game_board.set_tile(3, 3, 4) print o_game_board o_game_board.move(2) # o_game_board.move(3) # o_game_board.move(1) print o_game_board row1 = o_game_board.arr_grid[0].count(4) row2 = o_game_board.arr_grid[1].count(0) if row1 != 1: print "row1 != 4 test failed" # print row1 if row2 != 2: print "row2 != 0 test failed"
tempx[0, j*16 + temp[j]] = 1 probs=y.eval(feed_dict={x1: tempx})[0] # print(probs) # dir = eminimax(x,2) # dir=minimax_alpha_beta(x,6) # dir=monte_carlo(x) avail_moves = x.get_available_moves() if(len(avail_moves)==0): break # print(avail_moves) bestmove = avail_moves[0]-1 for j in avail_moves: # print(j) if(probs[j-1]>probs[bestmove]): bestmove = j-1 dir = bestmove+1 # print("Score:"+str(x.score)+"\t Max Tile:"+str(x.maxValue())) x.move(dir) x.new_tile() # print("GAME ENDs") # print(x.maxValue()) print("Score:"+str(x.score)+"\t Max Tile:"+str(x.maxValue())) occ[int(math.log(x.maxValue(), 2))] += 1 # print(occ) # x.__str__() # print(x.get_available_moves()) for i in range(16): print(1<<i, " : ", occ[i])