我是netlogo的新手,并尝试通过将磁贴更改为六边形而不是squrares来扩展扫雷代码。我似乎无法通过清除程序清除所有适当的图块或为所有应设置的标签设置标签。我如何解释它的工作原理是,当被称为clear时,首先会有点击的六边形模具,如果有一个标记存在,那么它也将死亡,接下来它会要求每个六边形计算其上有炸弹的邻居的数量并将其总变量设置为该数字,如果total大于0,则应使用该数字标记自己,否则也应将其清除。我错过了什么?
globals [
clock ;; how many seconds the game has lasted so far
game-started? ;; initially false, becomes true when player first presses GO
game-over? ;; initially false, becomes true if the player loses
;;total
]
breed [ hexagons hex ] ;; these are the green squares the player hasn't tested yet
hexagons-own [hex-neighbors]
breed [ mines mine ] ;; the mines (initially invisible)
breed [ markers marker ] ;; show where the player thinks mines are
to setup
clear-all
set clock 0
set game-started? false
set game-over? false
set-default-shape hexagons "hex"
set-default-shape mines "bomb"
set-default-shape markers "flag"
ask patches
[ sprout-hexagons 1
[ set color magenta + 3 ;;
;; shift even columns down
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ]
]
set pcolor sky - 3
]
;; now set up the hex-neighbors agentsets
ask hexagons
[ ifelse pxcor mod 2 = 0
[ set hex-neighbors hexagons-on patches at-points [[0 1] [1 0] [1 -1] [0 -1] [-1 -1] [-1 0]] ]
[ set hex-neighbors hexagons-on patches at-points [[0 1] [1 1] [1 0] [0 -1] [-1 0] [-1 1]] ] ]
;; make the number of mines determined by the mine-count slider
ask n-of mine-count patches [
sprout-mines 1 [
set color black
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ]
hide-turtle
]
]
top-scores
reset-ticks
end
to go
if game-over? [
ask markers with [any? mines-here] [ die ]
ask markers [ set color gray - 2 ]
ask mines [ show-turtle ]
set game-over? true
ask patches [ set pcolor red ]
stop
]
if not game-started? [
;; this must be the first time through GO, so start the clock
reset-timer
set game-started? true
]
set clock timer
if all? hexagons [any? mines-here] [
;; you win!!!
ask mines [ show-turtle ]
ask patches [ set pcolor blue ]
write-to-file
stop
]
if mouse-down? [
ask patch (mouse-xcor) (mouse-ycor) [
ifelse any? mines-here
[ set game-over? true ] ;; aiggghhhh!
[ clear ] ;; whew!
]
]
tick
end
to clear ;; patch procedure
ask hexagons-here [ die ]
ask markers-here [ die ]
ask hexagons
[let total count hex-neighbors with [any? mines-here]
ifelse total > 0
[set plabel word total " "]
;; if none of our neighbors have mines on them, then they can
;; be cleared too, to save the user from extra clicking
[ ask hex-neighbors with [any? hexagons-here]
[ clear ] ]]
end
to mark/unmark
if not mouse-inside? [ stop ]
ask patch mouse-xcor mouse-ycor [
if any? hexagons-here [
ifelse any? markers-here
[ ask markers-here [ die ] ]
[ sprout-markers 1 [ set color yellow ]] ] ]
end
to write-to-file
file-open "highScores.txt"
file-print (word "Bomb count: " mine-count " Time: " clock)
file-print("")
file-close
end
to top-scores
file-open "highScores.txt"
while [not file-at-end?]
[output-print file-read-line]
file-close
end