Solved how to make an x y coordinate plane in batch

November 22, 2013 at 14:11:41
Specs: Windows 7
i am trying to make a riugelike game in batch...could any one post a simple x y coordinate plane map...just a simple one that allows you to walk through the map

See More: how to make an x y coordinate plane in batch

Report •

✔ Best Answer
December 2, 2013 at 21:07:44
@mike: thanks for helping to explain, especially emphasizing base zero, which can be confusing, but always critical.

@OP firehawk
"how is it that you can manipulate the players symbol "@" when the map isn't made of variables?" I don't! The entire map is made of variables, including the player-symbol, barriers, treasure, and any other active elements (other players or enemies). Do you understand arrays? Arrays, and looping, are like blood-brothers: one is never employed without the other, and the result is massivly more than the sum of these elements. I think that batch syntax is what is giving you fits, because it is NOT a user-friendly language (contrary to most initial opinions). For "basic" concept, I still favor BASIC (Qbasic or QuickBasic) because they're easy to aquire, and don't have much overhead or learning curve. But this is only as a learning platform. Win-7, and all 64 bit platforms, will not be compatible.
If you wish to pursue batch, you only need to understand simple concepts such as:
SET g%row%_%col%=@
IF "!g%row%_%col%!" equ "#" goto :WALL_NOMOVE
These are valid variables - nothing mysterious. g%row%_%col% is functionallty equivalent to g$(row,col) in BASIC. If row value is 3, and col is 5, then a variable named g3_5 is equiv. to a var g%row%_%col% (since row and column are replaced by %row% and %col% in batch) is equiv to g$(3,5) or g$(row,col) in BASIC. Inside a loop or code-block, you need to add the exclams to access elements, f/e:
@echo off & setlocal
set g0_0=A0
set g0_1=B0
set g0_2=C0
set g0_3=D0
SET g0_4=+++
set g1_0=A1
set g1_1=B1
set g1_2=C1
set g1_3=D1
SET g1_4=---
set g2_0=A2
set g2_1=B2
set g2_2=C2
set g2_3=D2
SET g2_4=###
set g3_0=A3
set g3_1=B3
set g3_2=C3
set g3_3=D3
SET g3_4=$$$
rem etc.

for /L %%a in (0,1,3) do (
set line%%a=
for /L %%b in (0,1,4) do (
:: this next line appends elements to an output ("raster") line:
rem echo g%%a_%%b
set line%%a=!line%%a!___!g%%a_%%b!
)
:: inside the loop, the raster-line is complete
echo line%%a:== !line%%a!
)

this builds a simple set of 4 lines (vars. line1, line2, line3, line4) out of 4 groups having five elements per group.
A loop will operate on subsets within the array for any "dimension" (row, column) and any subset within that dimension. As previously pointed out, a 2-dimension "plane" can be reduced to one dimension (using math) and vice versa because it's all regular sets (height, width) of data.

message edited by nbrane



#1
November 22, 2013 at 21:12:51
This also had a random-walk zombie feature that you may not want...
Ok, here's the last one I made, hope it helps:
@echo off & setlocal enabledelayedexpansion
:: allowable spaces for moves (walls vs halls/space) these are the allowed moves
set mov=@[]._V+
set @h=.
set zi=0
::speed, or frequency of random-zombie moves, in seconds
set spd=1
cls
:: load ascii "painting" or map of a room stored as text filename "room2"
set room=room2
call :loadroom
goto :input

:loadroom
set I=-1
for /f "tokens=*" %%a in (%room%) do (
set /a I+=1
set line!I!=%%a
)
:: setting blox to 1 disables random blockades, higher values will enable increasing
set /a blox=I*3
set blox=1
set r=
:: allow for variable widths of rooms
for /L %%a in (0 1 %I%) do (
for /L %%b in (0 1 80) do (
set a%%a_%%b=!line%%a:~%%b,1!
:: determine horiz. max since it's variable
if %%a equ 0 (
if defined a0_%%b set J=%%b
) else (
if !a%%a_%%b! equ . (
set /a x=!random!"%%"blox
if %%b equ !x! set a%%a_%%b=#& set r=%%a
)
)
if /i !a%%a_%%b!==Z (
set /a zi+=1
set z!zi!r=%%a
set z!zi!c=%%b
set z!zi!h=.
)
if !a%%a_%%b!==@ (
set @r=%%a
set @c=%%b
)
)
if defined r call :update
echo !line%%a!
)
set zs=%zi%
echo use numeric keypad for moves
set to=/t:5,%spd%
goto :eof

