PasteRack.org
Paste # 57362
2018-07-12 15:52:42

Fork as a new paste.

Paste viewed 873 times.


Embed:

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

Chet's new location is 6

The category is Sports

Sports Question 0

Answer was corrent!!!!

Chet now has 1 Gold Coins.

Pat is the current player

They have rolled a 6

Pat's new location is 6

The category is Sports

Sports Question 1

Answer was corrent!!!!

Pat now has 1 Gold Coins.

Sue is the current player

They have rolled a 6

Sue's new location is 6

The category is Sports

Sports Question 2

Answer was corrent!!!!

Sue now has 1 Gold Coins.

Chet is the current player

They have rolled a 3

Chet's new location is 9

The category is Science

Science Question 0

Answer was corrent!!!!

Chet now has 2 Gold Coins.

Pat is the current player

They have rolled a 6

Pat's new location is 0

The category is Pop

Pop Question 0

Answer was corrent!!!!

Pat now has 2 Gold Coins.

Sue is the current player

They have rolled a 1

Sue's new location is 7

The category is Rock

Rock Question 0

Answer was corrent!!!!

Sue now has 2 Gold Coins.

Chet is the current player

They have rolled a 1

Chet's new location is 10

The category is Sports

Sports Question 3

Answer was corrent!!!!

Chet now has 3 Gold Coins.

Pat is the current player

They have rolled a 3

Pat's new location is 3

The category is Rock

Rock Question 1

Answer was corrent!!!!

Pat now has 3 Gold Coins.

Sue is the current player

They have rolled a 2

Sue's new location is 9

The category is Science

Science Question 1

Answer was corrent!!!!

Sue now has 3 Gold Coins.

Chet is the current player

They have rolled a 3

Chet's new location is 1

The category is Science

Science Question 2

Answer was corrent!!!!

Chet now has 4 Gold Coins.

Pat is the current player

They have rolled a 3

Pat's new location is 6

The category is Sports

Sports Question 4

Answer was corrent!!!!

Pat now has 4 Gold Coins.

Sue is the current player

They have rolled a 3

Sue's new location is 0

The category is Pop

Pop Question 1

Answer was corrent!!!!

Sue now has 4 Gold Coins.

Chet is the current player

They have rolled a 3

Chet's new location is 4

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 1

Pat's new location is 7

The category is Rock

Rock Question 2

Answer was corrent!!!!

Pat now has 5 Gold Coins.

Sue is the current player

They have rolled a 5

Sue's new location is 5

The category is Science

Science Question 3

Answer was corrent!!!!

Sue now has 5 Gold Coins.

Chet is the current player

They have rolled a 2

Chet's new location is 6

The category is Sports

Sports Question 5

Answer was corrent!!!!

Chet now has 6 Gold Coins.