some changes to next_turn, not finished though
This commit is contained in:
parent
4d5b75ea82
commit
aedfb4839a
|
|
@ -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<S> 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<S> GameState<'_, S> where S: Read + Write {
|
|||
.expect(&GAME_INTEGRITY_ERROR)
|
||||
);
|
||||
}
|
||||
|
||||
// legal move?
|
||||
if
|
||||
!GameState::<S>::check_if_legal(
|
||||
self.current_card,
|
||||
|
|
@ -335,6 +340,8 @@ impl<S> 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<S> 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<S> 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<S> GameState<'_, S> where S: Read + Write {
|
|||
player.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR).said_uno == true &&
|
||||
GameState::<S>::cards_left(
|
||||
player.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR)
|
||||
) == 1
|
||||
) == 2
|
||||
{
|
||||
self.add_to_trash(
|
||||
GameState::<S>
|
||||
|
|
@ -404,7 +421,12 @@ impl<S> 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<S> 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<S> GameState<'_, S> where S: Read + Write {
|
|||
player_states: Vec<PlayerState<'a>>,
|
||||
player_connections: Vec<PlayerConnection<'a, S>>
|
||||
) -> 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<S> GameState<'_, S> where S: Read + Write {
|
|||
}
|
||||
cards_drawn
|
||||
}
|
||||
|
||||
fn is_game_over(&self, current_turn : u32) -> bool{
|
||||
if GameState::<S>::cards_left(self.player_states.get(current_turn as usize).expect(&GAME_INTEGRITY_ERROR)) == 1{
|
||||
true
|
||||
}
|
||||
else{
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user