summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authortslil clingman <>2020-01-27 23:12:51 -0800
committertslil clingman <>2020-01-27 23:12:51 -0800
commit04d6efc6eaf31a3ba8c304884725d3c5e9bab9ae (patch)
treed302225eda75570562d04fed20a6d005d0ef24cc /src/main.rs
parent38200848953b1d8d7ba083752c2621603842b54e (diff)
Names, who goes first, date in PTN
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs85
1 files changed, 59 insertions, 26 deletions
diff --git a/src/main.rs b/src/main.rs
index 6a2f70a..e46a855 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,5 @@
extern crate argparse;
+extern crate chrono;
mod consulter;
mod game;
@@ -11,6 +12,8 @@ use crate::gui::*;
use crate::parser::*;
use argparse::*;
+use chrono::Local;
+use std::env;
use std::io::Error;
use std::time::SystemTime;
@@ -79,10 +82,10 @@ fn init_gui(size: u8) -> GUI {
let action_entry = ActionEntry::new(board_bottom, 0, 13);
let message_log = MessageLog::new(
- board_bottom + 1,
+ board_bottom + 2,
0,
height - board_bottom - 1,
- width,
+ board_right,
has_colours,
red_colour,
);
@@ -116,9 +119,13 @@ fn human_action(gui: &GUI, game: &Game) -> Result<Action, Error> {
}
fn main() {
- let mut p1_name = String::from("Player1");
+ let mut p1_name = match env::var("USER") {
+ Err(_) => String::from("Player1"),
+ Ok(user) => user,
+ };
let mut p2_name = String::from("Player2");
- let mut computer_opponent = String::new();
+ let mut engine = String::new();
+ let mut name = String::new();
let mut size: u8 = 5;
{
@@ -128,25 +135,32 @@ fn main() {
ap.refer(&mut p1_name).add_option(
&["--player1"],
Store,
- "Name of first player, Black/White assignment is still random.",
+ "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, ignored if playing against a
- computer opponent.",
+ "Name of second player, defaults to ``Player2'' and is
+ overridden by the arument ENGINE of --engine when applicable.",
);
- ap.refer(&mut computer_opponent).add_option(
+ 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.",
+ 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,
@@ -161,39 +175,57 @@ fn main() {
ap.parse_args_or_exit();
}
- let human_first;
let call_proc;
- if !computer_opponent.is_empty() {
- p2_name = computer_opponent.clone();
+ if !engine.is_empty() {
+ p2_name = engine.clone();
call_proc = true;
- // Perhaps there's a better way.
- human_first = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
- Ok(n) => n.as_secs() % 2 == 0,
- Err(_) => false,
- };
+ // Perhaps there's a better way.
} else {
- human_first = false;
call_proc = false;
}
let gui = init_gui(size);
- let (mut game, warning) = Game::new(size, &p1_name, &p2_name);
+ 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 inp = match game.query_current_player() {
Player::Black => {
- if call_proc && human_first {
- consult_next_action(&game, &computer_opponent)
+ if call_proc && p1_white {
+ consult_next_action(&game, &engine)
} else {
human_action(&gui, &game)
}
}
Player::White => {
- if call_proc && (!human_first) {
- consult_next_action(&game, &computer_opponent)
+ if call_proc && (!p1_white) {
+ consult_next_action(&game, &engine)
} else {
human_action(&gui, &game)
}
@@ -208,10 +240,11 @@ fn main() {
Ok(act) => {
let stone_owner = game.query_stone_owner();
let player = game.query_current_player();
+ let pn = game.query_current_player_name();
gui.message_log.log_message(format!(
"{} performs {}{}",
- player,
+ pn,
act,
if stone_owner != player {
format!(" with a {} stone.", stone_owner)
@@ -224,8 +257,8 @@ fn main() {
gui.message_log.log_error(e);
let current = game.query_current_player();
if call_proc
- && ((human_first && current == Player::Black)
- || ((!human_first) && current == Player::White))
+ && ((p1_white && current == Player::Black)
+ || ((!p1_white) && current == Player::White))
{
gui.message_log.log_error(String::from(
"Error: external and internal game states disagree.",