Wankan

DiceRolls in CF

Sep 5th, 2020 (edited)
1,983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { choice } from "random.cf"
  2. import { stdout, write } from "sys.cf"
  3.  
  4.  
  5. data Dice = (...Num)
  6. data IndexedDice = (Dice d, Num n)
  7. IndexedDice.Dice = IndexedDice[0]   # IndexedDice.d
  8. IndexedDice.Index = IndexedDice[1]  # IndexedDice.n
  9.  
  10. dices = ( (0, 0, 4, 4, 4, 4),
  11.           (3, 3, 3, 3, 3, 3),
  12.           (2, 2, 2, 2, 6, 6),
  13.           (1, 1, 1, 5, 5, 5), )
  14.  
  15. Dice ->> Random Num
  16. roll = options => choice options
  17.  
  18. Dice ->> Random Num
  19. double_roll = dice => roll dice + roll dice
  20.  
  21. Dice, Dice ->> Random Bool
  22. duel = dice1, dice2 =>
  23.   double_roll dice1 > double_roll dice2
  24.  
  25. [Dice] ->> [(IndexedDice, IndexedDice)]
  26. create_pairs = with input(dices) do:
  27.   pairs = Mut []
  28.   for n, dice in zip [0..] dices:
  29.     for j, dice2 in zip [0..] dices[n+1:]:
  30.       pairs = [...pairs, (IndexedDice(dice1, n), IndexedDice(dice2, j))]
  31.   return Const pairs
  32.  
  33. [(IndexedDice, IndexedDice)] ->> [Random Num]
  34. round = {
  35.   pairs ''> 0,
  36.   (iDice1, iDice2) => duel iDice1.Dice iDice2.Dice
  37.     ? iDice1.Index
  38.     : iDice2.Index,
  39.   winner => @winner
  40. }
  41.  
  42. [T], T ->> Num
  43. count<T> = list, value => list >\ (acc, val => acc + Int <- val == value) 0
  44.  
  45. [(IndexedDice, IndexedDice)] ->> [Random Num]
  46. get_scores_from_round = pairs => count round pairs
  47.  
  48. [(IndexedDice, IndexedDice)], Num ->> [Random Num]
  49. complete_run = {
  50.   pairs, timesteps => (_ => get_scores_from_round pairs) [0..timesteps]''0
  51.   scores_map => (@scores_map)''1
  52.   sum,
  53. }
Add Comment
Please, Sign In to add comment