:input
set ix=@
set id=@
set r=%@r%
set c=%@c%
set test=
choice /c:123456789qe /n %to%
set test=%errorlevel%
if %test% gtr 9 goto :goodbye
call :move
rem if %test% equ 5 (
rem goto :zpass
rem ) else (
rem call :move
rem )

:: zombie match player, end game
if defined f goto :goodbye
rem call :%x% 2>nul
set @r=%r%
set @c=%c%

:zpass
:: lazy player, pass move to zombie random
set ix=Z
for /L %%a in (1 1 %zs%) do (
set id=z%%a
set f=
call :zombie z%%a
if defined f goto :goodbye
)
call :refresh
goto :input

:move
set f=
if %test% equ 5 goto :eof
set /a fwd=test"%%"3
set /a bck=(test-1)"%%"3
set tr=%r%
set tc=%c%
if %test% lss 4 set /a tr+=1
if %test% gtr 6 set /a tr-=1
if %fwd% equ 0 set /a tc+=1
if %bck% equ 0 set /a tc-=1
rem echo one: %tr% %tc% !a%tr%_%tc%! r:%r% c:%c%

set x=!a%tr%_%tc%!

set movtest=!mov:%x%=!
rem echo two: x[%x%][!x!] %mov% %movtest% id:%id%
:: enable the following to allow zombie wall penetration
rem if %id% equ @ if %movtest% equ %mov% goto :eof
if %movtest% equ %mov% goto :eof
if /i %x% equ @ set f=1
rem if /i %x% equ Z set f=1
set a%tr%_%tc%=%ix%
set a%r%_%c%=!%id%h!
set %id%h=%x%

call :update

set c=%tc%
if %tr% neq %r% (
set r=!tr!
call :update)
set r=%tr%
rem if defined f goto :eof
goto :eof

:refresh
cls
for /L %%a in (0 1 %I%) do echo !line%%a!
goto :eof

:update
set line%r%=
for /L %%a in (0 1 %J%) do (
set line%r%=!line%r%!!a%r%_%%a!
)
goto :eof

:zombie
set id=%1
rem echo zombie===========%id% !%id%r! !%id%c!
rem pause
rem set to=/t:5,%spd%
set /a test=%random%"%%"10
rem goto :z
set /a rr=%id%r-@r
set /a cc=%id%c-@c
set dx=%rr:-=%
set dy=%cc:-=%
if %dx% gtr 4 goto :z
if %dy% gtr 4 goto :z
set to=
set zlr=2
set zud=3
set test=5
if %rr% gtr 0 set test=8
if %rr% lss 0 set test=2
if %cc% lss 0 set /a test+=1
if %cc% gtr 0 set /a test-=1
:z
set r=!%id%r!
set c=!%id%c!
call :move
if /i %x% equ @ goto :eof
echo [%@r% %@c%][!%id%r! !%id%c!]
rem pause
rem if "%@r% %@c%" equ "!%id%r! !%id%c!" goto :goodbye
:: zombie intelligence very tedious. Try certain moves if no movement
rem if !zr%zi%!!zc%zi%! equ %r%%c% (
rem call :brains
rem goto :z
rem )
set %id%r=%r%
set %id%c=%c%
goto :eof

