PasteRack.org
Paste # 34563
2018-07-12 15:59:10

Fork as a new paste.

Paste viewed 905 times.


Embed:

A Racket translation of the trivia game used in Legacy Code retreats

  1. ; Also available at https://github.com/jbrains/trivia
  2.  
  3. #lang racket
  4.  
  5. (require data/queue)
  6.  
  7. (define players null)
  8. (define places null)
  9. (define purses null)
  10. (define in-penalty-box? null)
  11.  
  12. (define pop-questions null)
  13. (define science-questions null)
  14. (define sports-questions null)
  15. (define rock-questions null)
  16.  
  17. (define current-player null)
  18. (define is-getting-out-of-penalty-box? null)
  19.  
  20. (define (reset-all)
  21.   (set! players empty)
  22.   (set! places (make-vector 6))
  23.   (set! purses (make-vector 6))
  24.   (set! in-penalty-box? (make-vector 6))
  25.  
  26.   (set! pop-questions (make-queue))
  27.   (set! science-questions (make-queue))
  28.   (set! sports-questions (make-queue))
  29.   (set! rock-questions (make-queue))
  30.  
  31.   (set! current-player 0)
  32.   (set! is-getting-out-of-penalty-box? #f)
  33.  
  34.   (for ([i (in-range 50)])
  35.     (enqueue! pop-questions (~a "Pop Question " i))
  36.     (enqueue! science-questions (~a "Science Question " i))
  37.     (enqueue! sports-questions (~a "Sports Question " i))
  38.     (enqueue! rock-questions (create-rock-question i))))
  39.  
  40. (define (create-rock-question i)
  41.   (~a "Rock Question " i))
  42.  
  43. (reset-all)
  44.  
  45. (define (playable?)
  46.   (>= how-many-players 2))
  47.  
  48. (define (print . args)
  49.   (define s (apply ~a args))
  50.   (displayln s))
  51.  
  52. (define (add player-name)
  53.   (vector-set! places (how-many-players) 0)
  54.   (vector-set! purses (how-many-players) 0)
  55.   (vector-set! in-penalty-box? (how-many-players) #f)
  56.   (set! players (append players (list player-name)))
  57.  
  58.   (print player-name " was added")
  59.   (print "They are player number " (length players)))
  60.  
  61. (define (how-many-players)
  62.   (length players))
  63.  
  64. (define (roll roll)
  65.   (print (list-ref players current-player) " is the current player")
  66.   (print "They have rolled a " roll)
  67.  
  68.   (if (vector-ref in-penalty-box? current-player)
  69.       (cond [(odd? roll)
  70.              (set! is-getting-out-of-penalty-box? #t)
  71.  
  72.              (print (list-ref players current-player)
  73.                     " is getting out of the penalty box")
  74.              (vector-set! places current-player
  75.                           (+ roll (vector-ref places current-player)))
  76.              (when (> (vector-ref places current-player) 11)
  77.                (vector-set! places current-player
  78.                             (- (vector-ref places current-player) 12)))
  79.  
  80.              (print (list-ref players current-player)
  81.                     "'s new location is "
  82.                     (vector-ref places current-player))
  83.              (print "The category is " (current-category))
  84.              (ask-question)]
  85.             [else
  86.              (print (list-ref players current-player)
  87.                     " is not getting out of the penalty box")
  88.              (set! is-getting-out-of-penalty-box? #f)])
  89.       (begin
  90.         (vector-set! places current-player
  91.                      (+ roll (vector-ref places current-player)))
  92.         (when (> (vector-ref places current-player) 11)
  93.           (vector-set! places current-player
  94.                        (- (vector-ref places current-player) 12)))
  95.  
  96.         (print (list-ref players current-player)
  97.                "'s new location is "
  98.                (vector-ref places current-player))
  99.         (print "The category is " (current-category))
  100.         (ask-question))))
  101.  
  102. (define (ask-question)
  103.   (cond [(equal? (current-category) "Pop")
  104.          (print (dequeue! pop-questions))]
  105.         [(equal? (current-category) "Science")
  106.          (print (dequeue! science-questions))]
  107.         [(equal? (current-category) "Sports")
  108.          (print (dequeue! sports-questions))]
  109.         [(equal? (current-category) "Rock")
  110.          (print (dequeue! rock-questions))]))
  111.  
  112. (define (current-category)
  113.   (cond [(= (vector-ref places current-player) 0) "Pop"]
  114.         [(= (vector-ref places current-player) 4) "Pop"]
  115.         [(= (vector-ref places current-player) 8) "Pop"]
  116.         [(= (vector-ref places current-player) 1) "Science"]
  117.         [(= (vector-ref places current-player) 5) "Science"]
  118.         [(= (vector-ref places current-player) 9) "Science"]
  119.         [(= (vector-ref places current-player) 2) "Sports"]
  120.         [(= (vector-ref places current-player) 6) "Sports"]
  121.         [(= (vector-ref places current-player) 10) "Sports"]
  122.         [else "Rock"]))
  123.  
  124. (define (was-correctly-answered?)
  125.   (define winner null)
  126.   (if (vector-ref in-penalty-box? current-player)
  127.       (cond [is-getting-out-of-penalty-box?
  128.              (print "Answer was correct!!!!")
  129.              (vector-set! purses current-player (+ 1 (vector-ref purses current-player)))
  130.              (print (list-ref players current-player) " now has "
  131.                     (vector-ref purses current-player) " Gold Coins.")
  132.  
  133.              (set! winner (did-player-win?))
  134.              (set! current-player (+ current-player 1))
  135.              (when (= current-player (length players))
  136.                (set! current-player 0))
  137.              winner]
  138.             [else (set! current-player (+ current-player 1))
  139.                   (when (= current-player (length players))
  140.                     (set! current-player 0))
  141.                   #t])
  142.       (begin
  143.         (print "Answer was corrent!!!!")
  144.         (vector-set! purses current-player (+ 1 (vector-ref purses current-player)))
  145.         (print (list-ref players current-player) " now has "
  146.                (vector-ref purses current-player) " Gold Coins.")
  147.  
  148.         (set! winner (did-player-win?))
  149.         (set! current-player (+ current-player 1))
  150.         (when (= current-player (length players))
  151.           (set! current-player 0))
  152.         winner)))
  153.  
  154.  
  155. (define (wrong-answer)
  156.   (print "Question was incorrectly answered")
  157.   (print  (list-ref players current-player) " was sent to the penalty box")
  158.   (vector-set! in-penalty-box? current-player #t)
  159.  
  160.   (set! current-player (+ current-player 1))
  161.   (when (= current-player (length players))
  162.     (set! current-player 0))
  163.   #t)
  164.  
  165. (define (did-player-win?)
  166.   (not (= (vector-ref purses current-player) 6)))
  167.  
  168.  
  169. ; GAME LOOP
  170.  
  171. (define (play-game . players)
  172.   (reset-all)
  173.  
  174.   (for ([p players])
  175.     (add p))
  176.   (define not-a-winner #f)
  177.  
  178.   (let G ()
  179.     (roll (+ (random 6) 1))
  180.     (set! not-a-winner
  181.           (if (= (random 10) 7)
  182.               (wrong-answer)
  183.               (was-correctly-answered?)))
  184.  
  185.     (when not-a-winner
  186.       (G))))
  187.  
  188.  
  189. (play-game "Chet" "Pat" "Sue")

=>

Chet was added

They are player number 1

Pat was added

They are player number 2

Sue was added

They are player number 3

Chet is the current player

They have rolled a 1

Chet's new location is 1

The category is Science

Science Question 0

Answer was corrent!!!!

Chet now has 1 Gold Coins.

Pat is the current player

They have rolled a 2

Pat's new location is 2

The category is Sports

Sports Question 0

Answer was corrent!!!!

Pat now has 1 Gold Coins.

Sue is the current player

They have rolled a 1

Sue's new location is 1

The category is Science

Science Question 1

Answer was corrent!!!!

Sue now has 1 Gold Coins.

Chet is the current player

They have rolled a 1

Chet's new location is 2

The category is Sports

Sports Question 1

Answer was corrent!!!!

Chet now has 2 Gold Coins.

Pat is the current player

They have rolled a 5

Pat's new location is 7

The category is Rock

Rock Question 0

Answer was corrent!!!!

Pat now has 2 Gold Coins.

Sue is the current player

They have rolled a 2

Sue's new location is 3

The category is Rock

Rock Question 1

Answer was corrent!!!!

Sue now has 2 Gold Coins.

Chet is the current player

They have rolled a 5

Chet's new location is 7

The category is Rock

Rock Question 2

Answer was corrent!!!!

Chet now has 3 Gold Coins.

Pat is the current player

They have rolled a 5

Pat's new location is 0

The category is Pop

Pop Question 0

Answer was corrent!!!!

Pat now has 3 Gold Coins.

Sue is the current player

They have rolled a 2

Sue's new location is 5

The category is Science

Science Question 2

Answer was corrent!!!!

Sue now has 3 Gold Coins.

Chet is the current player

They have rolled a 3

Chet's new location is 10

The category is Sports

Sports Question 2

Answer was corrent!!!!

Chet now has 4 Gold Coins.

Pat is the current player

They have rolled a 4

Pat's new location is 4

The category is Pop

Pop Question 1

Answer was corrent!!!!

Pat now has 4 Gold Coins.

Sue is the current player

They have rolled a 6

Sue's new location is 11

The category is Rock

Rock Question 3

Answer was corrent!!!!

Sue now has 4 Gold Coins.

Chet is the current player

They have rolled a 2

Chet's new location is 0

The category is Pop

Pop Question 2

Answer was corrent!!!!

Chet now has 5 Gold Coins.

Pat is the current player

They have rolled a 2

Pat's new location is 6

The category is Sports

Sports Question 3

Answer was corrent!!!!

Pat now has 5 Gold Coins.

Sue is the current player

They have rolled a 5

Sue's new location is 4

The category is Pop

Pop Question 3

Answer was corrent!!!!

Sue now has 5 Gold Coins.

Chet is the current player

They have rolled a 5

Chet's new location is 5

The category is Science

Science Question 3

Answer was corrent!!!!

Chet now has 6 Gold Coins.