-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
131 lines (109 loc) · 3.7 KB
/
manager.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
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
from sys import platform
from core.gui import GameGrid
from core.logic import *
from core.utils import Actions
import copy
from algorithms.expectimax import expectimax
ACTIONS_MAP = {'w': 'UP', 'W': 'UP', 's': 'DOWN', 'S': 'DOWN',
'a': 'LEFT', 'A': 'LEFT', 'd': 'RIGHT', 'D': 'RIGHT'}
def win32_press():
import keyboard
flag = True
p = None
while flag:
try:
for i in ACTIONS_MAP:
if keyboard.is_pressed(i):
p = i
flag = False
break
for i in ["q", "Q"]:
if keyboard.is_pressed(i):
p = i
flag = False
break
except:
pass
return p
def linux_press():
p = key_press()
while p not in ACTIONS_MAP:
print("key press not recognized, please press wasd or WASD")
p = key_press()
return p
def run_console():
board = make_board(4)
initial_two(board)
print_board(board)
curr_score = 0
while not check_end(board):
print("\ncurrent score is, ", curr_score)
print("Possible actions: up, left, right, down, exit")
print("Please press WASD for UP LEFT DOWN RIGHT Q for exit")
if platform.startswith('linux') or platform == 'darwin':
p = linux_press()
elif platform == 'win32' or platform == 'cygwin':
p = win32_press()
else:
p = win32_press()
if p == 'q' or p == 'Q':
break
action = ACTIONS_MAP[p]
action = action.upper()
if action == "EXIT":
break
# take action here to do the move
# and clear current , then print board
if can_move(board, action):
move(board, action)
curr_score = add_up(board, action, curr_score)
# move(board, action)
clear()
simple_add_num(board)
print_board(board)
else:
clear()
print_board(board)
print("/nGame end/nTo run this game, type run_keyboard()")
def run_gui(algo):
GameGrid(AI_mode=True, which_AI=algo)
def run(mode = "gui", algo = "expectimax"):
if mode == "gui":
run_gui(algo = algo)
elif mode == "console":
run_console()
elif mode == "score":
# This part is put here just for test visualizer.
# Should be encapsulated in the future.
if algo == "expectimax":
board = make_board(4)
initial_two(board)
curr_score = 0
while not check_end(board):
depth = 2
best_move = None
best_val = -1
for direction in Actions:
if not can_move(board, direction):
# clear()
continue
temp_board = copy.deepcopy(board)
move(temp_board, direction)
add_up(temp_board, direction, 0)
move(temp_board, direction)
alpha = expectimax(temp_board, depth)
if best_val < alpha:
best_val = alpha
best_move = direction
move(board,best_move)
curr_score += add_up_v2(board, best_move)
move(board,best_move)
simple_add_num(board)
max_cell = find_max_cell(board)
return curr_score, max_cell
else:
raise ValueError("No such algorithm: {}".format(algo))
else:
raise ValueError("Unexpect Mode. Choose one from 'gui' or 'console'")
if __name__ == "__main__":
run(mode = "console", algo = "minimax_pruning")