# I need help to translate a simple VBScript to AutoIt :(

## Recommended Posts

If anyone can help me to translate this VBScript to AutoIt I will be very, very thankfull! I know this is very simple code, but I have difficulties translating it. Plz help!

Dim M(3,3)

L = 3
H = 3
Randomize

Sub g(byval x, byval y, d)
i = x-1
j = y-1
u = x+1
v = y+1
If d<4 Then M(x,y) = M(x,y) Or 2^d
Select Case d
Case 0
M(x,j) = M(x,j) Or 4
Case 1
M(u,y) = M(u,y) Or 8
Case 2
M(x,v) = M(x,v) Or 1
Case 3
M(i,y) = M(i,y) Or 2
End Select
If x<L And x*y>0 And y<H Then
While (M(x,y) And Not 2^d)=0
Select Case int(4*Rnd)
Case 0
If M(x,j) = 0 Then g x,j,2 Else If M(u,y)*M(x,v)*M(i,y)>0 Then M(x,y)=16
Case 1
If M(u,y) = 0 Then g u,y,3 Else If M(x,v)*M(i,y)*M(x,j)>0 Then M(x,y)=16
Case 2
If M(x,v) = 0 Then g x,v,0 Else If M(i,y)*M(x,j)*M(u,y)>0 Then M(x,y)=16
Case 3
If M(i,y) = 0 Then g i,y,1 Else If M(x,j)*M(u,y)*M(x,v)>0 Then M(x,y)=16
End Select
Wend
If M(x+1,y) = 0 Then g x+1,y,3
If M(x,y+1) = 0 Then g x,y+1,0
If M(x-1,y) = 0 Then g x-1,y,1
If M(x,y-1) = 0 Then g x,y-1,2
End If
End Sub

Anyone?

Edited by lokster

##### Share on other sites

I am try, but not sure if this wright...

Dim $M[3][3] = [[2, 3, 3], [2, 3, 3]]$L = 3
$H = 3 Dim$d

Func g(ByRef $x, ByRef$y, $d)$i = $x-1$j = $y-1$u = $x+1$v = $y+1 If$d < 4 Then $M[$x][$y] =$M[$x][$y]
Switch $d Case 0$M[$x][$j] = $M[$x][$j] Case 1$M[$u][$y] = $M[$u][$y] Case 2$M[$x][$v] = $M[$x][$v] Case 3$M[$i][$y] = $M[$i][$y] EndSwitch If$x<$L And$x*$y>0 And$y<$H Then While ($M[$x][$y] And Not 2^$d)=0 Switch Random(0, 4) Case 0 If$M[$x][$j] = 0 Then
$g =$x
$j = 2 ElseIf$M[$u][$y]*$M[$x][$v]*$M[$i][$y]>0 Then
$M[$x][$y]=16 EndIf Case 1 If$M[$u][$y] = 0 Then
$g =$u
$y = 3 ElseIf$M[$x][$v]*$M[$i][$y]*$M[$x][$j] > 0 Then
$M[$x][$y]=16 EndIf Case 2 If$M[$x][$v] = 0 Then
$g =$x
$v = 0 ElseIf$M[$i][$y]*$M[$x][$j]*$M[$u][$y]>0 Then
$M[$x][$y]=16 EndIf Case 3 If$M[$i][$y] = 0 Then
$g =$i
$y = 1 ElseIf$M[$x][$j]*$M[$u][$y]*$M[$x][$v]>0 Then
$M[$x][$y]=16 EndIf EndSwitch Wend If$M[$x+1][$y] = 0 Then
$g =$x+1
$y = 3 EndIf If$M[$x][$y+1] = 0 Then
$g =$x
$y = 1 EndIf If$M[$x-1][$y] = 0 Then
$g =$x-1
$y = 1 EndIf If$M[$x][$y-1] = 0 Then
$g =$x
$y = 2 EndIf EndIf EndFunc I think it would be easyer if you just tell what this script supose to do, and then build it by AutoIt functions will be more correct. Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 My Work... Spoiler Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team #### Share this post ##### Link to post ##### Share on other sites Here it is the whole script: Dim M(5,5) L = 5 H = 5 Randomize Sub g(byval x, byval y, d) i = x-1 j = y-1 u = x+1 v = y+1 If d<4 Then M(x,y) = M(x,y) Or 2^d Select Case d Case 0 M(x,j) = M(x,j) Or 4 Case 1 M(u,y) = M(u,y) Or 8 Case 2 M(x,v) = M(x,v) Or 1 Case 3 M(i,y) = M(i,y) Or 2 End Select If x<L And x*y>0 And y<H Then While (M(x,y) And Not 2^d)=0 Select Case int(4*Rnd) Case 0 If M(x,j) = 0 Then g x,j,2 Else If M(u,y)*M(x,v)*M(i,y)>0 Then M(x,y)=16 Case 1 If M(u,y) = 0 Then g u,y,3 Else If M(x,v)*M(i,y)*M(x,j)>0 Then M(x,y)=16 Case 2 If M(x,v) = 0 Then g x,v,0 Else If M(i,y)*M(x,j)*M(u,y)>0 Then M(x,y)=16 Case 3 If M(i,y) = 0 Then g i,y,1 Else If M(x,j)*M(u,y)*M(x,v)>0 Then M(x,y)=16 End Select Wend If M(x+1,y) = 0 Then g x+1,y,3 If M(x,y+1) = 0 Then g x,y+1,0 If M(x-1,y) = 0 Then g x-1,y,1 If M(x,y-1) = 0 Then g x,y-1,2 End If End Sub g 2,2,4 Set i = CreateObject("Scripting.FileSystemObject") j = "c:\$"
Set u = i.CreateTextFile(j)
For y = 0 To H
For x = 0 To L
u.Write("#" & Chr(61-(M(x,y) And 1)*29))
Next
u.Write("#" & vbCrLf & "|")
For x = 0 To L
u.Write(" " & Chr(124-(M(x,y) And 2)*46))
Next
u.Write(vbCrLf)
Next
u.Write(String(L*2+3,"#"))
u.Close

