-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (57 loc) · 2 KB
/
main.py
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
import sys
import curses
import time
from mancala.board import Board
from mancala.ai import decide_next_move
DEBUG_MODE = False
def get_screen(mancala_board):
board = mancala_board.board
player_turn = 'Player'
if not mancala_board.player0_turn:
player_turn = 'AI'
return f"""
{board[12]} {board[11]} {board[10]} {board[9]} {board[8]} {board[7]} Turn: {player_turn}
{board[13]} {board[6]}
{board[0]} {board[1]} {board[2]} {board[3]} {board[4]} {board[5]}
""".format(board, player_turn)
def print_screen(stdscr, mancala_board):
if not DEBUG_MODE:
stdscr.clear()
stdscr.addstr(0, 0, get_screen(mancala_board))
stdscr.refresh()
else:
print(get_screen(mancala_board))
def start_game(stdscr):
mancala_board = Board()
if not DEBUG_MODE:
curses.use_default_colors()
curses.curs_set(False)
curses.init_pair(1, curses.COLOR_MAGENTA, -1)
print_screen(stdscr, mancala_board)
while not mancala_board.game_finished():
if not DEBUG_MODE:
key = stdscr.getkey()
else:
key = input()
if key == 'q':
sys.exit(0)
else:
try:
key_as_int = int(key)
if key_as_int in (1, 2, 3, 4, 5, 6) and mancala_board.board[key_as_int - 1] != 0:
mancala_board.make_move(key_as_int - 1)
print_screen(stdscr, mancala_board)
while not mancala_board.player0_turn and not mancala_board.game_finished():
next_move = decide_next_move(mancala_board, 50000)
mancala_board.make_move(next_move)
print_screen(stdscr, mancala_board)
except:
pass
print_screen(stdscr, mancala_board)
if not DEBUG_MODE:
key = stdscr.getkey()
if __name__ == "__main__":
if not DEBUG_MODE:
curses.wrapper(start_game)
else:
start_game(None)