Clean up: spaces, logic duplicate code
This commit is contained in:
@@ -10,14 +10,12 @@ def game_logic(l):
|
||||
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)
|
||||
@@ -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():
|
||||
@@ -97,10 +101,7 @@ 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]
|
||||
add_number()
|
||||
board_reset = copy.deepcopy(board)
|
||||
|
||||
# def run():
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user