Hello! I want to steal the credit for a Snake game!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Locked
Message
Author
programmer
Posts: 4
Joined: 11 Nov 2013 22:32

Hello! I want to steal the credit for a Snake game!

#1 Post by programmer » 11 Nov 2013 22:42

hello!
I need this code
@echo off
SetLocal EnableExtensions EnableDelayedExpansion

mode con lines=34
color 00
echo.

call :GetConsoleWidth
call :GetDEL

Set DEC=5
set S=2
set E=%conWidth%
set /A HZ=%conWidth%/7

For /L %%H in (4,1,%HZ%) do (
echo.
set /A E-=5
For /L %%N in (1,1,!S!) do set /p "=%DEL% "<NUL
For /L %%N in (!S!,1,!E!) do >NUL ping 127.1 -n 1& set /p "=*"<NUL
echo.
For /L %%N in (1,1,!E!) do set /p "=%DEL% "<NUL
echo *
set /A S+=5
For /L %%N in (1,1,!E!) do set /p "=%DEL% "<NUL
set B=-1
For /L %%N in (!E!,-1,!S!) do (
set /A B+=1
For /L %%N in (1,1,!B!) do (
set /p "=%DEL%"<NUL
)
For /L %%N in (1,1,!B!) do (
set /p "=*"<NUL
)
)
echo *
For /L %%N in (1,1,!S!) do set /p "=%DEL% "<NUL
set /p "=*"<NUL
call :ChangeColor
)
md "%temp%\_ColorText" 2>nul& >NUL move /y "Default" "%temp%\_ColorText\SNAKE by Kozhabay Alisher"& pushd "%temp%\_ColorText"
findstr /V /a:0E "I Like It :)" "SNAKE by Kozhabay Alisher*" 2>NUL
popd& rd /s /q "%temp%\_ColorText"

pause>NUL
Exit /B

:GetConsoleWidth
For /F "skip=4 tokens=1,2" %%v In ('mode con') Do Set /A conWidth=%%w - 5& Exit /B

:GetDEL
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%A in (1) do rem"') do set "DEL=%%a"& <nul set /p "=%%a">Default
Exit /B

:ChangeColor
set /A DEC+=1
cmd /c exit /b %DEC%
color 1%=ExitCode%

to combine with this code

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: SNAKE.BAT - A pure Windows batch implementation of the classic game
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
if "%~1" == "startGame" goto :game
if "%~1" == "startController" goto :controller

::---------------------------------------------------------------------
:: setup some global variables used by both the game and the controller

