diff --git a/src/uno_logic.rs b/src/uno_logic.rs index c7ae956..53a803d 100644 --- a/src/uno_logic.rs +++ b/src/uno_logic.rs @@ -1,5 +1,5 @@ extern crate rand; -use std::{ io::{ Read, Write }, net::TcpStream }; +use std::{ io::{ Read, Write }, net::TcpStream, thread::current }; use rand::Rng; const GAME_INTEGRITY_ERROR: &'static str = "game integrity compromised"; const PROTOCOL_ERROR_STRING: &str = "Client/Server protocol mismatch"; @@ -291,11 +291,16 @@ impl GameState<'_, S> where S: Read + Write { } pub fn next_turn(&mut self, card_to_be_played: Card) -> TURN_RESULT { + + //init game if it hasn't been already if self.turns == 0 { self.game_init(); return TURN_RESULT::NEXT_TURN; } + let cards_left = self.player_states.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR).cards.len(); + + //check if a player has a card to play, if not can he draw a card to play? if !GameState::has_any_moves(&self, self.player_states .get(self.current_turn as usize) @@ -326,7 +331,7 @@ impl GameState<'_, S> where S: Read + Write { .expect(&GAME_INTEGRITY_ERROR) ); } - + // legal move? if !GameState::::check_if_legal( self.current_card, @@ -335,6 +340,8 @@ impl GameState<'_, S> where S: Read + Write { { return TURN_RESULT::REPEAT_TURN; } + + //is there a +2 chain ongoing? if self.current_card.value == CardValue::PLUS_TWO && @@ -365,7 +372,12 @@ impl GameState<'_, S> where S: Read + Write { .expect(&GAME_INTEGRITY_ERROR) ); self.next_player(); - return TURN_RESULT::NEXT_TURN; + if cards_left == 1 { + return TURN_RESULT::GAME_OVER + } + else{ + return TURN_RESULT::NEXT_TURN; + } } if card_to_be_played.value == CardValue::PLUS_FOUR { @@ -379,7 +391,12 @@ impl GameState<'_, S> where S: Read + Write { self.next_player(); self.current_card = card_to_be_played; self.turns += 1; - return TURN_RESULT::NEXT_TURN; + if (cards_left == 1) { + return TURN_RESULT::GAME_OVER; + } + else{ + return TURN_RESULT::NEXT_TURN; + } } if card_to_be_played.value == CardValue::SKIP { @@ -393,7 +410,7 @@ impl GameState<'_, S> where S: Read + Write { player.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR).said_uno == true && GameState::::cards_left( player.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR) - ) == 1 + ) == 2 { self.add_to_trash( GameState:: @@ -404,7 +421,12 @@ impl GameState<'_, S> where S: Read + Write { .expect(&GAME_INTEGRITY_ERROR) ); self.turns += 1; - return TURN_RESULT::GAME_OVER; + if (cards_left == 1){ + return TURN_RESULT::GAME_OVER + } + else{ + return TURN_RESULT::NEXT_TURN + } } if card_to_be_played.value == CardValue::REVERSE { @@ -452,7 +474,12 @@ impl GameState<'_, S> where S: Read + Write { self.current_card = card_to_be_played; self.turns += 1; self.next_player(); - return TURN_RESULT::NEXT_TURN; + if (cards_left == 1){ + return TURN_RESULT::GAME_OVER; + } + else { + return TURN_RESULT::NEXT_TURN; + } } fn new_game_helper<'a>( @@ -476,7 +503,7 @@ impl GameState<'_, S> where S: Read + Write { player_states: Vec>, player_connections: Vec> ) -> GameState<'a, S> { - let mut new_game = GameState::new_game_helper(player_states, player_connections); + let new_game = GameState::new_game_helper(player_states, player_connections); return new_game; } @@ -636,6 +663,15 @@ impl GameState<'_, S> where S: Read + Write { } cards_drawn } + + fn is_game_over(&self, current_turn : u32) -> bool{ + if GameState::::cards_left(self.player_states.get(current_turn as usize).expect(&GAME_INTEGRITY_ERROR)) == 1{ + true + } + else{ + false + } + } } #[cfg(test)]