class test2048: def __init__(self, manual_input=True, random=True, steps=10, sleep=0): self.gamegrid = GameGrid(manual_input=manual_input) self.random = random self.steps = steps self.sleep = sleep def run(self, input_value=None): if self.random: self.run_random() 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 get_status(self): return self.gamegrid.matrix def take_step(self, inp): event_rn.char = chr(inp) self.gamegrid.key_down(event_rn)
class run2048: def __init__(self, manual_input=True, random=True, steps=10, sleep=0, log=False): self.gamegrid = GameGrid(manual_input) if log: print('Initial GameGrid') print(self.gamegrid.matrix) self.random = random self.steps = steps self.sleep = sleep self.log = log self.gamegrid.win_status = False self.step = 0 self.old_matrix = [] self.check_value = 0 def run(self, input_value=None): self.old_matrix = self.gamegrid.matrix # if game_state(self.gamegrid.matrix) == 'lose': # if self.random and input_value is None: input_value = randint(0, 3) assert input_value in range(4) event_rn.char = chr(input_value) self.take_step(event_rn) if self.log: print(self.gamegrid.matrix) time.sleep(self.sleep) self.step += 1 #def run_random(self): # for k in range(self.steps): # num = randint(0,3) # event_rn.char = chr(num) # self.take_step(event_rn) # if game_state(self.gamegrid.matrix) == 'lose': # return # self.step += 1 # time.sleep(self.sleep) def get_status(self): # Need to figure out 'lose' state from check_matrix return self.gamegrid.matrix, int(self.check_matrix()) def take_step(self, inp): self.gamegrid.key_down(inp) def check_matrix(self): if self.old_matrix == self.gamegrid.matrix: self.check_value += 1 else: self.check_value = 0 return self.check_value