setlocal disableDelayedExpansion
:getSession
if defined temp (set "tempFileBase=%temp%\") else if defined tmp set "tempFileBase=%tmp%\"
set "tempFileBase=%tempFileBase%Snake%time::=_%"
set "keyFile=%tempFileBase%_key.txt"
set "cmdFile=%tempFileBase%_cmd.txt"
set "gameLock=%tempFileBase%_gameLock.txt"


::------------------------------------------
:: Lock this game session and launch.
:: Loop back and try a new session if failure.
:: Cleanup and exit when finished

call :launch 7>"%gameLock%" || goto :getSession
del "%tempFileBase%*"
exit /b


::------------------------------------------
:launch the game and the controller

copy nul "%keyFile%" >nul
copy nul "%cmdFile%" >nul
start "" /b cmd /c ^""%~f0" startController 9^>^>"%keyFile%" 8^<"%cmdFile%" 2^>nul ^>nul^"
cmd /c ^""%~f0" startGame 9^<"%keyFile%" 8^>^>"%cmdFile%" ^<nul^"
echo(


::--------------------------------------------------------------
:: Upon exit, wait for the controller to close before returning

:close
2>nul (>>"%keyFile%" call )||goto :close
exit /b 0


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:game
title %~nx0
cls

::---------------------------------------
:: user configurable options
:: Note - do not use the * ~ ! or = key

set "up=W"
set "down=S"
set "left=A"
set "right=D"

set "space= "
set "bound=:"
set "food=$"
set "head=@"
set "body=+"
set "death=X"

set "width=50" max=99
set "height=30" max=99
:: max playing field: (width-2)*(height-2) <= 1365


::----------------------------
:: resize the console window

set /a cols=width+1, lines=height+10, area=(width-2)*(height-2)
if %area% gtr 1365 (
echo ERROR: Playfield area too large
%sendCmd% quit
exit
)
if %lines% lss 14 set lines=14
if %cols% lss 46 set cols=46
mode con: cols=%cols% lines=%lines%


::----------------------------
:: define variables

set "playerSpace=%space%%food%"

set "spinner1=-"
set "spinner2=\"
set "spinner3=|"
set "spinner4=/"
set "spinner= spinner1 spinner2 spinner3 spinner4 "

set "xDiff%up%=+0"
set "xDiff%down%=+0"
set "xDiff%left%=-1"
set "xDiff%right%=+1"

set "yDiff%up%=-1"
set "yDiff%down%=+1"
set "yDiff%left%=+0"
set "yDiff%right%=+0"

set "%up%Axis=Y"
set "%down%Axis=Y"
set "%left%Axis=X"
set "%right%Axis=X"

set "delay1=20"
set "delay2=15"
set "delay3=10"
set "delay4=7"
set "delay5=5"
set "delay6=3"
set "delay0=0"

set "desc1=Sluggard"
set "desc2=Crawl"
set "desc3=Slow"
set "desc4=Normal"
set "desc5=Fast"
set "desc6=Insane"
set "desc0=Unplayable"

set "spinnerDelay=3"

set /a "width-=1, height-=1"


::---------------------------
:: define macros

::define a Line Feed (newline) string (normally only used as !LF!)
set LF=^


::Above 2 blank lines are required - do not remove

::define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

:: setErr
::: Sets the ERRORLEVEL to 1
set "setErr=(call)"

:: clrErr
::: Sets the ERRORLEVEL to 0
set "clrErr=(call )"


:: sendCmd command
::: sends a command to the controller
set "sendCmd=>&8 echo"


:: getKey ValidKey [ValidKey...]
::: Check for keypress from the controller. Only accept a ValidKey.
::: Token delimiters and poison characters must be quoted.
::: Accept any key if no ValidKey specified.
::: Return result in Key variable. Key is undefined if no valid keypress.
set getKey=%\n%
for %%# in (1 2) do if %%#==2 (%\n%
set key=%\n%
set inKey=%\n%
set keyTest=%\n%
^<^&9 set /p "inKey="%\n%
if defined inKey (%\n%
set inKey=!inKey:~0,-1!%\n%
for %%C in (!args!) do set /a keyTest=1^&if /i !inKey! equ %%~C set key=!inKey!%\n%
)%\n%
if not defined keyTest set key=!inKey!%\n%
) else set args=


:: draw
::: draws the board
set draw=%\n%
cls%\n%
for /l %%Y in (0,1,%height%) do echo(!line%%Y!%\n%
echo Speed=!Difficulty!%\n%
echo Score=!score!


:: test X Y ValueListVar
::: tests if value at coordinates X,Y is within contents of ValueListVar
set test=%\n%
for %%# in (1 2) do if %%#==2 (for /f "tokens=1-3" %%1 in ("!args!") do (%\n%
for %%A in ("!line%%2:~%%1,1!") do if "!%%3:%%~A=!" neq "!%%3!" %clrErr% else %setErr%%\n%
)) else set args=


:: plot X Y ValueVar
::: places contents of ValueVar at coordinates X,Y
set plot=%\n%
for %%# in (1 2) do if %%#==2 (for /f "tokens=1-3" %%1 in ("!args!") do (%\n%
set "part2=!line%%2:~%%1!"%\n%
set "line%%2=!line%%2:~0,%%1!!%%3!!part2:~1!"%\n%
)) else set args=


::--------------------------------------
:: start the game
setlocal enableDelayedExpansion
call :initialize


