2015-11-28 110 views
0

我是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

哇!很多代碼。我有一個元建議和一個建議。

1)元建議:編程是所有關於如何自己找出問題的方法。在這種情況下,你想看看是否可以去掉這個巨型程序的那部分不會影響答案,找到一個顯示問題的小程序。

2)在這種情況下,我認爲你的問題只是當它們是頂級表達式的結果而不是其他的時候打印的值。舉一個簡單的例子,比較

#lang racket 

(+ 3 4) 

#lang racket 

(+ (+ 1 2) 4) 

爲什麼沒有第二個打印「3」,這是(+ 1 2)結果呢?在我看來,這與show1的結果未被顯示的原因相同。

警告:每個點#1,很難讀取代碼,因爲它有這麼多...