diff options
| author | andromeda <andromeda@lenovo> | 2026-08-25 03:32:45 +0200 |
|---|---|---|
| committer | andromeda <andromeda@lenovo> | 2026-08-25 03:32:45 +0200 |
| commit | c0cd718e2d4ca24b427175ffaa602452a36c117c (patch) | |
| tree | 4b6f1bd4af4a6fc466f2b344a0daae8f100d75f5 | |
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.lock | 7 | ||||
| -rw-r--r-- | Cargo.toml | 6 | ||||
| -rw-r--r-- | flake.lock | 127 | ||||
| -rw-r--r-- | flake.nix | 36 | ||||
| -rw-r--r-- | src/lib.rs | 72 | ||||
| -rw-r--r-- | src/main.rs | 15 |
7 files changed, 265 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ea0ee8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +result* diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..20485d1 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "cards" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e830417 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cards" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..3caa6a1 --- /dev/null +++ b/flake.lock @@ -0,0 +1,127 @@ +{ + "nodes": { + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1776497206, + "narHash": "sha256-Em+RSdFnwyyKPGUBFtQYtVjm+1UvIc9gOR91Y22zlzg=", + "owner": "nix-community", + "repo": "fenix", + "rev": "df2295365fb081fe0745449762a771290782c22d", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "fenix_2": { + "inputs": { + "nixpkgs": [ + "naersk", + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src_2" + }, + "locked": { + "lastModified": 1752475459, + "narHash": "sha256-z6QEu4ZFuHiqdOPbYss4/Q8B0BFhacR8ts6jO/F/aOU=", + "owner": "nix-community", + "repo": "fenix", + "rev": "bf0d6f70f4c9a9cf8845f992105652173f4b617f", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "naersk": { + "inputs": { + "fenix": "fenix_2", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1776200608, + "narHash": "sha256-broZ6RFQr4Fv0wT73gGmzNX14A43TmTFF8g4wDKlNss=", + "owner": "nix-community", + "repo": "naersk", + "rev": "8b23250ab45c2a38cd91031aee26478ca4d0a28e", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "naersk", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1776329215, + "narHash": "sha256-a8BYi3mzoJ/AcJP8UldOx8emoPRLeWqALZWu4ZvjPXw=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "b86751bc4085f48661017fa226dee99fab6c651b", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "naersk": "naersk", + "nixpkgs": "nixpkgs" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1776441750, + "narHash": "sha256-1rVfG+mj8R4ze+lSYCa4iAv7FzrB03Cprtxmd1MfZak=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "251df518d73abb5c5d573c4d5d266a3edae9ca5a", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "rust-analyzer-src_2": { + "flake": false, + "locked": { + "lastModified": 1752428706, + "narHash": "sha256-EJcdxw3aXfP8Ex1Nm3s0awyH9egQvB2Gu+QEnJn2Sfg=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "591e3b7624be97e4443ea7b5542c191311aa141d", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..f84b821 --- /dev/null +++ b/flake.nix @@ -0,0 +1,36 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + fenix = { + inputs.nixpkgs.follows = "nixpkgs"; + url = "github:nix-community/fenix"; + }; + naersk = { + inputs.nixpkgs.follows = "nixpkgs"; + url = "github:nix-community/naersk"; + }; + }; + outputs = { + nixpkgs, + naersk, + fenix, + ... + }: let + pkgs = nixpkgs.legacyPackages.x86_64-linux; + toolchain = fenix.packages.x86_64-linux.minimal.toolchain; + in { + devShell.x86_64-linux.default = pkgs.mkShell { + buildInputs = [toolchain]; + shellHook = '' + export RUSTUP_TOOLCHAIN=${toolchain} + ''; + }; + packages.x86_64-linux.default = + (naersk.lib.x86_64-linux.override { + cargo = toolchain; + rustc = toolchain; + }).buildPackage { + src = ./.; + }; + }; +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e8f7511 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,72 @@ +#![feature(random)] +use std::random::random; + +#[derive(Debug, Clone)] +pub enum Suit { + Spades, + Hearts, + Clubs, + Diamonds, +} + +#[derive(Debug, Clone)] +pub enum Value { + Ace, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, + Jack, + Queen, + King, +} + +#[derive(Debug, Clone)] +pub struct Card { + pub suit: Suit, + pub value: Value, +} + +impl Card { + pub fn new(suit: Suit, value: Value) -> Self { + Card { + suit: suit, + value: value, + } + } +} + +#[derive(Debug)] +pub struct Stack(Vec<Card>); + +impl Stack { + pub fn new(cards: Vec<Card>) -> Self { + Stack(cards) + } + pub fn shuffle(&mut self) { + let n: usize = self.0.len(); + if n == 0 { + return; + } + for i in 0..(n - 1) { + let j = random::<usize>(..) % (n - i) + i; + self.0.swap(i, j); + } + } + pub fn shuffle_in(&mut self, stack: &mut Stack) { + stack.take_cards(&mut self.0); + self.shuffle(); + } + pub fn draw(&mut self) -> Option<Card> { + self.0.iter().next().cloned(); + } + fn take_cards(&mut self, cards: &mut Vec<Card>) { + cards.append(&mut self.0.clone()); + self.0.clear(); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..36018c4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,15 @@ +use cards::{Card, Stack, Suit, Value}; + +fn main() { + let card = Card::new(Suit::Spades, Value::Ace); + let card0 = Card::new(Suit::Hearts, Value::Ace); + let card1 = Card::new(Suit::Clubs, Value::Ace); + let card2 = Card::new(Suit::Diamonds, Value::Ace); + let mut stack = Stack::new(vec![card, card0, card1, card2]); + stack.shuffle(); + loop { + if let Ok(c) = stack.next() { + println!("{:?}", c); + } + } +} |