::--------------------------------------
:: main loop (infinite loop)
for /l %%. in (1 0 1) do (

%=== compute time since last move ===%
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"
if !tDiff! lss 0 set /a tDiff+=24*60*60*100

if !tDiff! geq !delay! (
%=== delay has expired, so time for movement ===%

%=== establish direction ===%
%getKey% "%up%" "%down%" "%left%" "%right%"
for %%K in (!key!) do if "!%%KAxis!" neq "!axis!" (
set /a "xDiff=xDiff%%K, yDiff=yDiff%%K"
set "axis=!%%KAxis!"
)

%=== erase the tail ===%
set "TX=!snakeX:~-2!"
set "TY=!snakeY:~-2!"
set "snakeX=!snakeX:~0,-2!"
set "snakeY=!snakeY:~0,-2!"
%plot% !TX! !TY! space

%=== compute new head location and attempt to move ===%
set /a "X=PX+xDiff, Y=PY+yDiff"
set "X= !X!"
set "Y= !Y!"
set "X=!X:~-2!"
set "Y=!Y:~-2!"
(%test% !X! !Y! playerSpace) && (

%=== move successful ===%

%=== remove the new head location from the empty list ===%
for %%X in ("!X!") do for %%Y in ("!Y!") do set "empty=!empty:#%%~X %%~Y=!"

(%test% !X! !Y! food) && (
%=== moving to food - eat it ===%

%=== restore the tail ===%
%plot% !TX! !TY! body
set "snakeX=!snakeX!!TX!"
set "snakeY=!snakeY!!TY!"

%=== increment score and locate and draw new food ===%
set /a "score+=1, F=(!random!%%(emptyCnt-=1))*6+1"
for %%F in (!F!) do (%plot% !empty:~%%F,5! food)

) || (
%=== moving to empty space ===%

%=== add the former tail position to the empty list ===%
set "empty=!empty!#!TX! !TY!"
)

%=== draw the new head ===%
if defined snakeX (%plot% !PX! !PY! body)
%plot% !X! !Y! head

%=== Add the new head position to the snake strings ===%
set "snakeX=!X!!snakeX!"
set "snakeY=!Y!!snakeY!"
set "PX=!X!"
set "PY=!Y!"

%draw%

) || (

%=== failed move - game over ===%
%plot% !TX! !TY! body
call :spinner !PX! !PY! death
%draw%
echo(
call :ask "Would you like to play again? (Y/N)" Y N
if /i "!key!" equ "N" (
%sendCmd% quit
exit
) else (
call :initialize
)
)

set /a t1=t2
)
)


::-------------------------------------
:ask Prompt ValidKey [Validkey]...
:: Prompt for a keypress.
:: Wait until a ValidKey is pressed and return result in Key variable.
:: Token delimiters, ), and poison characters must be quoted.
%sendCmd% prompt
<nul set /p "=%~1 "
(set validKeys=%*)
(set validKeys=!validKeys:%1=!)
:purge
(%getKey% {purged})
if not defined key goto :purge
:getResponse
(%getKey% !validKeys!)
if not defined key (
if defined inKey %sendCmd% one
goto :getResponse
)
exit /b


::-------------------------------------
:spinner X Y ValueVar
set /a d1=-1000000
for /l %%N in (1 1 5) do for %%C in (%spinner%) do (
call :spinnerDelay
%plot% %1 %2 %%C
%draw%
)
call :spinnerDelay
(%plot% %1 %2 %3)
exit /b


::-------------------------------------
:spinnerDelay
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"
if %dDiff% lss 0 set /a dDiff+=24*60*60*100
if %dDiff% lss %spinnerDelay% goto :spinnerDelay
set /a d1=d2
exit /b


