How to move turtles on a game board simulation? - netlogo

I'm creating a board game simulation similar to monopoly, where pieces are moving around the board. My turtle acts as the user's game piece and moves around the border of the world. The border is set up in alternating colors. I have a button that generates the number of steps the turtle moves. Those steps are stored as dice-num, a global variable. However, when the turtle lands on the patches before the patch (16 -16) (the bottom right-hand corner of the board) and the number of steps it needs to move exceeds 1, I can't turn the turtle's heading halfway to move up the board. As a result it only moves back to the beginning of the board.
I've tried to treat each case separately:
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
;dice-num refers to number of steps the turtle moves
Heres my code so far:
to setup
board
end
to go
dice-roll
end
to board
ask patches [if pxcor = max-pxcor or pycor = max-pycor or pxcor = min-
pxcor or pycor = min-pycor
[set pcolor blue]]
ask patches [if pycor = max-pycor or pycor = min-pycor
[if pxcor mod 2 = 0
[set pcolor orange]]]
ask patches [if pxcor = max-pxcor or pxcor = min-pxcor
[if pycor mod 2 = 0
[set pcolor orange]]]
ask patch min-pxcor min-pycor [set pcolor green]
end
to dice-roll
set dice [1 2 3 4 5 6]
set dice-num one-of dice
user-message (word "You rolled: " dice-num)
ask turtle 0 [
fd dice-num
]
;allows the turtle to turn if it lands on a corner
ask turtle 0 [if ycor = min-pycor and xcor = max-pxcor [set heading 0]
if xcor = max-pxcor and ycor = max-pycor [set heading 270]
if xcor = min-pxcor and ycor = max-pycor [set heading 180]
;add a statement to end game once player rereaches the green patch
]
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
end
I expect the output to be the turtle to start moving up the right hand side of the board once it lands on the patch (15 -16) and recieves a dice-num greater than 1. However, when the turtle does land on the patch (15 -16) and the dice-num is greater than 1 it simply moves back to the beginning of the board.

