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 minimax(self, move): state_hat_2 = 0 blocks = [] if move == 'a': next_matrix, _ = left(self.observation) elif move == 's': next_matrix, _ = down(self.observation) elif move == 'd': next_matrix, _ = right(self.observation) else: next_matrix, _ = up(self.observation) state_hat_1 = sum(cell.count(0) for cell in next_matrix) # for row in next_matrix: # for element in row: # blocks.append(element) # # blocks.sort(reverse=True) # state_hat_2 = max(blocks) # while 2**n == state_hat_2: # state_hat_2 = 10**n # if n != 1: # val = sum(cell.count(2**(n-1)) for cell in next_matrix) return state_hat_1, next_matrix
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 main(): move_left = np.zeros((16, 16, 16, 16, 4)) for a in range(16): for b in range(16): for c in range(16): for d in range(16): row = [a, b, c, d] mat = [row for i in range(4)] newMat, _ = logic.left(mat) move_left[a, b, c, d, :] = newMat[0] np.save('move', move_left)
def left(self): self.matrix, done, bonus_score = logic.left(self.matrix) self.total_moves += 1 if done: logic.add_two(self.matrix) self.score += bonus_score self.true_score += bonus_score else: self.invalid_moves += 1 self.score -= 1 bonus_score = -1 return bonus_score
def moveGrid(grid, i): if i == 0: new, _ = logic.up(grid) return new if i == 1: new, _ = logic.left(grid) return new if i == 2: new, _ = logic.down(grid) return new if i == 3: new, _ = logic.right(grid) return new
def getNextMoves(matrix): """ alrogithm to determine which moves to do next. return either a list of allowed moves (i.e. either 1,2,3 or 4, or as string "left", "right, "up", "down") or only the next move """ # if possible move to right bottom corner mat, done = logic.right(matrix) if done: return ["RIGHT"] mat, done = logic.down(matrix) if done: return ["DOWN"] # otherwise try to any other move mat, done = logic.left(matrix) if done: return ["LEFT", "RIGHT"] return ["UP", "DOWN"]
def step(self, action): #TODO: Check hier ob die neue matrix auch wirklich die neue ist und die alte die alte und nicht das beide gleich sind self.matrix_old = self.copy_matrix() if action == 0: self.matrix, done, new_points = logic.up(self.matrix) elif action == 1: self.matrix, done, new_points = logic.right(self.matrix) elif action == 2: self.matrix, done, new_points = logic.down(self.matrix) elif action == 3: self.matrix, done, new_points = logic.left(self.matrix) self.score += new_points self.biggest_tile = self.get_biggest_tile(self.matrix) reward = self.calc_reward(new_points) #self.matrix = self.matrix_nach_aktion #done = self.check_if_done(done) return (reward, done, action)
def eventFilter(self, source, event): if event.type() == QtCore.QEvent.MouseMove: self.frame.setCursor(QCursor(self.cursors[1], 35, 35)) if event.buttons() == QtCore.Qt.LeftButton: if len(self.points) > 2: startx, starty = self.points[0][0], self.points[0][1] for i in range(len(self.points)): self.points[i] = (self.points[i][0] - startx, self.points[i][1] - starty) self.points.append( (event.localPos().x(), event.localPos().y())) # print(self.points) if event.type() == QtCore.QEvent.MouseButtonRelease and event.button( ) == QtCore.Qt.LeftButton: # print("Released!") self.mouseDown = False strokes = moosegesture.getGesture(self.points) if len(strokes) > 0: strokeText = str(strokes[-1]) # print(strokeText) if strokeText == "R": self.matrix, done = logic.right(self.matrix) if done: self.stateOfGame() elif strokeText == "L": self.matrix, done = logic.left(self.matrix) if done: self.stateOfGame() elif strokeText == "U": self.matrix, done = logic.up(self.matrix) if done: self.stateOfGame() elif strokeText == "D": self.matrix, done = logic.down(self.matrix) if done: self.stateOfGame() strokes.clear() else: return True return self.frame.eventFilter(source, event)
def movement(self, move): if move == 'w': self.matrix, done, points = logic.up(self.matrix) elif move == 's': self.matrix, done, points = logic.down(self.matrix) elif move == 'a': self.matrix, done, points = logic.left(self.matrix) elif move == 'd': self.matrix, done, points = logic.right(self.matrix) else: done = False print('Error, not a valid move') points = 0 self.score += points self.state = logic.game_state(self.matrix) if done and self.state == 'not over': self.matrix = logic.add_two(self.matrix) return points
def left(self, channel): logic.left(self, channel)