Set u = WScript.CreateObject("WScript.Shell")
u.Run "notepad " & j, 3, -1
i.DeleteFile j

This is a maze generator I found somewhere on the web, and I want to port it to autoit.

It produces something like:

#=#=#=#=#=#=#
| | | | | | |
#=# # # # #=#
|    |   |
#=# # #=# #=#
|   | |  |
#=# #=# # #=#
|   | | |   |
#=# #=# # #=#
|      |   |
#=# # # # #=#
| | | | | | |
#############
Edited by lokster

##### Share on other sites

Ok, i think i got it...

Global $M[4][4]$L = 3
$H = 3 Func g($x, $y,$d)
$i =$x-1
$j =$y-1
$u =$x+1
$v =$y+1
If $d < 4 Then$M[$x][$y] = $M[$x][$y] Switch$d
Case 0
$M[$x][$j] =$M[$x][$j]
Case 1
$M[$u][$y] =$M[$u][$y]
Case 2
$M[$x][$v] =$M[$x][$v]
Case 3
$M[$i][$y] =$M[$i][$y]
EndSwitch
If $x<$L And $x*$y>0 And $y<$H Then
While ($M[$x][$y] > 0 And 2^$d <> 0)
Switch Random(0, 4)
Case 0
If $M[$x][$j] = 0 Then g($x, $j, 2) ElseIf$M[$u][$y]*$M[$x][$v]*$M[$i][$y]>0 Then
$M[$x][$y]=16 EndIf Case 1 If$M[$u][$y] = 0 Then
g($u,$y, 3)
ElseIf $M[$x][$v]*$M[$i][$y]*$M[$x][$j] > 0 Then$M[$x][$y]=16
EndIf
Case 2
If $M[$x][$v] = 0 Then g($x, $v, 0) ElseIf$M[$i][$y]*$M[$x][$j]*$M[$u][$y]>0 Then
$M[$x][$y]=16 EndIf Case 3 If$M[$i][$y] = 0 Then
g($i,$y, 1)
ElseIf $M[$x][$j]*$M[$u][$y]*$M[$x][$v]>0 Then$M[$x][$y]=16
EndIf
EndSwitch
Wend
If $M[$x+1][$y] = 0 Then g($x+1,$y,3) If$M[$x][$y+1] = 0 Then g($x,$y+1,0)
If $M[$x-1][$y] = 0 Then g($x-1,$y,1) If$M[$x][$y-1] = 0 Then g($x,$y-1,2)
EndIf
EndFunc

