Impement curses
This commit is contained in:
@@ -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,44 +72,56 @@ 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]
|
||||
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)
|
||||
|
||||
#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):
|
||||
def run(*args):
|
||||
#TODO: Make a class so i can remove globals and simplify code
|
||||
# class Game():
|
||||
# def init():
|
||||
global li
|
||||
global board
|
||||
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)
|
||||
|
||||
add_number()
|
||||
board_reset = copy.deepcopy(board)
|
||||
global stdscr
|
||||
stdscr = curses.initscr()
|
||||
add_number()
|
||||
board_reset = copy.deepcopy(board)
|
||||
|
||||
# def run():
|
||||
global moved
|
||||
while game_state:
|
||||
global moved
|
||||
while game_state:
|
||||
|
||||
print('\nnew choice')
|
||||
print_board()
|
||||
|
||||
try:
|
||||
@@ -117,8 +130,16 @@ while game_state:
|
||||
# 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')
|
||||
stdscr.addstr(13, 0, 'DID NOT MOVE')
|
||||
continue
|
||||
|
||||
try:
|
||||
@@ -127,3 +148,12 @@ while game_state:
|
||||
# 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()
|
||||
|
||||
Reference in New Issue
Block a user