-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayManually.py
77 lines (62 loc) · 1.77 KB
/
PlayManually.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
import Game
class _Getch:
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
def GetActionFromKeyBoard():
try:
print('Do an action (4:Left 6:right 8:up 5: down):')
actionInt=int(getch())
action = Game.Action.Left
if actionInt == 4:
action = Game.Action.Left
elif actionInt == 6:
action = Game.Action.Right
elif actionInt == 8:
action = Game.Action.Up
elif actionInt == 5:
action = Game.Action.Down
else:
raise ValueError(str(actionInt) + ' is an invalid input')
return action
except ValueError as err:
print(err.args)
return GetActionFromKeyBoard()
def Play():
state = Game.CreateState(None, None, None)
print(state)
maxMovesCount = 5
nbMoves = 0
while (not state.IsFinished()):
if nbMoves == maxMovesCount:
print('lost because did too many moves')
return
action = GetActionFromKeyBoard()
print(str(action) + '\n')
state.Move(action)
print(state)
nbMoves += 1
Play()