This commit is contained in:
mux 2024-09-02 23:53:03 +02:00
parent 5a0d6573db
commit 552b09730f
2 changed files with 54 additions and 37 deletions

View File

@ -37,7 +37,6 @@ pub enum CardColor {
BLUE, BLUE,
GREEN, GREEN,
YELLOW, YELLOW,
BLACK,
} }
pub enum Direction { pub enum Direction {
@ -79,7 +78,6 @@ pub struct GameState<'a, S: Read + Write> {
pub current_card: Card, pub current_card: Card,
pub player_states: Vec<PlayerState<'a>>, pub player_states: Vec<PlayerState<'a>>,
pub player_connections: Vec<PlayerConnection<'a, S>>, pub player_connections: Vec<PlayerConnection<'a, S>>,
pub current_color: CardColor,
pub current_direction: Direction, pub current_direction: Direction,
plus_twos: u32, plus_twos: u32,
} }
@ -153,7 +151,6 @@ impl CardValue {
impl CardColor { impl CardColor {
pub fn to_string<'a>(self) -> &'a str { pub fn to_string<'a>(self) -> &'a str {
match self { match self {
CardColor::BLACK => "BLACK",
CardColor::BLUE => "BLUE", CardColor::BLUE => "BLUE",
CardColor::GREEN => "GREEN", CardColor::GREEN => "GREEN",
CardColor::YELLOW => "YELLOW", CardColor::YELLOW => "YELLOW",
@ -250,7 +247,6 @@ impl<S> GameState<'_, S> where S: Read + Write {
if GameState::<S>::check_if_legal( if GameState::<S>::check_if_legal(
self.current_card, self.current_card,
card, card,
self.current_color
) )
{ {
return true; return true;
@ -281,11 +277,10 @@ impl<S> GameState<'_, S> where S: Read + Write {
fn check_if_legal( fn check_if_legal(
current_card: Card, current_card: Card,
card_to_be_played: Card, card_to_be_played: Card,
current_color: CardColor
) -> bool { ) -> bool {
if if
card_to_be_played.color == CardColor::BLACK || [CardValue::PLUS_FOUR,CardValue::CHANGE_COLOR].contains(&card_to_be_played.value) ||
card_to_be_played.color == current_color || card_to_be_played.color == current_card.color ||
card_to_be_played.value == current_card.value card_to_be_played.value == current_card.value
{ {
return true; return true;
@ -316,7 +311,6 @@ impl<S> GameState<'_, S> where S: Read + Write {
).get(0) ).get(0)
.expect(&GAME_INTEGRITY_ERROR) .expect(&GAME_INTEGRITY_ERROR)
.to_owned(), .to_owned(),
self.current_color
){ ){
self.next_player(); self.next_player();
return TURN_RESULT::NEXT_TURN; return TURN_RESULT::NEXT_TURN;
@ -336,7 +330,6 @@ impl<S> GameState<'_, S> where S: Read + Write {
!GameState::<S>::check_if_legal( !GameState::<S>::check_if_legal(
self.current_card, self.current_card,
card_to_be_played, card_to_be_played,
self.current_color
) )
{ {
return TURN_RESULT::REPEAT_TURN; return TURN_RESULT::REPEAT_TURN;
@ -358,8 +351,8 @@ impl<S> GameState<'_, S> where S: Read + Write {
card_to_be_played.value == CardValue::PLUS_TWO card_to_be_played.value == CardValue::PLUS_TWO
{ {
self.plus_twos += 1; self.plus_twos += 1;
if self.current_color != card_to_be_played.color { if self.current_card.color != card_to_be_played.color {
self.current_color = card_to_be_played.color; self.current_card.color = card_to_be_played.color;
} }
let mut player = self.player_states.clone(); let mut player = self.player_states.clone();
self.add_to_trash( self.add_to_trash(
@ -374,14 +367,18 @@ impl<S> GameState<'_, S> where S: Read + Write {
return TURN_RESULT::NEXT_TURN; return TURN_RESULT::NEXT_TURN;
} }
if self.current_card.value == CardValue::PLUS_FOUR { if card_to_be_played.value == CardValue::PLUS_FOUR {
let mut player = self.player_states.clone(); let mut player = self.player_states.clone();
self.draw( self.draw(
player player
.get_mut(self.current_turn as usize) .get_mut((self.current_turn + 1) as usize)
.expect(&GAME_INTEGRITY_ERROR).player_name, .expect(&GAME_INTEGRITY_ERROR).player_name,
4 4
); );
self.next_player();
self.current_card = card_to_be_played;
self.turns += 1;
return TURN_RESULT::NEXT_TURN;
} }
if card_to_be_played.value == CardValue::SKIP { if card_to_be_played.value == CardValue::SKIP {
@ -418,7 +415,7 @@ impl<S> GameState<'_, S> where S: Read + Write {
} }
if [CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR].contains(&card_to_be_played.value) { if [CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR].contains(&card_to_be_played.value) {
self.current_color = card_to_be_played.color; self.current_card.color = card_to_be_played.color;
} else if } else if
[ [
CardValue::EIGHT, CardValue::EIGHT,
@ -437,8 +434,8 @@ impl<S> GameState<'_, S> where S: Read + Write {
CardValue::PLUS_TWO, CardValue::PLUS_TWO,
].contains(&card_to_be_played.value) ].contains(&card_to_be_played.value)
{ {
if card_to_be_played.color != self.current_color { if card_to_be_played.color != self.current_card.color {
self.current_color = card_to_be_played.color; self.current_card.color = card_to_be_played.color;
} }
} }
@ -469,7 +466,6 @@ impl<S> GameState<'_, S> where S: Read + Write {
current_card: Card { value: CardValue::ZERO, color: CardColor::RED }, current_card: Card { value: CardValue::ZERO, color: CardColor::RED },
player_states: player_states.clone(), player_states: player_states.clone(),
player_connections: player_connections, player_connections: player_connections,
current_color: CardColor::RED,
current_direction: Direction::CLOCKWISE, current_direction: Direction::CLOCKWISE,
plus_twos: 0, plus_twos: 0,
} }
@ -480,7 +476,6 @@ impl<S> GameState<'_, S> where S: Read + Write {
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 mut new_game = GameState::new_game_helper(player_states, player_connections);
new_game.current_color = new_game.current_card.color;
return new_game; return new_game;
} }
@ -509,7 +504,6 @@ impl<S> GameState<'_, S> where S: Read + Write {
let mut deck: Deck = vec![]; let mut deck: Deck = vec![];
for color in [ for color in [
CardColor::BLACK,
CardColor::BLUE, CardColor::BLUE,
CardColor::GREEN, CardColor::GREEN,
CardColor::RED, CardColor::RED,
@ -532,13 +526,13 @@ impl<S> GameState<'_, S> where S: Read + Write {
CardValue::REVERSE, CardValue::REVERSE,
CardValue::SKIP, CardValue::SKIP,
] { ] {
if ![CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR, CardValue::ZERO].contains(&value) && color != CardColor::BLACK { if ![CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR, CardValue::ZERO].contains(&value) {
for _ in 0..2 { for _ in 0..2 {
deck.push(Card { value: value, color: color }); deck.push(Card { value: value, color: color });
} }
} else if value == CardValue::ZERO && color != CardColor::BLACK{ } else if value == CardValue::ZERO{
deck.push(Card { value: value, color: color }); deck.push(Card { value: value, color: color });
} else if [CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR].contains(&value) && color == CardColor::BLACK { } else if [CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR].contains(&value) && color == CardColor::RED {
for _ in 0..4 { for _ in 0..4 {
deck.push(Card { value: value, color: color }); deck.push(Card { value: value, color: color });
} }
@ -647,6 +641,8 @@ impl<S> GameState<'_, S> where S: Read + Write {
mod tests { mod tests {
use std::{collections::{HashSet, VecDeque}, iter::Skip}; use std::{collections::{HashSet, VecDeque}, iter::Skip};
use rand::random;
use super::*; use super::*;
fn get_card_set() -> Vec<Card>{ fn get_card_set() -> Vec<Card>{
@ -660,12 +656,15 @@ mod tests {
} }
fn get_card(card_value : CardValue, card_color : CardColor) -> Card{ fn get_card(card_value : CardValue, card_color : CardColor) -> Card{
for card in get_card_set(){ let card_set = get_card_set();
for card in &card_set{
if card_value == card.value && card_color == card.color { if card_value == card.value && card_color == card.color {
return card; return card.to_owned();
} }
} }
return Card {value : CardValue::ZERO, color : CardColor::BLACK}; let mut rng = rand::thread_rng();
let n: usize = rng.gen_range(0..card_set.len());
return card_set.get(n).unwrap().to_owned();
} }
fn get_test_gs<'a>() -> GameState<'a, VecDeque<u8>> { fn get_test_gs<'a>() -> GameState<'a, VecDeque<u8>> {
@ -732,7 +731,6 @@ mod tests {
let mut gs = get_test_gs(); let mut gs = get_test_gs();
//no moves //no moves
gs.current_card = Card {value : CardValue::TWO, color : CardColor::GREEN}; gs.current_card = Card {value : CardValue::TWO, color : CardColor::GREEN};
gs.current_color = CardColor::GREEN;
let player = PlayerState::new("detlef", [get_card(CardValue::FOUR, CardColor::RED), get_card(CardValue::EIGHT, CardColor::YELLOW)].to_vec()); let player = PlayerState::new("detlef", [get_card(CardValue::FOUR, CardColor::RED), get_card(CardValue::EIGHT, CardColor::YELLOW)].to_vec());
assert_eq!(gs.has_any_moves(player), false); assert_eq!(gs.has_any_moves(player), false);
} }
@ -742,16 +740,16 @@ mod tests {
let mut gs = get_test_gs(); let mut gs = get_test_gs();
//has moves //has moves
gs.current_card = Card {value : CardValue::TWO, color : CardColor::GREEN}; gs.current_card = Card {value : CardValue::TWO, color : CardColor::GREEN};
gs.current_color = CardColor::GREEN;
let player = PlayerState::new("detlef", [get_card(CardValue::FOUR, CardColor::RED), get_card(CardValue::EIGHT, CardColor::GREEN)].to_vec()); let player = PlayerState::new("detlef", [get_card(CardValue::FOUR, CardColor::RED), get_card(CardValue::EIGHT, CardColor::GREEN)].to_vec());
assert_eq!(gs.has_any_moves(player), true);
} }
#[test] #[test]
fn test_check_if_legal_false(){ fn test_check_if_legal_false(){
//illegal //illegal
let current_card = Card {value : CardValue::TWO, color : CardColor::GREEN}; let current_card = Card {value : CardValue::TWO, color : CardColor::GREEN};
assert_eq!(GameState::<VecDeque<u8>>::check_if_legal(current_card, get_card(CardValue::FOUR, CardColor::RED), CardColor::GREEN), false); assert_eq!(GameState::<VecDeque<u8>>::check_if_legal(current_card, get_card(CardValue::FOUR, CardColor::RED)), false);
assert_eq!(GameState::<VecDeque<u8>>::check_if_legal(current_card, get_card(CardValue::EIGHT, CardColor::YELLOW), CardColor::GREEN), false); assert_eq!(GameState::<VecDeque<u8>>::check_if_legal(current_card, get_card(CardValue::EIGHT, CardColor::YELLOW)), false);
} }
#[test] #[test]
@ -775,15 +773,19 @@ mod tests {
assert_eq!(GameState::<VecDeque<u8>>::play(gs.player_states.get_mut(0).unwrap(),get_card(CardValue::EIGHT, CardColor::BLUE)), Err("Card not held by player".to_owned())); assert_eq!(GameState::<VecDeque<u8>>::play(gs.player_states.get_mut(0).unwrap(),get_card(CardValue::EIGHT, CardColor::BLUE)), Err("Card not held by player".to_owned()));
} }
#[test]
fn test_draw(){
}
#[test] #[test]
fn test_next_turn_green_two_to_red_two(){ fn test_next_turn_green_two_to_red_two(){
let mut gs = get_test_gs(); let mut gs = get_test_gs();
gs.game_init(); gs.game_init();
gs.current_card = get_card(CardValue::TWO, CardColor::GREEN); gs.current_card = get_card(CardValue::TWO, CardColor::GREEN);
gs.current_color = CardColor::GREEN;
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::TWO, CardColor::RED)); gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::TWO, CardColor::RED));
assert_eq!(gs.next_turn(get_card(CardValue::TWO, CardColor::RED)), TURN_RESULT::NEXT_TURN); assert_eq!(gs.next_turn(get_card(CardValue::TWO, CardColor::RED)), TURN_RESULT::NEXT_TURN);
assert_eq!(gs.current_color, CardColor::RED); assert_eq!(gs.current_card.color, CardColor::RED);
assert_eq!(gs.current_card, get_card(CardValue::TWO, CardColor::RED)); assert_eq!(gs.current_card, get_card(CardValue::TWO, CardColor::RED));
assert_eq!(gs.plus_twos, 0); assert_eq!(gs.plus_twos, 0);
assert_eq!(gs.turns, 2); assert_eq!(gs.turns, 2);
@ -795,10 +797,9 @@ mod tests {
let mut gs = get_test_gs(); let mut gs = get_test_gs();
gs.game_init(); gs.game_init();
gs.current_card = get_card(CardValue::TWO, CardColor::GREEN); gs.current_card = get_card(CardValue::TWO, CardColor::GREEN);
gs.current_color = CardColor::GREEN;
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::FOUR, CardColor::YELLOW)); gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::FOUR, CardColor::YELLOW));
assert_eq!(gs.next_turn(get_card(CardValue::FOUR, CardColor::YELLOW)), TURN_RESULT::REPEAT_TURN); assert_eq!(gs.next_turn(get_card(CardValue::FOUR, CardColor::YELLOW)), TURN_RESULT::REPEAT_TURN);
assert_eq!(gs.current_color, CardColor::GREEN); assert_eq!(gs.current_card.color, CardColor::GREEN);
assert_eq!(gs.current_card, get_card(CardValue::TWO, CardColor::GREEN)); assert_eq!(gs.current_card, get_card(CardValue::TWO, CardColor::GREEN));
assert_eq!(gs.turns, 1); assert_eq!(gs.turns, 1);
assert_eq!(gs.current_turn, 0); assert_eq!(gs.current_turn, 0);
@ -809,10 +810,9 @@ mod tests {
let mut gs = get_test_gs(); let mut gs = get_test_gs();
gs.game_init(); gs.game_init();
gs.current_card = get_card(CardValue::TWO, CardColor::GREEN); gs.current_card = get_card(CardValue::TWO, CardColor::GREEN);
gs.current_color = CardColor::GREEN;
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::SKIP, CardColor::GREEN)); gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::SKIP, CardColor::GREEN));
assert_eq!(gs.next_turn(get_card(CardValue::SKIP, CardColor::GREEN)), TURN_RESULT::NEXT_TURN); assert_eq!(gs.next_turn(get_card(CardValue::SKIP, CardColor::GREEN)), TURN_RESULT::NEXT_TURN);
assert_eq!(gs.current_color, CardColor::GREEN); assert_eq!(gs.current_card.color, CardColor::GREEN);
assert_eq!(gs.current_card, get_card(CardValue::SKIP, CardColor::GREEN)); assert_eq!(gs.current_card, get_card(CardValue::SKIP, CardColor::GREEN));
assert_eq!(gs.turns, 2); assert_eq!(gs.turns, 2);
assert_eq!(gs.current_turn, 2); assert_eq!(gs.current_turn, 2);
@ -820,7 +820,15 @@ mod tests {
#[test] #[test]
fn test_next_turn_green_two_to_plus_four(){ fn test_next_turn_green_two_to_plus_four(){
let mut gs = get_test_gs();
gs.game_init();
gs.current_card = get_card(CardValue::TWO, CardColor::GREEN);
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::PLUS_FOUR, CardColor::BLUE));
assert_eq!(gs.next_turn(get_card(CardValue::PLUS_FOUR, CardColor::BLUE)), TURN_RESULT::NEXT_TURN);
assert_eq!(gs.player_states.get(gs.current_turn as usize).unwrap().cards.len(), 11);
assert_eq!(gs.current_turn, 1);
assert_eq!(gs.current_card.color, CardColor::BLUE);
assert_eq!(gs.current_card, get_card(CardValue::PLUS_FOUR, CardColor::BLUE));
} }
#[test] #[test]
@ -832,4 +840,14 @@ mod tests {
fn test_next_turn_green_two_to_change_color(){ fn test_next_turn_green_two_to_change_color(){
} }
#[test]
fn test_next_turn_yellow_five_to_yello_three_last_card_no_uno(){
}
#[test]
fn test_next_turn_yellow_five_to_yellow_three_last_card_yes_uno(){
}
} }

View File

@ -38,7 +38,6 @@ impl Turn{
fn get_color(card_color_line : &str) -> Result<CardColor, String> { fn get_color(card_color_line : &str) -> Result<CardColor, String> {
match &card_color_line.split(":").collect::<Vec<&str>>().get(1).unwrap()[..] { match &card_color_line.split(":").collect::<Vec<&str>>().get(1).unwrap()[..] {
"BLACK" => Ok(CardColor::BLACK),
"BLUE" => Ok(CardColor::BLUE), "BLUE" => Ok(CardColor::BLUE),
"GREEN" => Ok(CardColor::GREEN), "GREEN" => Ok(CardColor::GREEN),
"YELLOW" => Ok(CardColor::YELLOW), "YELLOW" => Ok(CardColor::YELLOW),