:brains
:: all zombie intelligence stored here.
goto :eof
if %test% gtr 6 (
set /a test-=3
if %test% equ 5 if %zlr% neq 6 (
set /a zlr+=2
set test=%zlr%
goto :z
)
)
if %test% lss 4 (
set /a test+=3
if %test% equ 5 if %zlr% neq 6 (
set /a zlr+=2
set test=%zlr%
goto :z
)
)
if %zud% neq -1 (
set /a test+=zud
set /a zud=-zud
if !zud! equ -3 set zud=1
goto :z
)
goto :eof

:chk
if %x% equ @ echo x: %x% & goto :goodbye
if %x% equ Z echo x: %x% & goto :goodbye
goto :eof

:goodbye
call :refresh
echo goodbye!
pause>nul
exit /b
::======== end batch
This code may seem tedious, but it tries for both maximum efficiency (subroutines)and maximum flexibility (room design, multiple rooms, multiple zombies, diagonal moves).


Report •

#2
November 23, 2013 at 13:41:54
I mean one without zombies on just a 10x10 grid just so I can dissect the code and learn how it works...and please include a map...so I know how to construct it...so instead of making the map "call" have it in the file...thankyou

Report •

#3
November 23, 2013 at 23:32:35
Ok. Here's a stripped-down version of my way of doing this. No two people think exactlly alike (thank God). I put in lots of comments to explain my actions, but again, as stated prior, delayed expansion (and some understanding of it) is necessary for this code to work. PS: I understand if you want new approaches, so I won't put any further responses unless specifically asked. So if you make a new post, it will remain "open" for others to contribute. Good luck/have fun!
It's a 10x10 room as per req. I did throw in one "obstacle", just as a demo since the rest of the room is built as a loop (because I'm lazy), and you will probably not want such a boring room in your future developments. You can define each line (or "row") like:
set line1=#....#v#....#
set line2=#....#_#....#

etc.

::====== begin batchscript
@echo off & setlocal enabledelayedexpansion
cls
::======== all initializations: Width and Height of room
set W=9
set H=9
:: --- begin build room..top, bottom rows
set line0=##########
:: all middle rows, looped due to laziness
set line1=#........#
for /L %%a in (2 1 %H%) do set line%%a=%line1%
set line%H%=%line0%
:: make one exception at row #4
set line4=#...#....#
:: so you can see how to build a room row-by-row.

:: build 2-dimension grid from the collection of rows
for /L %%a in (0 1 %H%) do (
for /L %%b in (0 1 %W%) do set g%%a_%%b=!line%%a:~%%b,1!
)

set id=@
:: set %id%h=.
set @h=.
:: initial position of player: row, colm
set r=3
set c=2
set g%r%_%c%=@
:: end of room-build
:: this wasn't necessary. It just reiterates building of rows from grid elems
for /L %%a in (0 1 9) do (
set xx=
for /L %%b in (0 1 9) do (
set xx=!xx!!g%%a_%%b!
)
echo !xx!
)
pause
::========== MAIN GAME LOOP BEGINS WITH USER INPUT
:input
choice /c:123456789qe /n
::-----------
set test=%errorlevel%
if %test% gtr 9 goto :goodbye
:: 2 steps: update altered grid rows and rebuild only affected lines,
call :move
:: then redisplay entire grid
call :refresh
goto :input

:move
:: test for movement and adjust accordingly based on 9 poss. moves: compass -
:: NSEW, diags, and "stay" (skip-move/"pass", or do action like FIRE)
:: mine uses numeric kpd for these moves, but not necc. for code to work.
if %test% equ 5 goto :eof
:: foll is zero if 9, 6 or 3 which are all "rightward" or East
set /a fwd=test"%%"3
:: foll is zero if 7, 4 or 1 which are all "leftward" or West
set /a bck=(test-1)"%%"3
set tr=%r%
set tc=%c%
:: foll is South, or down. Increment Row#
if %test% lss 4 set /a tr+=1
:: foll is North, or up. Decrement Row#
if %test% gtr 6 set /a tr-=1
if %fwd% equ 0 set /a tc+=1
if %bck% equ 0 set /a tc-=1
rem echo one: %tr% %tc% !g%tr%_%tc%! r:%r% c:%c%
:: hits a wall?
if !g%tr%_%tc%! equ # goto :eof

