diff options
| author | Botahamec <botahamec@outlook.com> | 2021-07-08 20:32:44 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2021-07-08 20:32:44 -0400 |
| commit | 7e73782a7d51b40a558b74793c1d6c1abdea857d (patch) | |
| tree | fba843de026f20cbcca0272c4ef521a6d4ac7ef9 /cli | |
here's what i have so far
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/Cargo.toml | 12 | ||||
| -rw-r--r-- | cli/src/main.rs | 37 | ||||
| -rw-r--r-- | cli/src/perft.rs | 26 |
3 files changed, 75 insertions, 0 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml new file mode 100644 index 0000000..093c594 --- /dev/null +++ b/cli/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "cli" +version = "0.1.0" +authors = ["Mike White <botahamec@outlook.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "2" +ai = {path = "../ai"} +rayon = "1.5"
\ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs new file mode 100644 index 0000000..f7fcddf --- /dev/null +++ b/cli/src/main.rs @@ -0,0 +1,37 @@ +use ai::CheckersBitBoard; +use clap::{App, Arg, SubCommand}; + +mod perft; + +fn main() { + let matches = App::new("Ampere") + .version("0.1") + .author("Botahamec <botahamec@outlook.com>") + .about("An American Checkers AI") + .subcommand( + SubCommand::with_name("perft") + .about("Calculate the number of possible moves") + .arg( + Arg::with_name("depth") + .required(true) + .short("d") + .takes_value(true) + .help("The depth to go to"), + ), + ) + .get_matches(); + + if let Some(matches) = matches.subcommand_matches("perft") { + println!( + "{}", + perft::positions( + CheckersBitBoard::starting_position(), + matches + .value_of("depth") + .unwrap() + .parse::<usize>() + .expect("Error: not a valid number") + ) + ); + } +} diff --git a/cli/src/perft.rs b/cli/src/perft.rs new file mode 100644 index 0000000..eba640e --- /dev/null +++ b/cli/src/perft.rs @@ -0,0 +1,26 @@ +use ai::{CheckersBitBoard, Move, PossibleMoves};
+use rayon::prelude::*;
+use std::fmt::{Display, Formatter};
+
+#[derive(Clone)]
+struct PerftResult {
+ result: Vec<(Move, usize)>,
+}
+
+pub fn positions(board: CheckersBitBoard, depth: usize) -> usize {
+ let moves = PossibleMoves::moves(board);
+
+ if depth == 0 {
+ 1
+ } else {
+ let mut total = 0;
+
+ for current_move in moves {
+ // safety: we got this move out of the list of possible moves, so it's definitely valid
+ let board = unsafe { current_move.apply_to(board) };
+ total += positions(board, depth - 1);
+ }
+
+ total
+ }
+}
|
