some changes to next_turn, not finished though
This commit is contained in:
parent
4d5b75ea82
commit
aedfb4839a
|
|
@ -1,5 +1,5 @@
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
use std::{ io::{ Read, Write }, net::TcpStream };
|
use std::{ io::{ Read, Write }, net::TcpStream, thread::current };
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
const GAME_INTEGRITY_ERROR: &'static str = "game integrity compromised";
|
const GAME_INTEGRITY_ERROR: &'static str = "game integrity compromised";
|
||||||
const PROTOCOL_ERROR_STRING: &str = "Client/Server protocol mismatch";
|
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 {
|
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 {
|
if self.turns == 0 {
|
||||||
self.game_init();
|
self.game_init();
|
||||||
return TURN_RESULT::NEXT_TURN;
|
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,
|
if !GameState::has_any_moves(&self,
|
||||||
self.player_states
|
self.player_states
|
||||||
.get(self.current_turn as usize)
|
.get(self.current_turn as usize)
|
||||||
|
|
@ -326,7 +331,7 @@ impl<S> GameState<'_, S> where S: Read + Write {
|
||||||
.expect(&GAME_INTEGRITY_ERROR)
|
.expect(&GAME_INTEGRITY_ERROR)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
// legal move?
|
||||||
if
|
if
|
||||||
!GameState::<S>::check_if_legal(
|
!GameState::<S>::check_if_legal(
|
||||||
self.current_card,
|
self.current_card,
|
||||||
|
|
@ -336,6 +341,8 @@ impl<S> GameState<'_, S> where S: Read + Write {
|
||||||
return TURN_RESULT::REPEAT_TURN;
|
return TURN_RESULT::REPEAT_TURN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//is there a +2 chain ongoing?
|
||||||
|
|
||||||
if
|
if
|
||||||
self.current_card.value == CardValue::PLUS_TWO &&
|
self.current_card.value == CardValue::PLUS_TWO &&
|
||||||
card_to_be_played.value != CardValue::PLUS_TWO
|
card_to_be_played.value != CardValue::PLUS_TWO
|
||||||
|
|
@ -365,7 +372,12 @@ impl<S> GameState<'_, S> where S: Read + Write {
|
||||||
.expect(&GAME_INTEGRITY_ERROR)
|
.expect(&GAME_INTEGRITY_ERROR)
|
||||||
);
|
);
|
||||||
self.next_player();
|
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 {
|
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.next_player();
|
||||||
self.current_card = card_to_be_played;
|
self.current_card = card_to_be_played;
|
||||||
self.turns += 1;
|
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 {
|
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 &&
|
player.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR).said_uno == true &&
|
||||||
GameState::<S>::cards_left(
|
GameState::<S>::cards_left(
|
||||||
player.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR)
|
player.get(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR)
|
||||||
) == 1
|
) == 2
|
||||||
{
|
{
|
||||||
self.add_to_trash(
|
self.add_to_trash(
|
||||||
GameState::<S>
|
GameState::<S>
|
||||||
|
|
@ -404,7 +421,12 @@ impl<S> GameState<'_, S> where S: Read + Write {
|
||||||
.expect(&GAME_INTEGRITY_ERROR)
|
.expect(&GAME_INTEGRITY_ERROR)
|
||||||
);
|
);
|
||||||
self.turns += 1;
|
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 {
|
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.current_card = card_to_be_played;
|
||||||
self.turns += 1;
|
self.turns += 1;
|
||||||
self.next_player();
|
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>(
|
fn new_game_helper<'a>(
|
||||||
|
|
@ -476,7 +503,7 @@ impl<S> GameState<'_, S> where S: Read + Write {
|
||||||
player_states: Vec<PlayerState<'a>>,
|
player_states: Vec<PlayerState<'a>>,
|
||||||
player_connections: Vec<PlayerConnection<'a, S>>
|
player_connections: Vec<PlayerConnection<'a, S>>
|
||||||
) -> GameState<'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;
|
return new_game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -636,6 +663,15 @@ impl<S> GameState<'_, S> where S: Read + Write {
|
||||||
}
|
}
|
||||||
cards_drawn
|
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)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user