Skip to content

Commit

Permalink
format and optional color
Browse files Browse the repository at this point in the history
  • Loading branch information
evanofslack committed Dec 27, 2024
1 parent c37e8fb commit f3690af
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ repository ="https://github.com/evanofslack/balatro-rs"

[dependencies]
text_io = "0.1.9"
balatro-rs = {path = "../core/", version = "0.0.1"}
colored = "2.2.0"
balatro-rs = {path = "../core/", version = "0.0.1", features = ["colored"]}
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ itertools = "0.13.0"
indexmap = "2.6.0"
strum = { version = "0.26", features = ["derive"] }
pyo3 = {version = "0.23.1", optional = true}
colored = {version = "2.2.0", optional = true}

[dev-dependencies]
criterion = "0.3"
Expand All @@ -26,6 +27,7 @@ criterion = "0.3"
default = ["serde", "python"]
python = ["dep:pyo3"]
serde = ["dep:serde", "dep:serde_json", "uuid?/serde"]
colored = ["dep:colored"]

[[bench]]
name = "benchmark"
Expand Down
8 changes: 4 additions & 4 deletions core/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl fmt::Display for Action {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::SelectCard(card) => {
write!(f, "SelectCard: {:?}", card)
write!(f, "SelectCard: {}", card)
}
Self::Play() => {
write!(f, "Play")
Expand All @@ -53,10 +53,10 @@ impl fmt::Display for Action {
write!(f, "Discard")
}
Self::MoveCard(dir, card) => {
write!(f, "MoveCard: {:?} - {:}", card, dir)
write!(f, "MoveCard: {} - {}", card, dir)
}
Self::CashOut(reward) => {
write!(f, "CashOut: {:}", reward)
write!(f, "CashOut: {}", reward)
}
Self::BuyJoker(joker) => {
write!(f, "BuyJoker: {:?}", joker)
Expand All @@ -65,7 +65,7 @@ impl fmt::Display for Action {
write!(f, "NextRound")
}
Self::SelectBlind(blind) => {
write!(f, "SelectBlind: {:?}", blind)
write!(f, "SelectBlind: {}", blind)
}
}
}
Expand Down
37 changes: 30 additions & 7 deletions core/src/card.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "colored")]
use colored::Colorize;
use pyo3::pyclass;
use std::{
fmt,
Expand Down Expand Up @@ -88,6 +90,14 @@ impl Suit {
pub const fn suits() -> [Self; 4] {
SUITS
}
pub fn unicode(&self) -> &str {
match self {
Self::Spade => "♤",
Self::Club => "♧",
Self::Heart => "♡",
Self::Diamond => "♢",
}
}
}

impl From<Suit> for char {
Expand Down Expand Up @@ -205,18 +215,31 @@ impl Card {

impl fmt::Debug for Card {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Card({}{})",
char::from(self.value),
char::from(self.suit)
)
#[cfg(feature = "colored")]
let suit = match self.suit {
Suit::Spade => self.suit.unicode().black(),
Suit::Club => self.suit.unicode().green(),
Suit::Heart => self.suit.unicode().red(),
Suit::Diamond => self.suit.unicode().blue(),
};
#[cfg(not(feature = "colored"))]
let suit = self.suit.unicode();
write!(f, "Card({}{})", char::from(self.value), suit)
}
}

impl fmt::Display for Card {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", char::from(self.value), char::from(self.suit))
#[cfg(feature = "colored")]
let suit = match self.suit {
Suit::Spade => self.suit.unicode().black(),
Suit::Club => self.suit.unicode().green(),
Suit::Heart => self.suit.unicode().red(),
Suit::Diamond => self.suit.unicode().blue(),
};
#[cfg(not(feature = "colored"))]
let suit = self.suit.unicode();
write!(f, "{}{}", char::from(self.value), suit)
}
}

Expand Down
33 changes: 33 additions & 0 deletions core/src/joker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::effect::Effects;
use crate::game::Game;
use crate::hand::MadeHand;
use pyo3::pyclass;
use std::fmt;
use std::sync::{Arc, Mutex};
use strum::{EnumIter, IntoEnumIterator};

Expand Down Expand Up @@ -33,6 +34,25 @@ pub enum Rarity {
Legendary,
}

impl fmt::Display for Rarity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Common => {
write!(f, "Common")
}
Self::Uncommon => {
write!(f, "Uncommon")
}
Self::Rare => {
write!(f, "Rare")
}
Self::Legendary => {
write!(f, "Legendary")
}
}
}
}

// We could pass around `Box<dyn Joker>` but it doesn't work so nice with pyo3 and serde.
// Since we know all variants (one for each joker), we define an enum that implements
// our `Joker` trait. This macro just reduces the amount of boilerplate we have to copy
Expand Down Expand Up @@ -122,6 +142,19 @@ impl Jokers {
}
}

impl fmt::Display for Jokers {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{} [${}, {}] - {}",
self.name(),
self.cost(),
self.rarity(),
self.desc()
)
}
}

#[derive(Debug, Clone, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "python", pyclass(eq))]
pub struct TheJoker {}
Expand Down
11 changes: 11 additions & 0 deletions core/src/stage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use pyo3::pyclass;
use std::fmt;

/// Types of blinds
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -29,6 +30,16 @@ impl Blind {
}
}

impl fmt::Display for Blind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Small => write!(f, "Small Blind"),
Self::Big => write!(f, "Big Blind"),
Self::Boss => write!(f, "Boss Blind"),
}
}
}

/// Game ending
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "python", pyclass(eq))]
Expand Down

0 comments on commit f3690af

Please sign in to comment.