:: begin actual update:
:: FROM-grid token restored to previously saved token.
set g%r%_%c%=%@h%
:: current TO-grid-token saved in x, {f/e usually a dot .}
set x=!g%tr%_%tc%!
::TO-grid updated to "@",
set g%tr%_%tc%=@
set @h=%x%

:: now update only the affected "raster/display" lines (two maximum)
:: current line ALWAYS needs updated.
call :update

set c=%tc%
:: adjacent line only needs updated if any vertical displacement detected
if %tr% neq %r% (
set r=!tr!
call :update
)
set r=%tr%
goto :eof

:: now just display the raster-lines
:refresh
cls
rem pause
for /L %%a in (0 1 %H%) do echo !line%%a!
goto :eof

:update
:: rebuild a line to reflect current token-values after move
set line%r%=
for /L %%a in (0 1 %W%) do (
set line%r%=!line%r%!!g%r%_%%a!
)
goto :eof
:goodbye
echo goodbye

message edited by nbrane


Report •

Related Solutions

#4
November 25, 2013 at 09:48:58
ok so I somewhat know whats happening here but at the same time not really... the loops are what confuse me...would it be possible to write the code without loops?
...I haven't learned how to use loops yet and with limited internet access it's very difficult to learn more ill work on learning how to use loops but for now could you write it without loops?

Report •

#5
November 25, 2013 at 20:33:33
Well, I must say, programming without looping is like flying without wings: it won't get you far, and you'll eventually feel the pain (never mind rockets and parachutes, for you literal minded interpreters!) Loops, and subroutines, are the essence of base-level (as opposed to object-oritented) programming. Probably more than loop concept (which is simple enough), with batch, the key is the concept of delayed expansion and enabling it. Meanwhile, I'll see what I can do, and maybe some other masochists on the forum will pitch in (just joking!) Turkey-day coming up etc...

Report •

#6
November 26, 2013 at 08:23:29
more customizable is my goal.. I would eventually like it so I could have a menu pop up to the side of the map when the player presses a button...so far ive figured out how to make the map bigger and how to put obstacles in there...but I don't even know how the map is displayed at this point because im use to displaying things using echo...but I do like this method much more it takes much less "computing power" to go... it goes faster than my original method...again I still don't quite get what all of the symbols in the loops do but I figured out how to have the map larger and display right off the bat... but how would I make it display a little menu to the right of the map...and if you could please explain the loops you've made... or how they work..i hate to ask too much but it would really be helpful!

Report •

#7
November 26, 2013 at 20:14:57
Before I do, can you take a look at:
for /? (page 1 primarily)
set /? (pages 2,3)
Because these help explain the symbols, and the concept of variable expansion, which is mostly unique to batch. My code uses "echo", same as yours. Only difference is that mine composes the lines from the elements (as explained earlier-on) so that only one or two lines need to be re-configured for each move and the rest do not. The larger the map, the more expedient this becomes. As for menu display, all you need do is append each menu-line to the display of each raster-line (map row). It would be somewhat simpler to put the menu above or below the map, but thats a minor thing. I suspect that delayed expansion, and use of !, is what is holding you up in my code. delayedexpansion allows two different abilities, a) access to var values inside a code-block, and b) allowing variables to be expressed inside of other variables before evaluation. item "b" F/e:
set row=3
set col=4
set line%row%_%col% = @
echo !line%row%_%col%!
If you try to "echo line%row%_%col%" you will get: "line3_4"
if you echo !line%row%_col%! you will get your desired target: "@"
because the evaluation of line%row%_%col% is subsequent to the evaluations of row and column variables. There's another way to do this, but this is the easiest to deal with.
Of course, delayedexpansion must be in effect to take advantage of this feature.

Report •

