def key_down(self, event): key = repr(event.char) if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() self.update_grid_cells() elif key in self.commands: self.matrix, done = self.commands[repr(event.char)](self.matrix) if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="Try", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Again!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) # Scoring: runs even after loss or win RightView.setCurrentScore(cfg.wrapper.app_gui.right_view, cfg.currentScore) if (cfg.currentScore > cfg.highScore): cfg.highScore = cfg.currentScore RightView.setHighScore(cfg.wrapper.app_gui.right_view)
def key_down(self, event): key = repr(event.char) if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() self.update_grid_cells() # print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: #AA: Perform key command self.matrix, done = self.commands[repr(event.char)](self.matrix) if done: #AA: Add a 2 to a random location? self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False # print("pastDone") if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) elif logic.game_state(self.matrix) == 'lose': # print("LOOOOOOOOOOOOOSER") self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
def main(): for e in range(episodes): _state = newGame() frame = 0 while True: frame += 1 state = preprocessState(_state) action = agent.act(state) if action in action_string: logging.debug("{}: action: {}".format(frame, action_string[action])) _next_state, action_done = takeAction(_state, action) reward = 0 done = False if not action_done: reward += -0.5 if game_state(_next_state) == 'win': reward += 1 done = True elif game_state(_next_state) == 'lose': reward += -1 done = True next_state = preprocessState(_next_state) agent.remember(state, action, reward, next_state, done) _state = _next_state if done: logging.info("{}: {}/{}".format(frame, e, episodes)) break
def main(): for iteration in range(ITERATION): print("Iteration: " + str(iteration + 1)) start = time.time() step = 0 matrix = np.zeros((4, 4), dtype=np.int) matrix = logic.add_two(matrix) while True: matrix = logic.add_two(matrix) if logic.game_state(matrix) == 'lose': break if logic.game_state(matrix) == 'lose': break matrix = montecarlo.getMove(matrix, NUM_BACKGROUND_RUNS) step += 1 # move=montecarlo.getMove(matrix, NUM_BACKGROUND_RUNS) # # print("got a move " + str(move)) # matrix=montecarlo.moveGrid(matrix,move) # step+=1 print(matrix) print("Step= " + str(step)) print("Max= " + str(2**np.max(matrix))) print("Score= " + str(logic.getScore(matrix))) print('Depth= ' + str(NUM_BACKGROUND_RUNS)) print('Time= ' + str(time.time() - start)) print('') stat.append((step, 2**np.max(matrix), logic.getScore(matrix)))
def key_down(self, event): key = repr(event.char) if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() self.update_grid_cells() print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: self.matrix, done = self.commands[repr(event.char)](self.matrix) if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) elif key == c.KEY_R: self.reset()
def update_grid_cells(self): for i in range(c.GRID_LEN): for j in range(c.GRID_LEN): new_number = self.matrix[i][j] if new_number == 0: self.grid_cells[i][j].configure( text="", bg=c.BACKGROUND_COLOR_CELL_EMPTY) else: self.grid_cells[i][j].configure( text=str(new_number), bg=c.BACKGROUND_COLOR_DICT[new_number], fg=c.CELL_COLOR_DICT[new_number]) file = open("output.txt", "w") if logic.game_state(self.matrix) == 'lose': print("pl0") file.write('0') elif logic.game_state(self.matrix) == 'win': print("pl0") file.write('1') else: for i in range(c.GRID_LEN): for j in range(c.GRID_LEN): file.write(str(self.matrix[i][j]) + ' ') if i != c.GRID_LEN: file.write('\n') self.update_idletasks()
def key_down(self, key): # key = repr(event.char) if self.done == 1: # time.sleep(SLEEP_TIME) self.score = self.getScore(self.matrix) max = np.max(np.max(self.matrix)) stat.append((self.score, max, self.step)) self.quit() if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() self.update_grid_cells() print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: self.matrix, done = self.commands[key](self.matrix) if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.done = 1 if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.done = 1 self.step += 1
def eval_genome(genome, config): """ This function will be run in threads by ThreadedEvaluator. It takes two arguments (a single genome and the genome class configuration data) and should return one float (that genome's fitness). """ # Initialize game game_matrix = logic.new_game(4) game_matrix = logic.add_two(game_matrix) game_matrix = logic.add_two(game_matrix) # Flatten function flatten = lambda l: [item for sublist in l for item in sublist] net = neat.nn.FeedForwardNetwork.create(genome, config) # Action functions actions = [logic.up, logic.down, logic.left, logic.right] fitness = 0 for i in range(3): while logic.game_state(game_matrix) == 'not over': # Flatten game matrix flat_matrix = flatten(game_matrix) # Predict moves output = net.activate(flat_matrix) # Copy list and sort predictions from lowest to highest sorted_output = sorted(enumerate(output), key=lambda x: x[1]) # Get max index from output list and use assosiated function from actions max_index = sorted_output[-1][0] new_game_matrix = actions[max_index](game_matrix) # If move is not valid use different direction if not new_game_matrix[1]: # Get second max index from output list and use assosiated function from actions second_max_index = sorted_output[-2][0] # TODO if output has same values all directions are not checked new_game_matrix = actions[second_max_index](game_matrix) # If move is not valid use different direction if not new_game_matrix[1]: # Get third max index from output list and use assosiated function from actions third_max_index = sorted_output[-3][0] new_game_matrix = actions[third_max_index](game_matrix) # If move is not valid use different direction if not new_game_matrix[1]: # Get fourth max index from output list and use assosiated function from actions fourth_max_index = sorted_output[-4][0] new_game_matrix = actions[fourth_max_index](game_matrix) # Set game matrix to updated matrix from (game, true) tuple game_matrix = new_game_matrix[0] # Generate new tile if logic.game_state(game_matrix) == 'not over': game_matrix = logic.add_two(game_matrix) #print(game_matrix) # Fitness function is a summation of all values on game board fitness += sum(flatten(game_matrix)) return fitness / 3
def key_down(self, event): key = event game_done = False game_result = False temp = 0 current_state = self.matrix if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() self.update_grid_cells() print('back on step total step:', len(self.history_matrixs)) elif key in self.game.commands: self.matrix, done = self.game.commands[key](self.matrix) if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells(self.game) done = False if logic.game_state(self.matrix) == 'win': game_done = True game_result = True self.game.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.game.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': game_done = True game_result = False self.game.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.game.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if (game_done and game_result): self.score = sum(np.array(self.matrix).flatten()) elif (game_done and not (game_result)): self.score = -1 * sum(np.array(self.matrix).flatten()) else: if (self.score != sum(np.array(self.matrix).flatten())): for i in range(4): for j in range(4): if (self.matrix[i][j] == 2 * current_state[i][j]): temp += self.matrix[i][j] self.score = sum(np.array(self.matrix).flatten()) return self.matrix, game_done, temp else: # print(0) return self.matrix, game_done, -100 return self.matrix, game_done, self.score
def run_random(self): for k in range(self.steps): num = randint(0, 3) event_rn.char = chr(num) self.gamegrid.key_down(event_rn) if game_state(self.gamegrid.matrix) == 'win' \ or game_state(self.gamegrid.matrix) == 'lose': #time.sleep(1) return time.sleep(self.sleep)
def key_down(self, event): game_board = self.matrix move = find_best_move( list( map( lambda x: list( map(lambda y: 0 if y == 0 else y.bit_length() - 1, x)), game_board))) if (move == 0): move = 'w' elif (move == 1): move = 's' elif (move == 2): move = 'a' elif (move == 3): move = 'd' else: print('matrix', self.matrix) print('move', move) return key = repr(move) if key in self.commands: self.matrix, done, self.score = self.commands[repr(move)]( self.matrix, self.score) #print("SCORE:") #print(self.score) if done: #self.matrix = logic.add_two(self.matrix) # record last move part_out = get_participant_output(self.player_file, self.matrix) if self.check_participant_output_validity( part_out, game_board): self.matrix[part_out[0][1]][part_out[0][0]] = part_out[1] else: penalty(game_board) self.highest_tile = max(map(lambda l: max(l), self.matrix)) self.num_moves += 1 print('move', self.num_moves) self.max_at_instance.append(self.highest_tile) self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
def process_move(self, key): if logic.game_state(self.game_matrix) == logic.STATE_PROGRESS: if key not in self.commands: return self.game_matrix, done = self.commands[key](self.game_matrix) if done: # done = not a fully filled game matrix self.game_matrix = logic.add_tile(self.game_matrix) self.print_game() if logic.game_state(self.game_matrix) == logic.STATE_WIN: print('You have won! Press <r> to reset, <q> to close.\n') if logic.game_state(self.game_matrix) == logic.STATE_LOSE: print('You lost! Press <r> to reset, <q> to close.\n')
def key_down(self, event): # 변수에 키보드 이벤트에서 발생하는 문자를 문자열 객체로 저장 key = repr(event.keycode) # 입력된 키가 'b' + 히스토리 매트릭스의 길이가 2이상일 경우 if (key == c.KEY_BACK) and (len(self.history_matrixs) >= 1): # 저장된 이전 매트릭스를 불러옴 self.matrix = self.history_matrixs.pop() # 화면 출력 업데이트 self.update_grid() # 되돌리기 문구 출력: 남은 히스토리 매트릭스 길이(되돌릴 수 있는 회수) print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: global TOTAL_SCORE # ex) logic.up(self.matrix) self.matrix, done, TOTAL_SCORE = self.commands[repr( event.keycode)](self.matrix) # done이 참이라면 if done: # 히스토리 매트릭스 리스트에 직전 움직임 기록 self.history_matrixs.append(self.matrix) # 타일 변경 self.matrix = logic.change_tile(self.matrix) # 매트릭스에 새로운 타일 생성 self.matrix = logic.create_tile(self.matrix) # 화면 출력 업데이트 self.update_grid() # done을 Flase로 전환 done = False # 게임에서 승리할 경우 if logic.game_state(self.matrix) == 'win': self.grid_fields[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_field_EMPTY, fg=c.GAME_STATE_TEXT) self.grid_fields[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_field_EMPTY, fg=c.GAME_STATE_TEXT) # 게임에서 패배할 경우 if logic.game_state(self.matrix) == 'lose': self.grid_fields[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_field_EMPTY) self.grid_fields[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_field_EMPTY)
def calc_reward(self, new_points): #momentan + potenzial reward = 0 if logic.game_state(self.matrix) == 'lose': return -100 if np.array_equal(self.matrix_old, self.matrix): return -10 # Wenn es eine neue größte kache gibt if self.biggest_tile > self.get_biggest_tile(self.matrix): reward += self.REWARD_AKTUELLES_STATE_GROSSTE_KACHEL #TODO: hier sollte mit der alten matrix gearbeitet werden max_points_possible = self.calc_max_points_possible(self.matrix_old) #TODO: Guter ansatz oder besser alle punkte geben ? #wenn keine Punkte möglich sind gib ihm hälfte der möglichen punkte if (max_points_possible == 0): reward = reward + self.REWARD_AKTUELLES_STATE_SCORE * 0.5 else: #TODO: Arbeitet er hier mit der MAtrix nach der Aktion oder davor ? sollte mit der matrix vor der aktion arbeiten reward = reward + (self.REWARD_AKTUELLES_STATE_SCORE * ((new_points * 100) / max_points_possible)) # an hier potenzial Komponente ---------------------------------------------------- return reward
def take_action(self, event=None, action=None): generated_new = False state, action, state_after, reward, terminal = (None, action, None, None, False) if event is None: key = action elif action is None: key = repr(event.char) if key in self.commands: state = self.matrix[:] self.matrix, done, score_increase = self.commands[key](self.matrix) action = key[:] reward = score_increase self.score += score_increase if [0 for row in self.matrix if 0 in row]: generated_new = True self.matrix = add_two_or_four(self.matrix) state_after = self.matrix[:] if game_state(self.matrix) == 'lose' or (state_after == state and generated_new == False): reward -= 100 terminal = True print(f"This EP Score: {self.score}") self.reset_episode() five_tup = (state_after, reward, terminal) [print(row) for row in state] print(action) return five_tup
def main(): for iteration in range(ITERATION): print("Iternaton: " + str(iteration + 1)) step = 0 matrix = np.zeros((4, 4)) matrix = logic.add_two(matrix) while True: matrix = logic.add_two(matrix) if logic.game_state(matrix) == 'lose': break while True: move = ExpectiMax.getMove(matrix, DEPTH) if move == "'w'": newMatrix, _ = logic.up(matrix) if move == "'a'": newMatrix, _ = logic.left(matrix) if move == "'s'": newMatrix, _ = logic.down(matrix) if move == "'d'": newMatrix, _ = logic.right(matrix) if ExpectiMax.isSame(newMatrix, matrix) == False: matrix = newMatrix break step += 1 print("Step= " + str(step)) print("Max= " + str(np.max(matrix))) print("Score= " + str(logic.getScore(matrix))) print('') stat.append((step, np.max(matrix), logic.getScore(matrix))) np.savetxt("stat.txt", stat)
def singlePredict(self, matrix, done): maxi_pos = np.argmax(matrix) cur_scores = 0 hor = 0 ver = 0 for i in range(3): for j in range(3): if (matrix[i][j] == matrix[i + 1][j]): hor += matrix[i][j] if (matrix[i][j] == matrix[i][j + 1]): ver += matrix[i][j] if (matrix[i][j] == matrix[i + 1][j] / 2 or matrix[i][j] / 2 == matrix[i + 1][j]): hor += min(matrix[i][j], matrix[i + 1][j]) if (matrix[i][j] == matrix[i][j + 1] / 2 or matrix[i][j] / 2 == matrix[i][j + 1]): ver += min(matrix[i][j], matrix[i][j + 1]) if (maxi_pos == 0 or maxi_pos == 3 or maxi_pos == 15 or maxi_pos == 12): cur_scores += np.amax(matrix) if done: cur_scores += np.count_nonzero(matrix == 0) * np.amax(matrix) / 16 else: cur_scores -= 10000 if logic.game_state(matrix) == 'lose': cur_scores -= 100000 # I think we need to weight this cur_scores += max(hor, ver) / 5 return cur_scores
def expectiMax(grid, agent, depth): score = 0 if (logic.game_state(grid) == 'lose'): return FAIL_SCORE if depth == 0: return logic.getScore(grid) * getUtility(grid) # Player's turn if agent == 0: for i in range(4): newGrid = moveGrid(grid, i) score = max(score, expectiMax(newGrid, 1, depth)) return score # Computer's turn elif agent == 1: count = 0 score = 0 for i in range(4): for j in range(4): if grid[i][j] == 0: grid[i][j] = 2 count += 1 score += expectiMax(grid, 0, depth - 1) grid[i][j] = 0 if count == 0: return FAIL_SCORE else: return score / count
def check_if_done(self, done): if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False if logic.game_state(self.matrix) == 'win': #TODO: Hier den limit für 2048 löschen um zu schauen wie weit er kommen wird self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': self.quit() self.destroy() return True
def key_down(self, dir): key = dir if key in self.commands: self.matrix, done, n, g = self.commands[dir](self.matrix) self.score += n * 2 if done: self.matrix = logic.add_two(self.matrix) self.history_matrixs.append(self.matrix) return logic.game_state(self.matrix)
def indicesHeu(self, board): if np.count_nonzero(board) == 16: # change to macro return -1, -1, float('-inf') zeroes = [] indexes = [] heu = float('-inf') for i in range(4): for j in range(4): if board[i][j] == 0: #tempB board[i][j] = 2 if logic.game_state(board) is 'lose': # board[i][j] = 2 return i, j, float('inf') board[i][j] = 0 zeroes.append([i, j]) for i in zeroes: #check if theres 2 below you, Line 0 temp = 0 n1 = 0 for k in range(i[0] + 1, 4): # check neighbor below if board[k][i[1]] == 0: continue n1 = board[k][i[1]] if n1 == 2: temp -= 2 break k = i[0] - 1 while k > -1: # check neighbors above curr square if board[k][i[1]] == 0: k = k - 1 continue if n1 == board[k][i[1]] and board[k][i[1]] != 2: temp += board[k][i[1]] elif board[k][i[1]] == 2: temp -= 2 break n1 = 0 for k in range(i[1] + 1, 4): # check my neighbor on right if board[i[0]][k] == 0: continue n1 = board[i[0]][k] if n1 == 2: temp -= 2 break k = i[1] - 1 while k > -1: # check my neighbor on left if board[i[0]][k] == 0: k = k - 1 continue if n1 == board[i[0]][k] and board[i[0]][k] != 2: temp += board[i[0]][k] elif board[i[0]][k] == 2: temp -= 2 break if temp > heu: heu = temp indexes = copy.deepcopy(i) return indexes[0], indexes[1], heu
def render(self, mode='human'): obs = self.get_observation() print(obs) if not self.is_done(): if not self.random: print("\nMake a move: Up: 0, Down: 1, Left: 2, Right: 3\n") else: if logic.game_state(obs) == 'win': print("You win!") else: print("You lose :(") print(f'Score: {self.score} Steps: {self.steps} Reward: {self.rew}')
def d(self): while logic.game_state(self.matrix) != 'lose': #for x in range(100000): mlist = ["w", "s", "a", "d"] dic = { 0: logic.up(self.matrix)[0], 1: logic.down(self.matrix)[0], 2: logic.left(self.matrix)[0], 3: logic.right(self.matrix)[0] } up = logic.up(self.matrix)[0] down = logic.down(self.matrix)[0] left = logic.left(self.matrix)[0] right = logic.right(self.matrix)[0] actt = [self.sx(up), self.sx(down), self.sx(left), self.sx(right)] max_val = max(actt) maxact = [i for i, x in enumerate(actt) if x == max_val] acttt = [] #time.sleep(1) for maxx in maxact: if logic.game_state(dic[maxx]) != 'lose': acttt.append(maxact.index(maxx)) #max_val = max(act) #actt = [i for i, x in enumerate(act) if x == max_val] if len(acttt) > 0: self.key_down(mlist[random.choice(acttt)]) elif len(actt) == 0: self.key_down(random.choice(mlist)) #time.sleep(.5) if logic.game_state(dic[0]) == 'lose' and logic.game_state( dic[1]) == 'lose' and logic.game_state( dic[2]) == 'lose' and logic.game_state(dic[3]) == 'lose': logic.new_game(4)
def auto_key_down(self, key): key = '\'' + key + '\'' if key in self.command: self.matrix, done = self.command[key](self.matrix) if done: self.matrix = l.gen(self.matrix) self.update_grid() done = False if l.game_state(self.matrix, c.winnum) == 'win': self.grid_rows[1][1].configure( text='You', bg=c.BACKGROUND_EMPTY_CELL_COLOR) self.grid_rows[1][2].configure( text='Win', bg=c.BACKGROUND_EMPTY_CELL_COLOR) c.winnum *= 2 if l.game_state(self.matrix, c.winnum) == 'lose': # self.grid_rows[1][1].configure(text='You', bg=c.BACKGROUND_EMPTY_CELL_COLOR) # self.grid_rows[1][2].configure(text='lose', bg=c.BACKGROUND_EMPTY_CELL_COLOR) box = messagebox.askquestion('Restart?', 'Sure?') if box == 'yes': self.restart() else: self.master.destroy()
def __init__(self, matrix, father=None, action=None): self.matrix = matrix self.is_over = False if logic.game_state(self.matrix) == 'win': self.is_win = True self.is_over = True if logic.game_state(self.matrix) == 'lose': self.is_win = False self.is_over = True self.square_sum = np.sum(np.array(self.matrix)**2) sum_value = sum(sum(np.array(self.matrix))) self.sum_square = sum_value**2 self.childs = [] self.father = father self.action = action self.is_dead = False self.N = 0 self.S = self.square_sum / self.sum_square self.Q = 0 self.U = 0
def action(self, event): key = event if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: self.matrix, done = self.commands[key](self.matrix) if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) done = False if logic.game_state(self.matrix) == 'win': self.is_win = True self.is_over = True if logic.game_state(self.matrix) == 'lose': self.is_win = False self.is_over = True self.step =self.step+1 self.max_value = max(max(row) for row in self.matrix) self.sum_value = sum(sum(np.array(self.matrix))) return self
def action(self, event): key = event if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() # self.update_grid_cells() print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: self.matrix, done = self.commands[key](self.matrix) if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) # self.update_grid_cells() done = False if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) # self.update_idletasks() # time.sleep(1) self.is_win = True self.is_over = True if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) # self.update_idletasks() # time.sleep(1) self.is_win = False self.is_over = True self.step = self.step + 1 self.max_value = max(max(row) for row in self.matrix) self.sum_value = sum(sum(np.array(self.matrix))) return self
def render_main(e, frame, _state, agent, grid_cells, root): state = preprocessState(_state) action = agent.act(state) if action in action_string: logging.debug("{}-{}: action: {}".format(e, frame, action_string[action])) _next_state, action_done = takeAction(_state, action) reward = 0 done = False if not action_done: reward += -0.5 if game_state(_next_state) == 'win': reward += 1 done = True elif game_state(_next_state) == 'lose': reward += -1 done = True next_state = preprocessState(_next_state) agent.remember(state, action, reward, next_state, done) _state = _next_state if done: logging.info("{}: {}/{}".format(frame, e, episodes)) _state = newGame() e += 1 frame = -1 logging.info("{}-{}: Replaying".format(e, frame)) agent.replay(-1) update_grid_cells(grid_cells, _state) frame += 1 if e < episodes: root.after(10, render_main, e, frame, _state, agent, grid_cells, root)
def key_down(self, event): key = repr(event.char) if key == c.KEY_BACK and len(self.last_matrix) > 0: self.matrix = copy.deepcopy(self.last_matrix) self.last_matrix = None self.update_grid_cells() elif key in self.commands: new_matrix, done = self.commands[repr(event.char)](self.matrix) if done: index = self.last_generated if self.last_matrix is None else None self.last_matrix = copy.deepcopy(self.matrix) self.matrix = new_matrix self.last_generated = self.generate_next(index) self.update_grid_cells() if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
def state(self): print("Aktueller Spielstand") print(game_grid.matrix) print("Momentane Punktzahl") print(logic.get_biggest_number(game_grid.matrix)) print("Spiel beendet?") status = logic.game_state(game_grid.matrix) if status == "not over": print("Spiel nicht vorbei") return False else: print("Spiel vorbei") return True
def key_down(self, event): key = repr(event.char) if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() self.update_grid_cells() print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: self.matrix, done = self.commands[repr(event.char)](self.matrix) if done: self.matrix = logic.add_two(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)