This commit is contained in:
2024-04-19 20:04:58 -04:00
parent ca47f2df2b
commit 6ab8839d6c
6 changed files with 186 additions and 6 deletions

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"rust-analyzer.showUnlinkedFileNotification": false
}

View File

@@ -26,7 +26,7 @@ pub enum CardValue {
Ace, Ace,
} }
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq, Clone, Copy)]
pub struct PlayingCard { pub struct PlayingCard {
pub value: CardValue, pub value: CardValue,
pub suit: CardSuit, pub suit: CardSuit,

69
src/game.rs Normal file
View File

@@ -0,0 +1,69 @@
use crate::{
card::{create_deck, draw_card, PlayingCard},
pokerplayer::PokerPlayer,
};
enum GameStage {
PreFlop,
Flop,
Turn,
River,
Reveal,
}
struct PokerGame {
players: Vec<PokerPlayer>,
big_blind: u32, // Index of player who should post the Big Blind
pot: u32, // Money in pennies
stage: GameStage,
deck: Vec<PlayingCard>,
}
impl PokerGame {
pub fn new(players: Vec<PokerPlayer>) -> PokerGame {
let first_player = 0;
PokerGame {
players,
big_blind: 0,
pot: 0,
stage: GameStage::PreFlop,
deck: create_deck(),
}
}
pub fn play_stage(&mut self) {
match self.stage {
GameStage::PreFlop => self.play_preflop(),
GameStage::Flop => self.play_flop(),
GameStage::Turn => self.play_turn_or_river(),
GameStage::River => self.play_turn_or_river(),
GameStage::Reveal => self.play_reveal(),
}
}
/// Preflop:
/// - Deal cards to all of the players
/// - Collect big and small blinds
/// - Players make their first round of actions
fn play_preflop(&mut self) {
for player in &mut self.players {
let dealt_cards = vec![draw_card(&mut self.deck), draw_card(&mut self.deck)];
player.deal_cards(dealt_cards);
}
}
/// Flop:
/// - Play three cards to the table
/// - Players make a round of bets
fn play_flop(&self) {}
/// Turn:
/// - Play another card to the table
/// - Players make a round of bets
fn play_turn_or_river(&self) {}
/// Reveal:
/// - All players cards are revealed
/// - Hands are compared, winner is decided
/// - Payout is presented
fn play_reveal(&self) {}
}

View File

@@ -16,7 +16,7 @@ pub enum HandType {
} }
#[derive(PartialEq)] #[derive(PartialEq)]
struct PokerHand { pub struct PokerHand {
cards: Vec<PlayingCard>, cards: Vec<PlayingCard>,
} }
@@ -143,7 +143,7 @@ impl PartialOrd for PokerHand {
} }
impl PokerHand { impl PokerHand {
fn new(mut cards: Vec<PlayingCard>) -> Self { pub fn new(mut cards: Vec<PlayingCard>) -> Self {
assert!(cards.len() == 5); assert!(cards.len() == 5);
// Sorted highest card first // Sorted highest card first
@@ -172,7 +172,7 @@ impl PokerHand {
count count
} }
fn hand_type(&self) -> HandType { pub fn hand_type(&self) -> HandType {
if is_straight_flush(self) { if is_straight_flush(self) {
return HandType::StraightFlush; return HandType::StraightFlush;
} }

View File

@@ -1,15 +1,17 @@
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused_variables)]
use std::vec; use std::vec;
mod card; mod card;
mod game;
mod hand; mod hand;
mod pokerplayer;
use crate::card::{create_deck, CardSuit, CardValue, PlayingCard}; use crate::card::{create_deck, CardSuit, CardValue, PlayingCard};
use crate::hand::HandType; use crate::hand::HandType;
fn main() { fn main() {
let mut deck = create_deck(); let deck = create_deck();
let card1: PlayingCard = PlayingCard { let card1: PlayingCard = PlayingCard {
suit: CardSuit::Clubs, suit: CardSuit::Clubs,

106
src/pokerplayer.rs Normal file
View File

@@ -0,0 +1,106 @@
use crate::card::PlayingCard;
use crate::hand::PokerHand;
use std::io;
pub enum TurnAction {
Fold,
Call,
Raise(u32),
}
pub struct PokerPlayer {
money: u32,
bet: u32,
cards: Vec<PlayingCard>,
is_remote: bool,
}
impl PokerPlayer {
pub fn new(money: u32) -> Self {
PokerPlayer {
money,
bet: 0,
cards: vec![],
is_remote: false,
}
}
pub fn deal_cards(&mut self, mut cards: Vec<PlayingCard>) {
self.cards.clear();
self.cards.append(&mut cards);
// Todo send cards over the network
println!(
"You've been dealt the cards: {}, {}",
self.cards[0], self.cards[1]
);
}
pub fn get_hand(self, cards_on_table: &Vec<PlayingCard>) -> PokerHand {
let mut hand_cards = self.cards.clone();
hand_cards.append(&mut cards_on_table.clone());
let hand = PokerHand::new(hand_cards);
hand
}
pub fn get_action(&self) -> TurnAction {
if self.is_remote {
return TurnAction::Fold;
}
let mut user_input = String::new();
let stdin = io::stdin();
loop {
print!("What would you like to do?\nR: Raise\nC: Call\n:F\n Fold\n> ");
let _ = stdin.read_line(&mut user_input);
if user_input.len() == 0 {
continue;
}
let first_char = user_input.to_lowercase().chars().next().unwrap();
match first_char {
'f' => {
return TurnAction::Fold;
}
'r' => {
return TurnAction::Raise(self.get_raise());
}
'c' => {
return TurnAction::Call;
}
_ => {
continue;
}
}
}
}
pub fn get_raise(&self) -> u32 {
if self.is_remote {
return 0;
}
let mut user_input: String = String::new();
let stdin = io::stdin();
loop {
print!("Enter your raise.\n> ");
let _ = stdin.read_line(&mut user_input);
let user_input = user_input.replace("$", "");
if user_input.len() == 0 {
continue;
}
let money = user_input.parse::<f64>();
match money {
Ok(_) => {
let pennies = (money.unwrap() * 100.0).floor() as u32;
if pennies > self.money {
println!("You flat-footed dingus, you don't have that much money!");
}
return pennies;
}
Err(err) => {
print!("That's not a number, dufus. What, are you trying to bet your bitcoin wallet or something? Enter a number.\n>");
continue;
}
}
}
}
}