252 lines
9.6 KiB
Rust
252 lines
9.6 KiB
Rust
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<Card>,
|
|
}
|
|
|
|
struct PlayerConnection<'a>{
|
|
player_name : &'a str,
|
|
reader: BufReader<TcpStream>,
|
|
writer: BufWriter<TcpStream>,
|
|
}
|
|
|
|
impl PlayerState<'_> {
|
|
pub fn new(player_name : &str, cards : Vec<Card>) -> PlayerState{
|
|
PlayerState {
|
|
player_name: player_name,
|
|
cards: cards,
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
impl PlayerConnection<'_> {
|
|
pub fn new(player_name : &str, reader : BufReader<TcpStream>, writer : BufWriter<TcpStream>) -> 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<Card>;
|
|
|
|
#[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<PlayerState<'a>>,
|
|
player_connections : Vec<PlayerConnection<'a>>,
|
|
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<PlayerState<'a>>, player_connections: Vec<PlayerConnection<'a>>) -> 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<PlayerState<'a>>, player_connections: Vec<PlayerConnection<'a>>) -> 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<Card>{
|
|
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<Card>) -> 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<Card>{
|
|
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<Card>{
|
|
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,
|
|
}
|