g(2,2,4)

$j = "c:\$"

For $y = 0 To$H
For $x = 0 To$L
FileWrite($j, "#" & Chr(61-($M[$x][$y] And 1)*29))
Next
FileWrite($j, "#" & @CRLF & "|") For$x = 0 To $L FileWrite($j, " " & Chr(124-($M[$x][$y] And 2)*46)) Next FileWrite($j, @CRLF)
Next
FileWrite($j, String("#########")) RunWait("notepad " &$j)
FileDelete($j) But i get this error: Recursion level has been exceeded - AutoIt will quit to prevent stack overflow.: If$M[$x+1][$y] = 0 Then g($x+1,$y,3)

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

My Work...

Spoiler

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating )

* === My topics === *

==================================================

==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

##### Share on other sites

can you not just compile the VB script? if you convert to autoit it will still do the same thing.

##### Share on other sites

can you not just compile the VB script?

Then why not use direct VBS file, why he need the AutoIt?

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

My Work...

Spoiler

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating )

* === My topics === *

==================================================

==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

##### Share on other sites

Hi,

I can't do it by direct conversion * See next post, got it! [ ***** Still might be better to use This maze3vbs.au3 as higher dimensions give recursion error in AutoIt version ; - 50*50 vs 40*40 approx!]

; but this works in AutoIt and might help debug it.

I suspect the issue might be a different meaning of "or" and "and" in vbs; [bitAND works in the autoIt display file generation]

Best, randall

;maze3vbs.au3
Local $LL = 50,$HH = 50, $MM[$LL+1][$HH+1],$mazeautxt = @ScriptDir & "\mazeau.txt",$arRead[1][1]$ar_GridMakeArray = _GridMakeArray(2,2,4, $MM,$LL, $HH)$j = FileOpen($mazeautxt, 2) For$y = 0 To $HH For$x = 0 To $LL FileWrite($j, "#" & Chr(61 - BitAND($ar_GridMakeArray[$y] [$x], 1) * 29)) Next FileWrite($j, "#" & @CRLF & "|")
For $x = 0 To$LL
FileWrite($j, " " & Chr(124 - BitAND($ar_GridMakeArray[$y] [$x], 2) * 46))
Next
FileWrite($j, @CRLF) Next FileWrite($j, String("#############"))
FileClose($j) RunWait("notepad " &$mazeautxt)
FileDelete($j) Func _GridMakeArray($x, $y,$d,  $M2,$L2, $H2)$code = 'function G_MakeArray( xx,yy,dd, M2,  L2,  H2)'
$code &= @LF & ' Randomize'$code &= @LF & '    gg=g(xx,yy,dd,M2,L2,H2)'
$code &= @LF & ' G_MakeArray=M2'$code &= @LF & 'End function'
$code &= @LF & 'function g(byval x, byval y, d , byref M, byref L, byref H)'$code &= @LF & '    i = x-1'
$code &= @LF & ' j = y-1'$code &= @LF & '    u = x+1'
$code &= @LF & ' v = y+1'$code &= @LF & '    If d<4 Then M(x,y) = M(x,y) Or 2^d'
$code &= @LF & ' Select Case d'$code &= @LF & '        Case 0'
$code &= @LF & ' M(x,j) = M(x,j) Or 4'$code &= @LF & '        Case 1'
$code &= @LF & ' M(u,y) = M(u,y) Or 8'$code &= @LF & '        Case 2'
$code &= @LF & ' M(x,v) = M(x,v) Or 1'$code &= @LF & '        Case 3'
$code &= @LF & ' M(i,y) = M(i,y) Or 2'$code &= @LF & '    End Select'
$code &= @LF & ' If x<L And x*y>0 And y<H Then'$code &= @LF & '        While (M(x,y) And Not 2^d)=0'
$code &= @LF & ' Select Case int(4*Rnd)'$code &= @LF & '                Case 0'
$code &= @LF & ' If M(x,j) = 0 Then g x,j,2,M,L,H Else If M(u,y)*M(x,v)*M(i,y)>0 Then M(x,y)=16'$code &= @LF & '                Case 1'
$code &= @LF & ' If M(u,y) = 0 Then g u,y,3,M,L,H Else If M(x,v)*M(i,y)*M(x,j)>0 Then M(x,y)=16'$code &= @LF & '                Case 2'
$code &= @LF & ' If M(x,v) = 0 Then g x,v,0,M,L,H Else If M(i,y)*M(x,j)*M(u,y)>0 Then M(x,y)=16'$code &= @LF & '                Case 3'
$code &= @LF & ' If M(i,y) = 0 Then g i,y,1,M,L,H Else If M(x,j)*M(u,y)*M(x,v)>0 Then M(x,y)=16'$code &= @LF & '            End Select'
$code &= @LF & ' Wend'$code &= @LF & '        If M(x+1,y) = 0 Then g x+1,y,3,M,L,H '
$code &= @LF & ' If M(x,y+1) = 0 Then g x,y+1,0,M,L,H '$code &= @LF & '        If M(x-1,y) = 0 Then g x-1,y,1,M,L,H '
$code &= @LF & ' If M(x,y-1) = 0 Then g x,y-1,2,M,L,H '$code &= @LF & '    End If'
$code &= @LF & 'End function'$vbs = ObjCreate("ScriptControl")
$vbs.language = "vbscript"$vbs.addcode ($code)$returnArray = $vbs.Run("G_MakeArray",$x, $y,$d,  $M2,$L2, $H2)$vbs = ""
Return $returnArray EndFunc ;==>_GridMakeArray maze3.au3 is my attempt at conversion [modified MScreatoR] * See next post Edited by randallc #### Share this post ##### Link to post ##### Share on other sites OK, Now I think i have it; [ ***** Still might be better to use maze3vbs.au3 as higher dimensions give recursion error in AutoIt version ; - 50*50 vs 40*40 approx!] ; but this works in AutoIt and might help debug it. ;maze3.au3 Global$L = 5, $H = 5,$M[$L+1][$H+1],  $uuWrite,$mazeautxt = @ScriptDir & "\mazeau.txt"
Local $a = 2,$b = 2
g($a,$b, 4)
$ar_GridMakeArray =$M
$j = FileOpen($mazeautxt, 2)
For $y = 0 To$H
For $x = 0 To$L
FileWrite($j, "#" & Chr(61 - BitAND($ar_GridMakeArray[$x][$y], 1) * 29))
Next
FileWrite($j, "#" & @CRLF & "|") For$x = 0 To $L FileWrite($j, " " & Chr(124 - BitAND($ar_GridMakeArray[$x][$y], 2) * 46)) Next FileWrite($j, @CRLF)
Next
FileWrite($j, String("#############")) FileClose($j)
RunWait("notepad " & $mazeautxt) FileDelete($j)
Func g($x,$y, $d) Local$i = $x - 1,$j = $y - 1,$u = $x + 1,$v = $y + 1 If$d < 4 Then $M[$x][$y] = BitOR($M[$x][$y], (2 ^ $d)) Switch$d
Case 0
$M[$x][$j] = BitOR($M[$x][$j], 4)
Case 1
$M[$u][$y] = BitOR($M[$u][$y], 8)
Case 2
$M[$x][$v] = BitOR($M[$x][$v], 1)
Case 3
$M[$i][$y] = BitOR($M[$i][$y], 2)
EndSwitch
If ($x <$L) And ($x *$y) And ($y <$H) Then
While Not BitAND($M[$x][$y], BitNOT(2 ^$d))
Switch Int(Random(0, 4))  ;Select Case int(4*Rnd)
Case 0
If $M[$x][$j] = 0 Then g($x, $j, 2) ElseIf$M[$u][$y] * $M[$x][$v] *$M[$i][$y] > 0 Then
$M[$x][$y] = 16 EndIf Case 1 If$M[$u][$y] = 0 Then
g($u,$y, 3)
ElseIf $M[$x][$v] *$M[$i][$y] * $M[$x][$j] > 0 Then$M[$x][$y] = 16
EndIf
Case 2
If $M[$x][$v] = 0 Then g($x, $v, 0) ElseIf$M[$i][$y] * $M[$x][$j] *$M[$u][$y] > 0 Then
$M[$x][$y] = 16 EndIf Case 3 If$M[$i][$y] = 0 Then
g($i,$y, 1)
ElseIf $M[$x][$j] *$M[$u][$y] * $M[$x][$v] > 0 Then$M[$x][$y] = 16
EndIf
EndSwitch
WEnd
If $M[$x + 1][$y] = 0 Then g($x + 1, $y, 3) If$M[$x][$y + 1] = 0 Then g($x,$y + 1, 0)
If $M[$x - 1][$y] = 0 Then g($x - 1, $y, 1) If$M[$x][$y - 1] = 0 Then g($x,$y - 1, 2)
EndIf
EndFunc   ;==>g
Best, Randall Edited by randallc

