From ecf122ab6ebeba5d90226b2302bba2df6bac1165 Mon Sep 17 00:00:00 2001 From: dece Date: Thu, 4 Jun 2020 19:20:22 +0200 Subject: [PATCH] cli: remove module, too lazy to maintain --- src/cli.rs | 66 ----------------------------------------------------- src/main.rs | 25 -------------------- 2 files changed, 91 deletions(-) delete mode 100644 src/cli.rs diff --git a/src/cli.rs b/src/cli.rs deleted file mode 100644 index 9c14d19..0000000 --- a/src/cli.rs +++ /dev/null @@ -1,66 +0,0 @@ -//! Functions to play games over the command-line. - -use std::io::{self, Write}; - -use rand::seq::IteratorRandom; - -use crate::board; -use crate::rules; - -pub fn start_game(player_color: u8) { - println!("Starting new game."); - println!("Player is {}.", if player_color == board::SQ_WH { "white" } else { "black" }); - println!(""); - let ai_color = board::opposite(player_color); - let mut rng = rand::thread_rng(); - let mut b = board::new(); - let mut turn = board::SQ_WH; - loop { - board::draw(&b, &mut io::stdout()); - println!(""); - let m = if turn == player_color { - println!("Player turn."); - let legal_moves = rules::get_player_legal_moves(&b, player_color); - let mut m; - loop { - m = get_player_move(); - if legal_moves.contains(&m) { - break - } else { - println!("Illegal move."); - } - } - m - } else { - println!("Computer turn."); - let moves = rules::get_player_legal_moves(&b, ai_color); - *moves.iter().choose(&mut rng).unwrap() - }; - println!("Move: {:?}", m); - board::apply_into(&mut b, &m); - println!(""); - turn = board::opposite(turn); - } -} - -fn get_player_move() -> board::Move { - loop { - let from = if let Some(s) = get_input("From: ") { board::pos(&s) } else { continue }; - let to = if let Some(s) = get_input("To: ") { board::pos(&s) } else { continue }; - if board::is_valid_pos(from) && board::is_valid_pos(to) { - return (from, to, None) // TODO this does not handle promotion. - } - println!("Bad input."); - } - -} - -fn get_input(message: &str) -> Option { - print!("{}", message); - io::stdout().flush().unwrap(); - let mut input = String::new(); - if io::stdin().read_line(&mut input).is_err() { - return None - } - Some(input) -} diff --git a/src/main.rs b/src/main.rs index 48e5a2d..9b119a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use std::process; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; pub mod board; -pub mod cli; pub mod engine; pub mod notation; pub mod rules; @@ -12,12 +11,6 @@ pub mod uci; fn main() { let matches = App::new("Vatu") .setting(AppSettings::ArgRequiredElseHelp) - .subcommand(SubCommand::with_name("cli") - .about("Start a game in command-line") - .arg(Arg::with_name("color") - .help("Color for the player") - .short("c").long("color").takes_value(true).required(false) - .possible_values(&["w", "white", "b", "black"]))) .subcommand(SubCommand::with_name("uci") .about("Start engine in UCI mode") .arg(Arg::with_name("log_file") @@ -26,29 +19,11 @@ fn main() { .get_matches(); process::exit(match matches.subcommand() { - ("cli", Some(a)) => cmd_cli(a), ("uci", Some(a)) => cmd_uci(a), _ => 0, }) } -fn cmd_cli(args: &ArgMatches) -> i32 { - let color = if let Some(c) = args.value_of("color") { - match c { - s if ["w", "white"].contains(&s) => board::SQ_WH, - s if ["b", "black"].contains(&s) => board::SQ_BL, - _ => { eprintln!("Choose white or black as color."); return 1 } - } - } else if rand::random() { - board::SQ_WH - } else { - board::SQ_BL - }; - - cli::start_game(color); - 0 -} - fn cmd_uci(args: &ArgMatches) -> i32 { let output = args.value_of("log_file"); uci::Uci::start(output);