Skip to content

Commit 6cfd1d0

Browse files
committed
Added min raise mechanic to Table
1 parent cd82363 commit 6cfd1d0

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

src/casino/table.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub struct Table {
134134
pub muck: CardsCell,
135135
pub pot: Stack,
136136
pub bet: Cell<usize>,
137+
pub raise_increment: Cell<usize>,
137138
pub event_log: TableLog,
138139
}
139140

@@ -211,6 +212,7 @@ impl Table {
211212
muck: CardsCell::default(),
212213
pot: Stack::default(),
213214
bet: Cell::new(forced.big_blind),
215+
raise_increment: Cell::new(0),
214216
event_log,
215217
}
216218
}
@@ -346,6 +348,7 @@ impl Table {
346348

347349
match self.seats.act_bet(seat_number, amount) {
348350
Ok(remaining) => {
351+
self.set_raise_increment(seat_number, amount)?;
349352
self.bet.set(amount);
350353
self.log_info(TableAction::Bet(seat_number, amount));
351354
self.action_to_next();
@@ -529,6 +532,7 @@ impl Table {
529532

530533
match self.seats.act_raise(seat_number, amount) {
531534
Ok(remaining) => {
535+
self.set_raise_increment(seat_number, amount - self.bet.get())?;
532536
self.bet.set(amount);
533537
self.log_info(TableAction::Raise(seat_number, amount));
534538
// self.action_to.up();
@@ -538,6 +542,25 @@ impl Table {
538542
}
539543
}
540544

545+
/// # Errors
546+
///
547+
/// `PKError::InsufficientIncrement` if the raise amount is less than the minimum raise
548+
pub fn set_raise_increment(&self, seat_number: u8, amount: usize) -> Result<(), PKError> {
549+
match self.get_seat(seat_number) {
550+
None => {}
551+
Some(seat) => {
552+
if !seat.is_all_in() {
553+
if amount < self.min_raise() {
554+
return Err(PKError::InsufficientIncrement);
555+
}
556+
self.raise_increment.set(amount);
557+
}
558+
}
559+
}
560+
561+
Ok(())
562+
}
563+
541564
pub fn act_shuffle_deck(&self) {
542565
self.set_phase(GamePhase::ShuffleNewDeck);
543566
self.deck.shuffle_in_place();
@@ -561,6 +584,8 @@ impl Table {
561584
}
562585
let _ = self.bet.take();
563586
let brought_in = self.seats.bring_it_in()?;
587+
// Reset the raise increment at the end of the round
588+
self.raise_increment.set(0);
564589
self.log_info(TableAction::BringItIn(brought_in.count()));
565590
self.pot.add_to(brought_in);
566591
self.log_debug(TableAction::PotSize(self.pot.count()));
@@ -1179,8 +1204,12 @@ impl Table {
11791204
}
11801205

11811206
#[must_use]
1182-
pub fn min_bet(&self) -> usize {
1183-
self.forced.big_blind
1207+
pub fn min_raise(&self) -> usize {
1208+
if self.raise_increment.get() > 0 {
1209+
self.raise_increment.get()
1210+
} else {
1211+
self.forced.big_blind
1212+
}
11841213
}
11851214

11861215
/// Returns the minimum number of dealt cards among all seats. Used to determine the next player
@@ -1290,10 +1319,6 @@ impl Table {
12901319
self.seats.are_dealt()
12911320
}
12921321

1293-
// pub fn set_action_to(&self, seat_number: u8) {
1294-
// self.action_to.set(seat_number);
1295-
// }
1296-
12971322
pub fn set_board(&self, cards: Cards) {
12981323
let _ = self.board.take();
12991324
self.deck.remove_all(&CardsCell::from(&cards));
@@ -1381,6 +1406,7 @@ impl Default for Table {
13811406
muck: CardsCell::default(),
13821407
pot: Stack::default(),
13831408
bet: Cell::new(0),
1409+
raise_increment: Cell::new(0),
13841410
event_log: TableLog::default(),
13851411
}
13861412
}
@@ -1917,7 +1943,7 @@ mod casino__table_tests {
19171943
fn is_betting_started_true_when_any_in_hand_player_has_bet() {
19181944
let table = Table::nlh_from_seats(Seats::new(TestData::min_seats()), ForcedBets::new(50, 100));
19191945

1920-
table.act_bet(0, 100).unwrap();
1946+
table.act_bet(0, 200).unwrap();
19211947

19221948
assert!(table.is_betting_started());
19231949
}
@@ -2135,7 +2161,25 @@ mod casino__table_tests {
21352161
}
21362162

21372163
#[test]
2138-
fn min_increment() {
2164+
fn min_raise() {
2165+
let table = TestData::split_pot_table(&cc!(
2166+
"K♠ Q♠ A♦ J♠ A♣ T♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠ K♥ Q♥ J♥ T♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥ K♦ J♦ T♦ 9♦ 8♦ 7♦ 6♦ 5♦ 3♦ 2♦ K♣ J♣ T♣ 9♣ 8♣ 7♣ 6♣ 5♣ 3♣ 2♣"
2167+
));
2168+
table.act_forced_bets().unwrap();
2169+
assert_eq!(100, table.min_raise());
2170+
2171+
table.act_bet(0, 200).expect("raises to 200");
2172+
assert_eq!(200, table.min_raise());
2173+
2174+
table.act_raise(1, 400).expect("raises to 400");
2175+
assert_eq!(200, table.raise_increment.get());
2176+
assert_eq!(200, table.min_raise());
2177+
2178+
table.act_raise(2, 701).expect("raises to 701");
2179+
assert_eq!(301, table.raise_increment.get());
2180+
assert_eq!(301, table.min_raise());
21392181

2182+
let bad_raise = table.act_raise(0, 802);
2183+
assert!(bad_raise.is_err());
21402184
}
21412185
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ pub enum PKError {
432432
Incomplete,
433433
InvalidAction,
434434
InsufficientChips,
435+
InsufficientIncrement,
435436
InvalidBinaryFormat,
436437
InvalidCard,
437438
InvalidCardNumber,
@@ -479,6 +480,7 @@ impl Display for PKError {
479480
PKError::Fubar => "Unexpected Error",
480481
PKError::Incomplete => "Incomplete Error",
481482
PKError::InsufficientChips => "Insufficient chips Error",
483+
PKError::InsufficientIncrement => "Insufficient increment Error",
482484
PKError::InvalidAction => "Invalid Action Error",
483485
PKError::InvalidBinaryFormat => "Invalid binary format Error",
484486
PKError::InvalidCard => "Invalid Card Error",

0 commit comments

Comments
 (0)