diff --git a/Cargo.lock b/Cargo.lock index 1987e34..aba0f45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,95 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "c2-chacha" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "getrandom" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ppv-lite86" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_chacha" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tictactoe" version = "0.1.0" +dependencies = [ + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] +[[package]] +name = "wasi" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" +"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e7db7ca94ed4cd01190ceee0d8a8052f08a247aa1b469a7f68c6a3b71afcf407" +"checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" +"checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" +"checksum rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412" +"checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" +"checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +"checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +"checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d" diff --git a/Cargo.toml b/Cargo.toml index a12d0c2..434d956 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,11 @@ authors = ["shimun "] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.release] +opt-level = 'z' # Optimize for size. +lto = true # Enable Link Time Optimization +codegen-units = 1 # Reduce number of codegen units to increase optimizations. +panic = 'abort' # Abort on panic [dependencies] +rand = "0.7.2" diff --git a/src/main.rs b/src/main.rs index 16258c7..1595157 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use rand::Rng; use std::env::args; use std::fmt; use std::io; @@ -169,17 +170,15 @@ fn main() { let dim = board.dimension(); (board, criteria.unwrap_or(dim)) }; + let game_mode = 1usize; let stdin = std::io::stdin(); - println!("{}", &board); - let winner = loop { - if let Some(winner) = board.winner_with_criteria(criteria) { - break winner; - } - let mut input = String::new(); - for s in State::players() { - loop { + let mut input = String::new(); + let mut rng = rand::thread_rng(); + let mut next_move = |player: &State, board: &Board| -> (usize, usize) { + match game_mode { + 0 => loop { let (x, y) = loop { - print!("{}, your move: (x y) ", s); + print!("{}, your move: (x y) ", player); io::stdout().flush().unwrap(); input.clear(); stdin.read_line(&mut input).unwrap(); @@ -204,12 +203,36 @@ fn main() { }; match board[(x, y)] { State::N => { - board[(x, y)] = *s; - break; + break (x, y); } _ => eprintln!("({}, {}) is already occupied! Try again", x, y), } + }, + _ => { + //Pretty naive + let dim = board.dimension(); + std::thread::sleep(std::time::Duration::from_millis( + 1000 * (9.0 / ((dim * dim) as f64)) as u64, + )); + loop { + let x: usize = rng.gen_range(0, dim); + let y: usize = rng.gen_range(0, dim); + if board[(x, y)] == State::N { + break (x, y); + } + } } + } + }; + + println!("{}", &board); + let winner = loop { + if let Some(winner) = board.winner_with_criteria(criteria) { + break winner; + } + for s in State::players() { + let movee = next_move(s, &board); + board[movee] = *s; if let Some(_) = board.winner_with_criteria(criteria) { break; }