AI : wolf,geit,kool en rivier puzzel

Algemene discussies en vragen omtrent Liberty BASIC programma's. Stuur zoveel mogelijk delen van listings (snippets) in als je hulp wilt.

Moderators: anthonio, Abcott

AI : wolf,geit,kool en rivier puzzel

Berichtdoor bluatigro » vr okt 10, 2014 10:34 am

dit is n oude puzzel die in vele vormen bestaat
ik weet de oplossing
maar ik wil dat de computer hem vindt

Code: Selecteer alles
''blua tirgo 10-okt-2014
''this is a try at a cat-bird-grain problem
''senario :
''you are a farmer who wants to cros a river
''there is a boat whitch is smal
''it is just big enoug to carry you + 1 item
''there are 2 problems :
''the cat and the bird cant be left alone
''otherwise the cat wil eat the bird
''ist the same whit the bird and the grain

''cros the river whitout something gets eaten

global catbit , birdbit , grainbit
catbit = 1
birdbit = 2
grainbit = 4
now = 0
tel = 0
dim p( 7 )
while now <> 7 and tel < 12
  tel = tel + 1
  dice = int( rnd(0) * 3 )
  select case dice
    case 0
      if now and catbit then
        future = now and ( 7 - catbit )
      else
        future = now or catbit
      end if
      if not( p( future ) ) and not( eat( future ) ) then
        now = future
        p( now ) = 1
        if now and catbit then
          print "move cat <="
        else
          print "move cat =>"
        end if
      end if
    case 1
      if now and birdbit then
        future = now and ( 7 - birdbit )
      else
        future = now or birdbit
      end if
      if not( p( future ) ) and not( eat( future ) ) then
        now = future
        p( now ) = 1
        if now and birdbit then
          print "move bird <="
        else
          print "move bird =>"
        end if
      end if
    case 2
      if now and grainbit then
        future = now and ( 7 - grainbit )
      else
        future = now or grainbit
      end if
      if not( p( future ) ) and not( eat( future ) ) then
        now = future
        p( now ) = 1
        if now and grainbit then
          print "move grain <="
        else
          print "move grain =>"
        end if
      end if
    case else
  end select
  print scene$( now )
wend
if tel < 12 then
  print "puzle sovled ."
else
  print "end program ."
end if
end
function scene$( x )
  uit$ = ""
  if x and catbit then uit$ = uit$ + "cat "
  if x and birdbit then uit$ = uit$ + "bird "
  if x and grainbit then uit$ = uit$ + "grain "
  uit$ = uit$ + "|~~~~| "
  if not( x and catbit ) then uit$ = uit$ + "cat "
  if not( x and birdbit ) then uit$ = uit$ + "bird "
  if not( x and grainbit ) then uit$ = uit$ + "grain "
  scene$ = uit$
end function
function eat( x )
  uit = 0
  if ( x and catbit ) and ( x and birdnit ) then uit = 1
  if ( x and bridbit ) and ( x and grainbit ) then uit = 1
  if not( x and catbit ) and not( x and birdbit ) then uit = 1
  if not( x and bridbit ) and not( x and grainbit ) then uit = 1
  eat = uit
end function
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: AI : wolf,geit,kool en rivier puzzel

Berichtdoor bluatigro » do okt 16, 2014 12:55 pm

i heb eat() verandererd
het is echter niet foutloos
Code: Selecteer alles
''blua tirgo 10-okt-2014
''this is a try at a cat-bird-grain problem
''senario :
''you are a farmer who wants to cros a river
''there is a boat whitch is smal
''it is just big enoug to carry you + 1 item
''there are 2 problems :
''the cat and the bird cant be left alone
''otherwise the cat wil eat the bird
''ist the same whit the bird and the grain

''cros the river whitout something gets eaten

global cat , bird , grain , you , now , ffff
cat = 1
bird = 2
grain = 4
you = 8
now = 0
future = 0
ffff = 2 ^ 16 - 1
tel = 0
dim p( 16 )
''while now <> ffff and tel < 50
''  tel = tel + 1
''  dice = int( rnd(0) * 4 )
''  select case dice
''    case 0
''      call move cat
''    case 1
''      call move bird
''    case 2
''      call move grain
''    case else
''      call move you
''  end select
''wend
''if tel < 50 then
''  print "puzle sovled ."
''else
''  print "end program ."
''end if
for i = 0 to 15
  print i , i and cat ; i and bird ; i and grain _
  ; i and you , eat( i )
next i
end
sub move bit
  if ( now and bit ) and ( now and you ) then
    future = now and ( ffff - bit )
    future = future and ( ffff - you )
  end if
  if not( now and bit ) and not( now and you ) then
    future = now or bit
    future = future or you
  end if
  if not( p( future ) ) and not( eat( future ) ) then
    now = future
    p( now ) = 1
    select case bit
      case cat
        if now and cat then
          print "move farmer & cat <="
        else
          print "move farmer & cat =>"
        end if
      case bird
        if now and bird then
          print "move farmer & bird <="
        else
          print "move farmer & bird =>"
        end if
      case grainbit
        if now and grain then
          print "move farmer & grain <="
        else
          print "move farmer & grain =>"
        end if
      case else
        if now and you then
          print "move farmer <="
        else
          print "move farmer =>"
        end if
    end select
    print scene$( now )
  end if