Okay, this was simply too long for comments so I had to make some guesses. I expect the problem was that you were forwarding the full amount of the die roll after checking location. This would mean you have to test all the potential patches that could reach the corner. Instead, it is easier to move one patch and test location before moving the next patch. This is a cleaned up version of your code that implements this move and check process.
to setup
clear-all
board
ask one-of patches with [pcolor = green]
[ sprout 1
[ set color white
set heading 90
forward 1
]
]
end
to go
let die-num dice-roll
ask one-of turtles [ move die-num ]
end
to board
ask patches with [ pxcor = max-pxcor or pxcor = min-pxcor ]
[ set pcolor ifelse-value (pycor mod 2 = 0) [orange][blue] ]
ask patches with [ pycor = max-pycor or pycor = min-pycor ]
[ set pcolor ifelse-value (pxcor mod 2 = 0) [orange][blue] ]
ask patch min-pxcor min-pycor [set pcolor green]
end
to-report dice-roll
let dice-num one-of [1 2 3 4 5 6]
user-message (word "You rolled: " dice-num)
report dice-num
end
to move [#roll]
while [ #roll > 0 ]
[ if pxcor = max-pxcor and pycor = max-pycor [set heading one-of [180 270] ]
if pxcor = max-pxcor and pycor = min-pycor [set heading one-of [0 270] ]
if pxcor = min-pxcor and pycor = max-pycor [set heading one-of [180 90] ]
if pxcor = min-pxcor and pycor = min-pycor [ stop ]
fd 1
set #roll #roll - 1
]
end
The alternative is simply to add the die roll to the current location and see if it goes past. If it does, calculate where you want to get to.

Related

Turtles randomly walking in specific area

Hi I would like to hatch turtles at random patches but confined within a specific area.
ask employees [
set shape "person"
set size 1.5
set color blue
setxy random-xcor random-ycor ]
How do I adjust the setxy line to force the turtles to all be confined to: [ pxcor <= 8 and pxcor >= -8 and pycor <= 8 and pycor >= -8 ]
After looking through various bits of different code, what I've done is:
globals [office-space]
set office-space patches with [ pxcor <= 8 and pxcor >= -8 and pycor <= 8 and pycor >= -8 ]
ask office-space [ set pcolor grey]
place-on-color-employees
to place-on-color-employees
let _patches (patches with [pcolor = grey])
ask employees [
move-to one-of (_patches with [not any? turtles-here])
]
end

expected input to be a number but got the TRUE/FALSE true instead

I want to write an evacuation with leader code in net-logo but i have this error (AND expected input to be a TRUE/FALSE but got the number 0 instead.)the error came from function (follow_leader). my project is that i evacuate an environment and evacuate it with the nearest leader from the crowd . the code is not perfect that because iam knew to netlogo .
this is my code
globals[ goal-x goal-y n number leader
percent_of_leader wall
window
door largecircle]
patches-own [path?
obstacle
goal]
turtles-own [direction
fast?
fear?
leader?
is-leader?
follower
other-nearby]
to setup
clear-all
set-default-shape turtles "person"
;create-turtles 50 [ set color yellow ]
;ask turtles [fd random 13 ]
drwa-walls
ask n-of population patches with [ pcolor = black]
[sprout 1
[ set color white
set size 0.9
set shape "person"
set leader? false
;set follower self
if xcor < 9 and ycor > 9 [set heading 0]
if xcor >= 9 and ycor >= -10[set heading -90]
if xcor > -12 and ycor < -10 [set heading 90]
if xcor <= -8 and ycor > -10 and ycor < 0 [set heading 180]
]]
choose-leaders
end
to choose-leaders
ask turtle 6
[
set leader? true
set color yellow
set size 1
set shape "default"
set leader self
set xcor 3
set ycor 10
set heading 0
]
end
to go
move-for
ask turtle 6[move-to-exit1
fd 1 ]
ask turtles with [shape = "person"][
ifelse (xcor < 9 and ycor > 9 ) [follow_leader] [move-to-exit1]
if xcor >= 9 and ycor >= -10 [move-to-exit2]
if xcor > -12 and ycor < -10 [move-to-exit3]
if xcor < -12 and ycor < -10 [move-to-exit3]
if xcor <= -8 and ycor > -10 and ycor < 0 [move-to-exit4]
;avoid obstcao
; avoid_obstacles
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ] ;; Otherwise, it is safe to move forward.
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;blue or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;blue or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;blue
;[lt random-float 360 ]
;[fd 1]
; while [patch-ahead 1 = nobody]
;[
;lt random-float 360
;]
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ]
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;white or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;white or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;white
; [lt random-float 360 ]
; [fd 1]
;ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;green or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;green or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;green
;[lt random-float 360]
; [fd 1]
]
reset-ticks
tick
end
to rt-random
while [patch-ahead 3 = nobody]
[
rt random 360
]
end
to avoide
ask turtles [
if [pcolor] of patch-ahead 1 = green
[lt random 360 fd 1]
]
end
to avoid_obstacles ;; all obstacles set as green patches
let i 1
while [[pcolor] of patch-ahead i != blue ]
[set i (i + 1) ]
if ([pcolor] of patch-ahead i = blue)
[
ifelse [pcolor] of patch-at-heading-and-distance (heading - 20) i + 1 = blue
[
ifelse [pcolor] of patch-at-heading-and-distance (heading + 20) i + 1 = blue
[
ifelse random 1 = 0
[ rt 360 ]
[ lt 360]
]
[ rt 360 ]
]
[lt 360]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;
to bounce
if [pcolor] of patch-at dx 8 = blue [
set heading (90)
]
if [pcolor] of patch-at 0 dy = blue [
set heading (-90)
]
end
;;;;;;;;;;;;;;;;;;;;;;;
to move-for
ask turtles with [shape = "person"][fd 1]
end
to drwa-walls
draw-exit1
draw-exit2
draw-exit3
draw-exit4
;rows x
ask patches with [pxcor <= 30 and pxcor >= 0 and pycor = -10]
[set pcolor blue]
ask patches with [pxcor >= -30 and pxcor <= 0 and pycor = 9]
[set pcolor blue]
;coilmn
;ask patches with [pycor <= 9 and pycor >= -10 and pxcor = 0]
;[set pcolor green]
;ask patches with [pycor <= 4 and pycor >= 0 and pxcor = 0]
;[set pcolor green]
;circle shape
ask patch -14 -9 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor green
]
ask patch 16 16 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor white
]
end
to draw-exit1
;exits at top of concourse area, where turtles will leave
set goal-x -1
set goal-y -30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit2
set goal-x -1
set goal-y 30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit3
set goal-x -30
set goal-y -6
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit4
set goal-x 30
set goal-y 4
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
;;;;;;;;;;;;;;;;;;;;;;exit goals;;
to move-to-exit1
facexy -1 30
end
to move-to-exit2
facexy 30 4
end
to move-to-exit3
facexy -1 -30
end
to move-to-exit4
facexy -30 -6
end
;;;;;;;;;;;;;;;;;;;;;;;;;;
;to follow-leader
;if not leader? ;; we only want to ask non-leaders
;[let nearby-leaders turtles with [leader? and distance myself < 3] ;; find nearby leaders
;if any? nearby-leaders ;; to avoid 'nobody'-error, check if there are any first
; [ set heading (towards min-one-of nearby-leaders [distance myself]) ]] ;; then face the one closest to myself
; end
;;;;;;;;;;;;;;;;;;;;;;;;;;
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10] ;; nearby leaders
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
In the future, please only provide relevant code. For NetLogo, that is usually the procedure that throws the error (or doesn't work correctly etc) and whichever procedure calls it.
In your case, this is the procedure you mentioned:
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10]
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
So the and that is throwing the error must be [is-leader? and distance myself < 10] (also, please state which line if you know it). My guess would be that you haven't initialised the variable is-leader? so it is 0 (the default value) instead of either true or false.
If this is the problem, wherever you creates turtles, initialise with set is-leader? false

