SNAKE.BAT 4.1 - An arcade style game using pure batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT - An arcade style game using pure batch

#16 Post by dbenham » 13 Jul 2013 17:33

Thanks miskox and aGerman :D

I've updated the code in the initial post.


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: SNAKE.BAT - An arcade style game using pure batch

#17 Post by foxidrive » 13 Jul 2013 18:20

That's neat Dave. I jumped in and run it - picked crawl speed and started playing.
Snake is a favourite game of mine...

I struck something odd though. when I reached this score no more food appeared. I moved round for 30 seconds or so and I couldn't see a + anywhere on the screen.

Speed=Crawl
Score=6

Edit: I played some more and that didn't reoccur.

This speed is a good one on my I7 3.2 GHz CPU and is comfortable to play. Here's my current high score. :)

http://i39.tinypic.com/b6oj8g.png

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT - An arcade style game using pure batch

#18 Post by dbenham » 13 Jul 2013 19:55

Yea, crawl is a good speed for running up a high score. I cracked 100 once, though I have no screen shot to prove it :twisted:

I too ran into disappearing food once, many plays ago. I've been through the code logic repeatedly and simply cannot find the source of that problem. I've played a great many times since without running into the problem. I'd love it if someone could discover the source of the bug, but I'm not very optimistic - it is so intermittent.


Dave Benham

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: SNAKE.BAT - An arcade style game using pure batch

#19 Post by penpen » 14 Jul 2013 08:20

I'm not sure, but i think the error is produced because you do that in the main loop:

Code: Select all

      %=== remove the new head location from the empty list ===%
      for %%X in ("!X!") do for %%Y in ("!Y!") do set "empty=!empty:#%%~X %%~Y=!"
::    ...
        set /a "score+=1, F=(!random!%%emptyCnt)*6+1, emptyCnt-=1"
::    ...
        %=== add the former tail position to the empty list ===%
        set "empty=!empty!#!TX! !TY!"
So if F=(!random!%%emptyCnt)*6+1 points to the maximum empty element, there is none because you shortened the list by one while setting the new food.
The plot macro then operates on the variables part2 and line, so no other problem occurs as part2 is overwritten the next time plot is used, and line is never used.

penpen

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: SNAKE.BAT - An arcade style game using pure batch

#20 Post by penpen » 14 Jul 2013 08:42

Nearly forgotten:
I downloaded the choice from http://winsupport.org/utilities/freedos-choice.html as it was not present on my xp home, and the game then prints:

Code: Select all