::-------------------------------------
:initialize
cls
color 4
echo =========================
echo SNAKE BY KOZHABAY ALISHER
echo =========================
echo Speed Options:
echo delay
echo # Description (seconds)
echo --- ----------- ---------
echo 1 Sluggard 0.20
echo 2 Crawl 0.15
echo 3 Slow 0.10
echo 4 Normal 0.07
echo 5 Fast 0.05
echo 6 Insane 0.03
echo 0 Unplayable none
echo(
call :ask "Pick a speed (1-6, 0):" 1 2 3 4 5 6 0
set "difficulty=!desc%key%!"
set "delay=!delay%key%!"
echo %key% - %difficulty%
echo(
<nul set /p "=Initializing."
set "axis=X"
set "xDiff=+1"
set "yDiff=+0"
set "empty="
set /a "PX=1, PY=height/2, FX=width/2+1, FY=PY, score=0, emptyCnt=0, t1=-1000000"
set "snakeX= %PX%"
set "snakeY= %PY%"
set "snakeX=%snakeX:~-2%"
set "snakeY=%snakeY:~-2%"
for /l %%Y in (0 1 %height%) do (
<nul set /p "=."
set "line%%Y="
for /l %%X in (0,1,%width%) do (
set "cell="
if %%Y equ 0 set "cell=%bound%"
if %%Y equ %height% set "cell=%bound%"
if %%X equ 0 set "cell=%bound%"
if %%X equ %width% set "cell=%bound%"
if %%X equ %PX% if %%Y equ %PY% set "cell=%head%"
if not defined cell (
set "cell=%space%"
set "eX= %%X"
set "eY= %%Y"
set "empty=!empty!#!eX:~-2! !eY:~-2!"
set /a emptyCnt+=1
)
if %%X equ %FX% if %%Y equ %FY% set "cell=%food%"
set "line%%Y=!line%%Y!!cell!"
)
)
(%draw%)
echo(
echo Movement keys: %up%=up %down%=down %left%=left %right%=right
echo Avoid running into yourself (%body%%body%%head%) or wall (%bound%)
echo Eat food (%food%) to grow.
echo(
call :ask "Press a key to start..."
%sendCmd% go
exit /b


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:controller
:: Detects keypresses and sends the information to the game via a key file.
:: The controller has various modes of input that are activated by commands sent
:: from the game via a cmd file.
::
:: Modes:
::
:: hold - no input, wait for command
::
:: go - continuously get/send key presses
::
:: prompt - send {purged} marker to allow game to purge input buffer, then
:: get/send a single key press and hold
::
:: one - get/send a single key press and hold
::
:: quit - immediately exit the controller process
::
:: As written, this routine incorrectly reports ! as ), but that doesn't matter
:: since we don't need that key. Both <CR> and Enter key are reported as {Enter}.
:: An extra character is appended to the output to preserve any control chars
:: when read by SET /P.

setlocal enableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set "cmd=hold"
set "inCmd="
set "key="
for /l %%. in (1 0 1) do (
if "!cmd!" neq "hold" (
for /f "delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
if not defined key set "key=%%A"
)
set "key=!key:~-1!"
if !key! equ !CR! set "key={Enter}"
)
<&8 set /p "inCmd="
if defined inCmd (
if !inCmd! equ quit exit
set "cmd=!inCmd!"
set "inCmd="
)
if defined key (
if "!cmd!" equ "prompt" >&9 (echo {purged}.)
>&9 (echo(!key!.)
if "!cmd!" neq "go" set "cmd=hold"
set "key="
)
)

so that first code running, and after running game. its clear?))))
Can you help me?

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: snake

#2 Post by ShadowThief » 11 Nov 2013 23:16

You... want us to make a title screen that gives you credit for that game? You realize that that game was written by one of the members of this site, right?

Are you stupid or what?

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

Re: snake

#3 Post by dbenham » 12 Nov 2013 00:03

OMG - It's hard to believe there are such shameless @#$**& people in this world :roll: :evil:

This reminds me of a young alleged C programmer that couldn't write a piece of code to save his life. I had developed the guts of a significant application that did 2D chemical structure searching in a relational database. I was waiting on this "developer" to produce some simple inter-process communication routines to plug into the app. I patiently explained what was needed and how it might be done many times. After a month, he finally showed me some code - absolute garbage. I asked why he had certain constructs in the code that made absolutely no sense, and he said it was code from school that he thought was relevant. I was so fed up at that point, I wrote the routines myself the next day.

About one week later, my boss received a resume from a head hunter. The candidate claimed many skills and accomplishments, including development of complex inter-process communication software for a chemistry application. Of course that loser never bothered to tell the head hunter not to send his resume to his current employer :!: :roll:


Dave Benham (the author of SNAKE.BAT)

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: snake

#4 Post by Magialisk » 12 Nov 2013 02:42

Oh boy that gave me a good laugh (sorry Dave :lol:). I couldn't quite visualize what the first part of this guy's code was supposed to be doing, other than the part that said "SNAKE - By xxxxxx". "Oh cool maybe this guy built a new take on the snake game", I was thinking...

Skipping down further I recognized some very familiar looking code and even now typing this it's hard to control my laughter. I guess this guy won't be impressing his middle school friends with his "leet batch skillz" as he'd hoped. The irony of wanting to steal someone else's work but not having the ability to insert your name into their code without asking for help... what are people coming to?

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

Re: snake

#5 Post by foxidrive » 12 Nov 2013 03:17

Magialisk wrote:The irony of wanting to steal someone else's work but not having the ability to insert your name into their code without asking for help... what are people coming to?


This "programmer" person is a tool... (the thread title is more appropriate now)

