aboutsummaryrefslogtreecommitdiff
path: root/fifteen.py
blob: 070494d236966dfd25d88b8055bc84cb23b2b392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import curses
from curses import wrapper
import random

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):
    # make a number of random moves
    for i in range(0,rand_moves):
        r = random.choice(moves)
        if r is 'u':
            do_up()
        elif r is 'd':
            do_down()
        elif r is 'l':
            do_left()
        elif r is 'r':
            do_right()

# True if we've won, False if not
def has_won():
    if board == winning_board:
        return True
    return False

# returns the index of the blank tile
def blank():
    return board.index(16)

# Move validity
def can_down():
    return blank() // 4 != 0
def can_up():
    return blank() // 4 != 3
def can_right():
    return blank() % 4 != 0
def can_left():
    return blank() % 4 != 3

# perform a move
def move(offset):
    oc = blank()
    ov = board[oc]
    tc = blank() + offset
    tv = board[tc]
    board[oc] = tv
    board[tc] = ov
def do_right():
    if can_right():
        move(-1)
        return True
    return False
def do_left():
    if can_left():
        move(1)
        return True
    return False
def do_down():
    if can_down():
        move(-4)
        return True
    return False
def do_up():
    if can_up():
        move(4)
        return True
    return False

# main method
def main(stdscr):

    # set up a board with 400 random moves
    init_board(400)
    mvs = 0
    print_board(stdscr, mvs)

    # loop until someone presses "q"
    while 1:

        # move based on input
        c = stdscr.getch()
        if c == curses.KEY_LEFT:
            if do_left():
                mvs = mvs + 1
        elif c == curses.KEY_RIGHT:
            if do_right():
                mvs = mvs + 1
        elif c == curses.KEY_UP:
            if do_up():
                mvs = mvs + 1
        elif c == curses.KEY_DOWN:
            if do_down():
                mvs = mvs + 1
        elif c == ord('q'):
            break

        # print the new board
        print_board(stdscr, mvs)

# print an empty board
def print_empty_board(stdscr, y, x):
    # print the first line of dashes
    stdscr.addstr(y,x*5,"{:-^21}".format(""), curses.A_BOLD)

    # empty squares
    for i in range(1,5):
        for j in range(0,5):
            stdscr.addstr((i+y), (j+x)*5, "|", curses.A_BOLD)

    # print the last line of dashes
    stdscr.addstr((5+y), x*5,"{:-^21}".format(""), curses.A_BOLD)

# print the tiles of a board
def print_board_tiles(stdscr, b, y, x):

    # create the color pairs
    curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
    curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_RED)

    # put in numbers
    ocnt = y+1
    icnt = (x*5)+1
    for i,v in enumerate(b):
        # odds get one color, evens the other
        if v % 2 == 1:
            stdscr.addstr(ocnt,icnt,"{:^4}".format(v), curses.color_pair(1))
        elif v == 16:
            # blank tile gets no coloring
            stdscr.addstr(ocnt,icnt,"{:^4}".format(""))
        else:
            stdscr.addstr(ocnt,icnt,"{:^4}".format(v), curses.color_pair(2))

        # update counts
        if (i+1) % 4 == 0:
            ocnt = ocnt + 1
            icnt = (x*5) + 1
        else:
            icnt = icnt + 5


def print_board(stdscr, mvs):
    # clear screen
    stdscr.clear()

    # headers
    stdscr.addstr(0, 0, "{:~^56}".format("Fifteen Puzzle"), curses.A_BOLD)
    stdscr.addstr(2, 0, "{:^21}".format("Play"))
    stdscr.addstr(2, 35, "{:^21}".format("Target"))

    # print play board
    print_empty_board(stdscr, 3, 0)
    print_board_tiles(stdscr, board, 3, 0)

    # 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())))

    # refresh the screen
    stdscr.refresh()

# wrap the main method in the curses wrapper
wrapper(main)