Fixed merging of two consecutive numbers and added undo

This commit is contained in:
2020-04-24 20:41:51 +03:00
parent acd0b04963
commit 56a6f363c7
+23 -19
View File
@@ -7,8 +7,7 @@ def game_logic(l):
line_state = True line_state = True
global moved global moved
ls=0 merge=True
#TODO: Should I double merge eg 4 2 2->8 0 0 or 4 4 0
while line_state: while line_state:
for i in range(board_line-1): for i in range(board_line-1):
if l[i+1] == None: if l[i+1] == None:
@@ -18,32 +17,34 @@ def game_logic(l):
l.pop(i) l.pop(i)
l.insert(i,mi) l.insert(i,mi)
l.insert(i+1,None) l.insert(i+1,None)
elif l[i] == l[i+1]: elif l[i] == l[i+1] and merge != False:
mi=l.pop(i) mi=l.pop(i)
l.insert(i,int(mi)*2) l.insert(i,int(mi)*2)
mi=l.pop(i+1) mi=l.pop(i+1)
l.insert(i+1,None) l.insert(i+1,None)
elif l[i] > l[+1]: merge=False
pass oldHash = hash(tuple(l))
moved=True
newHash = hash(tuple(l)) newHash = hash(tuple(l))
if oldHash == newHash: if oldHash == newHash:
# print('Exiting: Boxes didn't move')
line_state = False line_state = False
# print('Exiting: Boxes moved',ls)
else: else:
ls=ls+1 # print('Repeating: Boxes moved')
moved=moved+ls
oldHash = newHash oldHash = newHash
# print('Boxes moved',ls,'Repeating') moved=True
return l return l
def movement(): def movement():
m = input('\nMove the numbers: ') m = input('\nMove the numbers: ')
global moved global moved
moved=0 global board
#TOOO: Implement undo function global board_reset
moved=False
board_temp=copy.deepcopy(board)
#TODO: Simplify movement #TODO: Simplify movement
# board_reset = board # board_reset = board
if m == 'h': if m == 'h':
@@ -65,12 +66,15 @@ def movement():
# print('line',i) # print('line',i)
rl = board[i:i+board_line] rl = board[i:i+board_line]
board[i:i+board_line]=game_logic(rl[::-1])[::-1] board[i:i+board_line]=game_logic(rl[::-1])[::-1]
# elif m == 'u': elif m == 'u':
# board=board_reset board=board_reset
# print(board) print(board)
# raise ValueError('Can not move this way') #raise ValueError('Can not move this way')
else: else:
raise ValueError('Can not move this way') raise ValueError('Can not move this way')
if m != 'u' and moved == True:
board_reset=copy.deepcopy(board_temp)
# try: # try:
# direction in ['h','j','k','l'] # direction in ['h','j','k','l']
# print(direction) # print(direction)
@@ -84,6 +88,8 @@ def print_board():
#TODO: Make a class so i can remove globals and simplify code #TODO: Make a class so i can remove globals and simplify code
# class Game(): # class Game():
# def init(): # def init():
global board
global board_reset
game_state = True game_state = True
board = [] board = []
board_line=3 board_line=3
@@ -95,7 +101,7 @@ for i in range(board_size):
indices = [i for i, x in enumerate(board) if x == None] indices = [i for i, x in enumerate(board) if x == None]
p = random.choice(indices) p = random.choice(indices)
board[p] = random.choices([2,4],weights=[80,20])[0] board[p] = random.choices([2,4],weights=[80,20])[0]
# board_reset=copy.deepcopy(board) board_reset=copy.deepcopy(board)
# def run(): # def run():
global moved global moved
@@ -108,18 +114,16 @@ while game_state:
# print('\nnew choice') # print('\nnew choice')
movement() movement()
except ValueError: except ValueError:
#board=copy.deepcopy(board_reset)
# continue # continue
pass pass
if moved == 0: if moved == False:
continue continue
try: try:
indices = [i for i, x in enumerate(board) if x == None] indices = [i for i, x in enumerate(board) if x == None]
p = random.choice(indices) p = random.choice(indices)
board[p] = random.choices([2,2],weights=[80,20])[0] board[p] = random.choices([2,2],weights=[80,20])[0]
board_reset=copy.deepcopy(board)
print('New number at position',p) print('New number at position',p)
except IndexError: except IndexError:
# catch game over here # catch game over here