mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-17 23:35:51 -08:00
10_blackjack rust more improvements
This commit is contained in:
@@ -323,12 +323,16 @@ impl<'a> GAME<'a> {
|
|||||||
* plays a round of blackjack
|
* plays a round of blackjack
|
||||||
*/
|
*/
|
||||||
fn play_game(&mut self) {
|
fn play_game(&mut self) {
|
||||||
|
//print score of every user
|
||||||
|
self.print_wins();
|
||||||
|
|
||||||
//deal cards to each player
|
//deal cards to each player
|
||||||
for _i in 0..2 { // do this twice
|
for _i in 0..2 { // do this twice
|
||||||
//draw card for each player
|
//draw card for each player
|
||||||
self.players.iter_mut().for_each(|player| {player.hand.add_card( self.decks.draw_card() );});
|
self.players.iter_mut().for_each(|player| {player.hand.add_card( self.decks.draw_card() );});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//keep track of player who haven't busted or stood yet
|
||||||
let mut players_playing: HashSet<usize> = HashSet::new(); // the numbers presenting each player still active in the round
|
let mut players_playing: HashSet<usize> = HashSet::new(); // the numbers presenting each player still active in the round
|
||||||
for i in 0..self.players.len() { //runs number of players times
|
for i in 0..self.players.len() { //runs number of players times
|
||||||
players_playing.insert(i);
|
players_playing.insert(i);
|
||||||
@@ -369,6 +373,9 @@ impl<'a> GAME<'a> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//check if player is still playing
|
||||||
|
if !players_playing.contains(&player.0) {print!("\n");continue;}//print a line and skip to the next iteration of the loop
|
||||||
|
|
||||||
//get play
|
//get play
|
||||||
let play = player.1.get_play();
|
let play = player.1.get_play();
|
||||||
|
|
||||||
@@ -429,6 +436,8 @@ impl<'a> GAME<'a> {
|
|||||||
|
|
||||||
//increment games_played
|
//increment games_played
|
||||||
self.games_played += 1;
|
self.games_played += 1;
|
||||||
|
//reset rounds
|
||||||
|
self.rounds = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -450,9 +459,6 @@ fn main() {
|
|||||||
|
|
||||||
//game loop, play game until user wants to stop
|
//game loop, play game until user wants to stop
|
||||||
loop {
|
loop {
|
||||||
//print score of every user
|
|
||||||
game.print_wins();
|
|
||||||
|
|
||||||
//play round
|
//play round
|
||||||
game.play_game();
|
game.play_game();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user