turing machine try

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

turing machine try

Postby bluatigro » Apr 01, 2020 14:17

got the chalence from a book [ 1980 ]
fill the code cards so that the "1"s be as many as poseble

error :
it does not work
i get only a black screen

card :
1 : what gets on tape [ b , 0 , 1 ]
2 : move on tape
3 : goto other card set

Code: Select all

'' bluatigro 1 apr 2020
'' program puzle
const as integer max = 50
dim as string code( 3 , 2 )
dim as string mem( max )
dim as integer i , j , plek
for i = 1 to 3
  for j = 0 to 2
    read code( i , j )
  next j
next i
''     b       0       1
data "1r2" , "br3" , "0l2"
data "1r3" , "1l1" , "bl3"
data "1l1" , "stop" , "1r1"

plek = int( max / 2 )
i = 0

while mem( plek ) <> "stop"
  j = instr( "b01" , mem( plek ) )
  print code( i , j )
  mem( plek ) = left( code( i , j ) , 1 )
  if mid( code( i , j ) , 2 , 1 ) = "r" then
    plek = ( plek + 1 ) mod max
  end if
  if mid( code( i , j ) , 2 , 1 ) = "l" then
    plek = ( plek - 1 ) mod max
  end if
  i = val( right( code( i , j ) , 1 ) )
wend
for i = 0 to max
  print mem( i ) ;
next i
print
sleep

angros47
Posts: 1628
Joined: Jun 21, 2005 19:04

Re: turing machine try

Postby angros47 » Apr 01, 2020 16:43

You made a mistake in

Code: Select all

for i = 1 to 3
  for j = 0 to 2
    read code( i , j )
  next j
next i


It should have been

Code: Select all

for i = 0 to 2


Otherwise, in the array, you will store the first instruction into code (1,0), then you read the instruction in code (0,0) (because you set "i = 0" in your code), and it can't work

Also, MOD can return negative number, so the line

Code: Select all

plek = ( plek - 1 ) mod max


can set plek to -1, causing a crash. A line like:

Code: Select all

plek = ( plek - 1 + max) mod max

would fix that
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: turing machine try

Postby bluatigro » Apr 20, 2020 8:09

update :
it works a little now

error :
i think the cardset's has a loop in it

Code: Select all

'' bluatigro 1 apr 2020
'' program puzle
const as integer max = 50
dim as string code( 3 , 3 )
dim as string tape( max )
dim as integer i , j , k , plek
for i = 1 to 3
  for j = 1 to 3
    read code( i , j )
  next j
next i
''     b       0       1
data "1r2" , ".r3" , "0l2"
data "1r3" , "1l1" , ".l3"
data "1l1" , "end" , "1r1"
for i = 0 to ubound( tape )
  tape(i) = "."
next i

plek = int( max / 2 )
i = 1
j = 1
while tape( plek ) <> "end"
  j = instr( ".01" , tape( plek ) )
  print code( i , j )
  tape( plek ) = left( code( i , j ) , 1 )
  if mid( code( i , j ) , 2 , 1 ) = "r" then
    plek = ( plek + 1 ) mod max
  end if
  if mid( code( i , j ) , 2 , 1 ) = "l" then
    plek = ( plek - 1 + max ) mod max
  end if
  i = val( right( code( i , j ) , 1 ) )
  for k = 0 to max
    print tape( k ) ;
  next k
  print
wend
print
sleep

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests