From b9dc550e72d046b8d247a16ca72bab9e092d77c4 Mon Sep 17 00:00:00 2001 From: mux Date: Thu, 15 Aug 2024 20:30:23 +0200 Subject: [PATCH] game logic fertig, (hoff ich zumindest) --- src/lib.rs | 96 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 70 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a252b4d..4fd52a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ extern crate rand; use std::{ - arch::global_asm, clone, io::{ prelude::*, BufReader, BufWriter, WriterPanicked }, net::{ TcpListener, TcpStream }, usize, vec + io::{ BufReader, BufWriter }, net::TcpStream, usize, vec }; use rand::Rng; const GAME_INTEGRITY_ERROR : &'static str = "game integrity compromised"; @@ -11,7 +11,7 @@ pub struct PlayerState<'a> { pub cards: Vec, } -struct PlayerConnection<'a>{ +pub struct PlayerConnection<'a>{ player_name : &'a str, reader: BufReader, writer: BufWriter, @@ -37,7 +37,7 @@ impl PlayerConnection<'_> { } } -struct Card { +pub struct Card { value: CardValue, color: CardColors, color_change : CardColors, @@ -49,6 +49,8 @@ impl PartialEq for Card{ } } +impl Copy for Card {} + type Deck = Vec; #[derive(Clone)] @@ -60,6 +62,10 @@ enum CardColors { BLACK, } +impl Copy for CardColors {} + +impl Copy for CardValue {} + impl PartialEq for CardColors{ fn eq(&self, other: &Self) -> bool { core::mem::discriminant(self) == core::mem::discriminant(other) @@ -104,13 +110,30 @@ pub struct GameState<'a>{ pub current_card : Card, player_states : Vec>, player_connections : Vec>, - pub current_color : CardColors, - pub current_direction : Direction, + current_color : CardColors, + current_direction : Direction, pub plus_twos : u32, } impl GameState<'_>{ + fn next_player(&mut self) { + if self.current_direction == Direction::CLOCKWISE{ + if self.current_turn+1 >= self.player_states.len() as u32{ + self.current_turn = 0; + return; + } + self.current_turn +=1; + } + else { + if self.current_turn == 0{ + self.current_turn = (self.player_states.len()-1) as u32; + return; + } + self.current_turn -=1; + } + } + fn has_any_moves(&self, player : PlayerState) -> bool{ for card in player.cards{ if GameState::check_if_legal(self.current_card.clone(), card, self.current_color.clone()){ @@ -120,7 +143,7 @@ impl GameState<'_>{ return false; } - fn add_to_trash<'a>(&mut self, card : Card){ + fn add_to_trash(&mut self, card : Card){ self.trash.push(card); } @@ -141,11 +164,11 @@ impl GameState<'_>{ } } - pub fn next_turn<'a>(mut self, card_to_be_played : Card) -> Result{ + pub fn next_turn(mut self, card_to_be_played : Card) -> Result{ if self.turns == 0 { let mut players : Vec = vec![]; for player in &self.player_states{ - &mut players.push(player.to_owned()); + players.push(player.to_owned()); } for mut player in &mut players{ self.draw(&mut player, 7); @@ -154,20 +177,21 @@ impl GameState<'_>{ return Ok(String::from("Game initialized")); } - if !GameState::check_if_legal(self.current_card.clone(), card_to_be_played.clone(), self.current_color.clone()){ + if !GameState::check_if_legal(self.current_card, card_to_be_played, self.current_color){ return Err(String::from("Please learn the rules at https://www.unorules.com/")); } if !GameState::has_any_moves(&self, self.player_states.get(self.current_turn as usize).expect(GAME_INTEGRITY_ERROR).to_owned()){ - let mut player = &mut self.player_states.clone(); - if !GameState::check_if_legal(self.current_card.clone(), self.draw(player.get_mut(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR), 1).get(0).expect(&GAME_INTEGRITY_ERROR).to_owned(), self.current_color.clone()){ + let player = &mut self.player_states.clone(); + if !GameState::check_if_legal(self.current_card, self.draw(player.get_mut(self.current_turn as usize).expect(&GAME_INTEGRITY_ERROR), 1).get(0).expect(&GAME_INTEGRITY_ERROR).to_owned(), self.current_color){ + self.next_player(); return Ok(String::from("Next turn")); } let mut player = self.player_states.clone(); - self.add_to_trash(GameState::play(player.get_mut(self.current_turn as usize).expect("game integrity compromised"), card_to_be_played.clone()).expect("game integrity compromised")); + self.add_to_trash(GameState::play(player.get_mut(self.current_turn as usize).expect("game integrity compromised"), card_to_be_played).expect("game integrity compromised")); } - let card = self.current_card.clone(); + let card = self.current_card; let card_value = card.value; if card_value == CardValue::PLUS_TWO && card_to_be_played.value != CardValue::PLUS_TWO{ let player = &mut self.player_states.clone(); @@ -175,28 +199,42 @@ impl GameState<'_>{ } else if card_value == CardValue::PLUS_TWO && card_to_be_played.value == CardValue::PLUS_TWO { self.plus_twos+=1; - if self.current_color != card_to_be_played.color.clone(){ + if self.current_color != card_to_be_played.color{ self.current_color = card_to_be_played.color; } + let mut player = self.player_states.clone(); + self.add_to_trash(GameState::play(player.get_mut(self.current_turn as usize).expect("game integrity compromised"), card_to_be_played).expect("game integrity compromised")); + self.next_player(); return Ok(String::from("Next turn")); } if self.current_card.value == CardValue::PLUS_FOUR{ let mut player = self.player_states.clone(); self.draw(player.get_mut(self.current_turn as usize).expect("game integrity compromised"), 4); } - if card_value == CardValue::CHANGE_COLOR { - self.current_color = card_to_be_played.clone().color_change; + if self.current_card.value == CardValue::SKIP{ + self.next_player(); + self.turns+=1; + return Ok(String::from("Next turn")); } - else if card_value == CardValue::PLUS_FOUR{ - self.current_color = card_to_be_played.color; - } else if [CardValue::EIGHT, CardValue::FIVE, CardValue::FOUR, CardValue::NINE, CardValue::ONE, CardValue::SEVEN, CardValue::SIX, CardValue::THREE, CardValue::TWO, CardValue::ZERO, CardValue::PLUS_TWO].contains(&card_value) { - if card_to_be_played.color != self.current_color{ - self.current_color = card_to_be_played.color.clone(); + if card_to_be_played.value == CardValue::REVERSE{ + if self.current_direction == Direction::CLOCKWISE{ + self.current_direction = Direction::COUNTER_CLOCKWISE; + } + else { + self.current_direction = Direction::CLOCKWISE; } - let mut player = self.player_states.clone(); - self.add_to_trash(GameState::play(player.get_mut(self.current_turn as usize).expect("game integrity compromised"), card_to_be_played).expect("game integrity compromised")); } + if [CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR].contains(&card_to_be_played.value) { + self.current_color = card_to_be_played.color_change; + } else if [CardValue::EIGHT, CardValue::FIVE, CardValue::FOUR, CardValue::NINE, CardValue::ONE, CardValue::SEVEN, CardValue::SIX, CardValue::THREE, CardValue::TWO, CardValue::ZERO, CardValue::PLUS_TWO, CardValue::SKIP, CardValue::REVERSE, CardValue::PLUS_TWO].contains(&card_to_be_played.value) { + if card_to_be_played.color != self.current_color{ + self.current_color = card_to_be_played.color; + } + } + let mut player = self.player_states.clone(); + self.add_to_trash(GameState::play(player.get_mut(self.current_turn as usize).expect("game integrity compromised"), card_to_be_played).expect("game integrity compromised")); self.turns+=1; + self.next_player(); return Ok(String::from("Next turn")); } @@ -229,7 +267,7 @@ impl GameState<'_>{ return currently_held; } - pub fn reset_deck(&mut self, currently_held : Vec) -> Deck{ + fn reset_deck(&mut self, currently_held : Vec) -> Deck{ self.deck = GameState::base_deck(); for current_cart in currently_held{ self.deck.remove(self.deck.iter().position(|x| *x == current_cart).expect("game integrity compromised")); @@ -328,7 +366,7 @@ impl GameState<'_>{ return deck; } - pub fn get_cards(&mut self, count : u32) -> Vec{ + fn get_cards(&mut self, count : u32) -> Vec{ let mut cards_drawn = vec![]; for _ in 0..count{ let mut rng = rand::thread_rng(); @@ -338,7 +376,7 @@ impl GameState<'_>{ return cards_drawn } - pub fn draw(&mut self, player : &mut PlayerState, count : u32) -> Vec{ + fn draw(&mut self, player : &mut PlayerState, count : u32) -> Vec{ if self.deck.len() <= count as usize{ let currently_held = self.get_currently_held(); self.reset_deck(currently_held); @@ -355,3 +393,9 @@ enum Direction { CLOCKWISE, COUNTER_CLOCKWISE, } + +impl PartialEq for Direction{ + fn eq(&self, other: &Self) -> bool { + core::mem::discriminant(self) == core::mem::discriminant(other) + } +}