mod consulter; mod game; mod gui; mod parser; use crate::consulter::*; use crate::game::*; use crate::gui::*; use crate::parser::*; use std::io::Error; fn human_action( action_entry: &ActionEntry, message_log: &MessageLog, game: &Game, ) -> Result { loop { let inp = action_entry.read_action(); match parse_action(&game, &inp) { Err(err) => message_log.log_error(format!("Input \"{}\": {}", inp, err)), Ok(action) => return Ok(action), } } } fn main() { let screen = initscr(); cbreak(); 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); 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.refresh(); let size = 5; 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 mut board_gui = BoardGUI::new( 0, 0, size, cell_height, cell_width, hoff, has_colours, white_colour, black_colour, ); let game_log = GameLog::new( 0, board_right, height, 20, 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 + 1, 0, height - board_bottom - 1, width, has_colours, red_colour, ); let (mut game, warning) = Game::new(5, "tslil", "taktician"); message_log.log_error(warning); game_log.update(&game); const CONSULT_PROC: &str = "/home/tslil/bin/wrap_taktician"; loop { let inp = match game.query_current_player() { Player::Black => consult_next_action(&game, CONSULT_PROC), Player::White => human_action(&action_entry, &message_log, &game), }; match inp { Err(err) => { message_log.log_error(format!("{}", err)); break; } Ok(act) => { let stone_owner = game.query_stone_owner(); let player = game.query_current_player(); message_log.log_message(format!( "{} performs {}{}", player, act, if stone_owner != player { format!(" with a {} stone.", stone_owner) } else { String::from(".") } )); match game.perform_action(act) { Err(e) => { message_log.log_error(e); if game.query_current_player() == Player::Black { // external and internal gamestates diverged break; } } Ok((pos_vec, win)) => { board_gui.update(&game, pos_vec); game_log.update(&game); if let Some(w) = win { message_log.log_message(format!("{}", w)); break; } } } } } action_entry.clear_entry_area(); } screen.getch(); // board_gui.read_action(); endwin(); }