Compare commits

..

2 Commits
cvc ... master

Author SHA1 Message Date
d9740a28a4
build musl images
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-06-06 14:39:18 +02:00
419d3d7dcc
pick whoever makes the first move at random
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2019-11-16 18:18:19 +01:00
4 changed files with 25 additions and 131 deletions

View File

@ -3,11 +3,11 @@ name: default
steps: steps:
- name: test - name: test
image: rust:1.38.0 image: rust:1.38.0-alpine
commands: commands:
- cargo test - cargo test
- name: build_relase - name: build_relase
image: rust:1.38.0 image: rust:1.38.0-alpine
commands: commands:
- cargo install --path . --root . -f - cargo install --path . --root . -f
- strip bin/tictactoe - strip bin/tictactoe

89
Cargo.lock generated
View File

@ -1,95 +1,6 @@
# 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"

View File

@ -5,11 +5,5 @@ 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"

View File

@ -1,4 +1,3 @@
use rand::Rng;
use std::env::args; use std::env::args;
use std::fmt; use std::fmt;
use std::io; use std::io;
@ -6,6 +5,7 @@ use std::io::Write;
use std::iter; use std::iter;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::str::FromStr; use std::str::FromStr;
use std::time::SystemTime;
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
enum State { enum State {
@ -170,15 +170,28 @@ fn main() {
let dim = board.dimension(); let dim = board.dimension();
(board, criteria.unwrap_or(dim)) (board, criteria.unwrap_or(dim))
}; };
let game_mode = 1usize; let players_rev = State::players().iter().rev().cloned().collect::<Vec<_>>();
let players: &[State] = if SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.map(|d| d.as_millis() % 2 == 0)
.unwrap_or(false)
{
State::players()
} else {
&players_rev[..]
};
let stdin = std::io::stdin(); let stdin = std::io::stdin();
let mut input = String::new(); println!("{}", &board);
let mut rng = rand::thread_rng(); let winner = loop {
let mut next_move = |player: &State, board: &Board| -> (usize, usize) { if let Some(winner) = board.winner_with_criteria(criteria) {
match game_mode { break winner;
0 => loop { }
let mut input = String::new();
for s in players {
loop {
let (x, y) = loop { let (x, y) = loop {
print!("{}, your move: (x y) ", player); print!("{}, your move: (x y) ", s);
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
input.clear(); input.clear();
stdin.read_line(&mut input).unwrap(); stdin.read_line(&mut input).unwrap();
@ -203,36 +216,12 @@ fn main() {
}; };
match board[(x, y)] { match board[(x, y)] {
State::N => { State::N => {
break (x, y); board[(x, y)] = *s;
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);
}
}
} }
}
};
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) { if let Some(_) = board.winner_with_criteria(criteria) {
break; break;
} }