##### Share on other sites

Or, To compact it! [ ***** Still might be better to use maze3vbs.au3 as higher dimensions give recursion error in AutoIt version ; - 50*50 vs 40*40 approx!]

#include<misc.au3>;maze6.au3
#include<string.au3>
Global $L = 50,$H = 20,$M[$L+1][$H+1],$uuWrite, $mazeautxt = @ScriptDir & "\mazeau.txt",$a = 2, $b = 2,$j = FileOpen($mazeautxt, 2) g(2, 2, 4) For$y = 0 To $H For$x = 0 To $L FileWrite($j, "#" & Chr(61 - BitAND($M[$x][$y], 1) * 29)&_iif($x=$L,"#" & @CRLF & "|","")) Next For$x = 0 To $L FileWrite($j, " " & Chr(124 - BitAND($M[$x][$y], 2) * 46)&_iif($x=$L,_iif($y=$H,@CRLF&_StringRepeat("#",$L*2+3) ,@CRLF),""))
Next
Next
FileClose($j) RunWait("notepad " &$mazeautxt)
Func g($x,$y, $d) Local$i = $x - 1,$j = $y - 1,$u = $x + 1,$v = $y + 1 If$d < 4 Then $M[$x][$y] = BitOR($M[$x][$y], (2 ^ $d)) If$d = 0 Then $M[$x][$j] = _Iif($d = 0, BitOR($M[$x][$j], 4),$M[$x][$j])
If $d = 1 Then$M[$u][$y] = _Iif($d = 1, BitOR($M[$u][$y], 8), $M[$u][$y]) If$d = 2 Then $M[$x][$v] = _Iif($d = 2, BitOR($M[$x][$v], 1),$M[$x][$v])
If $d = 3 Then$M[$i][$y] = _Iif($d = 3, BitOR($M[$i][$y], 2), $M[$i][$y]) If ($x < $L) And ($x * $y) And ($y < $H) Then While Not BitAND($M[$x][$y], BitNOT(2 ^ $d)) Local$z = Int(Random(0, 4)), $w0 =$M[$x][$j], $w1 =$M[$u][$y], $w2 =$M[$x][$v], $w3 =$M[$i][$y]
If $z = 0 And Not$w0 Then g($x,$j, 2)
If $z = 0 And$w0 Then $M[$x][$y] = _Iif($M[$u][$y] * $M[$x][$v] *$M[$i][$y], 16, $M[$x][$y]) If$z = 1 And Not $w1 Then g($u, $y, 3) If$z = 1 And $w1 Then$M[$x][$y] = _Iif($M[$x][$v] *$M[$i][$y] * $M[$x][$j], 16,$M[$x][$y])
If $z = 2 And Not$w2 Then g($x,$v, 0)
If $z = 2 And$w2 Then $M[$x][$y] = _Iif($M[$i][$y] * $M[$x][$j] *$M[$u][$y], 16, $M[$x][$y]) If$z = 3 And Not $w3 Then g($i, $y, 1) If$z = 3 And $w3 Then$M[$x][$y] = _Iif($M[$x][$j] *$M[$u][$y] * $M[$x][$v], 16,$M[$x][$y])
WEnd
If $M[$x + 1][$y] = 0 Then g($x + 1, $y, 3) If$M[$x][$y + 1] = 0 Then g($x,$y + 1, 0)
If $M[$x - 1][$y] = 0 Then g($x - 1, $y, 1) If$M[$x][$y - 1] = 0 Then g($x,$y - 1, 2)
EndIf
EndFunc   ;==>g
Edited by randallc

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account