我是Racket的新手,我正在尝试为我的编程语言课写一个Mastermind游戏。我已经完成了代码,它也运行了。在下面的代码中,“解决方案”是4个随机生成的颜色列表。我想问一下如何修改我的'constructSolution'函数,这样我就可以运行循环/递归而不是编写set! 4次不同。我尝试修改constructSolution函数,如下所示:
(define i 0)
(define (constructSolution)
(cond
((if (< i 4)
(set! solution (append solution (list (random-color))))
(set! i (+ i 1))))
(else #f))
)
但它没有用。理想情况下,用户将输入列表的长度,并且该函数将检查'(if(&lt; i length))'而不是'(if(&lt; i 4))'。任何帮助,将不胜感激。这是我的代码:
#lang racket
(require racket/gui/base)
(define solution1 '(yellow red black blue))
(define solution '())
(define blackPegs 0)
(define totalPegs 0)
(define whitePegs 0)
(define countPegs 0)
(define numBlackPegs 0)
(define guessText "")
(define displayGuess "Guess ")
(define displayScore "Black Pegs = ")
(define displaySolution "Solution: ")
(define numGuess 1)
(define f (new frame% [label "Mastermind"]
[width 300]
[height 500]
[alignment '(center center)]))
(send f show #t)
(define c (new editor-canvas% [parent f]))
(define t (new text%))
(send c set-editor t)
(send t insert "Welcome to Mastermind!\n\n")
(define (constructSolution)
(set! solution (append solution (list (random-color))))
(set! solution (append solution (list (random-color))))
(set! solution (append solution (list (random-color))))
(set! solution (append solution (list (random-color))))
)
(define (random-color)
(choose '(red blue green yellow orange purple black)))
(define (choose xs)
(list-ref xs (random (length xs))))
(define (alist->string alst)
(string-join (map symbol->string alst) " "))
(constructSolution)
(set! displaySolution (string-append displaySolution (alist->string solution)))
(send t insert displaySolution)
(send t insert "\n\n")
(define (try guess)
(compareBlackPegs guess solution blackPegs)
(printf "(~a, " numBlackPegs)
(compareWhitePegs guess solution)
(printf "~a) ~n" whitePegs)
(set! guessText (alist->string guess))
(set! totalPegs 0)
(set! displayGuess (string-append displayGuess (number->string numGuess)))
(set! displayGuess (string-append displayGuess ": "))
(set! displayGuess (string-append displayGuess guessText))
(set! displayGuess (string-append displayGuess "\n"))
(send t insert displayGuess)
(set! displayGuess "Guess ")
(set! displayScore (string-append displayScore (number->string numBlackPegs)))
(set! displayScore (string-append displayScore ", White Pegs = "))
(set! displayScore (string-append displayScore (number->string whitePegs)))
(set! displayScore (string-append displayScore "\n\n"))
(send t insert displayScore)
(set! displayScore "Black Pegs = ")
(set! numGuess (+ numGuess 1))
)
(define (compareBlackPegs guess solution blackPegs)
(if (null? guess) (set! numBlackPegs blackPegs)
(if (equal? (car guess) (car solution)) (compareBlackPegs (cdr guess) (cdr solution) (+ blackPegs 1))
(compareBlackPegs (cdr guess) (cdr solution) blackPegs)))
)
(define (compareWhitePegs guess solution)
(cond ((null? solution)
(set! whitePegs (- totalPegs numBlackPegs)))
;(printf "~a) ~n" totalPegs))
(else
(let ((a (count (car solution) guess countPegs))
(b (count (car solution) solution countPegs)))
(if (equal? (< a b) #t) (set! totalPegs (+ totalPegs a))
(set! totalPegs (+ totalPegs b))))
(compareWhitePegs guess (cdr solution))))
)
(define (count x alist countPegs)
(if (null? alist) countPegs
(if (equal? x (car alist)) (count x (cdr alist) (+ countPegs 1))
(count x (cdr alist) countPegs)))
)
(try '(red red red red))
(try '(red yellow yellow yellow))
(try '(yellow red green green))
(try '(yellow red blue blue))
(try '(yellow red blue white))
(try '(yellow red black blue))
(send t insert "\nGoodbye!\n")
答案 0 :(得分:0)
请避免使用set!
在Scheme中实现循环,这不是可行的方法 - 使用递归或内置looping constructs。如果你想重复一个函数调用固定次数(比如说4),这里有一个如何使用显式递归来做这个的例子:
(define (repeat f n)
(cond ((zero? n) 'done)
(else (f)
(repeat f (sub1 n)))))
上述内容将调用f
一定次数。如有必要,请修改它以将其他参数传递给f
。例如:
(repeat (lambda () (display 'xo)) 4)
=> xoxoxoxo
'done