Guest User

Untitled

a guest
Apr 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #![feature(rustc_private)]
  2. extern crate rand;
  3. use rand::{Rng, thread_rng};
  4.  
  5. #[derive(Clone, Copy)]
  6. pub struct Board {
  7. // 0 is closest to the finish
  8. slots: [usize; 6],
  9. }
  10.  
  11. impl Board {
  12. // instantiate with a few pieces per slot
  13. pub fn new() -> Self {
  14. Board {
  15. slots: [2, 2, 2, 2, 2, 2],
  16. }
  17. }
  18. // see the board
  19. pub fn see(self) {
  20. self.slots.into_iter().enumerate()
  21. .for_each(|(i,x)|
  22. println!("slot {:?}: {:?}",i+1, vec![0; *x]),
  23. );
  24. }
  25. // searches for the highest slot with a piece (farthest from finish)
  26. // and moves that piece toward the finish with the roll
  27. // (Iterate high -> low)
  28. pub fn move_far(&mut self, roll: usize) {
  29. if self.is_done() { return }
  30. let mut success = false;
  31. let mut try = self.slots.len();
  32.  
  33. while success == false {
  34. success = self.move_piece(try, roll);
  35. try -= 1;
  36. }
  37. }
  38. // searches for the lowest slot with a piece (close to finish)
  39. // starting with the # of the roll. (eg roll 4: search slot 4)
  40. // (Iterate low -> high)
  41. // if there are no more slots higher, then go lower
  42. pub fn move_close(&mut self, roll: usize) {
  43. if self.is_done() { return }
  44. let mut increasing = true;
  45. let mut success = false;
  46. let mut try = roll;
  47.  
  48. while success == false {
  49. if try > self.slots.len() {
  50. increasing = false;
  51. try = roll - 1;
  52. }
  53. success = self.move_piece(try, roll);
  54. if increasing == true {
  55. try += 1;
  56. } else {
  57. try -= 1;
  58. }
  59. }
  60. }
  61. // checks if the current roll exactly moves a piece into crib;
  62. // if not, goes with move_far strategy.
  63. pub fn move_far_with_check(&mut self, roll: usize) {
  64. if self.is_done() { return }
  65. if self.has_piece(roll) {
  66. self.move_piece(roll, roll);
  67. } else {
  68. self.move_far(roll);
  69. }
  70. }
  71. fn has_piece(self, position: usize) -> bool {
  72. self.slots[position - 1] > 0
  73. }
  74. fn move_piece(&mut self, position: usize, roll: usize) -> bool {
  75. if self.has_piece(position) {
  76. // println!("there is a piece in this position");
  77. self.slots[position - 1] -= 1;
  78. if position <= roll {
  79. // println!("this piece goes to the crib");
  80. } else {
  81. // println!("there is a spot for it to move to");
  82. self.slots[position - roll - 1] += 1;
  83. }
  84. return true
  85. }
  86. return false
  87. }
  88. fn is_done(self) -> bool {
  89. let sum = self.slots.into_iter().fold(0,|a, &b| a + b);
  90. sum == 0
  91. }
  92. }
  93.  
  94.  
  95. fn main() {
  96. let mut cathie_scores = Vec::new();
  97. let mut harry_scores = Vec::new();
  98. let mut albert_scores = Vec::new();
  99.  
  100. for _ in 0..20 {
  101. let mut cathie = Board::new();
  102. let mut harry = Board::new();
  103. let mut albert = Board::new();
  104. let mut cathie_counter = 0;
  105. let mut harry_counter = 0;
  106. let mut albert_counter = 0;
  107.  
  108. while !(cathie.is_done() && harry.is_done() && albert.is_done()) {
  109. let roll = roll_die();
  110. // println!("roll: {:?}", roll);
  111.  
  112. if !cathie.is_done() {
  113. cathie.move_close(roll);
  114. // println!("cathie: ");
  115. // cathie.see();
  116. cathie_counter += 1;
  117. }
  118. if !harry.is_done() {
  119. harry.move_far(roll);
  120. // println!("harry: ");
  121. // harry.see();
  122. harry_counter += 1;
  123. }
  124. if !albert.is_done() {
  125. albert.move_far_with_check(roll);
  126. // println!("albert: ");
  127. // albert.see();
  128. albert_counter += 1;
  129. }
  130. }
  131. cathie_scores.push(cathie_counter);
  132. harry_scores.push(harry_counter);
  133. albert_scores.push(albert_counter);
  134. }
  135. // println!("cathie scores: {:?}", cathie_scores);
  136. // println!("harry scores: {:?}", harry_scores);
  137. // println!("albert scores: {:?}", albert_scores);
  138. println!("cathie average: {:?}", average(cathie_scores));
  139. println!("harry average: {:?}", average(harry_scores));
  140. println!("albert average: {:?}", average(albert_scores));
  141. }
  142.  
  143. fn average(list: Vec<usize>) -> f64 {
  144. list.iter().fold(0,|a, &b| a + b) as f64 / (list.len() as f64)
  145. }
  146.  
  147. // rolls between 1 and 6 (inclusive)
  148. fn roll_die() -> usize {
  149. let mut rng = thread_rng();
  150. rng.gen_range(1, 7)
  151. }
Add Comment
Please, Sign In to add comment