end sub
function scene$( x )
  uit$ = ""
  if x and cat then uit$ = uit$ + "cat "
  if x and bird then uit$ = uit$ + "bird "
  if x and grain then uit$ = uit$ + "grain "
  if x and you then uit$ = uit$ + "you "
  uit$ = uit$ + "|~~~~| "
  if not( x and cat ) then uit$ = uit$ + "cat "
  if not( x and bird ) then uit$ = uit$ + "bird "
  if not( x and grain ) then uit$ = uit$ + "grain "
  if not( x and you ) then uit$ = uit$ + "you "
  scene$ = uit$
end function
function eat( x )
  uit = 0
  if ( x and cat ) _
  and ( x and bird ) _
  and not( x and you ) then uit = 1
  if ( x and brid ) _
  and ( x and grain ) _
  and not( x and you ) then uit = 1
  if not( x and cat ) _
  and not( x and bird ) _
  and ( x and you ) then uit = 1
  if not( x and brid ) _
  and not( x and grain ) _
  and ( x and you ) then uit = 1
  eat = uit
end function
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: AI : wolf,geit,kool en rivier puzzel

Berichtdoor bluatigro » za okt 18, 2014 1:24 pm

update :
- eat() nu foutloos [ denk ik ]

error :
- de code prints 1/2 van de oplossing

Code: Selecteer alles
''blua tirgo 10-okt-2014
''this is a try at a cat-bird-grain problem
''senario :
''you are a farmer who wants to cros a river
''there is a boat whitch is smal
''it is just big enoug to carry you + 1 item
''there are 2 problems :
''the cat and the bird cant be left alone
''otherwise the cat wil eat the bird
''ist the same whit the bird and the grain

''cros the river whitout something gets eaten

global cat , bird , grain , you , now , ffff , tel , max
cat = 1
bird = 2
grain = 4
you = 8
now = 0
future = 0
ffff = 2 ^ 16 - 1
tel = 0
max = 10000
dim p( 16 )
p( 0 ) = 1
print scene$( 0 )
while now <> ffff and tel < max
  tel =  tel + 1
  dice = int( rnd(0) * 4 )
  select case dice
    case 0
      call move cat
    case 1
      call move bird
    case 2
      call move grain
    case else
      call move you
  end select
wend
if tel < max then
  print "puzle sovled ."
else
  print "end program ."
end if
''for i = 0 to 15
''  print i , i and cat ; i and bird ; i and grain _
''  ; i and you , eat( i )
''next i
end
sub move bit
  if ( now and bit ) and ( now and you ) then
    future = now and ( ffff - bit )
    future = future and ( ffff - you )
  end if
  if not( now and bit ) and not( now and you ) then
    future = now or bit
    future = future or you
  end if
  if not( p( future ) ) and not( eat( future ) ) then
    now = future
    p( now ) = 1
    select case bit
      case cat
        if now and cat then
          print "move you & cat <="
        else
          print "move you & cat =>"
        end if
      case bird
        if now and bird then
          print "move you & bird <="
        else
          print "move you & bird =>"
        end if
      case grain
        if now and grain then
          print "move you & grain <="
        else
          print "move you & grain =>"
        end if
      case else
        if now and you then
          print "move you <="
        else
          print "move you =>"
        end if
    end select
    print scene$( now )
  end if
end sub
function scene$( x )
  uit$ = ""
  if x and cat then uit$ = uit$ + "cat "
  if x and bird then uit$ = uit$ + "bird "
  if x and grain then uit$ = uit$ + "grain "
  if x and you then uit$ = uit$ + "you "
  uit$ = uit$ + "|~~~~| "
  if not( x and cat ) then uit$ = uit$ + "cat "
  if not( x and bird ) then uit$ = uit$ + "bird "
  if not( x and grain ) then uit$ = uit$ + "grain "
  if not( x and you ) then uit$ = uit$ + "you "
  scene$ = uit$
end function
function eat( x )
  uit = 0
  if (( x and cat )<>0) _
  and (( x and bird )<>0) _
  and (not( x and you )<>0) then uit = 1
  if (( x and bird )<>0) _
  and (( x and grain )<>0) _
  and (not( x and you )<>0) then uit = 1
  if (not( x and cat )<>0) _
  and (not( x and bird )<>0) _
  and (( x and you )<>0) then uit = 1
  if (not( x and bird )<>0) _
  and (not( x and grain )<>0) _
  and (( x and you )<>0) then uit = 1
  eat = uit
end function
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm


Keer terug naar Liberty BASIC Code

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron