From: Greg Burri Date: Tue, 2 Jun 2020 18:11:29 +0000 (+0200) Subject: First commit X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=fc7379f52c9e5474d8aa587ba66f09a6e50b54b6;p=bataille.git First commit --- fc7379f52c9e5474d8aa587ba66f09a6e50b54b6 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0be5647 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,84 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bataille" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" + +[[package]] +name = "ppv-lite86" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3963733 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "bataille" +version = "0.1.0" +authors = ["Greg Burri "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.7" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..cb0e1ec --- /dev/null +++ b/src/main.rs @@ -0,0 +1,147 @@ +use std::cmp::{ Ordering, PartialOrd }; +use rand::{thread_rng, Rng}; +use rand::distributions::{Distribution, Uniform}; + +#[derive(PartialEq, Debug, Clone)] +enum Card { + As, + King, + Queen, + Jack, + Value(u8) // 2 to 10. +} + +impl PartialOrd for Card { + fn partial_cmp(&self, other: &Self) -> Option { + if self == other { + return Some(Ordering::Equal); + } + + let figures = vec![Card::As, Card::King, Card::Queen, Card::Jack]; + + for figure in figures { + if self == &figure { + return Some(Ordering::Greater); + } + if other == &figure { + return Some(Ordering::Less); + } + } + + match (self, other) { + (Card::Value(self_val), Card::Value(other_val)) => self_val.partial_cmp(other_val), + _ => None + } + } + +} + +fn create_deck() -> Vec { + let mut deck: Vec = vec![]; + for i in 0..4 { + deck.push(Card::As); + deck.push(Card::King); + deck.push(Card::Queen); + deck.push(Card::Jack); + for v in 2..=10 { + deck.push(Card::Value(v)); + } + } + deck +} + +fn shuffle_deck(deck: &mut Vec) { + let mut rng = thread_rng(); + let deck_range = Uniform::new(0, deck.len()); + + for _ in 0 .. deck.len() * 100 { + let a = deck_range.sample(&mut rng); + let b = deck_range.sample(&mut rng); + if a != b { + deck.swap(a, b); + } + } +} + +struct Deck { + main: Vec, + captured: Vec +} + +fn play() -> u32 { + let mut current_turn = 0u32; + + let mut deck = create_deck(); + shuffle_deck(&mut deck); + + let mut player1_deck = deck[0..deck.len() / 2].to_vec(); + let mut player1_captured = Vec::::new(); + + let mut player2_deck = deck[deck.len() / 2 .. deck.len()].to_vec(); + let mut player2_captured = Vec::::new(); + + while player1_deck.len() + player1_captured.len() > 0 && player2_deck.len() + player2_captured.len() > 0 { + current_turn += 1; + + if player1_deck.len() == 0 { + player1_deck.clone_from_slice(&player1_captured); + shuffle_deck(&mut player1_deck); + player1_captured.clear(); + } + + if player2_deck.len() == 0 { + player2_deck.clone_from_slice(&player2_captured); + shuffle_deck(&mut player2_deck); + player2_captured.clear(); + } + + let c1 = player1_deck.pop().unwrap() + let c2 = player1_deck.pop().unwrap() + +/* + if { + return current_turn; + }*/ + } + + //dbg!(deck); + + current_turn +} + +fn main() { + println!("Bataille simulator"); + + let n = 1; + for i in 0..n { + let nb_of_turns = play(); + dbg!(nb_of_turns); + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_card_comparisons() { + assert!(Card::As == Card::As); + assert!(Card::King == Card::King); + assert!(Card::Queen == Card::Queen); + assert!(Card::Jack == Card::Jack); + assert!(Card::Value(10) == Card::Value(10)); + assert!(Card::Value(2) == Card::Value(2)); + assert!(Card::As > Card::King); + assert!(Card::As > Card::Queen); + assert!(Card::As > Card::Jack); + assert!(Card::As > Card::Value(10)); + assert!(Card::King > Card::Queen); + assert!(Card::King > Card::Jack); + assert!(Card::King > Card::Value(10)); + assert!(Card::Queen > Card::Jack); + assert!(Card::Queen > Card::Value(10)); + assert!(Card::Jack > Card::Value(10)); + assert!(Card::Value(10) > Card::Value(9)); + } +}