def alg(Game, random_repeats, max_depth): best_move = 0 best_avg_score = 0 score_to_add = 0 for i in range(0, 4): Temp_game = game_logic.Board_game_2048() Temp_game.board = Game.board temp_avg_score = [] depth = 0 if (game_logic.main_loop(Temp_game.board, i)[0] == False): continue for repeat in range(0, random_repeats): temp_score = 0 while (game_logic.main_loop(Temp_game.board, 0)[3] == False): depth = depth + 1 for j in range(0, 4): temp_game_state = game_logic.main_loop(Temp_game.board, j) Temp_game.board = temp_game_state[1] temp_score += temp_game_state[2] if (depth == max_depth): depth = 0 temp_avg_score.append(temp_score) Temp_game.board = Game.board break if (len(temp_avg_score) > 0 and np.mean(temp_avg_score) > best_avg_score): best_avg_score = temp_score best_move = i temp_score = 0 print("Best move: " + str(best_move) + " with score: " + str(best_avg_score)) print(Game.board) return best_move
def get_best_move(alg_board): empty_cells = [] for x in range(0, 4): for y in range(0, 4): if (alg_board[x][y] == 0): empty_cells.append(0) test_depth = len(empty_cells) depth_map = [5, 5, 5, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2] depth = depth_map[test_depth] #depth = 5 score = 0 best_move = -1 for i in range(0, 4): temp_board = [] temp_board = array(alg_board) if (game_logic.main_loop(temp_board, i)[0] == False): print("NOT A LEGAL MOVE: " + str(i)) continue else: temp_board = game_logic.main_loop(alg_board, i, 0)[1] print("LEGAL MOVE: " + str(i)) current_score = expectimax(temp_board, depth - 1, "board") print("OH SHIE:" + str(current_score) + ">" + str(score) + " for move: " + str(i)) if (current_score > score): best_move = i score = current_score print("BEST MOVE: " + str(best_move) + " score: " + str(score)) return best_move
def getScore(board, depth): score = 0 if (depth == 0): for j in range(0, 4): for k in range(0, 4): score += weight_board[j][k] * board[j][k] return score empty_cells = [] for x in range(0,4): for y in range(0,4): if(board[x][y] == 0): empty_cells.append(0) for i in range(0,len(empty_cells)+1): temp_board = game_logic.fill_cell(board,2) for i in range(0, 4): if(game_logic.main_loop(temp_board,i)[0] == True): temp_board = game_logic.main_loop(board,i,0)[1] score += getScore(temp_board, depth-1) if (len(empty_cells) > 0): score = score/len(empty_cells) score += len(empty_cells) * 10 return score
def getMove(board, depth): score = 0 best_move = 0 false_counter = 0 empty_cells = [] for x in range(0, 4): for y in range(0, 4): if (board[x][y] == 0): empty_cells.append(0) test_depth = len(empty_cells) depth_map = [6, 5, 5, 5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3] depth = depth_map[test_depth] for i in range(0, 4): if (game_logic.main_loop(board, i)[0] == True): temp_board = game_logic.main_loop(board, i)[1] temp_score = getScore(temp_board, depth, 0) if (temp_score > score): best_move = i score = temp_score else: false_counter += 1 if (false_counter == 4): return -1 return best_move
def auto_alg(): init_game() best_move = -2 game_over = False depth = 4 start_timer = 0 end_timer = 0 timer_array = [] times_2048 = 0 while (not (game_over)): start_timer = timer() temp_board = [] temp_board = Game.board empty_cells = [] false_counter = 0 for x in range(0, 4): for y in range(0, 4): if (temp_board[x][y] == 0): empty_cells.append(0) test_depth = len(empty_cells) depth_map = [9, 9, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 5, 4, 4, 4] depth = depth_map[test_depth] best_move = minimax.minimax(temp_board, depth, "PLAYER", -np.inf, np.inf)[0] Game.board = game_logic.main_loop(temp_board, best_move)[1] print("New board with move: " + str(best_move)) print(Game.board) for i in range(0, 4): if (game_logic.main_loop(temp_board, i)[0] != True): false_counter += 1 if (false_counter == 4): print("NOOOOO GAME OVAAAHHR") game_over = True end_timer = timer() print("TIME ELAPSED THIS MOVE:") print(end_timer - start_timer) print("-") timer_array.append(end_timer - start_timer) print("TOTAL TIME ELAPSED:") print(sum(timer_array)) print("AVERAGE TIME EACH MOVE:") print(np.mean(timer_array)) for x in range(0, 4): for y in range(0, 4): if (Game.board[x][y] >= 2048): times_2048 += 1 return np.mean(timer_array), sum(timer_array), times_2048
def getScore(board, depth, score): if (depth == 0): score = 0 for j in range(0, 4): for k in range(0, 4): score += weight_board[j][k] * board[j][k] return score for i in range(0, 4): if (game_logic.main_loop(board, i)[0] == True): temp_board = game_logic.main_loop(board, i)[1] temp_score = getScore(temp_board, depth - 1, score) if (temp_score > score): score = temp_score return score
def minimax(board, depth, agent, alpha, beta): best_score = 0 best_move = -1 #print(depth) if (depth == 0): #print(board) return (best_move, weighted_table.getScore(board, 0, 0)) else: if (agent == "PLAYER"): #min best_score = -np.inf for i in range(0, 4): if (game_logic.main_loop(board, i)[0] == False): continue temp_board = game_logic.main_loop(board, i)[1] current_score = minimax(temp_board, depth - 1, "COMPUTER", alpha, beta)[1] if (current_score > best_score): alpha = max(alpha, best_score) best_score = current_score best_move = i if (beta <= alpha): break elif (agent == "COMPUTER"): #max best_score = np.inf empty_cells = [] for x in range(0, 4): for y in range(0, 4): if (board[x][y] == 0): empty_cells.append(0) if (len(empty_cells) == 0): best_score = 0 for i in range(0, len(empty_cells)): temp_board = game_logic.fill_cell(board) current_score = minimax(temp_board, depth - 1, "PLAYER", alpha, beta)[1] current_score += len(empty_cells) * 10 if (current_score < best_score): beta = min(beta, best_score) best_score = current_score if (beta <= alpha): break #print(best_move) return (best_move, best_score)
def auto_random(board, times): moves = 0 score = 0 stop = False for x in range(0, times): false_counter = 0 for i in range(0, 4): if (game_logic.main_loop(board, i)[3] == True): print("game over, with " + str(moves) + " moves and " + str(score) + " score") print(board) return state = game_logic.main_loop(board, i) score = score + state[2] board = state[1] if (game_logic.main_loop(board, i)[0] == True): moves = (moves + 1)
def expectimax(alg_board, depth, agent): if (depth == 0): return weighted_table.getScore(alg_board, 0, 0) elif (agent == "player"): score = 0 for i in range(0, 4): if (game_logic.main_loop(alg_board, i)[0] == False): continue new_board = game_logic.main_loop(alg_board, i, 0)[1] current_score = expectimax(new_board, depth - 1, "board") if (current_score > score): score = current_score return score elif (agent == "board"): score = 0 empty_cells = [] for x in range(0, 4): for y in range(0, 4): if (alg_board[x][y] == 0): empty_cells.append(0) for i in range(0, len(empty_cells) + 1): #4 temp_board = array(alg_board) temp_board = game_logic.fill_cell(temp_board, 4) temp_score = expectimax(temp_board, depth - 1, "player") score += (0.1 * temp_score) #2 temp_board = array(alg_board) temp_board = game_logic.fill_cell(temp_board, 2) temp_score = expectimax(temp_board, depth - 1, "player") score += (0.9 * temp_score) if (len(empty_cells) > 0): score = score / len(empty_cells) return score
def auto_smart_move(): timer_array = [] times_2048 = 0 game_over = False init_game() while (game_over == False): start_timer = 0 end_timer = 0 start_timer = timer() move = weighted_table.getMove(Game.board, 4) if (move == -1): break state = game_logic.main_loop(Game.board, move) Game.board = state[1] Game.score = Game.score + state[2] print Game.board end_timer = timer() print("TIME ELAPSED THIS MOVE:") print(end_timer - start_timer) print("-") timer_array.append(end_timer - start_timer) print Game.board print Game.score print("Game over!") for x in range(0, 4): for y in range(0, 4): if (Game.board[x][y] >= 2048): times_2048 += 1 return np.mean(timer_array), sum(timer_array), times_2048
def move(direction): Game.board = game_logic.main_loop(Game.board, direction)[1] print Game.board