Advertisement
alek_

SnakeGame in Command.exe [REMAKE - Working!!!]

Feb 21st, 2020
2,535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. if "%~1" == "startGame" goto :game
  3. if "%~1" == "startController" goto :controller
  4.  
  5.  
  6. ::---------------------------------------------------------------------
  7. :: setup some global variables used by both the game and the controller
  8.  
  9. setlocal disableDelayedExpansion
  10. :getSession
  11. if defined temp (set "tempFileBase=%temp%\") else if defined tmp set "tempFileBase=%tmp%\"
  12. set "tempFileBase=%tempFileBase%Snake%time::=_%"
  13. set "keyFile=%tempFileBase%_key.txt"
  14. set "cmdFile=%tempFileBase%_cmd.txt"
  15. set "gameLock=%tempFileBase%_gameLock.txt"
  16. set "gameLog=%tempFileBase%_gameLog.txt"
  17. set "signal=%tempFileBase%_signal.txt"
  18. set "saveLoc=%userprofile%\Snake"
  19. set "userPref=%saveLoc%\SnakeUserPref.txt"
  20. set "hiFile=%saveLoc%\Snake!growth!Hi"
  21. set "keyStream=9"
  22. set "cmdStream=8"
  23. set "lockStream=7"
  24.  
  25.  
  26. ::------------------------------------------
  27. :: Lock this game session and launch.
  28. :: Loop back and try a new session if failure.
  29. :: Cleanup and exit when finished
  30.  
  31. call :launch %lockStream%>"%gameLock%" || goto :getSession
  32. del "%tempFileBase%*"
  33. exit /b
  34.  
  35.  
  36. ::------------------------------------------
  37. :launch the game and the controller
  38.  
  39. call :fixLogs
  40. copy nul "%keyFile%" >nul
  41. copy nul "%cmdFile%" >nul
  42. start "" /b cmd /c ^""%~f0" startController %keyStream%^>^>"%keyFile%" %cmdStream%^<"%cmdFile%" 2^>nul ^>nul^"
  43. cmd /c ^""%~f0" startGame %keyStream%^<"%keyFile%" %cmdStream%^>^>"%cmdFile%" ^<nul^"
  44. echo(
  45.  
  46.  
  47. ::--------------------------------------------------------------
  48. :: Upon exit, wait for the controller to close before returning
  49.  
  50. :close
  51. 2>nul (>>"%keyFile%" call )||goto :close
  52. if not exist "%~dp0CursorPos.exe" (
  53.  echo Game play can be improved by installing
  54.  echo Aacini's CursorPos.exe, available at
  55.  echo http://goo.gl/hr6Kkn
  56.  echo(
  57.  echo %cmdcmdline%|find /i "%~f0">nul&&pause
  58. )
  59. exit /b 0
  60.  
  61.  
  62. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  63. :game
  64. title %~nx0
  65. cls
  66.  
  67. ::---------------------------------------
  68. :: Playfield size
  69. :: max playing field: (width-2)*(height-2) <= 1365
  70.  
  71. set "width=40"   max=99
  72. set "height=25"  max=99
  73.  
  74. ::----------------------------
  75. :: resize the console window
  76.  
  77. set /a cols=width+1, lines=height+10, area=(width-2)*(height-2)
  78. if %area% gtr 1365 (
  79.  echo ERROR: Playfield area too large
  80.  %sendCmd% quit
  81.  exit
  82. )
  83. if %lines% lss 14 set lines=14
  84. if %cols% lss 46 set cols=46
  85. mode con: cols=%cols% lines=%lines%
  86.  
  87.  
  88. ::----------------------------
  89. :: define variables
  90.  
  91. set "configOptions=diffCode difficulty growth moveKeys up down left right"
  92. set "configOptionCnt=9"
  93.  
  94. set "moveKeys=4"
  95.  
  96. set "up=W"
  97. set "down=S"
  98. set "left=A"
  99. set "right=D"
  100. set "pause=P"
  101.  
  102. set "space= "
  103. set "bound=#"
  104. set "food=+"
  105. set "head=@"
  106. set "body=O"
  107. set "death=X"
  108.  
  109. set "growth=1"
  110.  
  111. if exist "%userPref%" for /f "usebackq delims=" %%V in ("%userPref%") do set "%%V"
  112.  
  113. set "spinner1=-"
  114. set "spinner2=\"
  115. set "spinner3=|"
  116. set "spinner4=/"
  117. set "spinner= spinner1 spinner2 spinner3 spinner4 "
  118.  
  119. set "delay1=20"
  120. set "delay2=15"
  121. set "delay3=10"
  122. set "delay4=7"
  123. set "delay5=5"
  124. set "delay6=3"
  125.  
  126. set "desc1=Sluggard"
  127. set "desc2=Crawl"
  128. set "desc3=Slow"
  129. set "desc4=Normal"
  130. set "desc5=Fast"
  131. set "desc6=Insane"
  132.  
  133. set "spinnerDelay=3"
  134.  
  135. set /a "width-=1, height-=1"
  136.  
  137. :: define LF as a Line Feed (newline) character
  138. set ^"LF=^
  139.  
  140. ^" Above empty line is required - do not remove
  141.  
  142. :: define CR as a Carriage Return character
  143. for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"
  144.  
  145. :: define BS as a BackSpace character
  146. for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "BS=%%A"
  147.  
  148. set "upper=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
  149. set "invalid=*~="
  150.  
  151.  
  152. ::---------------------------
  153. :: define macros
  154.  
  155. if exist "%~dp0CursorPos.exe" (
  156.  set "cls=CursorPos 0 0"
  157.  set "ClearLine=echo(                                   &CursorPos 0 -1"
  158.  set "ClearPrev=CursorPos 0 -0&echo(                                   "
  159. ) else (
  160.  set "cls=cls"
  161.  set "ClearLine="
  162.  set "ClearPrev="
  163. )
  164.  
  165. :: define a newline with line continuation
  166. set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  167.  
  168. :: setErr
  169. :::  Sets the ERRORLEVEL to 1
  170. set "setErr=(call)"
  171.  
  172. :: clrErr
  173. :::  Sets the ERRORLEVEL to 0
  174. set "clrErr=(call )"
  175.  
  176.  
  177. :: sendCmd  command
  178. :::  sends a command to the controller
  179. set "sendCmd=>&%cmdStream% echo"
  180.  
  181.  
  182. :: getKey  [ValidKey]  [ValidKey...]
  183. ::: Check for keypress from the controller. Only accept a ValidKey.
  184. ::: Token delimiters and poison characters must be quoted.
  185. ::: Accept any key if no ValidKey specified.
  186. ::: Return result in Key variable. Key is undefined if no valid keypress.
  187. set getKey=%\n%
  188. for %%# in (1 2) do if %%#==2 (%\n%
  189.  set key=%\n%
  190.  set inKey=%\n%
  191.  set keyTest=%\n%
  192.  ^<^&%keyStream% set /p "inKey="%\n%
  193.  if defined inKey (%\n%
  194.    set inKey=!inKey:~0,-1!%\n%
  195.    for %%C in (!args!) do set /a keyTest=1^&if /i !inKey! equ %%~C set key=!inKey!%\n%
  196.  )%\n%
  197.  if not defined keyTest set key=!inKey!%\n%
  198. ) else set args=
  199.  
  200.  
  201. :: draw
  202. :::  draws the board
  203. set draw=%\n%
  204. set screen=%\n%
  205. for /l %%Y in (0,1,%height%) do set screen=!screen!!line%%Y!!LF!%\n%
  206. set screen=!screen!Speed = !Difficulty! !replay!!LF!Growth Rate = !growth!   HighScore = !hi!!LF!Score = !score!   Time = !m!:!s!%\n%
  207. if defined replay if not defined replayFinished (%\n%
  208.  set screen=!screen!!LF!!LF!Press a key to abort the replay%\n%
  209. )%\n%
  210. %cls%^&echo(!screen!
  211.  
  212. :: test  X  Y  ValueListVar
  213. :::  tests if value at coordinates X,Y is within contents of ValueListVar
  214. set test=%\n%
  215. for %%# in (1 2) do if %%#==2 (for /f "tokens=1-3" %%1 in ("!args!") do (%\n%
  216.  for %%A in ("!line%%2:~%%1,1!") do if "!%%3:%%~A=!" neq "!%%3!" %clrErr% else %setErr%%\n%
  217. )) else set args=
  218.  
  219.  
  220. :: plot  X  Y  ValueVar
  221. :::  places contents of ValueVar at coordinates X,Y
  222. set plot=%\n%
  223. for %%# in (1 2) do if %%#==2 (for /f "tokens=1-3" %%1 in ("!args!") do (%\n%
  224.  set "part2=!line%%2:~%%1!"%\n%
  225.  set "line%%2=!line%%2:~0,%%1!!%%3!!part2:~1!"%\n%
  226. )) else set args=
  227.  
  228.  
  229. ::--------------------------------------
  230. :: start the game
  231. setlocal enableDelayedExpansion
  232. if not exist "%saveLoc%\" md "%saveLoc%"
  233. set "replay= Aborting... "
  234. set "replayAvailable="
  235. call :loadHighScores
  236. call :mainMenu
  237.  
  238.  
  239. ::--------------------------------------
  240. :: main loop (infinite loop)
  241. for /l %%. in () do (
  242.  
  243.  %=== check for and process abort signal if in replay mode ===%
  244.  if defined replay if exist "%signal%" (
  245.    del "%signal%"
  246.    set "replayFinished=1"
  247.    %draw%
  248.    echo(
  249.    %ClearLine%
  250.    <nul set /p "=Aborting... "
  251.    findstr "^" >nul <&%keyStream%
  252.    for %%A in (!configOptions!) do set "%%A=!%%ASave!"
  253.    call :mainMenu
  254.  )
  255.  
  256.  %=== compute time since last move ===%
  257.  for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
  258.  if !tDiff! lss 0 set /a tDiff+=24*60*60*100
  259.  
  260.  if !tDiff! geq !delay! (
  261.    %=== delay has expired, so time for movement ===%
  262.    set /a t1=t2
  263.  
  264.    %=== compute game time ===%
  265.    if not defined gameStart set "gameStart=!t2!"
  266.    set /a "gameTime=(t2-gameStart)"
  267.    if !gameTime! lss 0 set /a "gameTime+=24*60*60*100"
  268.    set /a "gameTime=(gameTime-pauseTime)/100, m=gameTime/60, s=gameTime%%60"
  269.    if !m! lss 10 set "m=0!m!"
  270.    if !s! lss 10 set "s=0!s!"
  271.  
  272.    %=== get keypress ===%
  273.    %getKey% !keys!
  274.    if /i !key! equ !pause! (
  275.  
  276.      %=== pause game ===%
  277.      echo(
  278.      call :ask "PAUSED - Press a key to continue..."
  279.      %ClearPrev%
  280.      %sendCmd% go
  281.      for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
  282.      if !tDiff! lss 0 set /a tDiff+=24*60*60*100
  283.      set /a pauseTime+=tDiff
  284.  
  285.    ) else (
  286.  
  287.      %=== establish direction ===%
  288.      if not defined replay (echo(!key!.) >>"!gameLog!"
  289.      for %%K in (!key!) do if !moveKeys! equ 2 (
  290.        set /a "xDiff=xTurn%%K*!yDiff!, yDiff=yTurn%%K*!xDiff!"
  291.      ) else if "!%%KAxis!" neq "!axis!" (
  292.        set /a "xDiff=xDiff%%K, yDiff=yDiff%%K"
  293.        set "axis=!%%KAxis!"
  294.      )
  295.  
  296.      %=== erase the tail ===%
  297.      set "TX=!snakeX:~-2!"
  298.      set "TY=!snakeY:~-2!"
  299.      set "snakeX=!snakeX:~0,-2!"
  300.      set "snakeY=!snakeY:~0,-2!"
  301.      %plot% !TX! !TY! space
  302.  
  303.      %=== compute new head location and attempt to move ===%
  304.      set /a "X=PX+xDiff, Y=PY+yDiff"
  305.      set "X= !X!"
  306.      set "Y= !Y!"
  307.      set "X=!X:~-2!"
  308.      set "Y=!Y:~-2!"
  309.      (%test% !X! !Y! playerSpace) && (
  310.  
  311.        %=== move successful ===%
  312.  
  313.        %=== remove the new head location from the empty list ===%
  314.        for %%X in ("!X!") do for %%Y in ("!Y!") do set "empty=!empty:#%%~X %%~Y=!"
  315.  
  316.         %=== eat any food that may be present ===%
  317.         (%test% !X! !Y! food) && (
  318.           %=== initiate growth ===%
  319.           set /a grow+=growth
  320.  
  321.           %=== locate and draw new food ===%
  322.           if defined replay (
  323.             <&%keyStream% set /p "F="
  324.           ) else (
  325.             set /a "F=(!random!%%(emptyCnt-1))*6+1"
  326.             (echo !F!) >>"!gameLog!"
  327.           )
  328.           for %%F in (!F!) do (%plot% !empty:~%%F,5! food)
  329.         )
  330.  
  331.         if !grow! gtr 0 (
  332.           %=== restore the tail ===%
  333.           %plot% !TX! !TY! body
  334.           set "snakeX=!snakeX!!TX!"
  335.           set "snakeY=!snakeY!!TY!"
  336.           set /a emptyCnt-=1
  337.  
  338.           %=== manage score ===%
  339.           set /a "score+=1, grow-=1"
  340.           if not defined replay if !score! gtr !hi! set /a "hi+=1, newHi=1"
  341.  
  342.         ) else (
  343.           %=== add the former tail position to the empty list ===%
  344.           set "empty=!empty!#!TX! !TY!"
  345.         )
  346.  
  347.         %=== draw the new head ===%
  348.         if defined snakeX (%plot% !PX! !PY! body)
  349.         %plot% !X! !Y! head
  350.  
  351.         %=== Add the new head position to the snake strings ===%
  352.         set "snakeX=!X!!snakeX!"
  353.         set "snakeY=!Y!!snakeY!"
  354.         set "PX=!X!"
  355.         set "PY=!Y!"
  356.  
  357.         %draw%
  358.  
  359.       ) || (
  360.  
  361.         %=== failed move - game over ===%
  362.         set "replayFinished=1"
  363.         %plot% !TX! !TY! body
  364.         call :spinner !PX! !PY! death
  365.         %draw%
  366.         if defined newHi (
  367.           echo(
  368.           echo New High Score - Congratulations^^!
  369.           set "hi!diffCode!=!score!"
  370.           copy "!gameLog!" "%hiFile%!diffCode!.txt" >nul
  371.           >>"%hiFile%!diffCode!.txt" echo ::!score!
  372.         )
  373.         echo(
  374.         %ClearLine%
  375.         call :ask "Press a key to continue..."
  376.         for %%A in (!configOptions!) do set "%%A=!%%ASave!"
  377.         call :mainMenu
  378.       )
  379.     )
  380.   )
  381. )
  382.  
  383.  
  384. ::-------------------------------------
  385. :getString  Prompt  Var  MaxLen
  386. :: Prompt for a string with max lengh of MaxLen.
  387. :: Valid keys are alpha-numeric, space, underscore, and dash
  388. :: String is terminated by Enter
  389. :: Backspace works to delete previous character
  390. :: Result is returned in Var
  391. set /a "maxLen=%3"
  392. set "%2="
  393. %sendCmd% prompt
  394. <nul set /p "=%~1 "
  395. call :purge
  396. :getStringLoop
  397. (%getKey% !upper! 0 1 2 3 4 5 6 7 8 9 " " _ - {Enter} !BS!)
  398. if defined key (
  399.   if !key! equ {Enter} (
  400.     echo(
  401.     exit /b
  402.   )
  403.   if !key! neq !BS! if !maxLen! gtr 0 (
  404.     set /a maxLen-=1
  405.     <nul set /p "=.!BS!!key!"
  406.     set "%2=!%2!!key!
  407.  )
  408.  if !key! equ !BS! if defined %2 (
  409.    set /a maxLen+=1
  410.    <nul set /p "=!BS! !BS!"
  411.    set "%2=!%2:~0,-1!"
  412.  )
  413. )
  414. if defined inKey %sendCmd% one
  415. goto :getStringLoop
  416.  
  417.  
  418. ::-------------------------------------
  419. :ask  Prompt  ValidKey [Validkey]...
  420. :: Prompt for a keypress.
  421. :: Wait until a ValidKey is pressed and return result in Key variable.
  422. :: Token delimiters, ), and poison characters must be quoted.
  423. %sendCmd% prompt
  424. <nul set /p "=%~1 "
  425. (set validKeys=%*)
  426. (set validKeys=!validKeys:%1=!)
  427. call :purge
  428. :getResponse
  429. (%getKey% !validKeys!)
  430. if not defined key (
  431.  if defined inKey %sendCmd% one
  432.  goto :getResponse
  433. )
  434. exit /b
  435.  
  436.  
  437. :purge
  438. set "inKey="
  439. for /l %%N in (1 1 1000) do (
  440.  set /p "inKey="
  441.  if "!inKey!" equ "{purged}." exit /b
  442. )<&%keyStream%
  443. goto :purge
  444.  
  445.  
  446. ::-------------------------------------
  447. :spinner  X  Y  ValueVar
  448. set /a d1=-1000000
  449. for /l %%N in (1 1 5) do for %%C in (%spinner%) do (
  450.  call :spinnerDelay
  451.  %plot% %1 %2 %%C
  452.  %draw%
  453. )
  454. call :spinnerDelay
  455. (%plot% %1 %2 %3)
  456. exit /b
  457.  
  458.  
  459. ::-------------------------------------
  460. :delay  centiSeconds
  461. setlocal
  462. for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "spinnerDelay=%1, d1=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100"
  463. :: fall through to :spinnerDelay
  464.  
  465. ::-------------------------------------
  466. :spinnerDelay
  467. for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "d2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, dDiff=d2-d1"
  468. if %dDiff% lss 0 set /a dDiff+=24*60*60*100
  469. if %dDiff% lss %spinnerDelay% goto :spinnerDelay
  470. set /a d1=d2
  471. exit /b
  472.  
  473.  
  474. ::-------------------------------------
  475. :mainMenu
  476. cls
  477. set "loadAvailable="
  478. echo Growth rate = !growth!
  479. echo(
  480. echo Main Menu:
  481. echo(
  482. echo   N - New game
  483. if defined replayAvailable echo   R - Replay previous game
  484. if defined saveAvailable   echo   S - Save a game
  485. if exist *.snake.txt       echo   L - Load and watch a saved game&set "loadAvailable=L"
  486.  
  487. echo   C - Control options
  488. echo   G - Graphic options
  489. echo   Q - Quit
  490. echo(
  491. set "hiAvailable="
  492. for /l %%N in (1 1 6) do if defined hi%%N (
  493.  if not defined hiAvailable (
  494.    echo Replay High Score:
  495.    echo(
  496.  )
  497.  set "desc=!desc%%N!........"
  498.  set "hiAvailable=!hiAvailable! %%N"
  499.  echo   %%N - !desc:~0,8! !hi%%N!
  500. )
  501. if defined hiAvailable echo(
  502. set "keys=N C G Q !hiAvailable! !replayAvailable! !saveAvailable! !loadAvailable!"
  503. call :ask ">" !keys!
  504. if /i !key! equ Q (
  505.  %sendCmd% quit
  506.  cls
  507.  exit
  508. )
  509. if /i !key! equ N (
  510.  set "replay="
  511.  set "replayAvailable=R"
  512.  set "saveAvailable=S"
  513.  goto :initialize
  514. )
  515. if /i !key! equ S (
  516.  if defined replayAvailable (
  517.    call :ask "HighScore # or P for Previous:" !hiAvailable! P
  518.   ) else (
  519.     call :ask "HighScore #:" !hiAvailable!
  520.   )
  521.   echo !key!
  522.   if /i !key! equ P (set "src=!gameLog!") else set "src=%hiFile%!key!.txt"
  523.   call :getString "Save file name:" file 20
  524.   copy "!src!" "!file!.snake.txt"
  525.   call :ask "Press a key to continue..."
  526. )
  527. if /i !key! equ L (
  528.   call :getString "Load file name:" file 20
  529.   if exist "!file!.snake.txt" (
  530.     set "replay=!file!.snake.txt"
  531.     goto :initialize
  532.   )
  533.   echo Error: File "!file!.snake.txt" not found
  534.   call :ask "Press a key to continue..."
  535. )
  536. if /i !key! equ R (
  537.   set "replay=!gameLog!"
  538.   goto :initialize
  539. )
  540. if !key! geq 1 if !key! leq 6 (
  541.   set "replay=%hiFile%!key!.txt"
  542.   goto :initialize
  543. )
  544. if /i !key! equ C call :controlOptions
  545. if /i !key! equ G call :graphicOptions
  546. goto :mainMenu
  547.  
  548.  
  549. ::-------------------------------------
  550. :controlOptions
  551. cls
  552. set "keys={Enter} T L R P"
  553. if !moveKeys! equ 4 set "keys=!keys! U D"
  554.                     echo Control Options:
  555.                     echo(
  556.                     echo   T - Type... = !moveKeys! keys
  557.                     echo(
  558.                     echo   L - Left... = !left!
  559.                     echo   R - Right.. = !right!
  560. if !moveKeys! equ 4 echo   U - Up..... = !up!
  561. if !moveKeys! equ 4 echo   D - Down... = !down!
  562.                     echo(
  563.                     echo   P - Pause.. = !pause!
  564.                     echo(
  565.                     echo   {Enter} - Return to Main Menu
  566.                     echo(
  567. call :ask ">" !keys!
  568. if !key! equ {Enter} goto :saveUserPrefs
  569. if /i !key! equ T (
  570.   if !moveKeys! equ 2 (set "moveKeys=4") else set "moveKeys=2"
  571.   goto :controlOptions
  572. )
  573. set "option= LLeft RRight UUp DDown PPause"
  574. for /f %%O in ("!option:* %key%=!") do (
  575.   call :ask "Press a key for %%O:"
  576.   for %%K in (0 1 2) do if "!key!" equ "!invalid:~%%K,1!" goto :controlOptions
  577.   for %%C in (!upper!) do set "key=!key:%%C=%%C!"
  578.   set "%%O=!key!"
  579. )
  580. goto :controlOptions
  581.  
  582.  
  583. ::-------------------------------------
  584. :graphicOptions
  585. cls
  586. echo Graphic Options:
  587. echo(
  588. echo   B - Border...... = !bound!
  589. echo   E - Empty space. = !space!
  590. echo   H - snake Head.. = !head!
  591. echo   S - Snake body.. = !body!
  592. echo   F - Food........ = !food!
  593. echo   D - Death....... = !death!
  594. echo(
  595. echo   G - Growth rate. = !growth!
  596. echo(
  597. echo   {Enter} - Rturn to Main Menu
  598. echo(
  599. call :ask ">" B E H S F D G {Enter}
  600. if !key! equ {Enter} goto :saveUserPrefs
  601. if /i !key! equ G (
  602.   call :ask "Press a digit for growth rate (0 = 10)" 0 1 2 3 4 5 6 7 8 9
  603.   if !key! equ 0 set "key=10"
  604.   set "growth=!key!"
  605.   call :loadHighScores
  606. ) else (
  607.   set "option=-BBorder:bound:-EEmpty Space:space:-HSnake Head:head:-SSnake Body:body:-FFood:food:-DDeath:death:"
  608.   for /f "tokens=1,2 delims=:" %%A in ("!option:*-%key%=!") do (
  609.     call :ask "Press a key for %%A"
  610.     for %%K in (0 1 2) do if "!key!" equ "!invalid:~%%K,1!" goto :graphicOptions
  611.     set "%%B=!key!"
  612.   )
  613. )
  614. goto :graphicOptions
  615.  
  616.  
  617. ::------------------------------------
  618. :saveUserPrefs
  619. (for %%V in (moveKeys up down left right space bound food head body death pause growth) do echo %%V=!%%V!) >"%userPref%"
  620. exit /b
  621.  
  622.  
  623. ::-------------------------------------
  624. :initialize
  625. cls
  626. if defined replay (
  627.   echo Replay Speed Options:
  628. ) else (
  629.   echo Speed Options:
  630. )
  631. echo                       delay
  632. echo    #   Description  (seconds)
  633. echo   ---  -----------  ---------
  634. for /l %%N in (1 1 6) do (
  635.   set "delay=0!delay%%N!"
  636.   set "desc=!desc%%N!           "
  637.   echo    %%N   !desc:~0,11!    0.!delay:~-2!
  638. )
  639. echo(
  640. call :ask "Pick a speed (1-6):" 1 2 3 4 5 6
  641. set "difficulty=!desc%key%!"
  642. set "delay=!delay%key%!"
  643. set "diffCode=%key%"
  644. echo %key% - %difficulty%
  645. echo(
  646. <nul set /p "=Initializing."
  647. set "axis=X"
  648. set "xDiff=+1"
  649. set "yDiff=+0"
  650. set "empty="
  651. set /a "PX=1, PY=height/2, FX=width/2+1, FY=PY, score=0, emptyCnt=0, t1=-1000000"
  652. set "gameStart="
  653. set "m=00"
  654. set "s=00"
  655. set "snakeX= %PX%"
  656. set "snakeY= %PY%"
  657. set "snakeX=%snakeX:~-2%"
  658. set "snakeY=%snakeY:~-2%"
  659. for /l %%Y in (0 1 %height%) do (
  660.   <nul set /p "=."
  661.   set "line%%Y="
  662.   for /l %%X in (0,1,%width%) do (
  663.     set "cell="
  664.     if %%Y equ 0        set "cell=%bound%"
  665.     if %%Y equ %height% set "cell=%bound%"
  666.     if %%X equ 0        set "cell=%bound%"
  667.     if %%X equ %width%  set "cell=%bound%"
  668.     if %%X equ %PX% if %%Y equ %PY% set "cell=%head%"
  669.     if not defined cell (
  670.       set "cell=%space%"
  671.       set "eX= %%X"
  672.       set "eY= %%Y"
  673.       set "empty=!empty!#!eX:~-2! !eY:~-2!"
  674.       set /a emptyCnt+=1
  675.     )
  676.     if %%X equ %FX% if %%Y equ %FY% set "cell=%food%"
  677.     set "line%%Y=!line%%Y!!cell!"
  678.   )
  679. )
  680. for %%A in (!configOptions!) do set "%%ASave=!%%A!"
  681. set "replayFinished="
  682. if defined replay (
  683.   %sendCmd% replay
  684.   %sendCmd% !replay!
  685.   call :waitForSignal
  686.   set "replay=(REPLAY at !difficulty!)"
  687.   set "read=1"
  688.   <&%keyStream% (
  689.     for /l %%N in (1 1 !configOptionCnt!) do if defined read (
  690.       set /p "ln="
  691.       if "!ln!" equ "END" (set read=) else set "!ln!"
  692.     )
  693.   )
  694.   set "keys="
  695.   set "hi=0"
  696.   for /f "delims=:" %%A in ('findstr "^::" "%hiFile%!diffCode!.txt" 2^>nul') do set "hi=%%A"
  697.   (%draw%)
  698.   call :delay 100
  699. ) else (
  700.   if defined hi%diffCode% (set "hi=!hi%diffCode%!") else set "hi=0"
  701.   (%draw%)
  702.   >"!gameLog!" (
  703.     for %%A in (!configOptions!) do (echo %%A=!%%A!)
  704.     (echo END)
  705.   )
  706.   echo(
  707.   if !moveKeys! equ 4 (
  708.     echo Controls: !up!=up !down!=down !left!=left !right!=right !pause!=pause
  709.   ) else (
  710.     echo Controls: !left!=left !right!=right !pause!=pause
  711.   )
  712.   echo Avoid running into yourself (!body!!body!!head!^) or wall (!bound!^)
  713.   echo Eat food (!food!^) to grow.
  714.   echo(
  715.   call :ask "Press a key to start..."
  716.   %sendCmd% go
  717. )
  718. set "pauseTime=0"
  719. set "xDiff!up!=+0"
  720. set "xDiff!down!=+0"
  721. set "xDiff!left!=-1"
  722. set "xDiff!right!=+1"
  723. set "yDiff!up!=-1"
  724. set "yDiff!down!=+1"
  725. set "yDiff!left!=+0"
  726. set "yDiff!right!=+0"
  727. set "!up!Axis=Y"
  728. set "!down!Axis=Y"
  729. set "!left!Axis=X"
  730. set "!right!Axis=X"
  731. set "xTurn!left!=1"
  732. set "xTurn!right!=-1"
  733. set "yTurn!left!=-1"
  734. set "yTurn!right!=1"
  735. set "playerSpace=!space!!food!"
  736. set ^"keys="!left!" "!right!" "!pause!"^"
  737. set "newHi="
  738. set "grow=0"
  739. if !moveKeys! equ 4 set ^"keys=!keys! "!up!" "!down!"^"
  740. if exist "%~dp0CursorPos.exe" if not defined replay (
  741.   cursorpos 0 -4
  742.   for /l %%N in (1 1 5) do (echo(                                             )
  743. )
  744. exit /b
  745.  
  746.  
  747. ::-------------------------------------
  748. :waitForSignal
  749. if not exist "%signal%" goto :waitForSignal
  750. del "%signal%"
  751. exit /b
  752.  
  753.  
  754. ::-------------------------------------
  755. :loadHighScores
  756. set "saveAvailable="
  757. for /l %%N in (1 1 6) do (
  758.   set "hi%%N="
  759.   for /f "delims=:" %%A in ('findstr "^::" "%hiFile%%%N.txt" 2^>nul') do (
  760.     set "hi%%N=%%A"
  761.     set "saveAvailable=S"
  762.   )
  763. )
  764. exit /b
  765.  
  766.  
  767. ::-------------------------------------
  768. :fixLogs
  769. setlocal enableDelayedExpansion
  770. for %%F in (*.snake) do (
  771.   ren "%%F" "%%F.txt"
  772.   call :fixLog "%%F.txt"
  773. )
  774. pushd "%SaveLoc%"
  775. for /f "delims=" %%F in ('dir /b SnakeHi*.txt 2^>nul') do (
  776.   set "file=%%~nF"
  777.   set "file=Snake1Hi!file:~-1!.txt"
  778.   ren "%%F" "!file!"
  779.   call :fixLog "!file!"
  780. )
  781. popd
  782. exit /b
  783.  
  784. :fixLog  filePath
  785. >"%~1.new" (
  786.   <"%~1" (
  787.     for %%A in (diffCode difficulty moveKeys up down left right) do (
  788.       set /p "val="
  789.       (echo %%A=!val!)
  790.     )
  791.   )
  792.   (echo growth=1)
  793.   (echo END)
  794.   more +7 "%~1"
  795. )
  796. move /y "%~1.new" "%~1" >nul
  797. exit /b
  798.  
  799.  
  800. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  801. :controller
  802. :: Detects keypresses and sends the information to the game via a key file.
  803. :: The controller has various modes of input that are activated by commands sent
  804. :: from the game via a cmd file.
  805. ::
  806. :: Modes:
  807. ::
  808. ::   hold   - No input, wait for command
  809. ::
  810. ::   go     - Continuously get/send key presses
  811. ::
  812. ::   prompt - Send {purged} marker to allow game to purge input buffer, then
  813. ::            get/send a single key press and hold
  814. ::
  815. ::   one    - Get/send a single key press and hold
  816. ::
  817. ::   replay - Copy a game log to the key file. The next line in cmd file
  818. ::            specifies name of log file to copy. During replay, the controller
  819. ::            will send an abort signal to the game if a key is pressed.
  820. ::
  821. ::   quit   - Immediately exit the controller process
  822. ::
  823. :: As written, this routine incorrectly reports ! as ), but that doesn't matter
  824. :: since we don't need that key. Both <CR> and Enter key are reported as {Enter}.
  825. :: An extra character is appended to the output to preserve any control chars
  826. :: when read by SET /P.
  827.  
  828. setlocal enableDelayedExpansion
  829. for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
  830. set "cmd=hold"
  831. set "inCmd="
  832. set "key="
  833. for /l %%. in () do (
  834.   if "!cmd!" neq "hold" (
  835.     for /f "delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
  836.       if not defined key set "key=%%A"
  837.     )
  838.     set "key=!key:~-1!"
  839.     if !key! equ !CR! set "key={Enter}"
  840.   )
  841.   <&%cmdStream% set /p "inCmd="
  842.   if defined inCmd (
  843.     if !inCmd! equ quit exit
  844.     set "cmd=!inCmd!"
  845.     if !inCmd! equ replay (
  846.       <&%cmdStream% set /p "file="
  847.       type "!file!" >&%keyStream%
  848.       copy nul "%signal%"
  849.     )
  850.     set "inCmd="
  851.   )
  852.   if defined key (
  853.     if "!cmd!" equ "prompt" (echo {purged}.)
  854.     if "!cmd!" equ "replay" (
  855.       copy nul "%signal%" >nul
  856.       set "cmd=go"
  857.     ) else (echo(!key!.)
  858.     if "!cmd!" neq "go" set "cmd=hold"
  859.     set "key="
  860.   )>&%keyStream%
  861. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement