如何打印功能输出

时间:2015-11-28 19:54:06

标签: scheme racket

我是racket的新手。请帮帮我。

在我的代码中,我定义了show1。当我从另一个函数中调用show1时,不会绘制位图。但是,当我单独调用该函数时,如(show1 a),它可以工作。 是否有任何解决方案可以打印出show1函数

(require racket/draw)
(require racket/gui)
(define target (make-bitmap 60 60)) ; A 30x30 bitmap
(define dc (new bitmap-dc% [bitmap target]))
(define board
  (lambda(ls call x y)
  ;(send dc draw-rectangle
    ;     0 0   ; Top-left at (0, 10), 10 pixels down from top-left
    ;    60 60)
  (if (even? call)
      (send dc set-brush "lightblue" 'solid)
      (send dc set-brush "white" 'solid))

  (send dc set-pen "black" 0 'transparent)
  (send dc draw-rectangle x y 20 20)
  (cond
    [(equal? (list-ref ls call) 'x)(printx x y) ]
    [(equal? (list-ref ls call) 'o)(printy x y) ]
    [(number? (list-ref ls call)) ])
  (cond
    [(< call 2) (board  ls (+ call 1) (+ x 20) 0)]
    [(= call 2) (board ls (+ call 1) 0 20)]
    [(and (> call 2) (< call 5)) (board ls (+ call 1) (+ x 20) 20)]
    [(= call 5) (board ls (+ call 1) 0 40)]
    [(and (> call 5) (< call 8))  (board ls (+ call 1) (+ x 20) 40)])
  target))

(define printx
  (lambda (x y)
    (send dc set-pen "red" 2 'solid) 
    (send dc draw-line x y (+ x 18) (+ y 18))
    (send dc draw-line (+ x 18) y x (+ y 18))))
(define printy
  (lambda (x y)
    (send dc set-pen "red" 2 'solid)
    (send dc set-brush "green" 'transparent)
    (send dc draw-rounded-rectangle x y 20 20 10)))

(require unstable/list)
(require racket/mpair)

(define sot 3)
(define a (build-list (* sot sot) values))
(define dummy 1)
(define play1
  (lambda (dummy)
            (set! dummy (+ 1 dummy))
            (play (- dummy 1))
            (play1 dummy)
            ))
(define show1
  (lambda (ls)
      (board a 0 0 0)))

" 0 1 2"
" 3 4 5"
" 6 7 8"
(newline)
(printf "Above shown is the address of the tic-tac-toe box \n")
;(newline)
(define firs 
  (lambda (value addr pla)

                (cond
                  [(> addr (- (* sot sot) 1)) (error "input error run it    again")]
                  [(number? (list-ref a addr)) (set! a (list-set a addr    pla))  (show1 a) (win a)]
                  [else (printf "error the box is already filled run it again\n") (firs value (read) pla)] )show1))
                  ;(set! dummy (- dummy 1))  
;[(error "error the box is already filled run it again")]

(define play
  (lambda (dummy)    
                (cond
                  [(> dummy (* sot sot)) (printf "MATCH DRAW \n")(exit)]
                  [(even? dummy) (print "second player") (newline) (set! dummy (+ 1 dummy))(firs dummy (read) 'o)]
                  [(not (even? dummy)) (print "first player")(newline) (firs dummy (read) 'x)])))

(define win
  (lambda (a)
    (cond [(and (equal? (list-ref a 0) (list-ref a 3)) (equal? (list-ref a 0) (list-ref a 6))) (cond [(equal? (list-ref a 0) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                     [(equal? (list-ref a 0) 'o) (print "player 2 wins")(newline) (exit)])]
          [(and (equal? (list-ref a 1) (list-ref a 4)) (equal? (list-ref a 1) (list-ref a 7))) (cond [(equal? (list-ref a 1) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                                     [(equal? (list-ref a 1) 'o) (print "player 2 wins")(newline) (exit)])]
          [(and (equal? (list-ref a 2) (list-ref a 5)) (equal? (list-ref a 2) (list-ref a 8))) (cond [(equal? (list-ref a 2) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                                     [(equal? (list-ref a 2) 'o) (print "player 2 wins")(newline) (exit)])]
          [(and (equal? (list-ref a 0) (list-ref a 1)) (equal? (list-ref a 0) (list-ref a 2))) (cond [(equal? (list-ref a 0) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                                     [(equal? (list-ref a 0) 'o) (print "player 2 wins")(newline) (exit)])]
          [(and (equal? (list-ref a 3) (list-ref a 4)) (equal? (list-ref a 3) (list-ref a 5))) (cond [(equal? (list-ref a 3) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                                     [(equal? (list-ref a 3) 'o) (print "player 2 wins")(newline) (exit)])]
          [(and (equal? (list-ref a 6) (list-ref a 7)) (equal? (list-ref a 6) (list-ref a 8))) (cond [(equal? (list-ref a 6) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                                     [(equal? (list-ref a 6) 'o) (print "player 2 wins")(newline) (exit)])]
          [(and (equal? (list-ref a 0) (list-ref a 4)) (equal? (list-ref a 0) (list-ref a 8))) (cond [(equal? (list-ref a 0) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                                     [(equal? (list-ref a 0) 'o) (print "player 2 wins")(newline) (exit)])]
          [(and (equal? (list-ref a 2) (list-ref a 4)) (equal? (list-ref a 2)(list-ref a 6)))  (cond [(equal? (list-ref a 2) 'x) (print "player 1 wins")(newline) (exit)]
                                                                                                     [(equal? (list-ref a 2) 'o) (print "player 2 wins")(newline) (exit)])]
          [else (board a 0 0 0)]
          )));[(equal? value 8) (newline)"match draw"])))
(show1 a)
(play1 dummy)

1 个答案:

答案 0 :(得分:1)

哇!很多代码。我有一个meta-suggestion和一个建议。

1)元建议:编程就是学习如何自己解决问题。在这种情况下,您想看看是否可以删除影响答案的巨型程序部分,找到一个显示问题的小程序。

2)在这种情况下,我认为你的问题只是当值是顶层表达式的结果时打印,而不是其他。举一个简单的例子,比较

#lang racket

(+ 3 4)

#lang racket

(+ (+ 1 2) 4)

为什么第二个打印&#34; 3&#34;,这是(+ 1 2)的结果?在我看来,这与show1的结果没有显示的原因相同。

警告:每点#1,很难阅读代码,因为它有很多......