def moveGrid(grid, i): new = np.zeros((4, 4), dtype=np.int) if i == 0: # move up grid = np.transpose(grid) for row in range(4): new[row, :] = logic.move(grid[row, :]) new = np.transpose(new) if i == 1: # move left for row in range(4): new[row, :] = logic.move(grid[row, :]) if i == 2: # move down grid = np.transpose(grid) grid = np.fliplr(grid) for row in range(4): new[row, :] = logic.move(grid[row, :]) new = np.fliplr(new) new = np.transpose(new) if i == 3: # move right grid = np.fliplr(grid) for row in range(4): new[row, :] = logic.move(grid[row, :]) new = np.fliplr(new) return new
def main(): g = Klondike.new_game(False) print_game(g); print() game_state = 'Won' while not game_won(g): code, *instr = new_find_move(g) print(f'Code: {code!s}, instr: {instr}') if code == MOVE_CODE.DRAW: g.stock, g.pile = draw(g.stock, g.pile) elif code == MOVE_CODE.T_TO_F: g.tableaus[instr[0]], g.foundations[instr[1]] = move(1, g.tableaus[instr[0]], g.foundations[instr[1]]) elif code == MOVE_CODE.T_TO_T: g.tableaus[instr[0]], g.tableaus[instr[1]] = move(instr[2], g.tableaus[instr[0]], g.tableaus[instr[1]]) elif code == MOVE_CODE.P_TO_F: g.pile, g.foundations[instr[0]] = move(1, g.pile, g.foundations[instr[0]]) elif code == MOVE_CODE.P_TO_T: g.pile, g.tableaus[instr[0]] = move(1, g.pile, g.tableaus[instr[0]]) elif code == MOVE_CODE.ERROR: print('Cannot find any valid moves!') game_state = 'Over' break print_game(g) print() print(f'Game {game_state}')
def test_winrate(): wins: int = 0 random.seed(21522) for i in range(1000): g = Klondike.new_game() consecutive_draws = 0 while not game_won(g) and consecutive_draws < 24: code, *instr = new_find_move(g) if code == MOVE_CODE.DRAW: consecutive_draws += 1 g.stock, g.pile = draw(g.stock, g.pile, nb_cards=1) elif code == MOVE_CODE.T_TO_F: consecutive_draws = 0 g.tableaus[instr[0]], g.foundations[instr[1]] = move(1, g.tableaus[instr[0]], g.foundations[instr[1]]) elif code == MOVE_CODE.T_TO_T: consecutive_draws = 0 g.tableaus[instr[0]], g.tableaus[instr[1]] = move(instr[2], g.tableaus[instr[0]], g.tableaus[instr[1]]) elif code == MOVE_CODE.P_TO_F: consecutive_draws = 0 g.pile, g.foundations[instr[0]] = move(1, g.pile, g.foundations[instr[0]]) elif code == MOVE_CODE.P_TO_T: consecutive_draws = 0 g.pile, g.tableaus[instr[0]] = move(1, g.pile, g.tableaus[instr[0]]) if game_won(g): wins += 1 assert wins > 290 print(f'wins:\t\t{wins}\nlosses:\t\t{1000-wins}\npercentage:\t{wins/10}%')
def alpha_beta_max(depth, array): #极大极小值算法的极大值函数 scores = [0, 0, 0, 0] if depth == 0: for i in xrange(0, 4): newArray = move(array, i) scores[i] = evaultion(newArray) else: for i in xrange(0, 4): newArray = move(array, i) scores[i] = alpha_beta_min(depth - 1, newArray) return max(scores)
def key_down(self, key): """ # 0: left, 1: up, 2: right, 3: down """ self.matrix, moved, score = logic.move(self.matrix, key) if moved: self.matrix = logic.add_two_or_four(self.matrix)
def POST(self): # this gets called for the subsequent times whenever an cell is clicked upon global grid global old global player_id # here orbClicked gets the input about which cell was clicked orbClicked = web.input(position=None, playerid=None) orb = orbClicked.position pID = orbClicked.playerid coOrdinates = orb.split(',') for i in range(len(coOrdinates)): coOrdinates[i] = int(coOrdinates[i]) x = coOrdinates[0] y = coOrdinates[1] # need this to determine winner old = copy.deepcopy(grid) player_id = int(pID) grid = logic.move(x, y, player_id, grid) if player_id == 1: player_id = 2 else: player_id = 1 # determining if someone won or not? old1 = 0 old2 = 0 new1 = 0 new2 = 0 win = 0 for i in range(5): for j in range(5): if old[i][j] / 10 == 1: old1 += 1 if old[i][j] / 10 == 2: old2 += 1 if grid[i][j] / 10 == 1: new1 += 1 if grid[i][j] / 10 == 2: new2 += 1 if old1 != 0 and new1 == 0: win = 2 elif old2 != 0 and new2 == 0: win = 1 if win != 0: games.update({'_id': gameid}, {'$set': {"win": win}}) return render.game(input=grid, player=player_id, winner=win, Mtype=matchType, p1=NameP1, p2=NameP2)
def onClick(event): moved, tile, victory = logic.move(renderer.puzzle, frame.tile) if moved: swapFrames(renderer, tile, frame.tile) if victory: # TODO: Allow several rounds, give proper feedback (through GUI) print('Hurray!')
def alpha_beta(array): #alpha-beta剪枝算法,目前只是全部计算,还没有实现剪枝 scores = [0, 0, 0, 0] for i in xrange(0, 4): newArray = move(array, i) scores[i] = alpha_beta_min(maxDepth - 1, newArray) print scores #最大值对应的方向就是应该移动的方向 maxValue = scores[0] sequence = [] for i in xrange(0, 4): maxValue = -100000 index = 0 for j in xrange(0, 4): if scores[j] > maxValue: maxValue = scores[j] index = j scores[index] = -100000 sequence.append(index) print sequence direction = 0 for i in xrange(0, 4): if canMove(array, sequence[i]) == True: direction = sequence[i] break print direction return direction
def get_score(board, first_move): sboard = board.copy() sboard, moved, score = logic.move(sboard, first_move) if not moved: return -1 total_score = 0 for i in range(100): game_score = score simulation_board = sboard.copy() simulation_board = logic.add_two_or_four(simulation_board) while logic.game_state(simulation_board) != "lose": simulation_board, moved, move_score = logic.move( simulation_board, randint(0, 3)) if moved: simulation_board = logic.add_two_or_four(simulation_board) game_score += move_score total_score += game_score return total_score / 100
def moveGrid(grid,i): # new=np.zeros((4,4),dtype=np.int) new = None if i==0: # move up grid=np.transpose(grid) new = np.stack([logic.move(grid[row,:]) for row in range(4)], axis = 0).astype(int).T elif i==1: # move left new = np.stack([logic.move(grid[row,:]) for row in range(4)], axis = 0).astype(int) elif i==2: # move down grid=np.transpose(grid) new = np.stack([np.flip(logic.move(np.flip(grid[row,:]))) for row in range(4)], axis = 0).astype(int).T elif i==3: # move right new = np.stack([np.flip(logic.move(np.flip(grid[row,:]))) for row in range(4)], axis = 0).astype(int) return new
def alpha_beta_max(depth, array, alpha, beta): if depth == 0: for i in xrange(0, 4): newArray = move(array, i) tmp = evaluation(newArray) if tmp > alpha: alpha = tmp if alpha > beta: return alpha else: for i in xrange(0, 4): newArray = move(array, i) tmp = alpha_beta_min(depth - 1, newArray, alpha, beta) if tmp > alpha: alpha = tmp if alpha > beta: return alpha return alpha
def auto_solve(self): while True: input = get_best_input(self.matrix) self.matrix, moved, score = logic.move(self.matrix, input) if moved: self.matrix = logic.add_two_or_four(self.matrix) self.update_grid_cells() moved = False self.update()
def get_score(board, first_move): """ Given a board and a first_move, get a score by playing a lot of random games """ sboard = board.copy() sboard, moved, score = logic.move(sboard, first_move) if not moved: return -1 total_score = 0 for i in range(SIMULATION_NUMBER): game_score = score simulation_board = sboard.copy() simulation_board = logic.add_two_or_four(simulation_board) while logic.game_state(simulation_board) != "lose": simulation_board, moved, move_score = logic.move(simulation_board, random.randint(0, 3)) if moved: simulation_board = logic.add_two_or_four(simulation_board) game_score += move_score total_score += game_score return total_score / SIMULATION_NUMBER
def GET(self): global grid global player_id # print "AI MOVES BITCHES!!!" win = 0 validity = web.input(valid=None) if int(validity.valid) == 1: #print "HELL YEAH" # print "Player id? is "+str(player_id) postion = bot.main(grid, player_id) x = postion[0] y = postion[1] # need this to determine winner old = copy.deepcopy(grid) grid = logic.move(x, y, player_id, grid) if player_id == 1: player_id = 2 else: player_id = 1 # determining if someone won or not? old1 = 0 old2 = 0 new1 = 0 new2 = 0 for i in range(5): for j in range(5): if old[i][j] / 10 == 1: old1 += 1 if old[i][j] / 10 == 2: old2 += 1 if grid[i][j] / 10 == 1: new1 += 1 if grid[i][j] / 10 == 2: new2 += 1 if old1 != 0 and new1 == 0: win = 2 elif old2 != 0 and new2 == 0: win = 1 if win != 0: games.update({'_id': gameid}, {'$set': {"win": win}}) return render.game(input=grid, player=player_id, winner=win, Mtype=matchType, p1=NameP1, p2=NameP2)
def alpha_beta(array): #无alpha-beta剪枝,只是全部计算 directions = [] for i in xrange(0, 4): #得到所有可以移动的方向 if canMove(array, i) == True: directions.append(i) maxvalue = -10000 for i in xrange(0, len(directions)): newArray = move(array, directions[i]) tmp = alpha_beta_min(maxDepth - 1, newArray) if tmp > maxvalue: maxvalue = tmp direction = directions[i] return direction
def key_down(self, event): if self.over: return key = repr(event.char) if key in self.commands: done = logic.move(self.matrix, self.commands[key]) if done: logic.add_new(self.matrix) self.update() if logic.is_win(self.matrix): self.over = True result = Label(master=self.background, text="You Win", height=2) result.grid() elif logic.game_over(self.matrix): self.over = True result = Label(master=self.background, text="Game Over", height=2) result.grid()
def alpha_beta(array): #alpha-beta剪枝 alpha = MIN beta = MAX directions = [] for i in xrange(0, 4): #得到所有可以移动的方向 if canMove(array, i) == True: directions.append(i) direction = directions[0] for i in xrange(0, len(directions)): newArray = move(array, directions[i]) tmp = alpha_beta_min(maxDepth - 1, newArray, alpha, beta) if tmp > alpha: alpha = tmp direction = directions[i] return direction
def test_move(card_pos: int, pile_from: List[Card], pile_to: List[Card], expected: Tuple[List[Card], List[Card]]): assert expected == move(card_pos, pile_from, pile_to)
def moveGrid(grid,i): return logic.move(grid, i)
def build_test_games() -> Tuple[Klondike, Klondike, Klondike, Klondike]: # No games are shuffled, to avoid random stuff game2 = Klondike.new_game(False) game2.stock, game2.pile = draw(game2.stock, game2.pile) game4 = deepcopy(game2) game4.tableaus[1], game4.tableaus[2] = move(1, game4.tableaus[1], game4.tableaus[2]) game4.tableaus[3], game4.tableaus[4] = move(1, game4.tableaus[3], game4.tableaus[4]) game4.tableaus[5], game4.tableaus[6] = move(1, game4.tableaus[5], game4.tableaus[6]) game4.tableaus[5], game4.tableaus[6] = move(1, game4.tableaus[5], game4.tableaus[6]) game3 = deepcopy(game4) game3.pile, game3.tableaus[2] = move(1, game3.pile, game3.tableaus[2]) game3.pile, game3.tableaus[0] = move(1, game3.pile, game3.tableaus[0]) game3.pile, game3.tableaus[1] = move(1, game3.pile, game3.tableaus[1]) game3.stock, game3.pile = draw(game3.stock, game3.pile) game3.pile, game3.tableaus[2] = move(1, game3.pile, game3.tableaus[2]) game3.stock, game3.pile = draw(game3.stock, game3.pile) game3.stock, game3.pile = draw(game3.stock, game3.pile) game3.pile, game3.tableaus[2] = move(1, game3.pile, game3.tableaus[2]) game3.stock, game3.pile = draw(game3.stock, game3.pile) game3.stock, game3.pile = draw(game3.stock, game3.pile) game3.stock, game3.pile = draw(game3.stock, game3.pile) game1 = deepcopy(game3) game1.pile, game1.foundations[0] = move(1, game1.pile, game1.foundations[0]) game1.stock, game1.pile = draw(game1.stock, game1.pile) game1.pile, game1.foundations[1] = move(1, game1.pile, game1.foundations[1]) game1.pile, game1.foundations[2] = move(1, game1.pile, game1.foundations[2]) game1.pile, game1.foundations[3] = move(1, game1.pile, game1.foundations[3]) game1.pile, game1.foundations[1] = move(1, game1.pile, game1.foundations[1]) game1.pile, game1.foundations[2] = move(1, game1.pile, game1.foundations[2]) game1.pile, game1.foundations[3] = move(1, game1.pile, game1.foundations[3]) game1.pile, game1.foundations[0] = move(1, game1.pile, game1.foundations[0]) game1.pile, game1.foundations[1] = move(1, game1.pile, game1.foundations[1]) return game2, game4, game3, game1
def play(game): for i in range(100): # while not puzzle.ControlMechanism.state(): logic.move(game.matrix, random.choice(directions))
import data, screen, logic from clge import DefaultAssets as da info = data.get_data() scr = screen.open_screen(info["field_width"] + 1, info["field_height"] + 1, info["default_symbol"], info["timeout_duration"], info["auto_timeout"], info["auto_clear_objects"], info["default_color"]) player = da.SamplePlayer(info["player"]["begin_x"], info["player"]["begin_y"], info["player"]["step"], info["player"]["lives"], info["player"]["symbol"]) fruit = da.SampleObject(logic.fruit_first_pos_x(info["field_width"]), logic.fruit_first_pos_y(info["field_height"]), step=1, symbol=info["fruit"]["symbol"]) while True: logic.move(player, info["field_width"], info["field_height"], info["sound_enabled"]) info["score"] = logic.check_collision_and_add_points( player, fruit, info["score"], { "width": info["field_width"], "height": info["field_height"] }) player.add_to_screen(scr) fruit.add_to_screen(scr) scr.render(info["title"], "Score: {}".format(info["score"])) scr.do_timeout()