turtles change color on particular patch color

this code contains a road setup and turtle creation , after that on the go procedure that makes turtles take the pink path , and am trying to make turtles change color with they get to a red colored patch , but for unknown reason this code is not working and the cars never change their color and stay blue as there creation color
i hope i get some help to do it
turtles-own [
speed
s?
]
to setup-road
clear-all
ask patches [
ifelse pycor < -8 and pycor > -17 [set pcolor black ]
[set pcolor gray - 3 ]
if pycor < -9 and pycor > -15 [ set pcolor gray ]
if pycor > -10 and pycor < -8 and pxcor > 20 and pxcor < 27
[set pcolor white ]
if pycor < -2 and pycor > -4 and pxcor < 20 and pxcor > -24
[set pcolor pink ]
if pycor < -3 and pycor > -10 and pxcor < -22 and pxcor > -24
[set pcolor green ]
if pxcor = 20 and pycor = -3
[set pcolor red
]
]
set-default-shape turtles "car"
create-turtles 2 [
set color blue
set size 2
set xcor random-xcor
set ycor -12
set heading 90
set speed 0.1 + random-float 0.9
set s? true
]
reset-ticks
end
to go
ask turtles [
let car-ahead one-of turtles-on patch-ahead 2
if car-ahead != nobody
[ fd speed]
let gate one-of patches in-radius 5 with [pcolor = green ]
let path one-of patches in-radius 5 with [pcolor = pink ]
if s?
[ ifelse gate != nobody
[ move-to gate
fd speed
ifelse path != nobody
[
move-to path
fd speed
if patch-ahead 1 != nobody and pcolor != pink
[set color green ]
]
[fd speed]]
[fd speed]
]
]
tick
end

How to make turtles move around patches with specific rules?

