-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathBotDebugger.py
77 lines (69 loc) · 2.07 KB
/
BotDebugger.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 numpy as np
import matplotlib.pyplot as mpl
from threading import Thread
import time
import traceback
import Const
def CF(pct):
if pct == Const.PL_C:
return 'y'
if pct == Const.PL_O:
return 'b'
if pct == 'E':
return 'r'
if pct == -1:
return 'k'
return 'g'
class BotDebugger:
def __init__(self, B):
self.ax = None
self.sct = None #Debug: Scatter plot for displaying grid
self.lf = False #Loop flag
self.B = B #The bot to debug
def DisplayPred(self):
for i in range(self.B.ts.m):
for j in range(self.B.ts.n):
rv = self.B.ts.CellLookup((i, j))
if rv is None:
rv = 'N'
print('{:4s}'.format(rv), end = '')
print('')
def PlotMap(self):
mm = self.B.mm
cp = mm.GetPosition()
if self.B.p is not None:
ppx = [self.B.p[0], cp[0]]
ppy = [self.B.p[1], cp[1]]
else:
ppx, ppy = [cp[0]], [cp[1]]
pc = ['r', 'g']
C, CC = [], []
for qp in mm.GetCells():
C.append(qp[0:2])
CC.append(mm.GetCellType(qp))
C = np.stack(C)
if self.sct is None:
mpl.ion()
fig, self.ax = mpl.subplots()
fig.canvas.manager.window.setGeometry(840, 5, 640, 545)
self.sct = 1
else:
self.ax.clear()
self.ax.scatter(C[:, 0], C[:, 1], color = [CF(j) for j in CC])
self.ax.scatter(ppx, ppy, c = pc, s = 64)
self.ax.scatter([mm.hp[0]], [mm.hp[1]], color = 'm', s = 64)
self.ax.set_title("At: " + str(cp))
mpl.pause(0.1)
def PlotLoopT(self):
while self.lf:
try:
self.PlotMap()
except:
traceback.print_exc()
time.sleep(0.5)
def PlotLoop(self):
self.lf = True
thrd = Thread(target = self.PlotLoopT)
thrd.start()
def PlotStop(self):
self.lf = False