2012-01-27 6 views
0

私はboe-botというロボットでプロジェクトを進めています。Pbasicでboe-botと迷路の最短距離を計算する

私の目標は、迷路を2回通過することです。最初の実行では、私のboe-botは、メモリに取り込まれたパスを格納している迷路を横断します。 2回目の実行では、すべてのパスがメモリに格納され、行き止まりにつながる悪いパスが削除されるため、boe-botは迷路の最後まで最短経路をとることができます。

これを行うには、行き詰まりにつながる悪いルートを解析するための置換ルールを作成する必要があります。

私はpbasicでコードを作成しましたが、コードにエラーが発生しました。これで私を助けることができる人がいますか?

' {$STAMP BS2} 
' {$PBASIC 2.5} 



' -----[ Variables ]---------------------------------------------------------- 
turn VAR Word 
turns VAR Word 
pointer VAR Byte 
ptr VAR pointer 'create an alias for pointer 

' -----[ Main Routine ]------------------------------------------------------- 
DO ' Begin main routine 

ptr = 0 'Points TO the NEXT available position in the array. 

turns(ptr) = turn 'This puts an L in the first position of the array or left turn in array 

ptr = ptr + 1 'Add one TO the pointer so the NEXT letter goes in the NEXT position in the array. 

IF (turns < 3)THEN 'Array needs at least three characters for this algorithm to work 
RETURN 
IF (turns(3)(ptr) - 1 <> "U")THEN 'EXIT IF the NEXT-TO-last turn is NOT a U-Turn 
RETURN 

IF (turns(3) = "LUL") 'Look at the right three characters in the array Left U-Turn Left 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "S" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "LUR") 'Look at the right three characters in the array Left U-Turn Right 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "U" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "LUS") 'Look at the right three characters in the array Left U-turn Straight 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "R" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 
' Increment/decrement routine only changes pulse durations by 2 at a time. 

IF (turns(3) == "RUL") 'Look at the right three characters in the array Right U-turn Left 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "U" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "RUR") 'Look at the right three characters in the array Right U-turn Right 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "L" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "RUS") 'Look at the right three characters in the array Right U-turn Straight 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "L" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer to point to the NEXT character in the array. 

IF (turns(3) == "SUL") 'Look at the right three characters in the array Straight U-turn Left 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "R" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "SUR") 'Look at the right three characters in the array Straight U-turn Right 
pointer = pointer - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(pointer) = "L" 'The turn we should have taken (AND will take NEXT time. 
pointer = pointer + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "SUS") 'Look at the right three characters in the array Straight U-turn Straight 
pointer = pointer - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(pointer) = "U" 'The turn we should have taken (AND will take NEXT time. 
pointer = pointer + 1 'set up the pointer TO point TO the NEXT character in the array. 
+0

構文やアルゴリズムに問題はありますか? –

+0

より良い回答を得たい場合は、タグ(pbasicなど)、出力されたエラー、(場合によっては)疑似コードを追加することができます。 http://sscce.org/ – Glycan

+0

本当にエラーを投稿する必要があります。 –

答えて

1

この迷路解決方法は、A *計画アルゴリズムの簡単なバージョンです。あなたは、この迷路解決方法を説明する私のページをチェックアウトすることができます:http://www.benaxelrod.com/robots/maze/index.html

あなたのコードを修正するのに役立つ疑似コードがあります。

関連する問題