Day 22/100: Still more Shut the Box

Did a little more development with Scheme / Racket and Shut the Box. I fixed a few bugs in the previous implementation where you could remove tiles by flipping unflipped tiles before the flipped tiles are checked. I also put in a check so that the tiles that are flipped set a flag so additional tiles can't be flipped.

#lang racket

#| Init Block |#

(define tiles '())
(define die1 0)
(define die2 0)
(define turn-number 1)
(define took-turn #f)

(define (start-game)
(set! tiles '(1 2 3 4 5 6 7 8 9)))

(define (dice)
(+ 1 (random 6)))

(define (dice-roll)
(set! die1 (dice))
(set! die2 (dice)))

(define (sum-of-dice)
(+ die1 die2))

(define (sum-of-tiles tilelist)
(apply + tilelist))

(define (list-check l1 l2)
(andmap (lambda (x) (not (boolean? (memq x l2)))) l1))

(define (shut-tiles tilelist)
(if (not took-turn) 
    (if (list-check tilelist tiles)
    (for ([i tilelist])
        (if (index-of tiles i)
            (begin
            (set! tiles (remove i tiles))
            (set! took-turn #t))
            (error "Tile already shut")))
    (error "Tile not available to be shut"))
(error "Already took your turn")))

(define (check-roll dice-sum tile-sum)
(= dice-sum tile-sum))

(define (player-turn tilelist)
(if (check-roll (sum-of-dice) (sum-of-tiles tilelist))
    (shut-tiles tilelist)
    (print "Roll does not equal shut tiles. Try again.")))

(start-game)
(dice-roll)

(define (next-turn)
(set! turn-number (+ 1 turn-number))
(set! took-turn #f)
(dice-roll)
(show-turn))

(define (show-turn)
(printf "Turn: ")
(println turn-number)
(println tiles)
(printf "Dice roll ~v ~v = ~v" die1 die2 (sum-of-dice) ))

links

social