-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexample.py
142 lines (121 loc) · 3.43 KB
/
example.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
130
131
132
133
134
135
136
137
138
139
140
141
142
import random
import pisqpipe as pp
from pisqpipe import DEBUG_EVAL, DEBUG
pp.infotext = 'name="pbrain-pyrandom", author="Jan Stransky", version="1.0", country="Czech Republic", www="https://github.com/stranskyjan/pbrain-pyrandom"'
MAX_BOARD = 100
board = [[0 for i in range(MAX_BOARD)] for j in range(MAX_BOARD)]
def brain_init():
if pp.width < 5 or pp.height < 5:
pp.pipeOut("ERROR size of the board")
return
if pp.width > MAX_BOARD or pp.height > MAX_BOARD:
pp.pipeOut("ERROR Maximal board size is {}".format(MAX_BOARD))
return
pp.pipeOut("OK")
def brain_restart():
for x in range(pp.width):
for y in range(pp.height):
board[x][y] = 0
pp.pipeOut("OK")
def isFree(x, y):
return x >= 0 and y >= 0 and x < pp.width and y < pp.height and board[x][y] == 0
def brain_my(x, y):
if isFree(x,y):
board[x][y] = 1
else:
pp.pipeOut("ERROR my move [{},{}]".format(x, y))
def brain_opponents(x, y):
if isFree(x,y):
board[x][y] = 2
else:
pp.pipeOut("ERROR opponents's move [{},{}]".format(x, y))
def brain_block(x, y):
if isFree(x,y):
board[x][y] = 3
else:
pp.pipeOut("ERROR winning move [{},{}]".format(x, y))
def brain_takeback(x, y):
if x >= 0 and y >= 0 and x < pp.width and y < pp.height and board[x][y] != 0:
board[x][y] = 0
return 0
return 2
def brain_turn():
if pp.terminateAI:
return
i = 0
while True:
x = random.randint(0, pp.width)
y = random.randint(0, pp.height)
i += 1
if pp.terminateAI:
return
if isFree(x,y):
break
if i > 1:
pp.pipeOut("DEBUG {} coordinates didn't hit an empty field".format(i))
pp.do_mymove(x, y)
def brain_end():
pass
def brain_about():
pp.pipeOut(pp.infotext)
if DEBUG_EVAL:
import win32gui
def brain_eval(x, y):
# TODO check if it works as expected
wnd = win32gui.GetForegroundWindow()
dc = win32gui.GetDC(wnd)
rc = win32gui.GetClientRect(wnd)
c = str(board[x][y])
win32gui.ExtTextOut(dc, rc[2]-15, 3, 0, None, c, ())
win32gui.ReleaseDC(wnd, dc)
######################################################################
# A possible way how to debug brains.
# To test it, just "uncomment" it (delete enclosing """)
######################################################################
"""
# define a file for logging ...
DEBUG_LOGFILE = "/tmp/pbrain-pyrandom.log"
# ...and clear it initially
with open(DEBUG_LOGFILE,"w") as f:
pass
# define a function for writing messages to the file
def logDebug(msg):
with open(DEBUG_LOGFILE,"a") as f:
f.write(msg+"\n")
f.flush()
# define a function to get exception traceback
def logTraceBack():
import traceback
with open(DEBUG_LOGFILE,"a") as f:
traceback.print_exc(file=f)
f.flush()
raise
# use logDebug wherever
# use try-except (with logTraceBack in except branch) to get exception info
# an example of problematic function
def brain_turn():
logDebug("some message 1")
try:
logDebug("some message 2")
1. / 0. # some code raising an exception
logDebug("some message 3") # not logged, as it is after error
except:
logTraceBack()
"""
######################################################################
# "overwrites" functions in pisqpipe module
pp.brain_init = brain_init
pp.brain_restart = brain_restart
pp.brain_my = brain_my
pp.brain_opponents = brain_opponents
pp.brain_block = brain_block
pp.brain_takeback = brain_takeback
pp.brain_turn = brain_turn
pp.brain_end = brain_end
pp.brain_about = brain_about
if DEBUG_EVAL:
pp.brain_eval = brain_eval
def main():
pp.main()
if __name__ == "__main__":
main()