Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Nim-game/Racket/nim-game.rkt
Normal file
33
Task/Nim-game/Racket/nim-game.rkt
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#lang racket
|
||||
|
||||
(define (print-remaining tokens-remaining)
|
||||
(printf "~a tokens remain.\n" tokens-remaining))
|
||||
|
||||
(define (read-tokens)
|
||||
(define num (read))
|
||||
(cond
|
||||
[(and (natural? num) (< num 4)) num]
|
||||
[else
|
||||
(display "Please enter a number between 1 to 3\n")
|
||||
(read-tokens)]))
|
||||
|
||||
(define (pturn tokens-remaining)
|
||||
(cond
|
||||
[(not (zero? tokens-remaining))
|
||||
(print-remaining tokens-remaining)
|
||||
(display "Your turn. How many tokens? ")
|
||||
(define n (read-tokens))
|
||||
(cturn (- tokens-remaining n) n)]
|
||||
[else (display "Computer wins!")]))
|
||||
|
||||
|
||||
(define (cturn tokens-remaining p-took)
|
||||
(cond
|
||||
[(not (zero? tokens-remaining))
|
||||
(print-remaining tokens-remaining)
|
||||
(define c-take (- 4 p-took))
|
||||
(printf "Computer takes ~a tokens\n" c-take)
|
||||
(pturn (- tokens-remaining c-take))]
|
||||
[else (display "You win!")]))
|
||||
|
||||
(pturn 12)
|
||||
Loading…
Add table
Add a link
Reference in a new issue