Rodrigo Sargaço
Rodrigo Sargaço

Reputation: 31

How to make two agents fight

I need to make two teams of agents fight if they see each other. Each team has a 50% chance to win. If an agent has less energy than initially configured, it loses 50% of its energy. How do I do that? My code seems wrong. Thank you.

to move-teamA
ask teamA
[
ifelse any? teamB-on patch-right-and-ahead -90 1 or any? teamB-on patch-
ahead 1  
[
  fd 1
  if energy < advance_energy [set energy (energy / 2)]                            
  let x random 1
            if x = 0 [fd 1]
            if x = 1 [die]]
end  

Upvotes: 2

Views: 162

Answers (1)

Luke C
Luke C

Reputation: 10291

Have a look at this approach- I think it's a simpler version of what you're trying to do. I ignored your energy component as I'm not sure how you set that up, but you can put that in as you like.

breed [ teamA one-teamA ]
breed [ teamB one-teamb ]

to setup
  ca
  create-teamA 10 [
    setxy random-xcor random-ycor
    set color red
  ]
  create-teamB 10 [
    setxy random-xcor random-ycor
    set color blue
  ]
  reset-ticks
end

to go
  ask turtles [
    let enemy one-of turtles in-cone 1 90 with [ breed != [breed] of myself ]
    ifelse enemy != nobody [
      ifelse random 2 = 1 [ ; if visible enemy exists, flip coin
        ask enemy [ die ]   ; if 1, enemy dies, I move forward
        fd 1
      ] [ 
        die 
      ]                     ; if 0, I die
    ] [ 
      fd 1 
    ]                       ; if no enemy, move forward
  ]
  tick
end

Upvotes: 2

Related Questions