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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
extern crate argparse;
extern crate chrono;
mod consulter;
mod game;
mod gui;
mod parser;
use crate::consulter::*;
use crate::game::*;
use crate::gui::*;
use crate::parser::*;
use argparse::*;
use chrono::Local;
use std::env;
use std::time::SystemTime;
struct GUI {
screen: Window,
// width: i32,
// height: i32,
game_log: GameLog,
message_log: MessageLog,
action_entry: ActionEntry,
board_gui: BoardGUI,
}
fn init_gui(size: u8) -> GUI {
let screen = initscr();
cbreak();
noecho();
curs_set(2);
let has_colours = has_colors();
let (black_colour, white_colour, red_colour): (u8, u8, u8) = (1, 2, 3);
if has_colours {
start_color();
use_default_colors();
init_pair(black_colour as i16, -1, COLOR_RED);
init_pair(white_colour as i16, -1, COLOR_BLUE);
init_pair(red_colour as i16, COLOR_RED, -1);
}
screen.addstr(format!(
"takwrap {} | <TAB> to switch GUI element",
env!("CARGO_PKG_VERSION").to_string()
));
screen.mvchgat(0, 0, -1, A_REVERSE, -1);
let voff = 2;
let width = screen.get_max_x();
let height = screen.get_max_y() - voff;
screen.refresh();
let cell_height = 4;
let cell_width = 8;
let hoff = 3;
let board_bottom = voff + (size * cell_height + 3) as i32;
let board_right = (hoff + size * cell_width + 3) as i32;
let board_gui = BoardGUI::new(
voff,
0,
size,
cell_height,
cell_width,
hoff,
has_colours,
white_colour,
black_colour,
);
let game_log = GameLog::new(
voff,
board_right,
height,
width - board_right,
has_colours,
white_colour,
black_colour,
);
// Need room for '8___011111111', for example
let action_entry = ActionEntry::new(board_bottom, 0, 13);
let message_log = MessageLog::new(
board_bottom + 2,
0,
height - board_bottom - 1,
board_right,
has_colours,
red_colour,
);
GUI {
screen,
// width,
// height,
game_log,
message_log,
action_entry,
board_gui,
}
}
fn main() {
let mut p1_name = match env::var("USER") {
Err(_) => String::from("Player1"),
Ok(user) => user,
};
let mut p2_name = String::from("Player2");
let mut engine = String::new();
let mut name = String::new();
let mut size: u8 = 5;
{
let mut ap = ArgumentParser::new();
ap.set_description("Test description.");
ap.refer(&mut p1_name).add_option(
&["--player1"],
Store,
"Name of first player, defaults to $USER. See --name-white below for Black/White assignment.",
);
ap.refer(&mut p2_name).add_option(
&["--player2"],
Store,
"Name of second player, defaults to ``Player2'' and is
overridden by the arument ENGINE of --engine when applicable.",
);
ap.refer(&mut engine).add_option(
&["-e", "--engine"],
Store,
"Name of process which will be used as a computer opponent.
The process must accept a single argument point to a PTN file
of the current game state, and must generate a single PTN action
on STDOUT. The engine will replace the second player, and the name
of player 2 is set to this argument.",
);
ap.refer(&mut name).add_option(
&["--name-white"],
Store,
"Force the player named NAME to play white. If not supplied, assignment is ``random''.",
).metavar("NAME");
ap.refer(&mut size).add_option(
&["-s", "--size"],
Store,
"Size of board (one dimension), default is 5.",
);
ap.add_option(
&["-V", "--version"],
Print(env!("CARGO_PKG_VERSION").to_string()),
"Show version",
);
ap.parse_args_or_exit();
}
let call_proc;
if !engine.is_empty() {
p2_name = engine.clone();
call_proc = true;
// Perhaps there's a better way.
} else {
call_proc = false;
}
let gui = init_gui(size);
let p1_white;
if !name.is_empty() {
if name == p1_name {
p1_white = true;
} else if name == p2_name {
p1_white = false;
} else {
panic!(
"Player named as white ({}) is not a player in this game ({} and {}).",
name, p1_name, p2_name
);
}
} else {
p1_white = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n.as_millis() % 2 == 0,
Err(_) => false,
};
}
let date_string = Local::now().to_string();
let (mut game, warning) = if p1_white {
Game::new(size, &p1_name, &p2_name, &date_string)
} else {
Game::new(size, &p2_name, &p1_name, &date_string)
};
gui.message_log.log_error(warning);
gui.game_log.update(&game);
loop {
let player = game.query_current_player();
let engine_turn = call_proc
&& ((p1_white && player == Player::Black) || ((!p1_white) && player == Player::White));
let result = match player {
Player::Black => {
if call_proc && p1_white {
gui.message_log
.log_message(String::from("Opponent is thinking..."));
consult_next_action(&game, &engine)
} else {
Ok(gui.action_entry.read_action())
}
}
Player::White => {
if call_proc && (!p1_white) {
gui.message_log
.log_message(String::from("Opponent is thinking..."));
consult_next_action(&game, &engine)
} else {
Ok(gui.action_entry.read_action())
}
}
};
match result {
Err(e) => {
gui.message_log.log_error(format!("Engine error: {}", e));
break;
}
Ok(gui_result) => match gui_result {
GUIResult::SwitchElement => {
if engine_turn {
gui.message_log.log_error(String::from(
"Internal error: engine passed switch-element message somehow.",
));
break;
}
}
GUIResult::Undo => {
if engine_turn {
gui.message_log.log_error(String::from(
"Internal error: engine passed undo message somehow.",
));
break;
}
}
GUIResult::Raw(raw) => {
let stone_owner = game.query_stone_owner();
let act = parse_action(stone_owner, &raw);
match act {
Err(err) => {
gui.message_log
.log_error(format!("Input \"{}\": {}", raw, err));
if engine_turn {
break;
}
}
Ok(act) => {
let pn = game.query_current_player_name();
gui.message_log.log_message(format!(
"{} performs {}{}",
pn,
act,
if stone_owner != player {
format!(" with a {} stone.", stone_owner)
} else {
String::from(".")
}
));
match game.perform_action(act) {
Err(e) => {
gui.message_log.log_error(e);
if engine_turn {
gui.message_log.log_error(String::from(
"Error: external and internal game states disagree.",
));
break;
}
}
Ok((pos_vec, win)) => {
gui.board_gui.update(&game, pos_vec);
gui.game_log.update(&game);
if let Some(w) = win {
gui.message_log.log_message(format!("{}", w));
break;
}
}
}
}
}
}
},
}
}
gui.screen.getch();
// board_gui.read_action();
endwin();
}
|