Clean up: spaces, logic duplicate code

This commit is contained in:
2020-04-24 21:33:27 +03:00
parent 56a6f363c7
commit 068598565d
+12 -14
View File
@@ -10,14 +10,12 @@ def game_logic(l):
merge = True merge = True
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] == None and l[i+1] != None:
pass
elif l[i+1] != None and l[i] == None:
mi=l.pop(i+1) mi=l.pop(i+1)
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] and merge != False: elif l[i] != None and 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)
@@ -46,7 +44,6 @@ def movement():
moved=False moved=False
board_temp=copy.deepcopy(board) board_temp=copy.deepcopy(board)
#TODO: Simplify movement #TODO: Simplify movement
# board_reset = board
if m == 'h': if m == 'h':
for i in range(0,board_size,board_line): for i in range(0,board_size,board_line):
# print('line',i) # print('line',i)
@@ -85,6 +82,13 @@ def print_board():
for i in range(0,board_size,board_line): for i in range(0,board_size,board_line):
print(board[i:i+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 #TODO: Make a class so i can remove globals and simplify code
# class Game(): # class Game():
# def init(): # def init():
@@ -97,10 +101,7 @@ board_size=board_line**2
for i in range(board_size): for i in range(board_size):
board.insert(i,None) board.insert(i,None)
# def add_number(): 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) board_reset = copy.deepcopy(board)
# def run(): # def run():
@@ -111,20 +112,17 @@ while game_state:
print_board() print_board()
try: try:
# print('\nnew choice')
movement() movement()
except ValueError: except ValueError:
# continue # continue
pass pass
if moved == False: if moved == False:
print('DID NOT MOVE')
continue continue
try: try:
indices = [i for i, x in enumerate(board) if x == None] add_number()
p = random.choice(indices)
board[p] = random.choices([2,2],weights=[80,20])[0]
print('New number at position',p)
except IndexError: except IndexError:
# catch game over here # catch game over here
game_state = False game_state = False