我是Racket的新手。我试图从here的问题1。以下是我可以让代码:在Racket中使用struct
#lang racket
(require 2htdp/image)
(require rackunit)
(require rackunit/text-ui)
(require "extras.rkt")
(define curr-dir "n")
(define curr-x 30)
(define curr-y 40)
;; Structure that I thought about
(define-struct robot (x y direction))
(define irobot (make-robot curr-x curr-y curr-dir))
(define MAX_HEIGHT 200)
(define MAX_WIDTH 400)
(define (render-robot)
(place-image
(crop 14 0 10 20 (circle 10 "solid" "blue"))
19 10
(circle 10 "solid" "red"))
)
(define (spawn-robot x y direction)
(place-image
(cond
[(string=? "n" direction) (rotate 90 (render-robot))]
[(string=? "e" direction) (render-robot)]
[(string=? "w" direction) (rotate 180 (render-robot))]
[(string=? "s" direction) (rotate 270 (render-robot))]
)
x y
(empty-scene MAX_WIDTH MAX_HEIGHT)
)
)
(define (initial-robot x y)
(if (and (<= x (- MAX_HEIGHT 10)) (<= y (- MAX_WIDTH 10)))
(spawn-robot x y curr-dir)
(error "The placement of the robot is wrong!")
)
)
(define robot1 (initial-robot curr-x curr-y))
;;-----------------------------------------------------;;
;; Doubt Here (Make the robot turn left)
(define (robot-left robot-obj)
(set! curr-dir "w") ;; Hard coded for north direction
(initial-robot curr-x curr-y)
)
;; Doubt Here (Function checks whether robot is facing north or not.)
(define (robot-north? robot-obj)
(if (string=? "n" curr-dir) (true) (false))
)
在解释我尝试了这一点:
我在想,代码可能会很好,但还是有些疑虑冒出我的脑海里:
在代码根据我使用的结构(使-结构)应 一直不错,但accordin克来解释我认为 问题的机器人实例是功能
initial-robot
的结果。 使用结构是否可行?在功能
robot-left
和robot-north?
我应该如何使用robot1
作为参数?设置存储对象当前方向的全局变量可用于提到的功能 。我应该在这里做什么?
任何建议是值得欢迎的。 谢谢!
大家知道,这个问题更适合于http://codereview.stackexchange.com/ – 2014-08-30 19:41:57