I am new to Netlogo, and have some questions. That would be great if you could help me.
I would like to create some fruit flies moving around a tree that is made up by green patches. Fruit flies are attracted to the tree. They will turn back to the tree if fruit flies move away certain distance (such as 5 grids) from the tree.
In the beginning, they will not stop on the green patches because they have enough energy. As time passed, they will loss their energy. They will find the closest green patch, and stay on it for certain time once their energy reaches 0. They gain energy after that, and they only can do short hops from the bottom branch to the top one (3 hops). Flies will move back to the bottom branch when they are on the top part of tree. I think I need to do a WHILE loop, but I have no idea how to do that. Please look at my codes.
breed [flies fly]
breed [suns sun]
turtles-own [energy]
flies-own [count-down]
to setup
clear-all
setup-suns
setup-flies
setup-patches
reset-ticks
end
to setup-suns
;; Create the sun
set-default-shape suns "sun"
create-suns 1 [
setxy max-pxcor - 3
max-pycor - 3
set color yellow
set size 7
]
end
to setup-flies
set-default-shape flies "bee 2"
create-flies number-fly [ set color white setxy random-xcor random-ycor set count-down 15]
end
to setup-patches
;; Create sky and grass
ask patches
[ set pcolor blue ]
ask patches with [pycor < min-pycor + 2]
[ set pcolor 66 ]
;; Create trunk and branches
ask patches with [ pxcor = -15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = -15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 8 and pycor <= 2 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 22 and pycor <= 2 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 1 and pycor <= -1 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 29 and pycor <= -1 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = 15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = 15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 22 and pycor <= 2 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 8 and pycor <= 2 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 29 and pycor <= -1 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 1 and pycor <= -1 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = -26 and pycor = -3 ] [ set pcolor red ]
ask patches with [ pxcor = -10 and pycor = 5 ] [ set pcolor red ]
ask patches with [ pxcor = 21 and pycor = -1 ] [ set pcolor red ]
end
to go
move-flies
tick
end
to move-flies
ask flies [
set energy 6
ifelse energy > 10 [
;rt random 50 lt random 50 jump random-float 1 ;
let nearest-leaf min-one-of (patches with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 5 grids - long range search.
if is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
;set heading 45
;fd distance nearest-leaf
rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
]
[
ifelse pcolor != lime
[ rt random 50 lt random 50 jump random-float 1 ; Initialization - flies fly around to search their resources.
continue]
[ stay ]
]
]
end
to continue
let nearest-leaf min-one-of (patches in-radius 1 with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 2 grids - short hops.
ifelse is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
fd random distance nearest-leaf
;ask patches in-radius 1 [set pcolor red]
;rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
[
let turn-back min-one-of (patches with [pcolor = lime] ) [distance myself] ;
;set heading 180
face turn-back
jump random-float 5 ]
move-up ;Flies tend to move up through all branches by short hops. Need a while loop.
; Move down if they reach the top of tree
let canopy patch-at-heading-and-distance 0 1
let canopy-left patch-left-and-ahead 45 1
let canopy-right patch-right-and-ahead 45 1
if canopy != lime or canopy-left != lime or canopy-right != lime
[
move-down
]
end
to move-up
ask flies [
set heading one-of [0 30 -30]
]
end
to move-down
ask flies [
set heading one-of [180 120 -120]
]
end
to stay
set count-down count-down - 1 ;;decrement timer
set label count-down
if count-down = 0
[
rt random 50 lt random 50 jump random-float 1
set label ""
reset-count-down ;;it's another procedure
]
end
to reset-count-down
set count-down 30
end
I am sorry if you are confused by my codes. I appreciate your help. Thanks.
Kind Regards,
Ming
From your description, I think you almost certainly don't want a while loop. In a NetLogo model's go procedure, you're only specifying what happens in a single tick. So you would only use while if need it to express something that happens all in a single instant, not a process that unfolds over multiple ticks.
(Agree with Frank this is too much code to post to have a good chance of getting help. It would take quite a while for me to read and study this much code, let alone try to help you with it. In this answer I've tried to extract and address a single aspect.)

avoiding patch that has turtle netlogo

Can anyone help me with this. I'am trying to simulate a situation wherein the turtle will randomly sit on a bench (red patch) and if the bench is occupied it will find another one.
1 turtle: 1 patch
breed [kids kid]
breed [adults adult]
breed [oldies old]
kids-own [step]
adults-own [step]
oldies-own [step]
to setup
__clear-all-and-reset-ticks
ask patches [setup-world]
set-default-shape turtles "person"
create-kids number-of-kids
create-adults number-of-adults
create-oldies number-of-oldies
ask kids[
set color green
set size 1
setxy -10 0
set heading random-float 90
rt 45 - random-float 90]
ask adults[
set color orange
set size 1
setxy -10 0
set heading random-float 45
rt 45 - random-float 90]
to setup-world
set pcolor white
if ( pxcor = 10 ) and ( pycor < 10 and pycor > -11 ) [ set pcolor brown ]
if ( pxcor = -10 ) and ( pycor < 10 and pycor > 1 ) [ set pcolor brown ]
if ( pxcor = -10 ) and ( pycor < -1 and pycor > -11 ) [ set pcolor brown ]
if ( pycor = 10 ) and ( pxcor < 11 and pxcor > -11 ) [ set pcolor brown ]
if ( pycor = -10 ) and ( pxcor < 10 and pxcor > -11 ) [ set pcolor brown ]
if ( pxcor = 8 ) and ( pycor < 8 and pycor > 2 ) [ set pcolor red ]
if ( pxcor = 8 ) and ( pycor < -2 and pycor > -8 ) [ set pcolor red ]
end
to go
ask kids[
if pcolor = red and not any? other turtles-here[
move-to patch-here
stop]
fd 1
ifelse pcolor = red and any? other turtles-here
[rt random 90]
[fd 1]
]
ask adults[
if pcolor = red and not any? other turtles-here[
move-to patch-here
stop]
fd 1
ifelse pcolor = red and any? other turtles-here
[rt random 90]
[fd 1]
]
tick
end
you have already done most of the coding in the right way, I have tested your code and it works fine except your second condition in your go function for kids and adults.
One way to do the same thing is adding a turtle variable for example seated? Variable and make it false at initialization and make it true if the turtle is sitting on a bench. And only ask turtles with false seated? to look for another bench.
turtles-own [seated?]
ask kids with [not seated? ][
rt random 10
fd 1
if pcolor = red and not any? other turtles-here [
move-to patch-here
set seated? true]
]
ask adults with [not seated?]
[
rt random 10
fd 1
if pcolor = red and not any? other turtles-here[
move-to patch-here
set seated? true]
]
I have tested the code by showing number of turtles in each pacth and there was only one turtle per red patch
ask patches with [pcolor = red ][set plabel count turtles-here]