请编写一个可以让海龟上下左右移动的程序。
程序中的海龟需要存储一个位置(x, y),正数坐标表示向下或向右,并通过使用方法对相应的变量实施自增和自减来实现移动。
请使用 main 函数测试这些方法并打印出海龟的最终位置。
提示:为了修改海龟的 x 值和 y 值,你需要将方法的接收者设置为指针。
标准答案:
结合反射和随机包的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package main
import ( "fmt" "math/rand" "reflect" "time" )
type seaTurtle struct { x, y int }
func (s *seaTurtle) Up() { s.y++ }
func (s *seaTurtle) Down() { s.y-- }
func (s *seaTurtle) Left() { s.x-- }
func (s *seaTurtle) Right() { s.x++ }
var positions = []string{ "Up", "Down", "Left", "Right", }
func (s *seaTurtle) randMove() { rand.Seed(time.Now().UnixMilli()) time.Sleep(time.Millisecond * 5) randNum := rand.Intn(4) positionStr := positions[randNum] valueOf := reflect.ValueOf(s) method := valueOf.MethodByName(positionStr) method.Call(nil) fmt.Printf("seaTurtle %s, now position: (%d, %d)\n", positionStr, s.x, s.y) }
func main() { turtle := &seaTurtle{-5, 10} for i := 0; i < 5; i++ { turtle.randMove() } }
|
运行结果:
1 2 3 4 5
| seaTurtle Up, now position: (-5, 11) seaTurtle Left, now position: (-6, 11) seaTurtle Left, now position: (-7, 11) seaTurtle Up, now position: (-7, 12) seaTurtle Down, now position: (-7, 11)
|