多行批处理文件打印中的奇怪错误

时间:2017-07-05 19:52:42

标签: batch-file

我一直在为自己制作一个项目,它可以批量生成一个网格,可以通过批量游戏等方式访问和修改特定点以获得更好的图形。

@echo off
setlocal ENABLEDELAYEDEXPANSION

!x!
:::::::::::
:: ALIAS ::
:::::::::::
:@alias
    set x=exit/b
    goto:@main
    !x!
:::::::::::
:: MAIN  ::
:::::::::::
:@main
call:$Canvas _canvas 16 16 .
set px=%~1
set py=%~2
set _canvas_point[!px!][!py!]=#
!_canvas#draw!

!x!
:::::::::::
::OBJECTS::
:::::::::::
:@objects
!x!
:$Canvas
    set this=%~1
    set /a !this!_width=%~2
    set /a !this!_height=%~3
    set !this!_default_char=%~4
    set !this!#draw=call:#$canvas_proto_draw !this!
    for /L %%i in (1, 1, !%this%_height!) do (
        for /L %%j in (1, 1, !%this%_width!) do (
            set !this!_point[%%j][%%i]=!%this%_default_char!
            set !this!_row[%%i]=!%this%_row[%%i]!!%this%_point[%%j][%%i]!
            )
        )
    !x!
    :#$Canvas_proto_draw
        set this=%~1
        for /L %%i in (1, 1, !%this%_height!) do (
        for /L %%j in (1, 1, !%this%_width!) do (
            set !this!_row[%%i]=!%this%_row[%%i]!!%this%_point[%%j][%%i]!
            )
        )
        for /L %%i in (1, 1, !%this%_height!) do (
            echo !%this%_row[%%i]!
            )
    !x!

我的问题出现在最终输出中:给它1, 1将导致一个点出现在大约一半的顶行。我不知道为什么会这样,需要帮助。我已经在启用了echo的情况下梳理了所有输出,并且值_canvas_point[1][1]确实在更新时获得了#的值。

这是输出:

C:\Home>canvas 1 1
................#...............
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................

C:\Home>

Here是一个包含@echo on

输出的pastebin链接

快速的事情: 是的,这是对批处理文件中面向对象编程的微弱尝试 是的,我知道其他语言对于这种事情更好,我只想用批处理文件来做 是的,我有奇怪的风格选择,包括名称和标签等。这样我就可以更好地阅读它,因为标签用于转到位置和功能标签。

2 个答案:

答案 0 :(得分:0)

以下示例可能接近您尝试完成的内容(面向对象的.BAT文件方法):

@echo off
setlocal ENABLEDELAYEDEXPANSION

rem  Initialize object "_canvas" (index numbers are row then column)
call :#$proto_init _canvas 16 16 .

rem  Set a few "_canvas" array characters to non-default values
call :#$proto_set  _canvas  1  1 ?
call :#$proto_set  _canvas  8  8 #
call :#$proto_set  _canvas 16 16 $

rem  Display "_canvas"
call :#$proto_draw _canvas
endlocal
exit/b


:#$proto_init
set /a %1_height=%2, %1_width=%3, %1_len=%2 * %3
set %1_default_char=%4
set %1_data=X
for /l %%i in (1, 1, !%1_len!) do set %1_data=!%1_data!!%1_default_char!
exit /b


:#$proto_set
for /f %%i in ('set /a ^(%2 - 1^) * !%1_height! + %3') do (
  for /f %%j in ('set /a %%i + 1') do (
     set %1_data=!%1_data:~0,%%i!%4!%1_data:~%%j!
  )
)
exit/b


:#$proto_draw
for /l %%w in (!%1_width!, 1, !%1_width!) do (
  for /l %%h in (1, %%w, !%1_len!) do echo !%1_data:~%%h,%%w!
)
exit /b

答案 1 :(得分:0)

在:#$ Canvas_proto_draw

:#$Canvas_proto_draw
set this=%~1
for /L %%i in (1, 1, !%this%_height!) do (
 SET "!this!_row[%%i]="
 for /L %%j in (1, 1, !%this%_width!) do (
...

您可能会发现显示的数据是32列x16行。这是因为在附加值之前不清除每一行。