hawk's and dove's sim

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

hawk's and dove's sim

Postby bluatigro » Nov 07, 2018 12:06

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 dove
const as integer pop = 1000
dim shared as integer bird( pop ) , e( pop )
const as integer hawk = 1
const as integer dove = 2
function irange( low as integer , high as integer ) as integer
  return int( rnd * ( high - low + 1 ) + low )
end function
function 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 if
end function
dim as integer i , j , dice , hawktel , dovetel
dim as double q
for i = 0 to pop
  bird( i ) = irange( 1 , 2 )
  e( i ) = 10
next i
for 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 if
next j
print "[ end sim . ]"
sleep
bluatigro
Posts: 581
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: hawk's and dove's sim

Postby bluatigro » Nov 07, 2018 12:15

this is a other sim

Code: Select all

'' bluatigro 7 nov 2018
'' grass cow lion sim
const as integer world = 10000
dim as integer q( world ) ,  e( world )
dim as integer i , tel , cowtel , liontel , grasstel , dice
const as integer empty = 0
const as integer cow = 1
const as integer lion = 2
const as integer grass = 3
const as integer energie = 10
randomize timer
for 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 if
next i

for 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 if
next tel
print "[ end sim ]"
sleep

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests