#1 25 Dec 2014 14:55

apis
Member
Registered: 30 May 2014
Posts: 18

[Batch game] Guess the Number v1.3

A simple game by me. Created in 10 minutes. If you found any bugs, post it.

Guess_the_Number_v1.3.bat
Updated!

@echo off
if exist config.cfg (ren config.cfg config.bat
call config.bat
ren config.bat config.cfg) else (pause)
set "t=Guess the Number"
set "_m=for %%! in ("
set "p=rem "
if "%speak%" equ "on" if exist speech.bat (set "p=taskkill /f /im wscript.exe >nul 2>&1 & call speech.bat ")
set "m_=) do echo; %%~!"
for /f %%i in ('"prompt $H&echo on&for %%j in (1) do rem"') do set "BS=%%i"
set "k=call :scol "
:title
%k%%titleC%
title %t% - by apis
cls
mode 21,12
%_m%
""
"=================="
" %t%"
"=================="
""
"1.Play"
"2.Help"
"3.Config"
"4.Exit"
""
"5.Changes"
%m_%
%p%"guess the number
choice /c 12345c >nul
if %errorlevel% equ 6 (if "%cheat%" equ "" call :rainbow
goto title)
goto s%errorlevel%
:s1
%p%play /r
%k%%s1C%
title %t% - Play
cls
mode 71,8
%_m%
""
"Select the difficulty."
"1.Easy (Starting score: 80, Right score: 60, Wrong score: -10)"
"2.Medium (Starting score: 50, Right score: 40, Wrong score: -15)"
"3.Hard (Starting score: 30, Right score: 40, Wrong score: -20)"
"4.Impossible (Starting score: 10, Right score: 20, Wrong score: -20)"
"5.Custom"
%m_%
%p%"select the difficulty
choice /c 12345 >nul
if %errorlevel% equ 1 set /a sp=80,sc=80,rg=60,wr=10 & %p%easy /r
if %errorlevel% equ 2 set /a sp=50,sc=50,rg=40,wr=15 & %p%medium /r
if %errorlevel% equ 3 set /a sp=30,sc=30,rg=40,wr=20 & %p%hard /r
if %errorlevel% equ 4 set /a sp=10,sc=10,rg=20,wr=20 & %p%impossible /r
if %errorlevel% equ 5 %p%custom /r & goto s1a
:s1d
cls
mode 47,5
%_m%
""
"Set the max number for guessing. (normal:10)"
"Just press enter to go back."
%m_%
%p%"set the max number for guessing. just press enter to go back
set /p max="%BS% %%max%%:" || goto title
if /i %max% geq a goto s1d
if %max% lss 1 %p%"too less" /r & goto s1d
if %max% gtr 30 %p%"too greater" /r & goto s1d
cls
mode 42,5
echo.
echo  Who will starting to guess the number?
%_m%
"1.Player"
"2.Computer"
%m_%
%p%"who will starting to guess the number
choice /c 12 >nul
set /a r=1,al=1
goto p%errorlevel%
:s1a
cls
mode 36,4
%_m%
""
"Set the value for starting score."
%m_%
%p%"set the value for starting score
set /p sp="%BS% Value:" || goto s1a
if /i %sp% geq a goto s1a
set sc=%sp%
:s1b
cls
mode 33,4
%_m%
""
"Set the value for right score."
%m_%
%p%"set the value for right score
set /p rg="%BS% Value:" || goto s1b
if /i %rg% geq a goto s1b
:s1c
cls
%_m%
""
"Set the value for wrong score."
%m_%
%p%"set the value for wrong score
set /p wr="%BS% Value:" || goto s1c
if /i %wr% geq a goto s1c
goto s1d
:s2
%p%help /r
%k%%s2C%
title %t% - Help
cls
mode 62,10
%_m%
""
"This game is very simple, someone select a number"
"between 0 to 10/%%max%%. then the other has to guess"
"what the number is."
"Try to get high score!"
"You has 50/%%starting score%% score at start."
"If you guessed a right number, you get 100/%%right score%%."
"If you guessed a wrong number, you get -10/%%wrong score%%."
"If your score is below 0 you lose."
%m_%
pause>nul
goto title


:s3
if "%yy%" equ "" %p%config /r
set yy=1
%k%%s3C%
title %t% - Configuration
cls
mode 81,22
%_m%
""
"1.Speaking      : %speak%"
"- If speaking is on, and you have speech.bat, the game will speaks some words."
"  Turn off speaking if you want the game run faster."
""
"2.Colors        : %colors%"
"- If colors is on, the screen will drawed with specified colors. Else,"
"  the colors is just white. You can specify your own colors in"
"  'Specify your own colors' option."
"  Turn off colors if you can't see text with colors."
""
"3.Specify your own colors"
""
"4.Save options"
"5.Back"
%m_%
choice /c 12345 >nul
goto sm%errorlevel%
:sm1
if "%speak%" equ "off" (if exist speech.bat (set "speak=on" && set "p=taskkill /f /im wscript.exe >nul 2>&1 & call speech.bat ")) else (set "speak=off" && set "p=rem ")
goto s3
:sm2
if "%colors%" equ "off" (set "colors=on") else (set "colors=off")
goto s3
:sm3
cls
%k%%s3C%
mode 52,15
if not exist cpick.bat (echo.
echo  Sorry, this fuction isn't available
echo  because cpick.bat not found.
echo  There is alternative way to change color:
echo  Save config options. after that, edit
echo  config.cfg values.
pause>nul
goto s3)
%_m%
""
"Select one of options below to specify the color."
""
"a.Menu screen color: %titleC%"
"b.Play screen color: %s1C%"
"c.Help screen color: %s2C%"
"d.Config screen color : %s3C%"
"e.Exit screen color: %s4C%"
"f.Changes screen color: %s5C%"
"g.'While playing' screen color: %pC%"
"h.'Wrong number' screen color: %wrC%"
"i.'Right number' screen color: %rgC%"
"j.Losing screen color: %lsC%"
""
"1.Load default"
"2.Back"
%m_%
choice /c abcdefghij12 >nul
goto cc%errorlevel%
:cc1
call cpick.bat /c
set titleC=%color_%
goto sm3
:cc2
call cpick.bat /c
set s1C=%color_%
goto sm3
:cc3
call cpick.bat /c
set s2C=%color_%
goto sm3
:cc4
call cpick.bat /c
set s3c=%color_%
goto sm3
:cc5
call cpick.bat /c
set s4C=%color_%
goto sm3
:cc6
call cpick.bat /c
set s5C=%color_%
goto sm3
:cc7
call cpick.bat /c
set pC=%color_%
goto sm3
:cc8
call cpick.bat /c
set wrC=%color_%
goto sm3
:cc9
call cpick.bat /c
set rgC=%color_%
goto sm3
:cc10
call cpick.bat /c
set lsC=%color_%
goto sm3
:cc11
call :colordefault
goto sm3
:cc12
goto s3
:sm4
mode 74,7
cls
echo.
echo  Are you really want to save all config options?
echo  You can load back the default settings.
echo  the saved setting will loaded automatically if saved settings is exist.
echo  [Y/N]
choice >nul
if %errorlevel% equ 1 (call :saveopt) else (goto s3)
goto s3
:sm5
set "yy="
goto title
:s4
%p%exit /r
%k%%s4C%
title %t% - Exit
cls
mode 31,4
echo.
echo  Do you really want to exit?
echo  [Y/N]
%p%"do you really want to exit
choice >nul
if %errorlevel% equ 1 exit
goto title
:s5
%p%changes /r
color %s5C%
cls
mode 60,37
%_m%
""
"Changes:"
""
"v1.0"
"===================="
"- Game released, first version"
""
"v1.1"
"===================="
"- Updated the game"
"- Added difficulty settings"
"- Added some checker scripts"
"- Fixed 'Guess a Number' typo"
"- Spaces for set /p at first"
"- Speaking"
""
"v1.2"
"===================="
"- Swapped exit screen color with play screen color."
"- Changing screen size"
"- Fixed some unspeaked texts"
""
"v1.3"
"===================="
"- Added configuration menu"
"- Added cheats!"
"- Edited some scripts."
""
"===================="
"+ Found a bug! Every set /p questions about number"
"  can enter words. Ex:"
"   Set the max number for guessing. (normal:10)"
"   Just press enter to go back."
"   %%max%%:10g"
"  Someone who can fix it will credited in the game. ;)"
"===================="
%m_%
pause >nul
goto title
:p1
call :check
color %pC%
title %t% - Play, player turn
cls
mode 60,30
set /a num=%random% %% %max%
if "%cheat%" equ "1" (echo %num%) else (echo.)
%_m%
"Round %r%"
"Score - Player: %sp% | Computer: %sc%"
""
"Player, your turn."
%m_%
%p%"player, your turn
if %al% equ 1 ping localhost -n 3 >nul
set al=0
echo.
echo  Computer has select a number. Guess it!
%p%"computer has select a number. guess it. the number is
set /p mun="%BS% The number is:" || goto p1
if %mun% gtr %max% goto pw
if %mun% lss 0 goto pw
goto wp
:pw
echo  Please guess a number between 0 and %max%.
%p%"please guess a number between 0 and %max%
pause >nul
goto p1 
:wp
%p%%mun% /r
echo.
ping localhost -n 2 >nul
if %num% equ %mun% (color %rgC%
echo    Right!
%p%right
pause >nul
set /a sp+=%rg%) else (color %wrC%
echo    Wrong! The number is %num%.
%p%"wrong! the number is %num%
pause >nul
set /a sp-=%wr%)
set /a r+=1,al=1
goto p2
:p2
call :check
color %pC%
title %t% - Play, computer turn
cls
mode 49,5
%_m%
""
"Ssshh...! Don't let computer know your number!"
%m_%
%p%"dont let computer know your number. select a number
set /p mun="%BS% Select a number:" || goto p2
if %mun% gtr %max% (echo  Please select a number between 0 and %max%
%p%"please select a number between 0 and %max%
pause >nul
goto p2 )
cls
mode 60,30
set /a num=%random% %% %max%
%_m%
""
"Round %r%"
"Score - Player: %sp% | Computer: %sc%"
""
"Computer, your turn."
%m_%
%p%"computer, your turn
ping localhost -n 3 >nul
echo.
echo  Player has select a number. Guess it!
%p%"player has select a number. guess it. the number is"
set /p "=%BS% The number is:" <nul
ping localhost -n 5 >nul
set /p "=%num%" <nul
%p%%num% /r
echo.
echo.
ping localhost -n 2 >nul
if %num% equ %mun% (color %rgC%
echo    Right!
%p%right
ping localhost -n 3 >nul
set /a sc+=%rg%) else (color %wrC%
echo    Wrong! The number is %mun%
%p%"wrong! the number is %mun%" /r
ping localhost -n 3 >nul
set /a sc-=%wr%)
set /a r+=1
goto p1
:check
set /a r-=1
if %sp% leq 0 goto pl
if %sc% leq 0 goto cl
set /a r+=1
exit /b
:pl
cls
mode 50,6
color %lsC%
title %t% - Player lose!
echo.
echo  Player lose at round %r%!
echo  Computer win at round %r% with score %sc%!
echo.
echo  Press any key to back.
%p%"player lose. press any key to back
pause >nul
goto title
:cl
cls
mode 50,6
color %lsC%
title %t% - Computer lose!
echo.
echo  Computer lose at round %r%!
echo  Player win at round %r% with score %sp%!
echo.
echo  Press any key to back.
%p%"computer lose. press any key to back
pause >nul
goto title
:rainbow
if "%speak%" equ "off" exit /b
%p%"warning: cheat will be enabled" /r
for /l %%# in (1,1,10) do (
%p%"%%~#
for %%a in (1f 2f 3f 4f 5f 6f 7f 8f 9f af bf cf df ef) do (
Color %%a
for /l %%f in (1,1,35000) do rem
))
cls
set cheat=1
color 0f
%p%"warning: cheat has been enabled" /r
exit /b
:saveopt
del /q /f config.cfg >nul 2>&1
setlocal enabledelayedexpansion
echo ::Editing this file may cause error! >> config.cfg
for %%s in (
"speak"
"colors"
"titleC"
"s1C"
"s2C"
"s3C"
"s4C"
"s5C"
"pC"
"wrC"
"rgC"
"lsC"
) do echo set "%%~s=!%%~s!" >> config.cfg
endlocal disabledelayedexpansion
echo    Settings saved!
ping localhost -n 2 >nul
exit /b
:scol
if "%colors%" equ "on" (color %~1) else (color 0f)
exit/b
:default
for %%# in (
"speak=on"
"colors=on"
) do set "%%~#"
:colordefault
for %%# in (
"titleC=0f"
"s1C=0e"
"s2C=0d"
"s3C=0f"
"s4C=09"
"s5C=0f"
"pC=0b"
"wrC=0c"
"rgC=0a"
"lsC=0f"
) do set "%%~#"
exit /b

You must have cpick.bat to change colors.

cpick.bat

@echo off
for /f %%i in ('"prompt $H&echo on&for %%j in (1) do rem"') do set "BS=%%i"
if "%~1" equ "/?" goto _help
set "v1_=%~1"
set "v2_=%~2"
set "v3_=%~3"
if /i "%v1_%" equ "/c" set Opt1_=1
if /i "%v2_%" equ "/c" set Opt1_=1
if /i "%v3_%" equ "/c" set Opt1_=1
if /i "%v1_%" equ "/t" set Opt2_=1
if /i "%v2_%" equ "/t" set Opt2_=1
if /i "%v3_%" equ "/t" set Opt2_=1
if /i "%v1_:~0,2%" equ "/v" (set Opt3_=1
set var_=%v1_:~3%)
if /i "%v2_:~0,2%" equ "/v" (set Opt3_=1
set var_=%v2_:~3%)
if /i "%v3_:~0,2%" equ "/v" (set Opt3_=1
set var_=%v3_:~3%)
if defined Opt2_ goto :_test
goto _pick
:_help
echo.
echo  Syntax %~nx0 [/v:rtn] [/c] [/t]
echo.
echo  /v:rtn - Specifies a variable. If rtn is specified, this variable used to
echo           return the picked color. Else, the picked color will returned
echo           in "color_" variable.
echo  /c     - Allow %~nx0 to control cmd settings. (mode,cls,color,title)
echo  /t     - Means rtn is a color code. %~nx0 will just check the color
echo           codes. If specified color codes can accepted, %~nx0 will set
echo           errorlevel variable to 1.
echo. ^/^?     - Display this help.
echo.
echo  Run %~nx0 without any arguments (or with [rtn]) to start Color Picker.
echo  Color Picker is very useful with batch programs to help picking colors
echo  and use the picked colors.
echo  Example batch file:
echo   @echo off
echo   color 0f
echo   :main
echo   echo Do you want to change screen color?
echo   choice
echo   if "%%errorlevel%%"=="1" (call %~nx0 myColor
echo   color %%myColor%%)
echo   if "%%errorlevel%%"=="2" exit
echo   goto main
exit /b
:_test
if not defined var_ (echo.
echo  Color codes not specified. Specify with /v option.
exit /b)
if defined Opt1_ (cls
color f0)
:_r
set errorlevel=0
if "%var_:~2%" neq "" exit /b %errorlevel%
if /i "%var_%" gtr "ff" exit /b %errorlevel%
if /i "%var_%" lss "00" exit /b %errorlevel%
if /i "%var_:~0,1%" equ "%var_:~1%" exit /b %errorlevel%
if /i "%var_:~1%" equ "" exit /b %errorlevel%
set errorlevel=1
exit /b %errorlevel%
:_pick
if defined Opt1_ (cls
color f0
mode 40,18
title Color Picker)
echo.
echo  Color Picker
echo  ============
echo.
echo  0 = Black       8 = Gray
echo  1 = Blue        9 = Light Blue
echo  2 = Green       A = Light Green
echo  3 = Aqua        B = Light Aqua
echo  4 = Red         C = Light Red
echo  5 = Purple      D = Light Purple
echo  6 = Yellow      E = Light Yellow
echo  7 = White       F = Bright White
echo.
echo  Just press enter to go back.
set /p color_="%BS% Background/Foreground Color:" || exit /b
if "%color_:~2%" neq "" goto _pick
if /i "%color_%" gtr "ff" goto _pick
if /i "%color_%" lss "00" goto _pick
if /i "%color_:~0,1%" equ "%color_:~1%" goto _pick
if /i "%color_:~1%" equ "" goto _pick
if defined Opt1_ color %color_%
echo  Use this color?
echo  [Y/N]
choice>nul
if "%errorlevel%" equ "1" (call :_sub %var_%
exit /b)
goto _pick
:_sub
if defined var_ set %~1=%color_%
exit /b

