-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ui.py
205 lines (166 loc) · 5.98 KB
/
Ui.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from battleship import Msg, update_visible_ships, load_ships
from battleship import FIELD_HEIGHT, FIELD_WIDTH, FIELD_SIZE
import skimage
import numpy as np
def trace_game(game):
states = {}
probed = {}
for state in game:
if state is None:
break
player_id = state.get("current_player_id", 0)
states[player_id] = state
response = state.response
if response in (Msg.HIT, Msg.SUNK, Msg.MISS):
probed[player_id] = probed.get(
player_id, np.zeros(FIELD_SIZE, np.uint8))
update_visible_ships(probed[player_id], state.pos, state.response)
yield states, probed
if state.response in (Msg.YOU_WON, Msg.YOU_LOST):
break
def run_on_console(game, dt):
from time import sleep
def get_console_printer(field_size=FIELD_HEIGHT):
active_players = [0]
def clear_players(number_of_players):
for _ in range(number_of_players):
print("\033[F\x1b[2K" * (field_size+3))
def print_player(player, trace, probed):
turn_num = trace.get("turn_num")
latest_result = trace.get("response")
pos = trace.get("pos")
print("Player {} after {} moves".format(player, turn_num))
print("Last result: {} on {}".format(latest_result, pos))
print(probed.astype(np.int))
def print_players(states, probed):
clear_players(active_players[0])
for player_id, trace in states.items():
print_player(player_id, trace, probed[player_id])
active_players[0] = len(states)
return print_players
def finish_game():
print("Game ended")
printer = get_console_printer()
for state, probed in trace_game(game):
printer(state, probed)
sleep(dt/1000)
finish_game()
def run_in_qt(game, dt):
from time import sleep
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout, QPushButton
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import QTimer
app = QApplication([])
window = QWidget()
player = QWidget()
layout = QVBoxLayout()
layout_player = QHBoxLayout()
window.setLayout(layout)
player.setLayout(layout_player)
p1 = QLabel()
p2 = QLabel()
continue_btn = QPushButton("Next step")
run_btn = QPushButton("Run")
pause_btn = QPushButton("Pause")
layout_player.addWidget(p1)
layout_player.addWidget(p2)
layout.addWidget(player)
layout.addWidget(continue_btn)
layout.addWidget(run_btn)
layout.addWidget(pause_btn)
timer = QTimer()
game_processor = trace_game(game)
def plot_fields(progress, probed):
for player, field in probed.items():
field = field*80
modelled_data = field.repeat(4)
modelled_data = modelled_data.reshape(FIELD_HEIGHT, FIELD_WIDTH, 4)
if progress[player]["response"] == Msg.YOU_LOST:
modelled_data[:,:,0] = 0
modelled_data[:,:,1] = 0
if progress[player]["response"] == Msg.YOU_WON:
modelled_data[:,:,0] = 0
modelled_data[:,:,2] = 0
img = QImage(modelled_data.data, field.shape[1], field.shape[0], QImage.Format_RGB32)
pix = QPixmap(img).scaled(300,300)
if player == 0:
p1.setPixmap(pix)
if player == 1:
p2.setPixmap(pix)
def on_next_clicked():
try:
progress, probed = next(game_processor)
plot_fields(progress, probed)
except StopIteration:
timer.stop()
def on_run_clicked():
timer.start(dt)
def on_pause_clicked():
timer.stop()
continue_btn.clicked.connect(on_next_clicked)
run_btn.clicked.connect(on_run_clicked)
pause_btn.clicked.connect(on_pause_clicked)
timer.timeout.connect(on_next_clicked)
window.show()
app.exec_()
if __name__ == "__main__":
from battleship import DefaultRules, battle, mini_battle, place_ships
from battleship import RandomAgent, SmartAgent, SuperAgent
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--qt", action="store_true",
default=False, help="Use qt ui")
parser.add_argument("--delay", type=int, default=500,
help="Delay between actions [ms]")
parser.add_argument("--agent1",
choices=["super",
"smart",
"random"],
default="random",
required=True)
parser.add_argument("--agent2",
choices=["super",
"smart",
"random"]
)
parser.add_argument("--ships", nargs="+",
help="Provide paths to ship files", default=[])
args = parser.parse_args()
if len(args.ships) < 1:
print("Cannot find ship files")
exit(1)
ships1 = load_ships(args.ships[0])
if len(args.ships) == 1:
ships2 = ships1
else:
ships2 = load_ships(args.ships[1])
if args.agent1 == "super":
agent = SuperAgent()
elif args.agent1 == "smart":
agent = SmartAgent()
elif args.agent1 == "random":
agent = RandomAgent()
else:
print("No valid agent1, abort")
exit(1)
if args.agent2 is None:
agent2 = None
elif args.agent2 == "super":
agent2 = SuperAgent()
elif args.agent2 == "smart":
agent2 = SmartAgent()
elif args.agent2 == "random":
agent2 = RandomAgent()
else:
agent2 = None
agent.ships = ships1
agent2.ships = ships2
if agent2 is None:
game = mini_battle(agent, ships1)
else:
game = battle((agent2, agent))
if args.qt:
run_in_qt(game, args.delay)
else:
run_on_console(game, args.delay)