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