From d12c69200b49798b8ba0acd45cda85b707f1590f Mon Sep 17 00:00:00 2001 From: hal8174 Date: Sun, 29 Dec 2024 23:19:38 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 140 +++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++ src/main.rs | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 356 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs 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..7c6b87a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,140 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "card-calculator" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c786062daee0d6db1132800e623df74274a0a87322d8e183338e01b3d98d058" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..53e6345 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "card-calculator" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..331d21a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,208 @@ +use std::collections::HashSet; + +use rand::seq::SliceRandom; + +#[derive(Clone, Copy, Hash, PartialEq, Eq)] +struct Card { + color: Color, + value: u8, +} + +impl std::fmt::Debug for Card { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}{}", self.color.get_symbol(), self.value) + } +} + +impl Card { + fn new(color: Color, value: u8) -> Self { + Self { color, value } + } + + fn cost(&self) -> u8 { + match self.color { + Color::Spades => { + if self.value == 5 { + 4 + } else { + 0 + } + } + Color::Hearts => 1, + _ => 0, + } + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum Color { + Clubs, + Spades, + Hearts, + Diamonds, +} + +impl Color { + fn get_symbol(&self) -> char { + match self { + Color::Clubs => '♣', + Color::Spades => '♠', + Color::Hearts => '♥', + Color::Diamonds => '♦', + } + } +} + +#[derive(Debug)] +enum Knowledge { + Full(Vec), + Partial([bool; 4]), +} + +fn solve( + current: usize, + knowledge: &[Knowledge], + unseen: &[Card], + stack: &mut Vec, + remaining: usize, + played: &mut HashSet, +) -> Option<(Option, Vec)> { + let num_player = knowledge.len(); + if remaining == 0 { + Some((None, vec![0.0; num_player])) + } else if stack.len() == num_player { + let first_card = stack.first().unwrap(); + + let winning_card = stack + .iter() + .enumerate() + .max_by_key(|g| { + if g.1.color == first_card.color { + g.1.value + } else { + 0 + } + }) + .unwrap(); + + let winning_player = (current + winning_card.0) % num_player; + + let cost = stack.iter().map(|c| c.cost()).sum::(); + + let mut new_stack = Vec::new(); + if let Some(mut s) = solve( + winning_player, + knowledge, + unseen, + &mut new_stack, + remaining - 1, + played, + ) { + s.1[winning_player] += cost as f64; + Some(s) + } else { + None + } + } else { + match &knowledge[current] { + Knowledge::Full(cards) => { + if let Some(f) = stack + .first() + .copied() + .filter(|&f| cards.iter().any(|&c| f.color == c.color)) + { + let mut min: Option<(Option, Vec)> = None; + for &c in cards.iter().filter(|&&c| c.color == f.color) { + if !played.contains(&c) { + stack.push(c); + played.insert(c); + if let Some(m) = solve( + (current + 1) % num_player, + knowledge, + unseen, + stack, + remaining, + played, + ) { + if let Some(min) = &mut min { + if m.1[current] < min.1[current] { + *min = (Some(c), m.1); + } + } else { + min = Some((Some(c), m.1)); + } + } + + stack.pop(); + played.remove(&c); + } + } + min + } else { + let mut min: Option<(Option, Vec)> = None; + for &c in cards { + if !played.contains(&c) { + stack.push(c); + played.insert(c); + + if let Some(m) = solve( + (current + 1) % num_player, + knowledge, + unseen, + stack, + remaining, + played, + ) { + if let Some(min) = &mut min { + if m.1[current] < min.1[current] { + *min = (Some(c), m.1); + } + } else { + min = Some((Some(c), m.1)); + } + } + + stack.pop(); + played.remove(&c); + } + } + min + } + } + Knowledge::Partial(_) => todo!(), + } + } +} + +fn main() { + let mut full_deck = (1..=8) + .flat_map(|u| { + [ + Card::new(Color::Clubs, u), + Card::new(Color::Spades, u), + Card::new(Color::Hearts, u), + Card::new(Color::Diamonds, u), + ] + .into_iter() + }) + .collect::>(); + + let mut rng = rand::thread_rng(); + + full_deck.shuffle(&mut rng); + + let knowledge = [ + Knowledge::Full(full_deck[0..10].to_vec()), + Knowledge::Full(full_deck[10..20].to_vec()), + Knowledge::Full(full_deck[20..30].to_vec()), + ]; + + dbg!(&knowledge); + + let unseen = []; + + let mut stack = Vec::new(); + let mut played = HashSet::new(); + + dbg!(solve(0, &knowledge, &unseen, &mut stack, 5, &mut played)); +}