From 3f905dca8936b90712b31f9471f1a79afe8bb8e1 Mon Sep 17 00:00:00 2001 From: mux Date: Mon, 12 Aug 2024 21:37:10 +0200 Subject: [PATCH] added a lot of stuff, can draw cards, reset a deck and create a game --- Cargo.toml | 1 + src/lib.rs | 251 ++++++++++++++++++++++++++++++++++++++++++++++ src/tcp_server.rs | 4 +- 3 files changed, 254 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 61224fb..233b2c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +rand = "0.8" diff --git a/src/lib.rs b/src/lib.rs index e69de29..ffe8703 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,251 @@ +extern crate rand; +use std::{ + arch::global_asm, + io::{ prelude::*, BufReader, BufWriter, WriterPanicked }, + net::{ TcpListener, TcpStream }, + usize, +}; +use rand::Rng; + +#[derive(Clone)] +pub struct PlayerState<'a> { + pub player_name: &'a str, + pub cards: Vec, +} + +struct PlayerConnection<'a>{ + player_name : &'a str, + reader: BufReader, + writer: BufWriter, +} + +impl PlayerState<'_> { + pub fn new(player_name : &str, cards : Vec) -> PlayerState{ + PlayerState { + player_name: player_name, + cards: cards, + } + } +} + + +impl PlayerConnection<'_> { + pub fn new(player_name : &str, reader : BufReader, writer : BufWriter) -> PlayerConnection{ + PlayerConnection { + player_name : player_name, + reader : reader, + writer : writer + } + } +} + +struct Card { + value: CardValue, + color: CardColors, +} + +impl PartialEq for Card{ + fn eq(&self, other: &Self) -> bool { + self.value == other.value && self.color == other.color + } +} + +type Deck = Vec; + +#[derive(Clone)] +enum CardColors { + RED, + BLUE, + GREEN, + YELLOW, + BLACK, +} + +impl PartialEq for CardColors{ + fn eq(&self, other: &Self) -> bool { + core::mem::discriminant(self) == core::mem::discriminant(other) + } +} + +impl Clone for Card { + fn clone(&self) -> Self { + Self { value: self.value.clone(), color: self.color.clone() } + } +} +#[derive(Clone)] +enum CardValue { + PLUS_TWO, + PLUS_FOUR, + ZERO, + ONE, + TWO, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, + REVERSE, + SKIP, + CHANGE_COLOR, +} + +impl PartialEq for CardValue{ + fn eq(&self, other: &Self) -> bool { + core::mem::discriminant(self) == core::mem::discriminant(other) + } +} + +pub struct GameState<'a>{ + pub turns : u64, + pub current_turn : PlayerState<'a>, + deck : Deck, + pub current_card : Card, + player_states : Vec>, + player_connections : Vec>, + pub current_color : CardColors, + pub current_direction : Direction, +} + +impl GameState<'_>{ + pub fn next_turn(game : GameState) -> GameState{ + return game; + } + + fn new_game_helper<'a>(player_states: Vec>, player_connections: Vec>) -> GameState<'a>{ + GameState{ + turns : 0, + current_turn : player_states[0].clone(), + deck : GameState::base_deck(), + current_card : Card{ value : CardValue::ZERO, color : CardColors::RED,}, + player_states : player_states.clone(), + player_connections : player_connections, + current_color : CardColors::RED, + current_direction : Direction::CLOCKWISE, + } + } + + pub fn new_game<'a>(player_states : Vec>, player_connections: Vec>) -> GameState<'a>{ + let mut new_game = GameState::new_game_helper(player_states, player_connections); + new_game.current_color = new_game.current_card.color.clone(); + return new_game; + } + + fn get_currently_held(&mut self) -> Vec{ + let mut currently_held = vec![]; + for hand in &mut self.player_states{ + currently_held.append(&mut hand.cards); + } + return currently_held; + } + + 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")); + } + return vec![]; + } + + fn base_deck()-> Vec{ + let mut deck : Deck = vec![]; + + //CHANGE COLOR + for _ in 0..3 {deck.push(Card {value : CardValue::CHANGE_COLOR, color : CardColors::BLACK,})}; + //PLUS FOUR + for _ in 0..3 {deck.push(Card{ value : CardValue::PLUS_FOUR, color : CardColors::BLACK})}; + //PLUS TWO + for _ in 0..1 {deck.push(Card{ value : CardValue::PLUS_TWO, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::PLUS_TWO, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::PLUS_TWO, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::PLUS_TWO, color : CardColors::YELLOW})}; + //REVERSE + for _ in 0..1 {deck.push(Card{ value : CardValue::REVERSE, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::REVERSE, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::REVERSE, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::REVERSE, color : CardColors::YELLOW})}; + //SKIP + for _ in 0..1 {deck.push(Card{ value : CardValue::SKIP, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SKIP, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SKIP, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SKIP, color : CardColors::YELLOW})}; + //NINE + for _ in 0..1 {deck.push(Card{ value : CardValue::NINE, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::NINE, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::NINE, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::NINE, color : CardColors::YELLOW})}; + //EIGHT + for _ in 0..1 {deck.push(Card{ value : CardValue::EIGHT, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::EIGHT, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::EIGHT, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::EIGHT, color : CardColors::YELLOW})}; + //SEVEN + for _ in 0..1 {deck.push(Card{ value : CardValue::SEVEN, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SEVEN, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SEVEN, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SEVEN, color : CardColors::YELLOW})}; + //SIX + for _ in 0..1 {deck.push(Card{ value : CardValue::SIX, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SIX, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SIX, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::SIX, color : CardColors::YELLOW})}; + //FIVE + for _ in 0..1 {deck.push(Card{ value : CardValue::FIVE, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::FIVE, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::FIVE, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::FIVE, color : CardColors::YELLOW})}; + //FOUR + for _ in 0..1 {deck.push(Card{ value : CardValue::FOUR, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::FOUR, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::FOUR, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::FOUR, color : CardColors::YELLOW})}; + //THREE + for _ in 0..1 {deck.push(Card{ value : CardValue::THREE, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::THREE, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::THREE, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::THREE, color : CardColors::YELLOW})}; + //TWO + for _ in 0..1 {deck.push(Card{ value : CardValue::TWO, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::TWO, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::TWO, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::TWO, color : CardColors::YELLOW})}; + //ONE + for _ in 0..1 {deck.push(Card{ value : CardValue::ONE, color : CardColors::RED})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::ONE, color : CardColors::BLUE})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::ONE, color : CardColors::GREEN})}; + for _ in 0..1 {deck.push(Card{ value : CardValue::ONE, color : CardColors::YELLOW})}; + //ZERO + deck.push(Card{ value : CardValue::ZERO, color : CardColors::RED}); + deck.push(Card{ value : CardValue::ZERO, color : CardColors::BLUE}); + deck.push(Card{ value : CardValue::ZERO, color : CardColors::GREEN}); + deck.push(Card{ value : CardValue::ZERO, color : CardColors::YELLOW}); + + return deck; + } + + fn get_cards(&mut self, count : u32) -> Vec{ + let mut cards_drawn = vec![]; + for _ in 0..count{ + let mut rng = rand::thread_rng(); + let n: usize = rng.gen_range(0..self.deck.len()); + cards_drawn.push(self.deck.remove(n)); + } + return cards_drawn + } + + pub fn draw(&mut self, player : &mut PlayerState, count : u32) -> (){ + if self.deck.len() <= count as usize{ + let currently_held = self.get_currently_held(); + self.reset_deck(currently_held); + } + let mut cards_drawn = self.get_cards(count); + player.cards.append(&mut cards_drawn); + } + +} + +enum Direction { + CLOCKWISE, + COUNTER_CLOCKWISE, +} diff --git a/src/tcp_server.rs b/src/tcp_server.rs index ea40213..fb64910 100644 --- a/src/tcp_server.rs +++ b/src/tcp_server.rs @@ -1,5 +1,5 @@ use std::{ - io::{ prelude::*, BufReader, BufWriter }, + io::{ prelude::*, BufReader, BufWriter, WriterPanicked }, net::{ TcpListener, TcpStream }, }; @@ -54,4 +54,4 @@ fn check_protocol( // fn register_player(player_name : &str, reader : BufReader, writer : BufWriter){ // -// } +// } \ No newline at end of file