cli: remove module, too lazy to maintain
This commit is contained in:
parent
0daece72d3
commit
ecf122ab6e
66
src/cli.rs
66
src/cli.rs
|
@ -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<String> {
|
||||
print!("{}", message);
|
||||
io::stdout().flush().unwrap();
|
||||
let mut input = String::new();
|
||||
if io::stdin().read_line(&mut input).is_err() {
|
||||
return None
|
||||
}
|
||||
Some(input)
|
||||
}
|
25
src/main.rs
25
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);
|
||||
|
|
Reference in a new issue