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