Racket的游戏循环



我正在RACKET中开发一个非常基本的内存游戏,它向用户提供从1到10的7个随机数字,并要求他们按相同的顺序键入数字。我可以让它工作一次,但我希望它继续运行,直到用户退出。(重复主程序)关于如何实现这一点有什么想法吗???

(require math/base)
; =======================================
; Functions
; =======================================
; set essentially constants
(define lowerLimit 1)
(define upperLimit 11)
(define amount 7)

; builds a list with random integers between lowerLimit and upperLimit
(define (buildSimon x L)
  (cond [(= x 0) L]
        [else (buildSimon (- x 1) (cons (random-integer lowerLimit upperLimit) L))]))
; adds element to back of the list
(define (addBack L e)
  (if (null? L)
      (list e)
      (cons (first L) (addBack (rest L) e))))
; gets input from user and builds list
(define (getInput n x L)
  (cond [(= n 1) (addBack L x)]
        [else (getInput (- n 1) (read) (addBack L x))]))
; =======================================
; Main program below here
; =======================================
; Generate new random number sequence
(printf "Simon says: ")
(define sequence (buildSimon amount (list)))
(display sequence) (newline)
(printf "== Memorize and guess ==n")
(printf "== Type 'go' and hit enter to guess ==n")
(read)
; Add spacing to hide numbers
(printf "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn")
(printf "== Type the numbers in order as they appeared. ==n")
(printf "== Hit ENTER in between each number. ==n")
; Gather input for comparison
(define answer (getInput amount (read) (list)))
; Do comparison
(if (equal? sequence answer)
    (printf "Correct! Congratulations on your perfect memory.n")
    (printf "Wrong! I'm sorry your memory has failed you.n"))

提前谢谢。

您可以创建一个包含所有主代码的函数,函数的最后一行递归地调用自己。

相关内容

  • 没有找到相关文章