but I also find it disheartening to see all the requests for obvious school assignments to be done, in various forums.
This is what the Internet is good for, for young people....

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

Re: Hello! I want to steal the credit for a Snake game!

#6 Post by foxidrive » 12 Nov 2013 03:19

The tool will probably remove his post...

programmer wrote:hello!
I need this code
@echo off
SetLocal EnableExtensions EnableDelayedExpansion

mode con lines=34
color 00
echo.

call :GetConsoleWidth
call :GetDEL

Set DEC=5
set S=2
set E=%conWidth%
set /A HZ=%conWidth%/7

For /L %%H in (4,1,%HZ%) do (
echo.
set /A E-=5
For /L %%N in (1,1,!S!) do set /p "=%DEL% "<NUL
For /L %%N in (!S!,1,!E!) do >NUL ping 127.1 -n 1& set /p "=*"<NUL
echo.
For /L %%N in (1,1,!E!) do set /p "=%DEL% "<NUL
echo *
set /A S+=5
For /L %%N in (1,1,!E!) do set /p "=%DEL% "<NUL
set B=-1
For /L %%N in (!E!,-1,!S!) do (
set /A B+=1
For /L %%N in (1,1,!B!) do (
set /p "=%DEL%"<NUL
)
For /L %%N in (1,1,!B!) do (
set /p "=*"<NUL
)
)
echo *
For /L %%N in (1,1,!S!) do set /p "=%DEL% "<NUL
set /p "=*"<NUL
call :ChangeColor
)
md "%temp%\_ColorText" 2>nul& >NUL move /y "Default" "%temp%\_ColorText\SNAKE by Kozhabay Alisher"& pushd "%temp%\_ColorText"
findstr /V /a:0E "I Like It :)" "SNAKE by Kozhabay Alisher*" 2>NUL
popd& rd /s /q "%temp%\_ColorText"

pause>NUL
Exit /B

:GetConsoleWidth
For /F "skip=4 tokens=1,2" %%v In ('mode con') Do Set /A conWidth=%%w - 5& Exit /B

:GetDEL
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%A in (1) do rem"') do set "DEL=%%a"& <nul set /p "=%%a">Default
Exit /B

:ChangeColor
set /A DEC+=1
cmd /c exit /b %DEC%
color 1%=ExitCode%

to combine with this code

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: SNAKE.BAT - A pure Windows batch implementation of the classic game
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
if "%~1" == "startGame" goto :game
if "%~1" == "startController" goto :controller

::---------------------------------------------------------------------
:: setup some global variables used by both the game and the controller