ERROR: This game requires the CHOICE command, but it is missing.
Game aborted. :(
usw.

The tests you do to check the existance of choice produces:

Code: Select all

Z:\>choice /c:yn /t 0 /d y
CHOICE:Incorrect timeout syntax.  Expected form Tc,nn or T:c,nn

Z:\>choice /c:yn /t:y,-1
CHOICE:Incorrect timeout syntax.  Expected form Tc,nn or T:c,nn


In the upper case /t 0 and /d are not supported and choice sets an errorlevel of 0, and in the lower case the -1 produces the error and the errorlevel is set to 1.

I changed the code to:

Code: Select all

: ...
set "choice="
2>nul >nul choice /c:yn /t 0 /d y
if errorlevel 1 if not errorlevel 2 (
  set "choice=choice /cs"
)

2>nul >nul choice /c:yn /t:y,1
if errorlevel 1 if not errorlevel 2 set "choice=choice /s"

if not defined choice (
: ...

and it works fine.

penpen

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: SNAKE.BAT - An arcade style game using pure batch

#21 Post by Aacini » 14 Jul 2013 10:08

I remember that SNAKE game uses just two control keys: turn right and turn left, and that the turn direction was relative to snake movement. As a matter of fact, this behaviour was one of the most funny details of SNAKE game. I used such control in my SNAKE.BAT program (posted more than a year ago, look for it below program #6), that I wrote mainly as an example program on how to use my Window.exe, GetKey.exe, CursorPos.exe and Show.exe auxiliary .exe programs.

Antonio

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT - An arcade style game using pure batch

#22 Post by dbenham » 14 Jul 2013 10:55

@penpen - Woohooo :!: :D

You have killed the last known bug. I confirmed that my failure to decrement the emptyCnt prior to picking a random number was the source of the disappearing food. I fixed the code by using:

Code: Select all

set /a "score+=1, F=(!random!%%(emptyCnt-=1))*6+1"


Also, thanks for reporting the problem with the downloaded 16 bit CHOICE version. The 32 bit version uses -1 for no wait, but I guess the 16 bit version doesn't have that option. I couldn't test because all I have are 64 bit machines. I've updated the 2nd CHOICE test to wait for 1 second, as you suggested.

Now I can dare to hope that there are no more bugs :lol:

Thanks again to penpen, miskox, and aGerman for help in squashing the bugs.


Dave Benham

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: SNAKE.BAT - An arcade style game using pure batch

#23 Post by Squashman » 14 Jul 2013 11:03

I can't get it to work at all. Windows 7 32bit.
It gets stuck at Press any alpha-numeric key to start...

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT - An arcade style game using pure batch

#24 Post by dbenham » 14 Jul 2013 11:11

Squashman wrote:I can't get it to work at all. Windows 7 32bit.
It gets stuck at Press any alpha-numeric key to start...

I may have spoken too soon about no bugs :lol:

I have seen some intermittent issues where the game and controller get out of synch in some way and I have no control. The rare times that happens, I simply kill the window and start afresh with a new console window.

But the program fails entirely as you describe when I attempt to run it at work on a shared network drive. At work, I must run it on a local hard drive.


Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT - An arcade style game using pure batch

#25 Post by dbenham » 14 Jul 2013 11:21

Aacini wrote:I remember that SNAKE game uses just two control keys: turn right and turn left, and that the turn direction was relative to snake movement. As a matter of fact, this behaviour was one of the most funny details of SNAKE game. I used such control in my SNAKE.BAT program (posted more than a year ago, look for it below program #6), that I wrote mainly as an example program on how to use my Window.exe, GetKey.exe, CursorPos.exe and Show.exe auxiliary .exe programs.

Antonio

I remember scanning that post, but I generally avoid downloaded non-script based CMD extensions. Your utilities sure make development easier, but my goal was to build an arcade came using pure native batch commands.

I realize my program does not run on XP without downloading a non-standard CHOICE command, so I haven't met my goal for XP. But that is the best I could do. My SNAKE.BAT should work on any Windows platform from Vista onward, without any non-native extensions.

I might create a modified version of my SNAKE.BAT that uses only left and right controls. But I think I'm ready to give it a rest for now.


Dave Benham

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: SNAKE.BAT - An arcade style game using pure batch

#26 Post by carlos » 15 Jul 2013 15:09

dave, thanks for share you game.
time ago i wrote some batch games and i remember that there are two syntax for choice.
the choice.com and choice.exe that runs in windows 98 and the choice.exe from windows 2003.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: SNAKE.BAT - An arcade style game using pure batch

#27 Post by jeb » 15 Jul 2013 15:31

Hi Dave,

there is an pure batch workaround for the choice command, it even works with XP.
It's even better than choice, as it accepts more characters.
The only drawback is the missing timeout possibility

Code: Select all

:GetKey
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
  if not defined key set "key=%%L"
)
set "key=%key:~-1%"
exit /b


hope it helps
jeb

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: SNAKE.BAT - An arcade style game using pure batch

#28 Post by carlos » 15 Jul 2013 15:33

I remember this routine for detect choice and the switch for the different sintaxys:

for detect:

Code: Select all

SETLOCAL
(SET EXISTCHOICE=)
SET "FOLDER_TO_SEARCH=.\;%PATH%"
FOR %%A IN (CHOICE.EXE) DO (
   SET "EXISTCHOICE=%%~$FOLDER_TO_SEARCH:A"
)
IF NOT DEFINED EXISTCHOICE (
   SET "FOLDER_TO_SEARCH=%PATH%"
   FOR %%A IN (CHOICE.COM) DO (
      SET "EXISTCHOICE=%%~$FOLDER_TO_SEARCH:A"
   )
)
IF NOT DEFINED EXISTCHOICE (
ECHO CHOICE NOT EXIST
)


and for switch syntax:

Code: Select all

(SET KEYS=WASD)
(SET /A SECONDS=2)

CHOICE /C 0 /N /D 0 /T 1 <NUL >NUL 2>&1
IF NOT ERRORLEVEL 255 (
   REM CHOICE VERSION 2003 .EXE
   SET "ACTION1=CHOICE.EXE /C %KEYS% /N /D "
   SET "ACTION2= /T %SECONDS%"
   SET "AGAIN=CHOICE.EXE /C %KEYS%YN /N /M "PLAY AGAIN[Y,N]?""
) ELSE (
   REM CHOICE VERSION 98 .COM AND .EXE
   SET "ACTION1=CHOICE /C:%KEYS% /N /T:"
   SET "ACTION2=,%SECONDS%"
   SET "AGAIN=CHOICE /C:%KEYS%YN /N "PLAY AGAIN[Y,N]?""
)

:ACTION
%ACTION1%!MOVEMENT!%ACTION2% >NUL 2>&1


the variable movement define the default key pressed, this variable can change every time.
you can see the difference, for example for show a message the 98 version only enclose a message in quotes, the 2003 version need the switch /m
for time and default keys 2003 version uses /d default /t seconds
98 version uses /t:default,seconds
and for specify valid keys: 98 version uses /c:keys and 2003 uses: /c keys

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: SNAKE.BAT - An arcade style game using pure batch

#29 Post by aGerman » 15 Jul 2013 15:34

I realize my program does not run on XP without downloading a non-standard CHOICE command, so I haven't met my goal for XP.


I guess that could be solved. There we fiddled with Carlos' XCOPY solution for a hidden input. That method could be easily changed to achieve a CHOICE behaviour (without timeout of course).

Code: Select all

@echo off &setlocal
<nul set /p "=Yes or No [yn]: "
call :choice yn
echo Option #%errorlevel% was chosen.
pause
goto :eof


:choice
setlocal DisableDelayedExpansion
set "n=0" &set "c=" &set "e=" &set "map=%~1"
if not defined map endlocal &exit /b 0
for /f "delims=" %%i in ('2^>nul xcopy /lw "%~f0" "%~f0"') do if not defined c set "c=%%i"
set "c=%c:~-1%"
if defined c (
  for /f delims^=^ eol^= %%i in ('cmd /von /u /c "echo(!map!"^|find /v ""^|findstr .') do (
    set /a "n += 1" &set "e=%%i"
    setlocal EnableDelayedExpansion
    if /i "!e!"=="!c!" (
      echo(!c!
      for /f %%j in ("!n!") do endlocal &endlocal &exit /b %%j
    )
    endlocal
  )
)
endlocal &goto choice

There is another interresting technique used in that code. FIND interprets the 0x00 character of a unicode string as linebreak. I used that behaviour to separate the allowed keys.

Regards
aGerman

EDIT Seems I'm late :lol:

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: SNAKE.BAT - An arcade style game using pure batch

#30 Post by carlos » 17 Jul 2013 12:21

why is needed the /L parameter using /X parameter in xcopy?

Post Reply