#8
December 2, 2013 at 09:38:31
Hey sorry for taking so long to reply ive been on vacation for the week and there was't any wifi there so I couldn't keep up with this... anyway my main problem is I really don't know how the x y coordinate map works...how is it that you can manipulate the players symbol "@" when the map isn't made of variables? I know I probably just disregarded most of what you just said if not all of it but I think I just need to figure out how it works instead of expecting you to make it more..."dumbed down" for me. so my question is first how do you use loops? for this example. meaning, like a simple 10 by 10 map with nothing but the map making skills...no movement needed yet..id like to take this in steps. 1st map making, 2nd movement, 3rd "If I move here take me to a different map or :EXAMPLE(bookmark)", 4th how to add a menu with options that take you places. I really appreciate your patience with me, and not that you'd want it but if you can figure out a way ill send you a final copy of my game when it's done! :) thanks!

Report •

#9
December 2, 2013 at 11:38:45
From nbranes posts above
:: its important to note the grid top to bottom is 0-9 not 1-10


:: --- begin build room..top, bottom rows
set line0=##########
:: all middle rows, looped due to laziness
set line1=#........#
::this is for lines starting at 2 in steps of 1 to the variable H, which is set to 9
::this all of the other rows are set to #........#
for /L %%a in (2 1 %H%) do set line%%a=%line1%
set line%H%=%line0%
::this sets the last line H to the same as the first line
:: make one exception at row #4
set line4=#...#....#
::this edits line four to the above.
:: so you can see how to build a room row-by-row.
edit: note that I ment no offense nbrane :)
::mike

message edited by mikelinus


Report •

#10
December 2, 2013 at 21:07:44
✔ Best Answer
@mike: thanks for helping to explain, especially emphasizing base zero, which can be confusing, but always critical.

@OP firehawk
"how is it that you can manipulate the players symbol "@" when the map isn't made of variables?" I don't! The entire map is made of variables, including the player-symbol, barriers, treasure, and any other active elements (other players or enemies). Do you understand arrays? Arrays, and looping, are like blood-brothers: one is never employed without the other, and the result is massivly more than the sum of these elements. I think that batch syntax is what is giving you fits, because it is NOT a user-friendly language (contrary to most initial opinions). For "basic" concept, I still favor BASIC (Qbasic or QuickBasic) because they're easy to aquire, and don't have much overhead or learning curve. But this is only as a learning platform. Win-7, and all 64 bit platforms, will not be compatible.
If you wish to pursue batch, you only need to understand simple concepts such as:
SET g%row%_%col%=@
IF "!g%row%_%col%!" equ "#" goto :WALL_NOMOVE
These are valid variables - nothing mysterious. g%row%_%col% is functionallty equivalent to g$(row,col) in BASIC. If row value is 3, and col is 5, then a variable named g3_5 is equiv. to a var g%row%_%col% (since row and column are replaced by %row% and %col% in batch) is equiv to g$(3,5) or g$(row,col) in BASIC. Inside a loop or code-block, you need to add the exclams to access elements, f/e:
@echo off & setlocal
set g0_0=A0
set g0_1=B0
set g0_2=C0
set g0_3=D0
SET g0_4=+++
set g1_0=A1
set g1_1=B1
set g1_2=C1
set g1_3=D1
SET g1_4=---
set g2_0=A2
set g2_1=B2
set g2_2=C2
set g2_3=D2
SET g2_4=###
set g3_0=A3
set g3_1=B3
set g3_2=C3
set g3_3=D3
SET g3_4=$$$
rem etc.

for /L %%a in (0,1,3) do (
set line%%a=
for /L %%b in (0,1,4) do (
:: this next line appends elements to an output ("raster") line:
rem echo g%%a_%%b
set line%%a=!line%%a!___!g%%a_%%b!
)
:: inside the loop, the raster-line is complete
echo line%%a:== !line%%a!
)

this builds a simple set of 4 lines (vars. line1, line2, line3, line4) out of 4 groups having five elements per group.
A loop will operate on subsets within the array for any "dimension" (row, column) and any subset within that dimension. As previously pointed out, a 2-dimension "plane" can be reduced to one dimension (using math) and vice versa because it's all regular sets (height, width) of data.

message edited by nbrane


Report •

Ask Question