setlocal disableDelayedExpansion
:getSession
if defined temp (set "tempFileBase=%temp%\") else if defined tmp set "tempFileBase=%tmp%\"
set "tempFileBase=%tempFileBase%Snake%time::=_%"
set "keyFile=%tempFileBase%_key.txt"
set "cmdFile=%tempFileBase%_cmd.txt"
set "gameLock=%tempFileBase%_gameLock.txt"


::------------------------------------------
:: Lock this game session and launch.
:: Loop back and try a new session if failure.
:: Cleanup and exit when finished

call :launch 7>"%gameLock%" || goto :getSession
del "%tempFileBase%*"
exit /b


::------------------------------------------
:launch the game and the controller

copy nul "%keyFile%" >nul
copy nul "%cmdFile%" >nul
start "" /b cmd /c ^""%~f0" startController 9^>^>"%keyFile%" 8^<"%cmdFile%" 2^>nul ^>nul^"
cmd /c ^""%~f0" startGame 9^<"%keyFile%" 8^>^>"%cmdFile%" ^<nul^"
echo(


::--------------------------------------------------------------
:: Upon exit, wait for the controller to close before returning

:close
2>nul (>>"%keyFile%" call )||goto :close
exit /b 0


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:game
title %~nx0
cls

::---------------------------------------
:: user configurable options
:: Note - do not use the * ~ ! or = key

set "up=W"
set "down=S"
set "left=A"
set "right=D"

set "space= "
set "bound=:"
set "food=$"
set "head=@"
set "body=+"
set "death=X"

set "width=50" max=99
set "height=30" max=99
:: max playing field: (width-2)*(height-2) <= 1365


::----------------------------
:: resize the console window

set /a cols=width+1, lines=height+10, area=(width-2)*(height-2)
if %area% gtr 1365 (
echo ERROR: Playfield area too large
%sendCmd% quit
exit
)
if %lines% lss 14 set lines=14
if %cols% lss 46 set cols=46
mode con: cols=%cols% lines=%lines%


::----------------------------
:: define variables

set "playerSpace=%space%%food%"

set "spinner1=-"
set "spinner2=\"
set "spinner3=|"
set "spinner4=/"
set "spinner= spinner1 spinner2 spinner3 spinner4 "

set "xDiff%up%=+0"
set "xDiff%down%=+0"
set "xDiff%left%=-1"
set "xDiff%right%=+1"

set "yDiff%up%=-1"
set "yDiff%down%=+1"
set "yDiff%left%=+0"
set "yDiff%right%=+0"

set "%up%Axis=Y"
set "%down%Axis=Y"
set "%left%Axis=X"
set "%right%Axis=X"

set "delay1=20"
set "delay2=15"
set "delay3=10"
set "delay4=7"
set "delay5=5"
set "delay6=3"
set "delay0=0"

set "desc1=Sluggard"
set "desc2=Crawl"
set "desc3=Slow"
set "desc4=Normal"
set "desc5=Fast"
set "desc6=Insane"
set "desc0=Unplayable"

set "spinnerDelay=3"

set /a "width-=1, height-=1"


::---------------------------
:: define macros

::define a Line Feed (newline) string (normally only used as !LF!)
set LF=^


::Above 2 blank lines are required - do not remove

::define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

:: setErr
::: Sets the ERRORLEVEL to 1
set "setErr=(call)"

:: clrErr
::: Sets the ERRORLEVEL to 0
set "clrErr=(call )"


:: sendCmd command
::: sends a command to the controller
set "sendCmd=>&8 echo"


:: getKey ValidKey [ValidKey...]
::: Check for keypress from the controller. Only accept a ValidKey.
::: Token delimiters and poison characters must be quoted.
::: Accept any key if no ValidKey specified.
::: Return result in Key variable. Key is undefined if no valid keypress.
set getKey=%\n%
for %%# in (1 2) do if %%#==2 (%\n%
set key=%\n%
set inKey=%\n%
set keyTest=%\n%
^<^&9 set /p "inKey="%\n%
if defined inKey (%\n%
set inKey=!inKey:~0,-1!%\n%
for %%C in (!args!) do set /a keyTest=1^&if /i !inKey! equ %%~C set key=!inKey!%\n%
)%\n%
if not defined keyTest set key=!inKey!%\n%
) else set args=


:: draw
::: draws the board
set draw=%\n%
cls%\n%
for /l %%Y in (0,1,%height%) do echo(!line%%Y!%\n%
echo Speed=!Difficulty!%\n%
echo Score=!score!


:: test X Y ValueListVar
::: tests if value at coordinates X,Y is within contents of ValueListVar
set test=%\n%
for %%# in (1 2) do if %%#==2 (for /f "tokens=1-3" %%1 in ("!args!") do (%\n%
for %%A in ("!line%%2:~%%1,1!") do if "!%%3:%%~A=!" neq "!%%3!" %clrErr% else %setErr%%\n%
)) else set args=


:: plot X Y ValueVar
::: places contents of ValueVar at coordinates X,Y
set plot=%\n%
for %%# in (1 2) do if %%#==2 (for /f "tokens=1-3" %%1 in ("!args!") do (%\n%
set "part2=!line%%2:~%%1!"%\n%
set "line%%2=!line%%2:~0,%%1!!%%3!!part2:~1!"%\n%
)) else set args=


::--------------------------------------
:: start the game
setlocal enableDelayedExpansion
call :initialize


