-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·67 lines (57 loc) · 1.83 KB
/
main.py
File metadata and controls
executable file
·67 lines (57 loc) · 1.83 KB
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
#!/usr/bin/python
import curses
import random
import drawing
import autosolver
from board import Board
def main(stdscr):
size = 4
board_length, board_width = size * 5 + 1, size * 2 + 1
pad_pos_x, pad_pos_y = 5, 7
board = Board(size)
auto = False
win = drawing.InitWindow(150)
keypad_actions = {
curses.KEY_UP: board.MoveUp,
curses.KEY_DOWN: board.MoveDown,
curses.KEY_LEFT: board.MoveLeft,
curses.KEY_RIGHT: board.MoveRight, }
win.addstr(1, 0, "Arrow keys to move tiles")
win.addstr(2, 0, "a to enable auto solver")
win.addstr(5, 0, "Score: " + str(board.score))
pad = win.subpad(
board_width, board_length,
pad_pos_y, pad_pos_x)
stdscr.refresh()
win.refresh()
while True:
drawing.DrawingTiles(pad, board)
try:
if not auto:
c = stdscr.getch()
else:
c = autosolver.AutoSolver(board, keypad_actions.keys())
if c == ord('a'):
auto = not auto
if c == ord('q'):
return
else:
moved = keypad_actions[c]()
if moved:
board.NewTile()
drawing.DrawingTiles(pad, board)
win.addstr(5, 0, "Score: " + str(board.score))
win.refresh()
if len(board.empty_pos()) == 0:
if board.GameOver():
drawing.GameOverMsg(
pad_pos_y + board_width, 0,
board, stdscr)
if stdscr.getch() == ord('r'):
return main(stdscr)
return
except KeyError:
pass
drawing.TerminatedWindow(win)
if __name__ == "__main__":
curses.wrapper(main)