67 lines
2.0 KiB
Rust
67 lines
2.0 KiB
Rust
use std::io::{self, stdin};
|
|
|
|
use crate::blackjack::{blackjack_game::BlackjackGame, gamestate::GameState, play_moves::PlayMoves};
|
|
|
|
pub fn play() -> Result<(), Box<dyn std::error::Error>> {
|
|
let mut game = BlackjackGame::new();
|
|
|
|
loop {
|
|
match game.get_state() {
|
|
GameState::PlayerTurn{player_index, ..} => {
|
|
print_full_state(&game);
|
|
let play_move = get_move(*player_index)?;
|
|
if !game.play(play_move) {
|
|
println!("You can't do that");
|
|
}
|
|
}
|
|
GameState::Over => {
|
|
print_full_state(&game);
|
|
println!("Game over");
|
|
return Ok(());
|
|
}
|
|
GameState::Starting => {
|
|
game.play(PlayMoves::Deal{players: 2});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn get_move(player_index: usize) -> Result<PlayMoves, io::Error> {
|
|
loop {
|
|
println!("Turn {}: (H)it (S)tand (D)double, S(p)lit", player_index);
|
|
let mut buffer = String::new();
|
|
|
|
stdin().read_line(&mut buffer)?;
|
|
|
|
match buffer.trim() {
|
|
"h" | "H" => return Ok(PlayMoves::Hit),
|
|
"s" | "S" => return Ok(PlayMoves::Stand),
|
|
"d" | "D" => return Ok(PlayMoves::DoubleDown),
|
|
"p" | "P" => return Ok(PlayMoves::Split),
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn print_full_state(game: &BlackjackGame) {
|
|
println!(
|
|
"Dealer: {} ({})",
|
|
game.get_dealer_hand(),
|
|
game.get_dealer_hand().get_blackjack_value()
|
|
);
|
|
|
|
for player_index in 0..game.get_player_count() {
|
|
let player = game.get_player(player_index).unwrap();
|
|
println!("Player {}:", player_index);
|
|
for hand in player.get_hands().iter().enumerate() {
|
|
println!(
|
|
"{}: {} ({}) - {:?}",
|
|
hand.0,
|
|
hand.1.get_hand(),
|
|
hand.1.get_hand().get_blackjack_value(),
|
|
hand.1.get_state()
|
|
);
|
|
}
|
|
}
|
|
}
|