Add ID to player trait

This commit is contained in:
2024-04-27 12:27:23 -04:00
parent 899edd7c7f
commit a9e90bc447

View File

@@ -10,6 +10,7 @@ pub enum TurnAction {
} }
pub trait Player { pub trait Player {
fn id(&self) -> u32;
fn display_name(&self) -> &str; fn display_name(&self) -> &str;
fn deal_cards(&mut self, cards: Vec<PlayingCard>); fn deal_cards(&mut self, cards: Vec<PlayingCard>);
fn get_move(&self, game_state: &PokerGame) -> TurnAction; fn get_move(&self, game_state: &PokerGame) -> TurnAction;
@@ -20,18 +21,20 @@ pub trait Player {
} }
pub struct HumanPlayer { pub struct HumanPlayer {
id: u32,
name: String, name: String,
money: u32, money: u32,
cards: Vec<PlayingCard>, cards: Vec<PlayingCard>,
} }
impl HumanPlayer { impl HumanPlayer {
pub fn new(money: u32) -> HumanPlayer { pub fn new(money: u32, id: u32) -> HumanPlayer {
let mut user_input = String::new(); let mut user_input = String::new();
print!("Enter your name\n> "); print!("Enter your name\n> ");
let stdin = io::stdin(); let stdin = io::stdin();
let _ = stdin.read_line(&mut user_input); let _ = stdin.read_line(&mut user_input);
HumanPlayer { HumanPlayer {
id,
name: user_input, name: user_input,
money, money,
cards: vec![], cards: vec![],
@@ -40,6 +43,10 @@ impl HumanPlayer {
} }
impl Player for HumanPlayer { impl Player for HumanPlayer {
fn id(&self) -> u32 {
self.id
}
fn display_name(&self) -> &str { fn display_name(&self) -> &str {
&self.name &self.name
} }
@@ -133,14 +140,16 @@ impl HumanPlayer {
} }
struct RemotePlayer { struct RemotePlayer {
id: u32,
name: String, name: String,
money: u32, money: u32,
cards: Vec<PlayingCard>, cards: Vec<PlayingCard>,
} }
impl RemotePlayer { impl RemotePlayer {
pub fn new(name: &str, money: u32) -> RemotePlayer { pub fn new(name: &str, money: u32, id: u32) -> RemotePlayer {
RemotePlayer { RemotePlayer {
id,
name: name.to_string(), name: name.to_string(),
money, money,
cards: vec![], cards: vec![],
@@ -149,6 +158,10 @@ impl RemotePlayer {
} }
impl Player for RemotePlayer { impl Player for RemotePlayer {
fn id(&self) -> u32 {
self.id
}
fn display_name(&self) -> &str { fn display_name(&self) -> &str {
&self.name &self.name
} }
@@ -186,14 +199,16 @@ impl Player for RemotePlayer {
} }
struct BotPlayer { struct BotPlayer {
id: u32,
name: String, name: String,
money: u32, money: u32,
cards: Vec<PlayingCard>, cards: Vec<PlayingCard>,
} }
impl BotPlayer { impl BotPlayer {
pub fn new(name: &str, money: u32) -> BotPlayer { pub fn new(name: &str, money: u32, id: u32) -> BotPlayer {
BotPlayer { BotPlayer {
id,
name: format!("[BOT] {}", name.to_string()), name: format!("[BOT] {}", name.to_string()),
money, money,
cards: vec![], cards: vec![],
@@ -202,6 +217,10 @@ impl BotPlayer {
} }
impl Player for BotPlayer { impl Player for BotPlayer {
fn id(&self) -> u32 {
self.id
}
fn display_name(&self) -> &str { fn display_name(&self) -> &str {
&self.name &self.name
} }