Clean up: spaces, logic duplicate code

This commit is contained in:
2020-04-24 21:33:27 +03:00
parent 56a6f363c7
commit 068598565d
+19 -21
View File
@@ -7,24 +7,22 @@ def game_logic(l):
line_state = True
global moved
merge=True
merge = True
while line_state:
for i in range(board_line-1):
if l[i+1] == None:
pass
elif l[i+1] != None and l[i] == None:
if l[i] == None and l[i+1] != None:
mi=l.pop(i+1)
l.pop(i)
l.insert(i,mi)
l.insert(i+1,None)
elif l[i] == l[i+1] and merge != False:
elif l[i] != None and 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)
merge=False
merge = False
oldHash = hash(tuple(l))
moved=True
moved = True
newHash = hash(tuple(l))
@@ -34,7 +32,7 @@ def game_logic(l):
else:
# print('Repeating: Boxes moved')
oldHash = newHash
moved=True
moved = True
return l
@@ -46,7 +44,6 @@ def movement():
moved=False
board_temp=copy.deepcopy(board)
#TODO: Simplify movement
# board_reset = board
if m == 'h':
for i in range(0,board_size,board_line):
# print('line',i)
@@ -85,6 +82,13 @@ def print_board():
for i in range(0,board_size,board_line):
print(board[i:i+board_line])
def add_number():
global board
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]
print('\nNew number at position',p+1)
#TODO: Make a class so i can remove globals and simplify code
# class Game():
# def init():
@@ -92,16 +96,13 @@ global board
global board_reset
game_state = True
board = []
board_line=3
board_size=board_line**2
board_line = 3
board_size = board_line**2
for i in range(board_size):
board.insert(i,None)
# def add_number():
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)
add_number()
board_reset = copy.deepcopy(board)
# def run():
global moved
@@ -111,20 +112,17 @@ while game_state:
print_board()
try:
# print('\nnew choice')
movement()
except ValueError:
# continue
pass
if moved == False:
print('DID NOT MOVE')
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]
print('New number at position',p)
add_number()
except IndexError:
# catch game over here
game_state = False