Class implementation
This commit is contained in:
@@ -2,14 +2,35 @@
|
|||||||
|
|
||||||
import random, copy, curses
|
import random, copy, curses
|
||||||
|
|
||||||
def game_logic(l):
|
class G2048():
|
||||||
|
def __init__(self):
|
||||||
|
self.board = []
|
||||||
|
|
||||||
|
self.board_line = 3
|
||||||
|
self.board_size = self.board_line**2
|
||||||
|
for i in range(self.board_size):
|
||||||
|
self.board.insert(i,None)
|
||||||
|
|
||||||
|
self.moved = False
|
||||||
|
self.stdscr = curses.initscr()
|
||||||
|
|
||||||
|
self.undo = ['u','r']
|
||||||
|
|
||||||
|
self.up = ['k','w'] #straight
|
||||||
|
self.left = ['h','a'] #straight
|
||||||
|
self.down = ['j','s'] #back
|
||||||
|
self.right = ['l','d'] #back
|
||||||
|
|
||||||
|
self.line = self.left + self.right
|
||||||
|
self.column = self.up + self.down
|
||||||
|
|
||||||
|
def game_logic(self, l):
|
||||||
oldHash = hash(tuple(l))
|
oldHash = hash(tuple(l))
|
||||||
|
|
||||||
line_active= True
|
line_active= True
|
||||||
global moved
|
|
||||||
merged = False
|
merged = False
|
||||||
while line_active:
|
while line_active:
|
||||||
for i in range(board_line-1):
|
for i in range(self.board_line-1):
|
||||||
if l[i] == None and l[i+1] != None:
|
if l[i] == None and l[i+1] != None:
|
||||||
l.pop(i)
|
l.pop(i)
|
||||||
l.insert(i+1,None)
|
l.insert(i+1,None)
|
||||||
@@ -22,7 +43,7 @@ def game_logic(l):
|
|||||||
|
|
||||||
merged = True
|
merged = True
|
||||||
oldHash = hash(tuple(l))
|
oldHash = hash(tuple(l))
|
||||||
moved = True
|
self.moved = True
|
||||||
|
|
||||||
newHash = hash(tuple(l))
|
newHash = hash(tuple(l))
|
||||||
|
|
||||||
@@ -32,128 +53,111 @@ def game_logic(l):
|
|||||||
else:
|
else:
|
||||||
# print('Repeating: Boxes moved')
|
# print('Repeating: Boxes moved')
|
||||||
oldHash = newHash
|
oldHash = newHash
|
||||||
moved = True
|
self.moved = True
|
||||||
|
|
||||||
return l
|
return l
|
||||||
|
|
||||||
def movement():
|
def reverse(self, rl, m):
|
||||||
stdscr.addstr(10, 0, '\nMove the numbers: ')
|
if m in self.left + self.up:
|
||||||
m = stdscr.getkey()
|
gl = self.game_logic(rl) #k
|
||||||
global moved
|
|
||||||
global board
|
|
||||||
global board_reset
|
|
||||||
moved=False
|
|
||||||
board_temp=copy.deepcopy(board)
|
|
||||||
|
|
||||||
undo = ['u','r']
|
|
||||||
|
|
||||||
up = ['k','w'] #straight
|
|
||||||
left = ['h','a'] #straight
|
|
||||||
down = ['j','s'] #back
|
|
||||||
right = ['l','d'] #back
|
|
||||||
|
|
||||||
line = left + right
|
|
||||||
column = up + down
|
|
||||||
|
|
||||||
def rev(rl):
|
|
||||||
if m in left + up:
|
|
||||||
gl = game_logic(rl) #k
|
|
||||||
else:
|
else:
|
||||||
gl = game_logic(rl[::-1])[::-1] #j
|
gl = self.game_logic(rl[::-1])[::-1] #j
|
||||||
return gl
|
return gl
|
||||||
|
|
||||||
if m in line:
|
def movement(self):
|
||||||
for i in range(0,board_size,board_line):
|
self.stdscr.addstr(10, 0, '\nMove the numbers: ')
|
||||||
rl = board[i:i+board_line]
|
m = self.stdscr.getkey()
|
||||||
board[i:i+board_line] = rev(rl)
|
self.moved=False
|
||||||
elif m in column:
|
board_temp=copy.deepcopy(self.board)
|
||||||
for i in range(board_line):
|
self.move(m)
|
||||||
rl = board[i::board_line]
|
|
||||||
board[i::board_line] = rev(rl)
|
if m not in self.undo and self.moved == True:
|
||||||
elif m in undo:
|
self.board_reset = copy.deepcopy(board_temp)
|
||||||
board=board_reset
|
|
||||||
|
def move(self, m):
|
||||||
|
if m in self.line:
|
||||||
|
for i in range(0,self.board_size,self.board_line):
|
||||||
|
rl = self.board[i:i+self.board_line]
|
||||||
|
self.board[i:i+self.board_line] = self.reverse(rl,m)
|
||||||
|
elif m in self.column:
|
||||||
|
for i in range(self.board_line):
|
||||||
|
rl = self.board[i::self.board_line]
|
||||||
|
self.board[i::self.board_line] = self.reverse(rl,m)
|
||||||
|
elif m in self.undo:
|
||||||
|
self.board = self.board_reset
|
||||||
else:
|
else:
|
||||||
raise ValueError('Can not move this way')
|
raise ValueError('Can not move this way')
|
||||||
|
|
||||||
if m not in undo and moved == True:
|
|
||||||
board_reset=copy.deepcopy(board_temp)
|
|
||||||
|
|
||||||
|
def print_board(self):
|
||||||
|
for i in range(0,self.board_size,self.board_line):
|
||||||
|
self.stdscr.addstr(i, 0, '{!s:<4}'.join(' '*(self.board_line+1)).format(*self.board[i:i+self.board_line]))
|
||||||
|
self.stdscr.refresh()
|
||||||
|
|
||||||
def print_board():
|
def add_number(self):
|
||||||
for i in range(0,board_size,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 = []
|
||||||
indices = [i for i, x in enumerate(board) if x == None]
|
indices = [i for i, x in enumerate(self.board) if x == None]
|
||||||
li=len(indices)
|
self.li=len(indices)
|
||||||
p = random.choice(indices)
|
p = random.choice(indices)
|
||||||
board[p] = random.choices([2,4],weights=[80,20])[0]
|
self.board[p] = random.choices([2,4],weights=[80,20])[0]
|
||||||
stdscr.addstr(13, 0, '\nNew number at position '+str(p+1))
|
self.stdscr.addstr(13, 0, '\nNew number at position '+str(p+1))
|
||||||
stdscr.addstr(15,0,str(li)+str(p)+str(indices)+' ')
|
self.stdscr.addstr(15, 0,str(self.li)+str(p)+str(indices)+' ')
|
||||||
except e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
def run(*args):
|
def run(self, *args):
|
||||||
#TODO: Make a class so i can remove globals and simplify code
|
self.game_state = True
|
||||||
# class Game():
|
self.add_number()
|
||||||
# def init():
|
self.board_reset = copy.deepcopy(self.board)
|
||||||
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)
|
|
||||||
|
|
||||||
global stdscr
|
while self.game_state:
|
||||||
stdscr = curses.initscr()
|
|
||||||
add_number()
|
|
||||||
board_reset = copy.deepcopy(board)
|
|
||||||
|
|
||||||
global moved
|
self.print_board()
|
||||||
while game_state:
|
#self.check_playable()
|
||||||
|
|
||||||
print_board()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
movement()
|
self.movement()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
#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:
|
self.stdscr.addstr(13, 0, 'MOVED ')
|
||||||
stdscr.addstr(13, 0, 'DID NOT MOVE')
|
if self.moved == False:
|
||||||
|
self.stdscr.addstr(13, 0, 'DID NOT MOVE')
|
||||||
|
#TODO: Fix continue breaks game over but fixes undo
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
add_number()
|
self.add_number()
|
||||||
except IndexError:
|
except IndexError:
|
||||||
# catch game over here
|
# catch game over here
|
||||||
game_state = False
|
self.game_state = False
|
||||||
print('Game Over')
|
self.stdscr.addstr(15, 0, 'Game Over')
|
||||||
|
#print('Game Over')
|
||||||
|
|
||||||
curses.wrapper(run)
|
def check_playable(self):
|
||||||
|
l=0
|
||||||
|
for i in ['h','j','k','l']:
|
||||||
|
if self.li == 1 and self.moved == False:
|
||||||
|
self.move(i)
|
||||||
|
l=l+1
|
||||||
|
self.game_state = False
|
||||||
|
#print('Game Over')
|
||||||
|
#curses.endwin()
|
||||||
|
#self.print_board()
|
||||||
|
|
||||||
|
if l==4:
|
||||||
|
self.stdscr.addstr(15, 0, 'Game Over')
|
||||||
|
|
||||||
|
def close(self):
|
||||||
state = True
|
state = True
|
||||||
while state:
|
while state:
|
||||||
m = stdscr.getkey()
|
self.stdscr.addstr(16, 0, 'Press q to exit: ')
|
||||||
|
m = self.stdscr.getkey()
|
||||||
if m == 'q':
|
if m == 'q':
|
||||||
state = False
|
state = False
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
|
|
||||||
|
game = G2048()
|
||||||
|
curses.wrapper(game.run)
|
||||||
|
game.close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user