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 line_state = True
global moved global moved
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)
l.insert(i+1,None) l.insert(i+1,None)
merge=False merge = False
oldHash = hash(tuple(l)) oldHash = hash(tuple(l))
moved=True moved = True
newHash = hash(tuple(l)) newHash = hash(tuple(l))
@@ -34,7 +32,7 @@ def game_logic(l):
else: else:
# print('Repeating: Boxes moved') # print('Repeating: Boxes moved')
oldHash = newHash oldHash = newHash
moved=True moved = True
return l return l
@@ -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():
@@ -92,16 +96,13 @@ global board
global board_reset global board_reset
game_state = True game_state = True
board = [] board = []
board_line=3 board_line = 3
board_size=board_line**2 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] board_reset = copy.deepcopy(board)
p = random.choice(indices)
board[p] = random.choices([2,4],weights=[80,20])[0]
board_reset=copy.deepcopy(board)
# def run(): # def run():
global moved global moved
@@ -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