Skip to content

Commit

Permalink
show every card the last player played instead of just the last
Browse files Browse the repository at this point in the history
Before this change, this caused a weird bug when the AI played two cards
in a row, it could sometimes look like they broke the rules. For
example, if the last card played was a red skip, and the ai played first
a blue skip then a blue 3, to the next player it would look like the ai
played a blue 3 on top of a red skip, which is breaking the rules.
  • Loading branch information
dyllandry committed May 31, 2023
1 parent 3e0d8ce commit 91c3c56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
13 changes: 0 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
# Bugs

- there might be some display bugs since after an input I assume the next render will render that last player's move, but that's not always true. All upcoming ai players before the next human player will go after the last human player's input, but before the next render. That means before each render it's not the human that last went, but the ai. This can cause display bugs where I assume that the render is immediately after a human player's move.
- There's still a weird bug where the AI can play cards that do not match the last played card
- OH I think it's when the ai plays two skips in a row of different colors then a third card
- P1: red 5
- P2: red skip
- P2: blue skip
- P2: blue 3
- maybe something happens weird when the player is drawing cards and the discard is shuffled into the deck
- because on one of the occurences, it was the 3rd time a card was played when there's only 2 in the deck
- Maybe some sort of index isn't being updated or managed right. Like after drawing some cards and shuffling the deck.
- Maybe there's something in the automate_turn code that isn't validating a card right in some cases. Or playing the wrong one.
- Like it valides a card, goes to play it, but then picks the wrong card somehow

# Todo

# Someday
Expand Down
30 changes: 19 additions & 11 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rand::{seq::SliceRandom, thread_rng};

use crate::{
card::{Card, Color, DrawEffect, TurnEffect},
user_input::Input, ui::{UI, DisplayedHand, PlayerInstruction, TurnRecap},
user_input::Input, ui::{UI, PlayerInstruction, TurnRecap},
};

/**
Expand Down Expand Up @@ -123,6 +123,7 @@ impl Uno {
let current_player = &mut self.players[self.current_player_index as usize];
match validate_card_from_index(card_index, &current_player.hand, self.discard.last()) {
CardFromIndexValidationResult::Invalid(reason) => {
self.wild_card_index_to_pick_color_for = None;
self.ui.error = Some(reason);
},
CardFromIndexValidationResult::Valid => {
Expand All @@ -136,12 +137,12 @@ impl Uno {
let card_to_play = current_player.hand.remove((card_index - 1) as usize);
self.ui.last_turn_recap = Some(TurnRecap {
player: self.current_player_index + 1,
card: card_to_play,
played_cards: vec![card_to_play],
drawn_cards: 0,
});
self.ui.error = None;
self.play_card(card_to_play);
}
self.ui.error = None;
}
}
},
Expand All @@ -151,11 +152,18 @@ impl Uno {
if !self.game_over() {
let mut current_player = self.players.get(self.current_player_index as usize).unwrap();
if current_player.ai {
// The player may go many times in a row, so we add up each turn's recap into a
// single larger recap that represents the everything the player did before the
// next player got to go.
let mut player_turn_recap: TurnRecap = TurnRecap { player: self.current_player_index + 1, played_cards: vec![], drawn_cards: 0 };
while !self.game_over() && current_player.ai {
self.automate_current_player_turn();
let turn_recap = self.automate_current_player_turn();
player_turn_recap.drawn_cards += turn_recap.drawn_cards;
player_turn_recap.played_cards = player_turn_recap.played_cards.iter().cloned().chain(turn_recap.played_cards.iter().cloned()).collect();
current_player = self.players.get(self.current_player_index as usize).unwrap();
}
self.ui.player_instruction = Some(PlayerInstruction::PickCard);
self.ui.last_turn_recap = Some(player_turn_recap);
}
self.ui.display_hand(self.current_player_index + 1, &current_player.hand);

Expand Down Expand Up @@ -213,7 +221,6 @@ impl Uno {
play_card_draw_effect(
&draw_effect,
&mut self.players[next_player_index as usize],
next_player_index,
&mut self.deck,
&mut self.discard
);
Expand All @@ -227,7 +234,7 @@ impl Uno {
}
}

fn automate_current_player_turn(&mut self) {
fn automate_current_player_turn(&mut self) -> TurnRecap {
let player = &mut self.players[self.current_player_index as usize];
let mut num_drawn_cards = 0;
let mut card_index_to_play: Option<usize> = None;
Expand Down Expand Up @@ -261,13 +268,15 @@ impl Uno {
card_to_play.color = Some(color_with_most_cards);
}

self.ui.last_turn_recap = Some(TurnRecap {
let turn_recap = TurnRecap {
player: self.current_player_index + 1,
card: card_to_play,
played_cards: vec![card_to_play],
drawn_cards: num_drawn_cards,
});
};

self.play_card(card_to_play);

return turn_recap;
}

pub fn render(&self) {
Expand All @@ -282,7 +291,6 @@ impl Uno {
fn play_card_draw_effect(
draw_effect: &DrawEffect,
next_player: &mut Player,
next_player_index: i32,
deck: &mut Vec<Card>,
discard: &mut Vec<Card>
) {
Expand Down Expand Up @@ -827,7 +835,7 @@ mod tests {
let mut next_player = Player::default();
let draw_effect = DrawEffect::Draw(2);

play_card_draw_effect(&draw_effect, &mut next_player, 1, &mut deck, &mut discard);
play_card_draw_effect(&draw_effect, &mut next_player, &mut deck, &mut discard);

assert_eq!(deck.len(), 1);
assert_eq!(next_player.hand.len(), 2);
Expand Down
14 changes: 9 additions & 5 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ impl UI {
if last_turn_recap.drawn_cards > 0 {
println!("Player {} drew {} cards!", last_turn_recap.player, last_turn_recap.drawn_cards);
}
println!("Player {} played a {}!", last_turn_recap.player, last_turn_recap.card);
for played_card in &last_turn_recap.played_cards {
println!("Player {} played a {}!", last_turn_recap.player, played_card);
}
println!();
}

for player in &self.uno_declarations {
println!("Player {} has uno!", player);
if self.uno_declarations.len() > 0 {
for player in &self.uno_declarations {
println!("Player {} has uno!", player);
}
println!();
}
println!();

if let Some(displayed_hand) = &self.displayed_hand {
println!("Player {}'s cards:", displayed_hand.player);
Expand Down Expand Up @@ -77,7 +81,7 @@ impl UI {

pub struct TurnRecap {
pub player: i32,
pub card: Card,
pub played_cards: Vec<Card>,
pub drawn_cards: i32,
}

Expand Down

0 comments on commit 91c3c56

Please sign in to comment.