aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fifteen.py86
1 files changed, 66 insertions, 20 deletions
diff --git a/fifteen.py b/fifteen.py
index 070494d..41121d1 100644
--- a/fifteen.py
+++ b/fifteen.py
@@ -2,8 +2,14 @@ import curses
from curses import wrapper
import random
+winning_boards = {
+ "classic": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
+ "odds/evens": [1,3,5,7,9,11,13,15,2,4,6,8,10,12,14,16],
+ "odds/evens (alt)": [1,3,5,7,2,4,6,8,9,11,13,15,10,12,14,16],
+ "spiral": [7,8,9,10,6,1,2,11,5,4,3,12,16,15,14,13]
+}
+
board = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
-winning_board = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
moves = ['u', 'd', 'l', 'r']
def init_board(rand_moves):
@@ -20,8 +26,8 @@ def init_board(rand_moves):
do_right()
# True if we've won, False if not
-def has_won():
- if board == winning_board:
+def has_won(wb):
+ if board == wb:
return True
return False
@@ -68,36 +74,72 @@ def do_up():
return True
return False
+def choose_board(stdscr):
+
+ # headers
+ stdscr.addstr(0, 0, "{:~^56}".format("Fifteen Puzzle"), curses.A_BOLD)
+ stdscr.addstr(2, 0, "{:^21}".format("Choose a board"))
+
+ # print the first winning board
+ idx = 0
+ board_names = list(winning_boards.keys())
+ print_board_left(stdscr,winning_boards[board_names[idx]],"{:^21}".format(board_names[idx]))
+ stdscr.refresh()
+
+ while 1:
+ c = stdscr.getch()
+ if c == curses.KEY_LEFT:
+ if idx > 0:
+ idx -= 1
+ print_board_left(stdscr,winning_boards[board_names[idx]], "{:^21}".format(board_names[idx]))
+ stdscr.refresh()
+ elif c == curses.KEY_RIGHT:
+ if idx < len(board_names) - 1:
+ idx += 1
+ print_board_left(stdscr,winning_boards[board_names[idx]], "{:^21}".format(board_names[idx]))
+ stdscr.refresh()
+ elif c == ord('p'):
+ return winning_boards[board_names[idx]]
+ elif c == ord('q'):
+ return None
+
+ stdscr.getkey()
+
# main method
def main(stdscr):
+ # choose a board
+ bc = choose_board(stdscr)
+ if bc is None:
+ exit()
+
# set up a board with 400 random moves
init_board(400)
mvs = 0
- print_board(stdscr, mvs)
+ print_board(stdscr, mvs, bc)
# loop until someone presses "q"
while 1:
# move based on input
c = stdscr.getch()
- if c == curses.KEY_LEFT:
+ if c == curses.KEY_LEFT or c == ord('h'):
if do_left():
mvs = mvs + 1
- elif c == curses.KEY_RIGHT:
+ elif c == curses.KEY_RIGHT or c == ord('l'):
if do_right():
mvs = mvs + 1
- elif c == curses.KEY_UP:
+ elif c == curses.KEY_UP or c == ord('k'):
if do_up():
mvs = mvs + 1
- elif c == curses.KEY_DOWN:
+ elif c == curses.KEY_DOWN or c == ord('j'):
if do_down():
mvs = mvs + 1
elif c == ord('q'):
break
# print the new board
- print_board(stdscr, mvs)
+ print_board(stdscr, mvs, bc)
# print an empty board
def print_empty_board(stdscr, y, x):
@@ -139,8 +181,20 @@ def print_board_tiles(stdscr, b, y, x):
else:
icnt = icnt + 5
+# print a board on the left hand side of the screen
+def print_board_left(stdscr, b, cap):
+ print_empty_board(stdscr, 3, 0)
+ print_board_tiles(stdscr, b, 3, 0)
+ stdscr.addstr(9, 0, cap)
+
+# print a board on the left hand side of the screen
+def print_board_right(stdscr, b, cap):
+ print_empty_board(stdscr, 3, 7)
+ print_board_tiles(stdscr, b, 3, 7)
+ stdscr.addstr(9, 35, cap)
-def print_board(stdscr, mvs):
+# print the full boards
+def print_board(stdscr, mvs, wb):
# clear screen
stdscr.clear()
@@ -150,18 +204,10 @@ def print_board(stdscr, mvs):
stdscr.addstr(2, 35, "{:^21}".format("Target"))
# print play board
- print_empty_board(stdscr, 3, 0)
- print_board_tiles(stdscr, board, 3, 0)
+ print_board_left(stdscr, board, "{} moves".format(str(mvs)))
# print target board
- print_empty_board(stdscr, 3, 7)
- print_board_tiles(stdscr, winning_board, 3, 7)
-
- # print the moves
- stdscr.addstr(9, 0, "{} moves".format(str(mvs)))
-
- # print if we won
- stdscr.addstr(9, 35, "Have we won? {}".format(str(has_won())))
+ print_board_right(stdscr, wb, "Have we won? {}".format(str(has_won(wb))))
# refresh the screen
stdscr.refresh()