批处理的简单小游戏 - 猜数字

// 所有原创文章转载请注明作者及链接
//
blackboycpp(AT)gmail.com
// QQ群: 135202158

 

 

出差无聊的时候很多, 最近两天学会了一点点批处理, 写了一个很简单的游戏,主要用了一些条件和跳转语句,呵呵

@echo   off
mode  con: cols = 50  lines = 11
title 猜数字小游戏---by joppY
color 0a
setlocal  enabledelayedexpansion

REM  初始界面
:begin
        
cls
        
echo .
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │        猜 数 字 小 游 戏         │
        
echo      │                                  │
        
echo      │             By joppY             │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .

        
REM  产生随机数,并控制在0~100之间
         set   / a PUZZLE = %random%% %100
        
REM  初始化变量
         set  UserInput = ""
        
set   / a TIMES = 0
        

REM  游戏例程
:game         
        
set   / p UserInput = 请输入0~100之间的一个数字:
        
set   / a TIMES += 1
        
REM  简单检测用户输入是否合法
         echo  %UserInput% | findstr  " [0-9] "  ||  goto  error
        
echo  %UserInput% | findstr  / " [a-z] "  &&  goto  error
        
if  %UserInput% GTR  100   goto  overflow
        
if  %UserInput% LSS  0   goto  underflow
        
        
REM  echo %PUZZLE%
        
        
REM  判断用户输入
         if  %UserInput% GTR %PUZZLE%  goto  toobig
        
if  %UserInput% LSS %PUZZLE%  goto  toosmall
        
if  %UserInput% EQU %PUZZLE%  goto  bingo

        
:toobig
        
cls
        
echo .
        
echo                    第【!TIMES!】次
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │        ∶(    太大了!          │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .
        
goto  game

        
:toosmall
        
cls
        
echo .
        
echo                    第【!TIMES!】次
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │        ∶(    太小了!          │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .
        
goto  game

        
:bingo
        
cls
        
echo .
        
echo                    第【!TIMES!】次
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │      ∶〕Bingo! 你猜中了!      │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .
        color cf
        ping -n 
1   127.1 > nul
        color fc
        ping -n 
1   127.1 > nul
        color cf
        ping -n 
1   127.1 > nul
        color fc
        ping -n 
1   127.1 > nul
        color 0a
        
echo .
        
set   / p UserChoice = 退出请按q , 继续请按其他键 ...
        
if   / I %UserChoice% == goto   end
        
goto  begin

REM  处理异常
:error
        
cls
        
echo .
        
echo                    第【!TIMES!】次
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │      ∶(   拜托 , 请输入数字!    │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .
        
goto  game
:overflow
        
cls
        
echo .
        
echo                    第【!TIMES!】次
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │      ∶(   你给的数>100了!    │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .
        
goto  game
:underflow
        
cls
        
echo .
        
echo                    第【!TIMES!】次
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │      ∶(     你给的数<0了!    │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .
        
goto  game

REM  退出
:end
        
cls
        
echo .
        
echo .
        
echo      ╭─────────────────╮
        
echo      │                                  │
        
echo      │        欢迎下次来玩 , Bye Bye~     │
        
echo      │                                  │
        
echo      ╰─────────────────╯
        
echo .
        ping -n 
2   127.1 > nul
        
exit

 

你可能感兴趣的:(Python/Scripts,Windows)