Post by bluatigro on Jul 29, 2019 5:13:52 GMT
i want to solve thge folowing puzzle :
a farmer has a dog a rabbit and carrots
he wants to cross a river
there is a boot
but it can only carry the farmer and one item
the dog wil kil the rabbit when left alone on one shore
the rabbit wil eat the carrots when left alone on one shore
my chalence : write a program that solves this puzzle
first try : using rnd
a farmer has a dog a rabbit and carrots
he wants to cross a river
there is a boot
but it can only carry the farmer and one item
the dog wil kil the rabbit when left alone on one shore
the rabbit wil eat the carrots when left alone on one shore
my chalence : write a program that solves this puzzle
first try : using rnd
global dog , rabbit , carrots
global shore.begin , shore.end
dog = 1
rabbit = 2
carrots = 4
shore.begin = dog + rabbit + carrots
shore.end = 0
while shore.begin
scan
dice = irange( 0 , 3 )
select case dice
case 0
if shore.begin and dog then
shore.begin = shore.begin - dog
if issave( shore.begin ) then
shore.end = shore.end + dog
print "farmer moves dog ."
else
shore.begin = shore.begin + dog
end if
end if
case 1
if shore.begin and rabbit then
shore.begin = shore.begin - rabbit
if issave( shore.begin ) then
shore.end = shore.end + rabbit
print "farmer moves rabbit ."
else
shore.begin = shore.begin + rabbit
end if
end if
case 2
if shore.begin and carrots then
shore.begin = shore.begin - carrots
if issave( shore.begin ) then
shore.end = shore.end + carrots
print "farmer moves carrots ."
else
shore.begin = shore.begin + carrots
end if
end if
case else
if issave( shore.begin ) then
print "farmer moves nothing ."
end if
end select
dice = irange( 0 , 3 )
select case dice
case 0
if shore.end and dog then
shore.end = shore.end - dog
if issave( shore.end ) then
shore.begin = shore.begin + dog
print "farmer moves dog back ."
else
shore.end = shore.end + dog
end if
end if
case 1
if shore.end and rabbit then
shore.end = shore.end - rabbit
if issave( shore.end ) then
shore.begin = shore.begin + rabbit
print "farmer moves rabbit back ."
else
shore.end = shore.end + rabbit
end if
end if
case 0
if shore.end and carrots then
shore.end = shore.end - carrots
if issave( shore.end ) then
shore.begin = shore.begin + rarrots
print "farmer moves dog back"
else
shore.end = shore.end + dog
end if
end if
case else
if issave( shore.end ) then
print "farmer moves nothing back ."
end if
end select
wend
print "puzzle solved !!"
end
function irange( low , high )
irange = int( rnd(0) * ( high - low + 1 ) ) + low
end function
function issave( shore )
a = ( shore and ( dog + rabbit ) )
b = ( shore and ( rabbit + carrots ) )
issave = not( a or b )
end function