-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_tkinter.py
132 lines (110 loc) · 3.27 KB
/
main_tkinter.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
import subprocess
import serial
from othello_py import *
import tkinter as tk
serial_port = '/dev/ttyUSB0'
executable = '"./Egaroucid_for_Console/Egaroucid_for_Console.out" -level 10 -t 2 -quiet'
robot = serial.Serial(serial_port, 115200, timeout=1000000)
egaroucid = subprocess.Popen(executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True)
def send_cmds(cmds):
for cmd in cmds:
print(cmd)
cmd += '\n'
robot.write(cmd.encode())
print(robot.read(4))
response = robot.read()
print(response)
o = othello()
o.check_legal()
record = ''
def next_move():
global o, record
grid_str = 'setboard '
for yy in range(hw):
for xx in range(hw):
if o.grid[yy][xx] == black:
grid_str += 'X'
elif o.grid[yy][xx] == white:
grid_str += 'O'
else:
grid_str += '-'
grid_str += ' '
grid_str += 'X' if o.player == black else 'O'
grid_str += '\n'
egaroucid.stdin.write(grid_str.encode('utf-8'))
egaroucid.stdin.flush()
egaroucid.stdin.write('go\n'.encode('utf-8'))
egaroucid.stdin.flush()
coord = egaroucid.stdout.readline().decode()
print(coord)
record += coord
print(record)
policy = [int(coord[1]) - 1, ord(coord[0]) - ord('a')]
flips = o.flippable(policy[0], policy[1])
cmds = []
cmds.append('600')
cmds.append(str(o.player) + str(policy[1]) + str(policy[0]))
cmds_additional = []
for flip in flips:
cmds_additional.append(('3' if o.player == 0 else '2') + str(flip[1]) + str(flip[0]))
cmds_additional.sort(reverse=True)
cmds.extend(cmds_additional)
cmds.append(str(o.player + 4) + '00')
print(cmds)
send_cmds(cmds)
o.move(policy[0], policy[1])
print(policy)
o.print_info()
if not o.check_legal():
o.player = 1 - o.player
if not o.check_legal():
o.print_info()
egaroucid.kill()
o.player = -1
print('終局しました')
s = ''
if o.player == 0:
s += '*'
else:
s += ' '
s += '● '
s += str(o.n_stones[0])
s += ' - '
s += str(o.n_stones[1])
s += ' ○'
if o.player == 1:
s += '*'
else:
s += ' '
o.print_info()
def reset():
global o, record
o = othello()
o.check_legal()
o.print_info()
record = ''
def robot_reset():
quit_cmds = ['000', '107']
send_cmds(quit_cmds)
def exit_all():
egaroucid.kill()
quit_cmds = ['000', '107']
send_cmds(quit_cmds)
robot.close()
exit()
root = tk.Tk()
root.title("Isevot")
root.geometry("500x400")
frame = tk.Frame(root)
frame.pack(fill = tk.BOTH, padx=20,pady=10)
BUTTON_WIDTH = 450
BUTTON_HEIGHT = 5
next_button = tk.Button(frame, text="next", command=next_move, width=BUTTON_WIDTH, height=BUTTON_HEIGHT)
next_button.pack()
reset_button = tk.Button(frame, text="reset", command=reset, width=BUTTON_WIDTH, height=BUTTON_HEIGHT)
reset_button.pack()
reset_button = tk.Button(frame, text="robot_reset", command=robot_reset, width=BUTTON_WIDTH, height=BUTTON_HEIGHT)
reset_button.pack()
exit_button = tk.Button(frame, text="exit", command=exit_all, width=BUTTON_WIDTH, height=BUTTON_HEIGHT)
exit_button.pack()
root.mainloop()