def expectimax(board, depth, maxing, move): if depth == 0: return snake_strategy(board), move if maxing: score = (0, move) for c in board.possible_moves(): temp = getBoardCopy(board) temp.make_move(c) val = expectimax(temp, depth - 1, False, move)[0] if val >= score[0]: score = (val, c) return score if not maxing: score = 0 tempGrid = getBoardCopy(board).export_state() temp = [[(x, y) for y, col in enumerate(row) if col == None] for x, row in enumerate(tempGrid)] rcList = reduce(lambda x, y: x + y, temp) for cell in rcList: temp = getBoardCopy(board).export_state() temp[cell[0]][cell[1]] = 1 score += 0.8 * expectimax(Board(temp), depth - 1, True, move)[0] temp = getBoardCopy(board).export_state() temp[cell[0]][cell[1]] = 2 score += 0.2 * expectimax(Board(temp), depth - 1, True, move)[0] return score / (len(rcList)), move
def maxVal(board, depth, player, move): score = 0 for i in board.possible_moves(): x = board.export_state() tempBoard = Board(x) tempBoard.make_move(i) currentScore = expectimax(tempBoard, depth - 1, 2, i) if currentScore[0] > score: score = currentScore[0] move = currentScore[1] return score, move
def pickMove(board): move = None topScore = 0 for i in board.possible_moves(): x = board.export_state() temp = Board(x) temp.make_move(i) x = expectimax(temp, 4, 2, i) if x[0] > topScore: topScore = x[0] move = x[1] return move
def main(): board = Board() board.add_random_tiles(2) print("main code") move_counter = 0 move = None move_result = False while True: print( "Number of successful moves:{}, Last move attempted:{}:, Move status:{}" .format(move_counter, move, move_result)) print(board) key = getchar() if key == b'q' or key == 'q': quit() if key == b'w' or key == 'w': move = 'UP' elif key == b'a' or key == 'a': move = 'LEFT' elif key == b's' or key == 's': move = 'DOWN' elif key == b'd' or key == 'd': move = 'RIGHT' else: move = None if move is not None: move_result = board.make_move(move) if move_result: add_tile_result = board.add_random_tiles(1) move_counter = move_counter + 1
def main(): driver = webdriver.Chrome( executable_path='C:/Program Files (x86)/chromedriver.exe') driver.get('https://play2048.co/') body = driver.find_element_by_tag_name('body') board = Board() key_map = { 'UP': Keys.ARROW_UP, 'RIGHT': Keys.ARROW_RIGHT, 'DOWN': Keys.ARROW_DOWN, 'LEFT': Keys.ARROW_LEFT } board.grid = update_grid(driver=driver) while True: begin = time.time() move = pickMove(board) next_key = key_map[move] body.send_keys(next_key) print(move, " at a time of ", (time.time() - begin)) board.grid = update_grid(driver=driver)
def main(): # allmoves = ['UP','LEFT','DOWN','RIGHT'] board = Board() board.add_random_tiles(2) print("main code") move_counter = 0 move = None move_result = False overalltime = time.time() while True: print( "Number of successful moves:{}, Last move attempted:{}:, Move status:{}" .format(move_counter, move, move_result)) print(board) # print(board.print_metrics()) if board.possible_moves() == []: if (board.get_max_tile()[0] < 2048): print("You lost!") else: print("Congratulations - you won!") break begin = time.time() ###################################### Your code should be inserted below ###################################### (feel free to define additional functions to determine the next move) # move = board.possible_moves()[random.randint(0,len(board.possible_moves())-1)] move = determine_next_move(board) board.make_move(move) ###################################### Do not modify 4 lines below ###################################### print("Move time: ", time.time() - begin) board.add_random_tiles(1) move_counter = move_counter + 1 print("Average time per move:", (time.time() - overalltime) / move_counter)
def main(): # Initialize pygame pygame.init() game = Game() board = Board() board.add_random_tiles(2) game.update_tiles(Game.convert_grid(board.grid)) game.draw_tiles() pygame.display.flip() move_counter = 0 move = None move_result = False # Variable to keep the main loop running running = True # Main loop while running: # Look at every event in the queue for event in pygame.event.get(): # Did the user hit a key? if event.type == KEYDOWN: # Was it the Escape key? If so, stop the loop. if event.key == K_ESCAPE: running = False else: if event.key == K_UP: move = 'UP' elif event.key == K_LEFT: move = 'LEFT' elif event.key == K_DOWN: move = 'DOWN' elif event.key == K_RIGHT: move = 'RIGHT' else: move = None if move is not None: move_result = board.make_move(move) if move_result: add_tile_result = board.add_random_tiles(1) move_counter = move_counter + 1 game.update_tiles(Game.convert_grid(board.grid)) game.draw_tiles() pygame.display.flip() # Did the user click the window close button? If so, stop the loop. elif event.type == QUIT: running = False
def determine_next_move(board): limit = 2000 sums = [0, 0, 0, 0] # UP DOWN LEFT RIGHT counts = [0, 0, 0, 0] for i in range(0, limit): newBoard = Board(board.export_state(), board.score, board.merge_count) result = play_random_game(newBoard) if result[0] == "UP": sums[0] += result[1] counts[0] += 1 elif result[0] == "DOWN": sums[1] += result[1] counts[1] += 1 elif result[0] == "LEFT": sums[2] += result[1] counts[2] += 1 elif result[0] == "RIGHT": sums[3] += result[1] counts[3] += 1 averages = [0, 0, 0, 0] if (counts[0] != 0): averages[0] = sums[0] / counts[0] if (counts[1] != 0): averages[1] = sums[1] / counts[1] if (counts[2] != 0): averages[2] = sums[2] / counts[2] if (counts[3] != 0): averages[3] = sums[3] / counts[3] maxAverage = averages[0] move = "UP" if averages[1] > maxAverage: maxAverage = averages[1] move = "DOWN" if averages[2] > maxAverage: maxAverage = averages[2] move = "LEFT" if averages[3] > maxAverage: maxAverage = averages[3] move = "RIGHT" return move
def expVal(board, depth, player, move): totalScore = 0 emptyCells = board.empty() for i in emptyCells: x = board.export_state() tempBoard = Board(x) fourTile = Tile(2) tempBoard.grid[i[1]][i[0]] = fourTile currentScore = expectimax(tempBoard, depth - 1, 1, move) totalScore += (0.2 * currentScore[0]) tempBoard = Board(x) twoTile = Tile(1) tempBoard.grid[i[1]][i[0]] = twoTile currentScore = expectimax(tempBoard, depth - 1, 1, move) totalScore += (0.8 * currentScore[0]) average = totalScore / len(emptyCells) return average, move
def getBoardCopy(board): copy = board.export_state() return Board(copy)