Impement curses

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