Also, you can put speech.bat if you want the game speaking.

speech.bat

@echo off
if "%~1" equ "/?" (goto _help) else (goto _speech)
:_help
echo.
echo  Syntax %~nx0 text [/r]
echo.
echo  text  - Text to be speaked.
echo          If text contains spaces, define the text inside quotes. (")
echo  /r    - If /r is specified, %~nx0 will still running before
echo          finished speaking. So, the caller script will paused.
echo. ^/^?    - Display this help.
echo.
echo  Note: %~nx0 can't work on some old OS.
echo  Examples:
echo  %~nx0 reading
echo  %~nx0 waiting /r
echo  %~nx0 "one quote can working for text with spaces
echo  %~nx0 "but two quote needed if /r is used" /r
exit /b
:_speech
set text=%~1
del /f /q %temp%\%~n0.vbs >nul 2>&1
echo set speech = Wscript.CreateObject("SAPI.spVoice") >> "%temp%\%~n0.vbs"
echo speech.speak "%text%" >> "%temp%\%~n0.vbs"
if /i "%~2" equ "/r" (%temp%\%~n0.vbs) else (start %temp%\%~n0.vbs)
exit /b

You may use speech.bat and cpick.bat for your batch files.

Last edited by apis (01 Jan 2015 07:54)

Offline

Board footer

Powered by FluxBB