::--------------------------------------
:: main loop (infinite loop)
for /l %%. in (1 0 1) do (

%=== compute time since last move ===%
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"
if !tDiff! lss 0 set /a tDiff+=24*60*60*100

if !tDiff! geq !delay! (
%=== delay has expired, so time for movement ===%

%=== establish direction ===%
%getKey% "%up%" "%down%" "%left%" "%right%"
for %%K in (!key!) do if "!%%KAxis!" neq "!axis!" (
set /a "xDiff=xDiff%%K, yDiff=yDiff%%K"
set "axis=!%%KAxis!"
)

%=== erase the tail ===%
set "TX=!snakeX:~-2!"
set "TY=!snakeY:~-2!"
set "snakeX=!snakeX:~0,-2!"
set "snakeY=!snakeY:~0,-2!"
%plot% !TX! !TY! space

%=== compute new head location and attempt to move ===%
set /a "X=PX+xDiff, Y=PY+yDiff"
set "X= !X!"
set "Y= !Y!"
set "X=!X:~-2!"
set "Y=!Y:~-2!"
(%test% !X! !Y! playerSpace) && (

%=== move successful ===%

%=== remove the new head location from the empty list ===%
for %%X in ("!X!") do for %%Y in ("!Y!") do set "empty=!empty:#%%~X %%~Y=!"

(%test% !X! !Y! food) && (
%=== moving to food - eat it ===%

%=== restore the tail ===%
%plot% !TX! !TY! body
set "snakeX=!snakeX!!TX!"
set "snakeY=!snakeY!!TY!"

%=== increment score and locate and draw new food ===%
set /a "score+=1, F=(!random!%%(emptyCnt-=1))*6+1"
for %%F in (!F!) do (%plot% !empty:~%%F,5! food)

) || (
%=== moving to empty space ===%

%=== add the former tail position to the empty list ===%
set "empty=!empty!#!TX! !TY!"
)

%=== draw the new head ===%
if defined snakeX (%plot% !PX! !PY! body)
%plot% !X! !Y! head

%=== Add the new head position to the snake strings ===%
set "snakeX=!X!!snakeX!"
set "snakeY=!Y!!snakeY!"
set "PX=!X!"
set "PY=!Y!"

%draw%

) || (

%=== failed move - game over ===%
%plot% !TX! !TY! body
call :spinner !PX! !PY! death
%draw%
echo(
call :ask "Would you like to play again? (Y/N)" Y N
if /i "!key!" equ "N" (
%sendCmd% quit
exit
) else (
call :initialize
)
)

set /a t1=t2
)
)


::-------------------------------------
:ask Prompt ValidKey [Validkey]...
:: Prompt for a keypress.
:: Wait until a ValidKey is pressed and return result in Key variable.
:: Token delimiters, ), and poison characters must be quoted.
%sendCmd% prompt
<nul set /p "=%~1 "
(set validKeys=%*)
(set validKeys=!validKeys:%1=!)
:purge
(%getKey% {purged})
if not defined key goto :purge
:getResponse
(%getKey% !validKeys!)
if not defined key (
if defined inKey %sendCmd% one
goto :getResponse
)
exit /b


::-------------------------------------
:spinner X Y ValueVar
set /a d1=-1000000
for /l %%N in (1 1 5) do for %%C in (%spinner%) do (
call :spinnerDelay
%plot% %1 %2 %%C
%draw%
)
call :spinnerDelay
(%plot% %1 %2 %3)
exit /b


::-------------------------------------
:spinnerDelay
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"
if %dDiff% lss 0 set /a dDiff+=24*60*60*100
if %dDiff% lss %spinnerDelay% goto :spinnerDelay
set /a d1=d2
exit /b


::-------------------------------------
:initialize
cls
color 4
echo =========================
echo SNAKE BY KOZHABAY ALISHER
echo =========================
echo Speed Options:
echo delay
echo # Description (seconds)
echo --- ----------- ---------
echo 1 Sluggard 0.20
echo 2 Crawl 0.15
echo 3 Slow 0.10
echo 4 Normal 0.07
echo 5 Fast 0.05
echo 6 Insane 0.03
echo 0 Unplayable none
echo(
call :ask "Pick a speed (1-6, 0):" 1 2 3 4 5 6 0
set "difficulty=!desc%key%!"
set "delay=!delay%key%!"
echo %key% - %difficulty%
echo(
<nul set /p "=Initializing."
set "axis=X"
set "xDiff=+1"
set "yDiff=+0"
set "empty="
set /a "PX=1, PY=height/2, FX=width/2+1, FY=PY, score=0, emptyCnt=0, t1=-1000000"
set "snakeX= %PX%"
set "snakeY= %PY%"
set "snakeX=%snakeX:~-2%"
set "snakeY=%snakeY:~-2%"
for /l %%Y in (0 1 %height%) do (
<nul set /p "=."
set "line%%Y="
for /l %%X in (0,1,%width%) do (
set "cell="
if %%Y equ 0 set "cell=%bound%"
if %%Y equ %height% set "cell=%bound%"
if %%X equ 0 set "cell=%bound%"
if %%X equ %width% set "cell=%bound%"
if %%X equ %PX% if %%Y equ %PY% set "cell=%head%"
if not defined cell (
set "cell=%space%"
set "eX= %%X"
set "eY= %%Y"
set "empty=!empty!#!eX:~-2! !eY:~-2!"
set /a emptyCnt+=1
)
if %%X equ %FX% if %%Y equ %FY% set "cell=%food%"
set "line%%Y=!line%%Y!!cell!"
)
)
(%draw%)
echo(
echo Movement keys: %up%=up %down%=down %left%=left %right%=right
echo Avoid running into yourself (%body%%body%%head%) or wall (%bound%)
echo Eat food (%food%) to grow.
echo(
call :ask "Press a key to start..."
%sendCmd% go
exit /b


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:controller
:: Detects keypresses and sends the information to the game via a key file.
:: The controller has various modes of input that are activated by commands sent
:: from the game via a cmd file.
::
:: Modes:
::
:: hold - no input, wait for command
::
:: go - continuously get/send key presses
::
:: prompt - send {purged} marker to allow game to purge input buffer, then
:: get/send a single key press and hold
::
:: one - get/send a single key press and hold
::
:: quit - immediately exit the controller process
::
:: As written, this routine incorrectly reports ! as ), but that doesn't matter
:: since we don't need that key. Both <CR> and Enter key are reported as {Enter}.
:: An extra character is appended to the output to preserve any control chars
:: when read by SET /P.

setlocal enableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set "cmd=hold"
set "inCmd="
set "key="
for /l %%. in (1 0 1) do (
if "!cmd!" neq "hold" (
for /f "delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
if not defined key set "key=%%A"
)
set "key=!key:~-1!"
if !key! equ !CR! set "key={Enter}"
)
<&8 set /p "inCmd="
if defined inCmd (
if !inCmd! equ quit exit
set "cmd=!inCmd!"
set "inCmd="
)
if defined key (
if "!cmd!" equ "prompt" >&9 (echo {purged}.)
>&9 (echo(!key!.)
if "!cmd!" neq "go" set "cmd=hold"
set "key="
)
)

so that first code running, and after running game. its clear?))))
Can you help me?

programmer
Posts: 4
Joined: 11 Nov 2013 22:32

Re: Hello! I want to steal the credit for a Snake game!

#7 Post by programmer » 12 Nov 2013 22:50

I know that I have taken this code on your forum, and so I applied it to you!
Will you help me?

programmer
Posts: 4
Joined: 11 Nov 2013 22:32

Re: Hello! I want to steal the credit for a Snake game!

#8 Post by programmer » 12 Nov 2013 23:15

I understand your aggression but that code is openly available
I did not write this code on all the forums and do not say that I wrote this code!
and so I turned to you! And I am grateful to the author because he helps many students and do not see the harm. Sorry for my bad English!

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Hello! I want to steal the credit for a Snake game!

#9 Post by ShadowThief » 12 Nov 2013 23:20

programmer wrote:I understand your aggression but that code is openly available
I did not write this code on all the forums and do not say that I wrote this code!
and so I turned to you! And I am grateful to the author because he helps many students and do not see the harm. Sorry for my bad English!

You say that you don't say that you wrote the code, and then your code includes the line

Code: Select all

echo SNAKE BY KOZHABAY ALISHER



That is where you say you wrote the code. Kozhabay Alisher did not write this code. Kozhabay Alisher is not a good enough programmer to even understand the code.

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

Re: Hello! I want to steal the credit for a Snake game!

#10 Post by Squashman » 13 Nov 2013 10:27

Yes the code is freely available but plagiarism is not allowed in all the schools I have ever been in. No different than writing a paper. You need to quote your sources.

programmer
Posts: 4
Joined: 11 Nov 2013 22:32

Re: Hello! I want to steal the credit for a Snake game!

#11 Post by programmer » 14 Nov 2013 12:09

GUYS HELP ME PLEASE!!!

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

Re: Hello! I want to steal the credit for a Snake game!

#12 Post by Squashman » 14 Nov 2013 13:02

programmer wrote:GUYS HELP ME PLEASE!!!

That is probably not going to happen.
closing thread.

Locked