From a51bcf3262350c4316a6bf7eaeee3134d5cd7e31 Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Tue, 28 Jan 2020 12:08:35 -0800 Subject: restructured main loop to handle GUI element focus --- src/main.rs | 155 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 89 insertions(+), 66 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index e46a855..9ddaa50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,6 @@ fn init_gui(size: u8) -> GUI { noecho(); curs_set(2); - let width = screen.get_max_x(); - let height = screen.get_max_y(); - let has_colours = has_colors(); let (black_colour, white_colour, red_colour): (u8, u8, u8) = (1, 2, 3); @@ -47,17 +44,28 @@ fn init_gui(size: u8) -> GUI { init_pair(red_colour as i16, COLOR_RED, -1); } + screen.addstr(format!( + "takwrap {} | 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 = (size * cell_height + 3) as i32; - let board_right = (size * cell_width + hoff + 3) as i32; + 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( - 0, + voff, 0, size, cell_height, @@ -69,10 +77,10 @@ fn init_gui(size: u8) -> GUI { ); let game_log = GameLog::new( - 0, + voff, board_right, height, - 20, + width - board_right, has_colours, white_colour, black_colour, @@ -101,23 +109,6 @@ fn init_gui(size: u8) -> GUI { } } -fn human_action(gui: &GUI, game: &Game) -> Result { - loop { - let inp = gui.action_entry.read_action(); - match parse_action(game.query_stone_owner(), &inp) { - Err(err) => { - gui.message_log - .log_error(format!("Input \"{}\": {}", inp, err)); - gui.action_entry.clear_entry_area(); - } - Ok(action) => { - gui.action_entry.clear_entry_area(); - return Ok(action); - } - } - } -} - fn main() { let mut p1_name = match env::var("USER") { Err(_) => String::from("Player1"), @@ -215,68 +206,100 @@ fn main() { gui.game_log.update(&game); loop { - let inp = match game.query_current_player() { + 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 { - human_action(&gui, &game) + 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 { - human_action(&gui, &game) + Ok(gui.action_entry.read_action()) } } }; - match inp { - Err(err) => { - gui.message_log.log_error(format!("{}", err)); + match result { + Err(e) => { + gui.message_log.log_error(format!("Engine error: {}", e)); break; } - 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 {}{}", - pn, - act, - if stone_owner != player { - format!(" with a {} stone.", stone_owner) - } else { - String::from(".") + 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; } - )); - match game.perform_action(act) { - Err(e) => { - gui.message_log.log_error(e); - let current = game.query_current_player(); - if call_proc - && ((p1_white && current == Player::Black) - || ((!p1_white) && current == Player::White)) - { - gui.message_log.log_error(String::from( - "Error: external and internal game states disagree.", - )); - break; - } + } + GUIResult::Undo => { + if engine_turn { + gui.message_log.log_error(String::from( + "Internal error: engine passed undo message somehow.", + )); + 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; + } + 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); + let current = game.query_current_player(); + 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; + } + } + } } } } - } + }, } } -- cgit v1.3.1