## hawk's and dove's sim

General FreeBASIC programming questions.
bluatigro
Posts: 647
Joined: Apr 25, 2012 10:35
Location: netherlands

### hawk's and dove's sim

there are birds
they are hawk or dove
2 bird's meet and try to eat
if 2 hawk's :
- they fight and get no food
if 2 dove's :
- they share the food
if 1 hawk :
- hawk eats al food

this is a sim that show's and proofs
that the dove's get it good

conclusion :
cooperation is better then competision

Code: Select all

`'' bluatigro 7 nov 2018'' hawk and doveconst as integer pop = 1000dim shared as integer bird( pop ) , e( pop )const as integer hawk = 1const as integer dove = 2function irange( low as integer , high as integer ) as integer  return int( rnd * ( high - low + 1 ) + low )end functionfunction eat( a as integer , b as integer ) as integer  if a = hawk then    if b = hawk then      return -4    else      return 2    end if  else    if b = hawk then      return 0    else      return 1    end if  end ifend functiondim as integer i , j , dice , hawktel , doveteldim as double qfor i = 0 to pop  bird( i ) = irange( 1 , 2 )  e( i ) = 10next ifor j = 1 to 2 ^ 15  hawktel = 0  dovetel = 0  for i = 0 to pop    dice = irange( 0 , pop )    if i <> dice then      e( i ) = e( i ) + eat( bird( i ) , bird( dice ) )      if e( i ) < 0 then        bird( i ) = irange( 1 , 2 )        e( i ) = 10      end if      if e( i ) > 20 then        bird( dice ) = bird( i )        e( dice ) = 10      end if    end if    if bird( i ) = hawk then hawktel = hawktel + 1    if bird( i ) = dove then dovetel = dovetel + 1  next i  q = log( j ) / log( 2 )  if q = int( q ) then    print j , "hawk's : "  + str( hawktel ) , "dove's : " + str( dovetel )  end ifnext jprint "[ end sim . ]"sleep `
bluatigro
Posts: 647
Joined: Apr 25, 2012 10:35
Location: netherlands

### Re: hawk's and dove's sim

this is a other sim

Code: Select all

`'' bluatigro 7 nov 2018'' grass cow lion simconst as integer world = 10000dim as integer q( world ) ,  e( world ) dim as integer i , tel , cowtel , liontel , grasstel , diceconst as integer empty = 0const as integer cow = 1const as integer lion = 2const as integer grass = 3const as integer energie = 10randomize timerfor i = 0 to world  q( i ) = grass  if rnd < .5 then    q( i ) = cow    e( i ) = energie  end if  if rnd < .2 then    q( i ) = lion    e( i ) = energie  end ifnext ifor tel = 0 to 1000  cowtel = 0  liontel = 0  grasstel = 0  for i = 0 to world    dice = int( rnd(0) * world )    if i <> dice then      select case q( i )        case cow          q( i ) = empty          if q( dice ) <> lion then            if q( dice ) = grass then              e( dice ) = e( i ) + 3            else              e( dice ) = e( i ) - 1            end if            q( dice ) = cow          end if          if e( dice ) > energie * 2 then            q( i ) = cow            e( i ) = energie            e( dice ) = energie          else            e( i ) = 0          end if        case lion          q( i ) = empty          if q( dice ) = cow then            e( dice ) = e( i ) + 3            q( dice ) = lion          else            e( dice ) = e( i ) - 1            q( dice ) = lion          end if          if e( dice ) > energie * 2 then            q( i ) = lion            e( i ) = energie            e( dice ) = energie          else            e( i ) = 0          end if          e( i ) = 0        case grass          if q( dice ) = empty then            q( dice ) = grass          end if        case else ''empty      end select      if e( i ) < 0 then        e( i ) = 0        q( i ) = empty      end if    end if    if q( i ) = cow then cowtel = cowtel + 1    if q( i ) = lion then liontel = liontel + 1    if q( i ) = grass then grasstel = grasstel + 1  next i  if tel mod 100 = 0 then    print tel , grasstel , cowtel , liontel  end ifnext telprint "[ end sim ]"sleep `