Impement curses

This commit is contained in:
2022-03-03 19:26:14 +02:00
parent 06d53ab9fe
commit a7d28cff02
+70 -40
View File
@@ -1,6 +1,6 @@
#! /usr/bin/python3
import random, copy
import random, copy, curses
def game_logic(l):
oldHash = hash(tuple(l))
@@ -37,7 +37,8 @@ def game_logic(l):
return l
def movement():
m = input('\nMove the numbers: ')
stdscr.addstr(10, 0, '\nMove the numbers: ')
m = stdscr.getkey()
global moved
global board
global board_reset
@@ -71,59 +72,88 @@ def movement():
board[i::board_line] = rev(rl)
elif m in undo:
board=board_reset
print(board)
else:
raise ValueError('Can not move this way')
if m not in undo and moved == True:
board_reset=copy.deepcopy(board_temp)
def print_board():
for i in range(0,board_size,board_line):
print('{!s:<4}'.join(' '*(board_line+1)).format(*board[i:i+board_line]))
stdscr.addstr(i, 0, '{!s:<4}'.join(' '*(board_line+1)).format(*board[i:i+board_line]))
stdscr.refresh()
def add_number():
try:
global board
global li
indices = []
indices = [i for i, x in enumerate(board) if x == None]
li=len(indices)
p = random.choice(indices)
board[p] = random.choices([2,4],weights=[80,20])[0]
stdscr.addstr(13, 0, '\nNew number at position '+str(p+1))
stdscr.addstr(15,0,str(li)+str(p)+str(indices)+' ')
except e:
print(e)
def run(*args):
#TODO: Make a class so i can remove globals and simplify code
# class Game():
# def init():
global li
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)
global board_reset
game_state = True
board = []
global board_size
global board_line
board_line = 2
board_size = board_line**2
for i in range(board_size):
board.insert(i,None)
#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
board_size = board_line**2
for i in range(board_size):
board.insert(i,None)
global stdscr
stdscr = curses.initscr()
add_number()
board_reset = copy.deepcopy(board)
add_number()
board_reset = copy.deepcopy(board)
global moved
while game_state:
# def run():
global moved
while game_state:
print_board()
print('\nnew choice')
print_board()
try:
movement()
except ValueError:
# continue
pass
try:
movement()
except ValueError:
# continue
pass
if moved == False and li == 0:
#game_state = False
stdscr.addstr(15, 0, 'Game Over')
#print('Game Over')
#curses.endwin()
return
stdscr.addstr(13, 0, 'MOVED ')
if moved == False:
print('DID NOT MOVE')
continue
if moved == False:
stdscr.addstr(13, 0, 'DID NOT MOVE')
continue
try:
add_number()
except IndexError:
# catch game over here
game_state = False
print('Game Over')
try:
add_number()
except IndexError:
# catch game over here
game_state = False
print('Game Over')
curses.wrapper(run)
state = True
while state:
m = stdscr.getkey()
if m == 'q':
state = False
curses.endwin()