Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate rand;
- use rand::prelude::*;
- use std::fmt;
- use std::collections::HashMap;
- #[derive(Debug, Copy, Clone)]
- enum CC{
- Spades,
- Hearts,
- Diamonds,
- Clubs,
- }
- impl fmt::Display for CC {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match *self {
- CC::Hearts => write!(f, "♥"),
- CC::Diamonds => write!(f, "♦"),
- CC::Spades => write!(f, "♠"),
- CC::Clubs => write!(f, "♣"),
- }
- }
- }
- static COLORS: &'static [CC] = &[CC::Spades, CC::Hearts, CC::Diamonds, CC::Clubs];
- enum CV{
- Two=2,
- Three=3,
- Four=4,
- Five=5,
- Six=6,
- Seven=7,
- Eight=8,
- Nine=9,
- Ten=10,
- Jack=11,
- Queen=12,
- King=13,
- Ace=14,
- }
- #[derive(Debug)]
- struct Card{
- color: CC,
- value: u8,
- }
- impl fmt::Display for Card {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let val = match self.value {
- 14 => String::from("A"),
- 13 => String::from("K"),
- 12 => String::from("Q"),
- 11 => String::from("J"),
- 10 => String::from("X"),
- _ => self.value.to_string()
- };
- write!(f, "[{}{}]", self.color, val)
- }
- }
- struct Player{
- hand: Vec<Card>,
- credit: u8,
- bet: u8,
- }
- impl Player{
- fn new() -> Player{
- Player{hand: vec!(), credit: 100, bet: 0}
- }
- fn draw_from(&mut self, deck: &mut Deck){
- deck.draw().and_then(|drawn_card|{
- Some(self.hand.push(drawn_card))
- });
- }
- fn increase_bet(&self, amount: u8){
- // TODO
- }
- fn is_all_in(&self){
- self.bet >= self.credit;
- }
- }
- impl fmt::Display for Player {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, " {:?}", self.hand)
- }
- }
- struct Deck{
- cards: Vec<Card>,
- }
- impl Deck{
- fn new() -> Deck{
- let mut new_deck = Deck{cards: vec!()};
- for color in COLORS.iter() {
- for num in 2..15 {
- new_deck.cards.push(
- Card{color:*color,value:num}
- );
- }
- }
- new_deck
- }
- fn draw(&mut self) -> Option<Card>{
- let mut rng = thread_rng();
- let rand_index: usize = rng.gen_range(0, self.cards.len());
- if !self.cards.is_empty() {
- return Some(self.cards.remove(rand_index));
- }
- None
- }
- }
- struct Game{
- // Base Data
- players: HashMap<String, Player>,
- table: Vec<Card>,
- deck: Deck,
- // Game State
- player_order: Vec<String>,
- }
- impl Game{
- fn new() -> Game{
- Game{
- players: HashMap::new(), table: vec!(), deck: Deck::new(),
- player_order: vec!(),
- }
- }
- fn join(&mut self, name: String){
- self.players.insert(name.clone(), Player::new());
- self.player_order.push(name);
- }
- fn start(&mut self){
- for rounds in 0..2 {
- for (name, player) in &mut self.players {
- player.draw_from(&mut self.deck);
- }
- }
- for reveal in 0..3 {
- self.deck.draw().and_then(|drawn_card|{
- Some(self.table.push(drawn_card))
- });
- }
- }
- fn current_player(&self) -> Option<&String> {
- Some(self.player_order.first().unwrap())
- }
- fn next(&mut self){
- if !self.player_order.is_empty() {
- let val = self.player_order.remove(0);
- self.player_order.push(val.to_string());
- }
- }
- fn pot(&self) -> u32 {
- let mut amount: u32 = 0;
- for (name, player) in &self.players {
- amount += player.bet as u32;
- }
- amount
- }
- }
- fn main(){
- let mut game = Game::new();
- println!("New Game created\n");
- game.join(String::from("Archina"));
- println!("Archina joined");
- game.join(String::from("Glazial"));
- println!("Glazial joined");
- game.join(String::from("Zack"));
- println!("Zack joined");
- game.start();
- println!("\nEveryone has drawn their two starting cards");
- for (name, player) in &game.players {
- println!("{} - {}", name, player.hand.len());
- }
- println!("\nPublic cards:\n {:?}\n",game.table);
- println!("Player order is: {:?}", game.player_order);
- println!("The starting player is \"{}\"\n", game.current_player().unwrap_or(&String::from("<ERROR>")));
- game.next();
- println!("Player order is: {:?}", game.player_order);
- println!("The starting player is \"{}\"\n", game.current_player().unwrap_or(&String::from("<ERROR>")));
- }
Add Comment
Please, Sign In to add comment