Guest User

Untitled

a guest
Jun 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. extern crate rand;
  2.  
  3. use rand::prelude::*;
  4. use std::fmt;
  5. use std::collections::HashMap;
  6.  
  7. #[derive(Debug, Copy, Clone)]
  8. enum CC{
  9. Spades,
  10. Hearts,
  11. Diamonds,
  12. Clubs,
  13. }
  14. impl fmt::Display for CC {
  15. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  16. match *self {
  17. CC::Hearts => write!(f, "♥"),
  18. CC::Diamonds => write!(f, "♦"),
  19. CC::Spades => write!(f, "♠"),
  20. CC::Clubs => write!(f, "♣"),
  21. }
  22. }
  23. }
  24.  
  25. static COLORS: &'static [CC] = &[CC::Spades, CC::Hearts, CC::Diamonds, CC::Clubs];
  26.  
  27. enum CV{
  28. Two=2,
  29. Three=3,
  30. Four=4,
  31. Five=5,
  32. Six=6,
  33. Seven=7,
  34. Eight=8,
  35. Nine=9,
  36. Ten=10,
  37. Jack=11,
  38. Queen=12,
  39. King=13,
  40. Ace=14,
  41. }
  42.  
  43. #[derive(Debug)]
  44. struct Card{
  45. color: CC,
  46. value: u8,
  47. }
  48. impl fmt::Display for Card {
  49. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  50. let val = match self.value {
  51. 14 => String::from("A"),
  52. 13 => String::from("K"),
  53. 12 => String::from("Q"),
  54. 11 => String::from("J"),
  55. 10 => String::from("X"),
  56. _ => self.value.to_string()
  57. };
  58. write!(f, "[{}{}]", self.color, val)
  59. }
  60. }
  61.  
  62. struct Player{
  63. hand: Vec<Card>,
  64. credit: u8,
  65. bet: u8,
  66. }
  67. impl Player{
  68. fn new() -> Player{
  69. Player{hand: vec!(), credit: 100, bet: 0}
  70. }
  71.  
  72. fn draw_from(&mut self, deck: &mut Deck){
  73. deck.draw().and_then(|drawn_card|{
  74. Some(self.hand.push(drawn_card))
  75. });
  76. }
  77.  
  78. fn increase_bet(&self, amount: u8){
  79. // TODO
  80. }
  81.  
  82. fn is_all_in(&self){
  83. self.bet >= self.credit;
  84. }
  85. }
  86. impl fmt::Display for Player {
  87. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  88. write!(f, " {:?}", self.hand)
  89. }
  90. }
  91.  
  92. struct Deck{
  93. cards: Vec<Card>,
  94. }
  95. impl Deck{
  96. fn new() -> Deck{
  97. let mut new_deck = Deck{cards: vec!()};
  98. for color in COLORS.iter() {
  99. for num in 2..15 {
  100. new_deck.cards.push(
  101. Card{color:*color,value:num}
  102. );
  103. }
  104. }
  105. new_deck
  106. }
  107.  
  108. fn draw(&mut self) -> Option<Card>{
  109. let mut rng = thread_rng();
  110. let rand_index: usize = rng.gen_range(0, self.cards.len());
  111. if !self.cards.is_empty() {
  112. return Some(self.cards.remove(rand_index));
  113. }
  114. None
  115. }
  116. }
  117.  
  118. struct Game{
  119. // Base Data
  120. players: HashMap<String, Player>,
  121. table: Vec<Card>,
  122. deck: Deck,
  123. // Game State
  124. player_order: Vec<String>,
  125. }
  126. impl Game{
  127. fn new() -> Game{
  128. Game{
  129. players: HashMap::new(), table: vec!(), deck: Deck::new(),
  130. player_order: vec!(),
  131. }
  132. }
  133.  
  134. fn join(&mut self, name: String){
  135. self.players.insert(name.clone(), Player::new());
  136. self.player_order.push(name);
  137. }
  138.  
  139. fn start(&mut self){
  140. for rounds in 0..2 {
  141. for (name, player) in &mut self.players {
  142. player.draw_from(&mut self.deck);
  143. }
  144. }
  145. for reveal in 0..3 {
  146. self.deck.draw().and_then(|drawn_card|{
  147. Some(self.table.push(drawn_card))
  148. });
  149. }
  150. }
  151.  
  152. fn current_player(&self) -> Option<&String> {
  153. Some(self.player_order.first().unwrap())
  154. }
  155.  
  156. fn next(&mut self){
  157. if !self.player_order.is_empty() {
  158. let val = self.player_order.remove(0);
  159. self.player_order.push(val.to_string());
  160. }
  161. }
  162.  
  163. fn pot(&self) -> u32 {
  164. let mut amount: u32 = 0;
  165. for (name, player) in &self.players {
  166. amount += player.bet as u32;
  167. }
  168. amount
  169. }
  170. }
  171.  
  172. fn main(){
  173.  
  174. let mut game = Game::new();
  175. println!("New Game created\n");
  176.  
  177. game.join(String::from("Archina"));
  178. println!("Archina joined");
  179. game.join(String::from("Glazial"));
  180. println!("Glazial joined");
  181. game.join(String::from("Zack"));
  182. println!("Zack joined");
  183.  
  184. game.start();
  185. println!("\nEveryone has drawn their two starting cards");
  186. for (name, player) in &game.players {
  187. println!("{} - {}", name, player.hand.len());
  188. }
  189. println!("\nPublic cards:\n {:?}\n",game.table);
  190.  
  191. println!("Player order is: {:?}", game.player_order);
  192. println!("The starting player is \"{}\"\n", game.current_player().unwrap_or(&String::from("<ERROR>")));
  193.  
  194. game.next();
  195. println!("Player order is: {:?}", game.player_order);
  196. println!("The starting player is \"{}\"\n", game.current_player().unwrap_or(&String::from("<ERROR>")));
  197.  
  198. }
Add Comment
Please, Sign In to add comment