I am creating two agents which are of breed players.
breed [players player]
The players will need to have certain attributes therefore I have used players-own
players-own
[
lives
health
score
]
I have then created the two players and set the values for the attributes.
create-players 2
ask player 0
[
set lives 3
set health 100
set score 0
]
ask player 1
[
set lives 5
set health 100
set score 0
]
The part I am struggling to work out is how can I display the value of player 0's lives in one monitor and the value of player 1's lives in another monitor.
Any help on this would be much appreciated :)
Put this code in one monitor:
[lives] of player 0
and this code in another:
[lives] of player 1
Related
I have a pool of turtles and I'm trying to select two random ones to play a game together. I use n-of on the turtles but sometimes n-of selects the same exact turtle twice and I have no idea why, as it should work without repetition. Can anyone help me understand what I'm doing wrong? This is an extract from the code:
to go
if ticks < cycleTime
[
playGame n-of 2 members
tick
]
if ticks = cycleTime
[
reset-ticks
]
end
to playGame[players]
let player1 item 0 [self] of players
let player2 item 1 [self] of players
;;sometimes it prints the exact member twice for player1 and player2
print(player1)
print(player2)
end
Check this reproducible example using global variables, that are easier to monitor:
globals [
players
player1
player2
]
to setup
clear-all
reset-ticks
create-turtles 100
end
to go
set players n-of 2 turtles
playGame players
tick
end
to playGame [plrs]
set player1 item 0 [self] of plrs
set player2 item 1 [self] of plrs
end
If you paste this code in your Code tab, create a monitor in the Interface for the [self] of players reporter, and hit setup+go (or else run repeat 10 [show [self] of players] in the Command Center), you will see that the two turtles' identifiers fluctuate, swapping their positions.
This brought to my mind something that I remember reading somewhere in the NetLogo documentation, but which I am unable to find: NetLogo constantly re-evaluates its variables, even if the model is not running.
When such variable is an agentset (such as players - because note that n-of reports an agentset if you pass it an agentset as input), or consequently depending on agentsets (such as [self] of players), this means that the internal ordering of the variable will be continuously shuffled.
This happens because agentsets are a random sorting of their relevant agents, and this variability is not a problem for NetLogo: on the contrary, it is at the core of its mechanism.
But in your case this means that sometimes the ordering of the players agentset (and therefore of the plrs argument, and therefore of the [self] of plrs list) changed in such a moment that what had been evaluated as item 0 became item 1 by the time item 1 was being evaluated.
How to solve this? What doesn't change its internal ordering are lists, so you can save your players as a list:
set players sort n-of 2 turtles
See that sort always reports a list.
This means that you can do:
set player1 item 0 players
set player2 item 1 players
and always have two different players.
If for some reason you do not want player1 to always be the oldest turtle, just directly set players as the list of selfs (as this will prevent sorting):
set players [self] of n-of 2 turtles
The problem is not n-of selecting the same turtle twice.
Instead, [self] of players is a list that is always generated in a random order. Since you generate this list twice, both lists are not necessarily in the same order. The following should illustrate this:
to playGame[players]
let thePlayers0 [self] of players
let thePlayers1 [self] of players
let thePlayers2 [self] of players
print(thePlayers0)
print(thePlayers1)
print(thePlayers2)
end
Generating the list of who's just once fixes your problem:
to playGame[players]
let thePlayers [self] of players
let player1 item 0 thePlayers
let player2 item 1 thePlayers
print(player1)
print(player2)
end
I am a beginner in Netlogo and trying to setup an agent (criminal) to shoot another agent (victim) when in range. One of the problems I am facing is how to keep the bullets - when ‘hatched’ from the criminal – to keep moving in a straight line. In other words, how can I make a turtle maintain its current heading?
Below is what I have unsuccessfully tried.
Hatch-bullets 1 [ set size 0.1
set color white
set label ""
set benergy 10
fd 1
set current-heading heading
]
ask bullets [ ifelse any? humans-here with [pcolor = green]
[ hit-target die ]
[ set heading current-heading
fd 1
set benergy benergy - 0.5
set heading current-heading
ask humans-here [die]
if benergy = 0 [die]
]
I was hoping bullet-heading (variable) will store the current heading at the poing of hatching, so that it can be used later for the life duration of the bullet as it travels in a straight line (which unfortunately does not on both counts)
The other related problem is that, one bullet will be hatched per tick (or every second tick) and each bullet needs to keep track of its own heading. Bullets should be moving on a straight line rather than following the movement of the parent.
I have seen the projectile game, but the code is not helpful
I'm doing code for making agents roaming around the world to forage.
After they find it, they should stay at the patch where they find the food for a certain time. But I have a problem to make them stay on the food patch for a certain time.
Certain time: each turtle has its own variable "s", and "s" is a random number generated by NetLogo and the number shouldn't change within each tick, and I have a problem to make "s" doesn't change.
For example, turtle 1 has "s"=60, after turtle 1 find the food, it will stay on the food for 60 ticks. After turtle 1 has stayed for 60 ticks there, it will leave the food and the value of "s" will be reset again to another random value. And each turtle has a different value of "s"
I tried to use timer coding, but the timer only decreases each time the agent come across the food (like they just walk on the food instead of staying there) I can't use bk 1 or fd -1 command because the turtle just keeps on moving forward after they walk backwards for 1 tick.
Any help is appreciated.
here is the code:
turtles-own
[
food
location
s
]
patches-own
[
food-quality
]
to go
move
forage
end
to move
ask turtles
[fd 1]
end
to forage
set s random 100
ask turtles
[ [ if food < food-quality ;meaning that turtle will only keep the best food's value each time they found food
[set food food-quality ;food-quality is patch-own, the food's value
set location patch-here]
if s >= 50
[stay] ]
]
]
end
to stay
set s s - 1 ;decrement-timer
if s = 0
[move
set label ""
reset-s
]
end
to reset-count-down
set s random 100
end
Edit: making the question clearer and update some code.
I think you have a logic problem rather than a coding problem, but it's not particularly clear. If your variable 's' is supposed to be something like a stay-timer (much more descriptive name), then don't set it at the beginning of the forage procedure. Instead, set it when the turtle finds food and then only move/forage if it's not being held by the stay-timer. You also don't have tick anywhere in your code, so you are not advancing the clock. I think you want something like this:
to forage
ask turtles
[ if food < food-quality ; has found food that is more than currently owned
[ set stay-timer random 5 + 20 ; sets number of ticks to stay between 5 and 24
set food food-quality ; food-quality is patch-own, the food's value
set location patch-here
]
set stay-timer stay-timer - 1
]
end
to go
ask turtles with [stay-timer = 0] [ move ]
forage
tick
end
I'm trying to create a 'Hierarchical' or 'Tree' network structure with a parameter expansion rate. At first, one node is placed at the top, and each node in the network is connected to a number of nodes below him equal to expansion rate. Currently my code looks like this:
to wire-tree
clear-all
ask patches [ set pcolor white ]
create-nodes 1 [ ; create root node of tree
set shape "circle"
set color red
set branch 0
expand-network
rewire-branches
]
radial-layout
reset-ticks
end
to expand-network
if expansion-rate = 0 [ stop ]
while [count nodes < num-nodes] [
ask nodes with-max [branch] [
hatch expansion-rate [
create-edge-with myself
set branch branch + 1
]
]
]
end
The network currently has the correct structure, but the number of nodes in the network exceed the number of nodes selected by the num-nodesslider. This is because there is first checked if count nodes < num-nodes after which the last hatch is performed. However, what I want is that this last hatch of nodes is performed up until num-nodes is reached and then stops. Thus, while each level of the hierarchy before the last one contains a number of nodes equal to a power of expansion rate, the last level may have fewer than this if the population does not divide properly.
How can I achieve this?
I need the turtle-owned branch variable because I later want to rewire the nodes in certain branches with a fixed probability. Will probably post a question about that later ;)
Replace your hatch expansion-rate with hatch min expansion-rate (num-nodes - count nodes) so it creates the minimum of two numbers - the expansion rate and the total you still need.
I'm quite new to Netlogo and am having difficulty with getting the turtles to change direction. I am trying to get my turtle to walk towards a 'bed' (which is a different breed of turtle) and then once it has arrived change direction.
This is what I have tried (as well as many other possibilities!)
if location = bed 3 [
face bed 0
if turtle-here = bed 0 [
set location bed 0
forward 1
stop
]
]
if location = bed 0 [
face bed 4
if turtle-here = bed 4 [
set location bed 4
forward 1
stop
]
]
It continues further to make a cyclic path. I think the problem is that when I'm asking about 'turtle-here' it will report true or false or give me a number, when what I need is the turtles name. But I can't find any primitives that will do that.
Any Ideas??
Thanks
Naomi
Replacing if turtle-here = bed 0 with if member? bed 0 turtles-here should fix it.
Not sure what you mean by a turtle's "name".
I recommend you look at Move Towards Target Example, in the Code Examples section of the Models Library. It's extremely similar to what you're trying to do.