Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
b29f0d4062
|
|||
6af10ba53d
|
|||
e703d914ed
|
89
Cargo.lock
generated
89
Cargo.lock
generated
@@ -1,6 +1,95 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# 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]]
|
[[package]]
|
||||||
name = "tictactoe"
|
name = "tictactoe"
|
||||||
version = "0.1.0"
|
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"
|
||||||
|
@@ -5,5 +5,11 @@ authors = ["shimun <shimun@shimun.net>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# 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]
|
[dependencies]
|
||||||
|
rand = "0.7.2"
|
||||||
|
172
src/main.rs
172
src/main.rs
@@ -1,7 +1,9 @@
|
|||||||
|
use rand::Rng;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::iter;
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
@@ -12,6 +14,12 @@ enum State {
|
|||||||
N,
|
N,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn players() -> &'static [State] {
|
||||||
|
&[State::O, State::X]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for State {
|
impl fmt::Display for State {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
@@ -86,11 +94,7 @@ impl FromStr for Board {
|
|||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
fn new(size: usize) -> Board {
|
fn new(size: usize) -> Board {
|
||||||
let mut b = Vec::with_capacity(size * size);
|
Board(iter::repeat(State::N).take(size * size).collect())
|
||||||
for _ in 0..b.capacity() {
|
|
||||||
b.push(State::N);
|
|
||||||
}
|
|
||||||
Board(b)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dimension(&self) -> usize {
|
fn dimension(&self) -> usize {
|
||||||
@@ -107,69 +111,52 @@ impl Board {
|
|||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
fn winner(&self) -> Option<State> {
|
fn winner_with_criteria(&self, criteria: usize) -> Option<State> {
|
||||||
let mut winners = [State::N; 4];
|
for s in State::players() {
|
||||||
let dim = self.dimension();
|
let update = |coords: (usize, usize), counter: &mut usize| {
|
||||||
fn winner(winners: &[State]) -> Option<State> {
|
if self[coords] == *s {
|
||||||
winners
|
*counter += 1;
|
||||||
.into_iter()
|
} else {
|
||||||
.find(|w| *w != &State::N)
|
*counter = 0;
|
||||||
.map(|w| w.clone())
|
}
|
||||||
}
|
};
|
||||||
for x in 0..dim {
|
let winner = |candidates: &[usize]| -> bool {
|
||||||
for y in 0..dim {
|
candidates.iter().find(|c| *c >= &criteria).is_some()
|
||||||
let row = (y, x);
|
};
|
||||||
let col = (x, y);
|
let (mut diar, mut dial) = (0usize, 0usize);
|
||||||
|
for i in 0..self.dimension() {
|
||||||
{
|
let (mut row, mut col) = (0usize, 0usize);
|
||||||
let r = self[row];
|
for j in 0..self.dimension() {
|
||||||
let c = self[col];
|
update((i, j), &mut row);
|
||||||
winners[0] = if y == 0 || winners[0] == r {
|
update((j, i), &mut col);
|
||||||
r
|
if winner(&[row, col]) {
|
||||||
} else {
|
return Some(*s);
|
||||||
State::N
|
}
|
||||||
};
|
}
|
||||||
|
update((i, i), &mut dial);
|
||||||
winners[1] = if y == 0 || winners[1] == c {
|
update((self.dimension() - i - 1, i), &mut diar);
|
||||||
c
|
if winner(&[diar, dial]) {
|
||||||
} else {
|
return Some(*s);
|
||||||
State::N
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
|
||||||
//diagonal
|
|
||||||
let diag_left = (x, x);
|
|
||||||
let diag_right = (dim - x - 1, x);
|
|
||||||
|
|
||||||
let l = self[diag_left];
|
|
||||||
let r = self[diag_right];
|
|
||||||
winners[2] = if x == 0 || winners[2] == l {
|
|
||||||
l
|
|
||||||
} else {
|
|
||||||
State::N
|
|
||||||
};
|
|
||||||
|
|
||||||
winners[3] = if x == 0 || winners[3] == r {
|
|
||||||
r
|
|
||||||
} else {
|
|
||||||
State::N
|
|
||||||
};
|
|
||||||
}
|
|
||||||
//Row and Col winners can be determined after one Y pass
|
|
||||||
if let Some(winner) = winner(&winners[0..2]) {
|
|
||||||
return Some(winner);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Diagonal winners require a full X pass
|
None.or(Some(State::N).filter(|_| self.0.iter().filter(|s| *s == &State::N).count() == 0))
|
||||||
winner(&winners[2..4])
|
}
|
||||||
.or(Some(State::N).filter(|_| self.0.iter().filter(|s| *s == &State::N).count() == 0))
|
|
||||||
|
#[allow(unused)]
|
||||||
|
fn winner(&self) -> Option<State> {
|
||||||
|
self.winner_with_criteria(self.dimension())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut board = {
|
let (mut board, criteria) = {
|
||||||
if let Some(arg) = args().skip(1).next() {
|
let mut args = args().skip(1);
|
||||||
|
let (size, criteria) = (
|
||||||
|
args.next(),
|
||||||
|
args.next().and_then(|c| c.parse::<usize>().ok()),
|
||||||
|
);
|
||||||
|
let board = if let Some(arg) = size {
|
||||||
if let Ok(size) = arg.parse::<usize>() {
|
if let Ok(size) = arg.parse::<usize>() {
|
||||||
Board::new(size)
|
Board::new(size)
|
||||||
} else if let Ok(board) = Board::from_str(&arg) {
|
} else if let Ok(board) = Board::from_str(&arg) {
|
||||||
@@ -179,19 +166,19 @@ fn main() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Board::default()
|
Board::default()
|
||||||
}
|
};
|
||||||
|
let dim = board.dimension();
|
||||||
|
(board, criteria.unwrap_or(dim))
|
||||||
};
|
};
|
||||||
|
let game_mode = 1usize;
|
||||||
let stdin = std::io::stdin();
|
let stdin = std::io::stdin();
|
||||||
println!("{}", &board);
|
let mut input = String::new();
|
||||||
let winner = loop {
|
let mut rng = rand::thread_rng();
|
||||||
if let Some(winner) = board.winner() {
|
let mut next_move = |player: &State, board: &Board| -> (usize, usize) {
|
||||||
break winner;
|
match game_mode {
|
||||||
}
|
0 => loop {
|
||||||
let mut input = String::new();
|
|
||||||
for s in &[State::X, State::O] {
|
|
||||||
loop {
|
|
||||||
let (x, y) = loop {
|
let (x, y) = loop {
|
||||||
print!("{}, your move: (x y) ", s);
|
print!("{}, your move: (x y) ", player);
|
||||||
io::stdout().flush().unwrap();
|
io::stdout().flush().unwrap();
|
||||||
input.clear();
|
input.clear();
|
||||||
stdin.read_line(&mut input).unwrap();
|
stdin.read_line(&mut input).unwrap();
|
||||||
@@ -216,13 +203,37 @@ fn main() {
|
|||||||
};
|
};
|
||||||
match board[(x, y)] {
|
match board[(x, y)] {
|
||||||
State::N => {
|
State::N => {
|
||||||
board[(x, y)] = *s;
|
break (x, y);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
_ => eprintln!("({}, {}) is already occupied! Try again", 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if let Some(_) = board.winner() {
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
break;
|
||||||
}
|
}
|
||||||
println!("{}", &board);
|
println!("{}", &board);
|
||||||
@@ -259,6 +270,19 @@ mod test {
|
|||||||
Board::from_str("XOX,OOX,OXO").unwrap().winner(),
|
Board::from_str("XOX,OOX,OXO").unwrap().winner(),
|
||||||
Some(State::N)
|
Some(State::N)
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Board::from_str("XOX,OOX,OXO")
|
||||||
|
.unwrap()
|
||||||
|
.winner_with_criteria(1),
|
||||||
|
Some(State::O)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Board::from_str("XOX,OOX,OXO")
|
||||||
|
.unwrap()
|
||||||
|
.winner_with_criteria(2),
|
||||||
|
Some(State::O)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Reference in New Issue
Block a user