Calculator Community > Axe
Looping Through an Array in Axe
E37:
Alright, here are a couple guesses.
For(K,1,L) is starting at 1 and not 0. Is that what you want?
Z+1≠N doesn't change any value and so the loop in CheckRight is pointless unless you use the value of N at some point outside the function. Did you mean Return!If Z+1≠N ? If I remember correctly, the For loop will increment at the end and return that value. That means that CheckRight should always return L no matter what data it is given. (I could be wrong but either way there is no way your code is doing what you want it to)
You are also spilling into CheckRight like you did at the end of your main loop. It shouldn't be a problem but does waste time.
Ki1o:
@E37 Thank you! Your advice was very helpful in better understanding how Axe works and I was able to get it to work perfectly! I think you were correct in saying that CheckRight was returning L and not the comparison like I assumed it would. Using Return!If inside the for loop is what allowed it to work. This is the result:
--- Code: ---If getKey(3) and (I=0) and (J=0) and ({Z+1+GDB0}=1)
If CheckRight()
1→I
32→S
End
End
If getKey(2) and (I=0) and (J=0) and ({Z-1+GDB0}=1)
If CheckLeft()
0-1→I
64→S
End
End
If getKey(1) and (I=0) and (J=0) and ({Z+37+GDB0}=1)
If CheckDown()
1→J
0→S
End
End
If getKey(4) and (I=0) and (J=0) and ({Z-37+GDB0}=1)
If CheckUp()
0-1→J
96→S
End
End
Lbl CheckRight
For(K,1,L)
{K*4+L₁-2}ʳ→N
Return!If Z+1≠N
End
Return
Lbl CheckLeft
For(K,1,L)
{K*4+L₁-2}ʳ→N
Return!If Z-1≠N
End
Return
Lbl CheckDown
For(K,1,L)
{K*4+L₁-2}ʳ→N
Return!If Z+37≠N
End
Return
Lbl CheckUp
For(K,1,L)
{K*4+L₁-2}ʳ→N
Return!If Z-37≠N
End
Return
--- End code ---
I appreciate the help.
E37:
Nice! I can't believe I guessed that right. That Return!If was a total shot in the dark.
Ki1o:
Its been a few months. I've been working on this project on and off and I have come across another issue that I need help with. I'm currently working on the procedural generation and I'm stuck. Currently I'm simply trying to select a starting locations for a set of rooms. The desired behavior I want is this: the first room generated is placed within the bounds of the map and the height and width are chosen so as not to go out of the map bounds. Then the X and Y of the starting location as well as the Xmax and the Ymax of that room is added to an array. Every subsequent starting room location that is generated must be compared to every other room in the array to ensure that the starting location is not going to be inside an already existing room. I've been trying different ways to tackle this problem and I thought I came up with a solution, but after running test after test after, I keep coming across starting locations that fail the check but are still being added to the array as valid rooms.
So, once again, I need some help. I wrote a test program that shows the X, Y, Xmax and Ymax of 9 total rooms. Here is the code:
--- Code: ---.G
L₄+12→→°XMin
L₄+14→→°XMax
L₄+16→→°YMin
L₄+18→→°YMax
L₄+20→→°Rooms
L₄+22→→°RoomStart
L₄+24→→°RoomWidth
L₄+26→→°RoomHeight
L₄+28→→°RoomX
L₄+30→→°RoomY
L₄+32→→°NewRoomX
L₄+34→→°NewRoomY
Fill(L₁,36,0)
0→Rooms→RoomX→RoomY→RoomHeight→RoomWidth→R
ClrDraw
GenFloor()
CheckFloor()
Main()
Lbl Main
While 1
DispGraph
Input()
EndIf getKey(15)
Return
Lbl CheckFloor
For(R,0,Rooms-1)
R+1*4+L₁-4→Z
R+1*5-4→Y
R+1*4+L₁-4→Z
{Z}→XMin
{Z+2}→XMax
{Z+1}→YMin
{Z+3}→YMax
DrawText(1,Y,"(")
DrawInt(4,Y,{Z})
DrawText(13,Y,",")
DrawInt(16,Y,{Z+1})
DrawText(24,Y,")")
DrawInt(29,Y,XMin)
DrawInt(38,Y,XMax)
DrawInt(47,Y,YMin)
DrawInt(56,Y,YMax)
End
Return
Lbl GenFloor
While Rooms<9
GenRoom()
PlaceRoom()
0→R
End
Return
Lbl GenRoom
!If Rooms
GetRoomStart()
While 1
GetRoomSide()→RoomWidth
End!If ChkRoomWidth()
While 1
GetRoomSide()→RoomHeight
End!If ChkRoomHeight()
Else
GetRoomStart()
While R<Rooms-1
R+1*4+L₁-4→Z
{Z}→XMin
{Z+2}→XMax
{Z+1}→YMin
{Z+3}→YMax
If (RoomX≥XMin and RoomX≤XMax) and (RoomY≥YMin and RoomY≤YMax)
0→R
GetRoomStart()
End
!If (RoomX≥XMin and RoomX≤XMax) and (RoomY≥YMin and RoomY≤YMax)
R++
End
End
While 1
GetRoomSide()→RoomWidth
End!If ChkRoomWidth()
While 1
GetRoomSide()→RoomHeight
End!If ChkRoomHeight()
End
Return
Lbl PlaceRoom
If Rooms<9
RoomY+RoomHeight-1→{RoomX+RoomWidth-1→{RoomY→{RoomX→{Rooms+1→Rooms*4+L₁-4}+1}+1}+1}
End
Return
Lbl GetRoomStart
6+(rand^33)→RoomX
4+(rand^37)→RoomY
Return
Lbl GetRoomSide
4+(rand^9)
Return
Lbl ChkRoomWidth
(RoomX+RoomWidth-1>41)
Return
Lbl ChkRoomHeight
(RoomY+RoomHeight-1>43)
Return
Lbl DrawText
For(I,0,length(r₃)-1)
Copy({r₃+I}-32*4+°CharSprites,L₄+50,4)
Fill(L₄+54,4,0)
DrawChar()
End
Return
Lbl DrawInt
r₃/10+16→{L₅}
r₃^10+16→{L₅+1}
For(I,0,1)
Copy({L₅+I}*4+°CharSprites,L₄+50,4)
Fill(L₄+54,4,0)
DrawChar()
End
Return
Lbl DrawChar
Pt-On(r₁+(I*((I>0)*4)),r₂,L₄+50)
Return
Lbl Input
If getKey(9)
ClrDraw
0→Rooms
GenFloor()
CheckFloor()
End
Return
[]→°CharSprites
[0000000080800080]
[A0A00000A0E0E0A0]
[60C060C0A060C0A0]
[40A060E040400000]
[4080804040202040]
[A040A0000040E040]
[000040800000E000]
[0000008000204080]
[E0A0A0E0C04040E0]
[E020C0E0E06020E0]
[A0A0E020E08060E0]
[E080E0E0E0204040]
[E0A0E0E0E0A0E020]
[4000400040004080]
[0020402000E000E0]
[00804080E0200040]
[40A0E04060A0E0A0]
[C0E0A0E0E08080E0]
[C0A0A0E0E0C080E0]
[E0C08080E080A0C0]
[A0E0A0A0E04040E0]
[6020A040A0C0A0A0]
[808080E0E0E0A0A0]
[C0A0A0A0C0A0A060]
[C0A0E080E0A0E040]
[C0A0C0A0E08020E0]
[E0404040A0A0A060]
[A0A0A040A0A0E0E0]
[A040A0A0A0A04040]
[E02080E000000000]
--- End code ---
I also attached a screenshot. In the screenshot, room (33,19) should not be a valid starting location for a room because of room (28,17). 33 is clearly between 28 and 35, and 19 is also between 17 and 26. Therefore, what should have happened is that GetRoomStart() should have been called and it should have been compared to the other rooms. Instead it was treated like a valid room and added to the array. I've been having weird issues with while loops all week so far as well. Any and all help would be greatly appreciated.
Xeda112358:
I'm thinking it might be an order-of-operations issue since Axe obeys parentheses, but is otherwise left-to-right instead of PEMDAS.
Instead of this:
--- Code: --- If (RoomX≥XMin and RoomX≤XMax) and (RoomY≥YMin and RoomY≤YMax)
0→R
GetRoomStart()
End
!If (RoomX≥XMin and RoomX≤XMax) and (RoomY≥YMin and RoomY≤YMax)
R++
End
--- End code ---
Try:
--- Code: --- If (RoomX≥XMin) and (RoomX≤XMax) and (RoomY≥YMin) and (RoomY≤YMax)
0→R
GetRoomStart()
Else
R++
End
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version