Compare commits
2 Commits
f0d851ebb3
...
ff801f259d
| Author | SHA1 | Date | |
|---|---|---|---|
| ff801f259d | |||
| 63452f76f9 |
@@ -4,11 +4,11 @@ from PIL import Image
|
||||
import random
|
||||
import pprint
|
||||
|
||||
IMAGE_LENGTH = 32
|
||||
IMAGE_LENGTH = 16
|
||||
NUM_COLOURS = 3
|
||||
|
||||
def generate_array(colours):
|
||||
array = [[9]*IMAGE_LENGTH for _ in range(IMAGE_LENGTH+1)]
|
||||
array = [[(0, 0, 0)]*IMAGE_LENGTH for _ in range(IMAGE_LENGTH+1)]
|
||||
for i in range(IMAGE_LENGTH):
|
||||
for j in range(IMAGE_LENGTH):
|
||||
array[j][i] = random.choice(colours)
|
||||
|
||||
15
card_stuffs/.gitignore
vendored
Normal file
15
card_stuffs/.gitignore
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
test_files/all-cards.json
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
72
card_stuffs/Cargo.lock
generated
Normal file
72
card_stuffs/Cargo.lock
generated
Normal file
@@ -0,0 +1,72 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.96"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b964d184e89d9b6b67dd2715bc8e74cf3107fb2b529990c90cf517326150bf4"
|
||||
|
||||
[[package]]
|
||||
name = "card_stuffs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
|
||||
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 = "syn"
|
||||
version = "2.0.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "2.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe"
|
||||
8
card_stuffs/Cargo.toml
Normal file
8
card_stuffs/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "card_stuffs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.96"
|
||||
thiserror = "2.0.11"
|
||||
146
card_stuffs/src/lib.rs
Normal file
146
card_stuffs/src/lib.rs
Normal file
@@ -0,0 +1,146 @@
|
||||
use thiserror::Error;
|
||||
use anyhow::Result;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Suit {
|
||||
Heart,
|
||||
Diamond,
|
||||
Club,
|
||||
Spade
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Colour {
|
||||
Black,
|
||||
Red,
|
||||
}
|
||||
|
||||
impl Suit {
|
||||
pub fn colour(&self) -> Colour {
|
||||
match *self {
|
||||
Suit::Heart => Colour::Red,
|
||||
Suit::Diamond => Colour::Red,
|
||||
Suit::Club => Colour::Black,
|
||||
Suit::Spade => Colour::Black,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Suit {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Suit::Heart => write!(f, "♥"),
|
||||
Suit::Diamond => write!(f, "♦"),
|
||||
Suit::Club => write!(f, "♣"),
|
||||
Suit::Spade => write!(f, "♠"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Value {
|
||||
Ace,
|
||||
Two,
|
||||
Three,
|
||||
Four,
|
||||
Five,
|
||||
Six,
|
||||
Seven,
|
||||
Eight,
|
||||
Nine,
|
||||
Ten,
|
||||
Jack,
|
||||
Queen,
|
||||
King
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn indexed_values(&self) -> u8 {
|
||||
// It might also make sense for Ace to be high... depends on context
|
||||
match self {
|
||||
Value::Ace => 1,
|
||||
Value::Two => 2,
|
||||
Value::Three => 3,
|
||||
Value::Four => 4,
|
||||
Value::Five => 5,
|
||||
Value::Six => 6,
|
||||
Value::Seven => 7,
|
||||
Value::Eight => 8,
|
||||
Value::Nine => 9,
|
||||
Value::Ten => 10,
|
||||
Value::Jack => 11,
|
||||
Value::Queen => 12,
|
||||
Value::King => 13,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Value::Ace => write!(f, "A"),
|
||||
Value::Two => write!(f, "2"),
|
||||
Value::Three => write!(f, "3"),
|
||||
Value::Four => write!(f, "4"),
|
||||
Value::Five => write!(f, "5"),
|
||||
Value::Six => write!(f, "6"),
|
||||
Value::Seven => write!(f, "7"),
|
||||
Value::Eight => write!(f, "8"),
|
||||
Value::Nine => write!(f, "9"),
|
||||
Value::Ten => write!(f, "T"),
|
||||
Value::Jack => write!(f, "J"),
|
||||
Value::Queen => write!(f, "Q"),
|
||||
Value::King => write!(f, "K"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Card {
|
||||
pub suit: Suit,
|
||||
pub value: Value,
|
||||
}
|
||||
|
||||
|
||||
impl fmt::Display for Card {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}{}", self.value, self.suit)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum StackingError {
|
||||
#[error("Trying to stack the same coloured suit")]
|
||||
SameColour,
|
||||
#[error("{0} is not \"next\" to {1}")]
|
||||
NotAdjacent(Card, Card),
|
||||
}
|
||||
|
||||
impl Card {
|
||||
pub fn can_be_placed_on_top(&self, top: Card) -> bool {
|
||||
// Can't be the same Colour
|
||||
if self.suit.colour() != top.suit.colour() {
|
||||
return false
|
||||
}
|
||||
|
||||
// Needs to be adjascent
|
||||
if self.value == Value::King || self.value.indexed_values() != top.value.indexed_values() + 1 {
|
||||
return false
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user