精华内容
下载资源
问答
  • shell 游戏

    2010-08-13 23:13:00
    #!/bin/bash  #shell游戏:贪吃蛇 # #期待各位帮忙测试,如有BUG,请及时告知,谢谢 #转载请保持版权声明完整,十分感谢 # #下载代码: #... #...

    #!/bin/bash   

    #shell游戏:贪吃蛇
    #
    #期待各位帮忙测试,如有BUG,请及时告知,谢谢
    #转载请保持版权声明完整,十分感谢
    #
    #下载代码:
    #http://bitbull.cn/works/snake
    #
    #六关卡屏幕截图:
    #http://bitbull.cn/pic/blog/2005072801.jpg
    #http://bitbull.cn/pic/blog/2005072802.jpg
    #http://bitbull.cn/pic/blog/2005072803.jpg
    #http://bitbull.cn/pic/blog/2005072804.jpg
    #http://bitbull.cn/pic/blog/2005072805.jpg
    #http://bitbull.cn/pic/blog/2005072806.jpg
    #
    #[color=red]由于程序运行过程中会产生己个临时文件,
    #请先确定/tmp目录可写可读..如果权限不够或无此目录,请把游戏
    #cpath="/tmp/snake_ctrl_pid.tmp"
    #dpath="/tmp/snake_disply_pid.tmp"
    #vartmp="/tmp/snake_var_tmpfile.tmp"
    #改到有权限读写的目录[/color]
    #
    #测试环境:
    #RHEL4 AS
    #bash 3.0
    #80x24终端
    #
    #usage:
    #w上 s下 a左 d右
    #p暂停
    #n新游戏
    #q退出
    #
    #规则:
    #吃完屏幕上所有的红色果实即可过关,共有6个关卡.碰到任何障碍蛇都将死亡,玩家有4次生命.
    #
    #代码:

     

    #-------------CopyRight-------------
    #   Name:Snake
    #   Version Number:1.00
    #   Type:game
    #   Language:bash shell
    #   Date:2005-07-28
    #   Author:BitBull
    #   Email:wengjianyi@tom.com
    #------------Environment------------
    #   Terminal: column 80 line 24
    #   Linux 2.6.9 i686
    #   GNU Bash 3.00.15
    #-----------------------------------


    #--------------variable--------------
    #game variable
    level=1
    score=0
    life=3
    length=8
    runtime=0.15
    fruitspare=8

    #game kernel variable
    x=2 #init snake x=2 y=2
    y=2
    direction=0
    shead=1 #snake's head in snake[]
    stail=1 #snake's tail in snake[]
    mappoint=1 #point exactmap[] bottom
    state=on #snake run or stop
    run=off  #if run=on,snake shadow is working
    displaypid=""
    controlpid=""

    #game temp file;if your system's /tmp unwrite or unread, you can change to home
    cpath="/tmp/snake_ctrl_pid.tmp"
    dpath="/tmp/snake_disply_pid.tmp"
    vartmp="/tmp/snake_var_tmpfile.tmp"

    #rename kill sign
    pause=23
    newgame=24
    gameover=25
    gameexit=26
    up=27
    down=28
    left=29
    right=22
    #---------------array---------------                                          
    #init exactmap
    exactmap=()

    #map format: y x HowLong "-- or |" ( 1=| 2=-- )
    map1=("6 14 6 2" "6 50 6 2" "14 14 6 2" "14 50 6 2")
    map2=("2 16 10 1" "2 48 10 1" "7 32 10 1" "7 64 10 1")
    map3=("4 16 24 2" "10 16 24 2" "16 16 24 2" "4 16 11 1")
    map4=("10 4 34 2" "4 20 12 1" "4 40 12 1" "4 60 12 1")
    map5=("5 10 29 2" "15 10 29 2" "5 16 7 1" "7 60 6 1" )
    map6=("8 4 35 2" "2 50 5 1" "10 4 36 2" "11 30 5 1" )

    #where is fruit? format:y x
    fruit1=("14 10" "13 56" "2 40" "3 8" "17 50" "18 76" "14 30" "6 66")
    fruit2=("4 14" "2 40" "14 48" "12 68" "9 30" "18 6" "3 76" "18 78")
    fruit3=("7 14" "18 4" "15 40" "11 24" "5 18" "9 56" "3 76" "17 64")
    fruit4=("11 10" "11 62" "9 38" "9 72" "6 58" "14 26" "17 58" "3 6")
    fruit5=("6 14" "16 14" "3 40" "6 22" "14 58" "12 34" "8 50" "9 62")
    fruit6=("2 52" "7 40" "7 60" "4 70" "11 28" "11 32" "15 22" "17 78" )

    #--------------function--------------
    #draw screen
    function Draw_line () {

     local i=1

     while [ "$i" -le "80" ]
     do
      echo -ne "/33[${1};${i}H*"
      (( i++ ))
     done
    }
    function Draw_row () {

     local i=2

     while [ "$i" -le "22" ]
     do
      echo -ne "/33[${i};${1}H*"
      (( i++ ))
     done
    }
    function Draw_help () {

     echo -ne "/33[7;31m/33[24;1HPlay:w s a d Pause:p Newgame:n Quit:q      -- CopyRight -- 2005-07-28 BitBull --/33[0m" 

    function Screen () {

            echo -ne "/33[37;44m"
            Draw_line 1
            Draw_line 19
            Draw_line 23
            Draw_row 1
            Draw_row 80
            echo -ne "/33[0m"
            Draw_help
    }

    #init
    function Init () {

            stty_save=$(stty -g) #backup stty
            clear
            trap "Game_exit;" 2 15
            stty -echo

            echo -ne "/33[?25l"  #hidden cursor

    #exit
    function Game_exit () {

        kill -9 $displaypid>/dev/null 2>&1 #kill display function

     #restore
     stty $stty_save
     stty echo
     clear
     trap 2 15
     echo -ne "/33[?25h/33[0;0H/33[0m"
     rm -f $cpath $dpath >/dev/null 2>&1

     exit 0
    }

    #draw level score life SnakeLong
    function Draw_ls () {

            echo -ne "/33[31m"
            echo -ne "/33[21;10HLevel=$level         Score=$score        /
     Life=$life        Snake=$length"
            echo -ne "/33[0m"
    }

    #output info to player
    function Info () {
      
     title="$1"
     content="$2"
     greeting="$3"
     
            printf "/33[31m"
            printf "/33[11;20H ------------------------------------------- "
            printf "/33[12;20H|         ======>$title<======           |"
            printf "/33[13;20H|         $content          |"
            printf "/33[14;20H|         ======>$greeting<======           |"
            printf "/33[15;20H ------------------------------------------- "
            printf "/33[0m"

    }

    #square:draw square in screen.you can define X Y COLOR LETTER
    function Square () {

     local color=$1;line=$2;row=$3;pic=$4

     echo -ne "/33[34444;${color}m/33[${line};${row}H${pic}/33[0m"
    }

    #show fruit
    function Show_fruits () {
     
     local red=45;fruitxy=""
     
     for (( i = 0; i < 8; i++ ))
     do
      fruitxy="$(printf "/${fruit%s[$i]}" $level)"
      eval Square $red $fruitxy '@@'
     done
    }

    #exact map:calculate mapXY into exactmap[]
    function Exact_map () {

     local mapin xtmp ytmp long line_row
     
     for (( i = 0; i < 4; i++ ))
     do
      mapin="$(printf "/${map%s[$i]}" $level)"
      xtmp=$(eval echo $mapin|cut -d" " -f2)
                    ytmp=$(eval echo $mapin|cut -d" " -f1)
                    long=$(eval echo $mapin|cut -d" " -f3)
                    line_row=$(eval echo $mapin|cut -d" " -f4)

      exactmap[$mappoint]="$ytmp $xtmp"
      (( mappoint++ ))

      #judge mapline | or --
      if [[ "$line_row" == "1" ]]
      then
       for (( j = 0; j <= long; j++ ))
       do
        (( ytmp++ ))
        exactmap[$mappoint]="$ytmp $xtmp"
        (( mappoint++ ))
       done
      else
       for (( k = 0; k <= long; k++ ))
       do
        (( xtmp += 2 ))
        exactmap[$mappoint]="$ytmp $xtmp"
        (( mappoint++ ))
       done
      fi
     done
    }


    #show map
    function Show_map () {

     local mapxy="";blue=46

     Exact_map

     for (( i = 1; i < mappoint; i++ ))
     do
      eval Square $blue ${exactmap[$i]} '[]'
     done   
    }

    #test snake is ok ?
    function Test_snake () {

    #snake self
     for (( i = 1; i <= length; i++ ))
     do
      if [[ "${snake[$i]}" == "$y $x" ]]
      then Dead
      fi
     done
    #borderline
     if [[ $x -lt 2 || $x -gt 79 || $y -lt 2 || $y -gt 18 ]]
     then Dead
     fi
    #map line
     for (( i = 0; i < mappoint; i++ ))
     do
      if [[ "${exactmap[$i]}" == "$y $x" ]]
      then Dead
      fi
     done
    }

    #eat
    function Eat () {

     local fruitxy="";xyvalue="";nowarray=""

     for (( i = 0; i < 8; i++ ))
     do
      fruitxy="$(printf "/${fruit%s[$i]}" $level)"
      xyvalue="$(eval echo $fruitxy)"

      if [[ "$xyvalue" = "$y $x" ]]
      then
       nowarray="$(printf "fruit%s[$i]=" $level)"
       eval $nowarray""
       (( score++ ))
       (( fruitspare-- ))
       Draw_ls
      fi
     done
     if [[ $fruitspare == 0 ]]
     then Next_level
     fi
    }

    #if snake dead
    function Dead () {

     state=off

     if (( "$life" == "0" ))
     then
      kill -$gameover $controlpid
     else
      (( life-- ))
      Info "SnakeDead" "OH!shit!You are a idiot!" "F**k  You"
      sleep 1  
      New_game
     fi
    }

    #next level
    function Next_level () {

     (( level++ ))
     (( length += 6 ))
     if [[ $level -gt 6 ]]
     then
      Info "Well Done" "   WOW!Congratulation!  " "Thank You"
      sleep 4
      kill -$gameexit $controlpid
     else
      Info "Well Done" "Level Update! Go Level $level" ".Loading."
      sleep 3
      New_game
     fi
    }

    #newgame
    function New_game () {
     
     kill -9 $displaypid >/dev/null 2>&1

     if [[ "$1" == "over" ]]
     then
      exec $0
     else
      echo "$level $score $life $length $runtime" > $vartmp
      exec $0 display
     fi
    }

    #game over
    function Game_over () {

     local y_n

     Info "Game Over" "Do you want replay?<y/n>" "Thank You"

     while read -s -n 1 y_n
     do
      case $y_n in
      [yY] ) New_game over
      ;;
      [nN] ) Game_exit
      ;;
      * ) continue
      ;;
      esac
     done
    }


    #main
    function Main () {
     
     local green=42;count=0
     
     case $direction in
     "$up" ) (( y-- ))
     ;;
     "$down" ) (( y++ ))
     ;;
     "$left" ) (( x -= 2 ))
     ;;
     "$right" ) (( x += 2 ))
     ;;
     *):
     ;;
     esac 
     Test_snake
     Eat

     #go go go
     Square $green $y $x /#/#
     snake[$shead]="$y $x"
     (( shead++ ))
     
     if [[ "$shead" == "$length" ]]
     then
      shead=1
      run=on #snake shadow run
     fi
     
     #snake shadow,it can erase snake's tail,otherwise,snake will very long!
     if [[ "$run" == "on" ]]
     then
      Square 0 ${snake[$stail]} "  "
      (( stail++ ))
      if [[ "$stail" == "$length" ]]
      then
       stail=1
      fi
     fi
    }

    #state change:off=snake stop.on=snake run
    function State_change () {
     if [[ $state == "on" ]]
     then state=off
     else state=on
     fi
    }
    #display
    function Display () {

     trap "State_change;" $pause
     trap "direction=$up;" $up
     trap "direction=$down;" $down
     trap "direction=$left;" $left
     trap "direction=$right;" $right

     echo $$ > $dpath
     read controlpid < $cpath
     if [[ -e $vartmp ]]
     then
      read level score life length runtime< $vartmp
      rm -f $vartmp
     fi

     #drow all
     Init                                                             
     Screen
     Draw_ls
     Show_fruits
     Show_map
     Main
     #game main loop
     while :
     do
      if [[ ( "$state" == "on" ) && ( "$direction" != "0" ) ]]
      then
       Main
       sleep $runtime
      fi
     done
    }


    #control
    function Control () {

     local sign=""

     echo $$ > $cpath
     
     trap "Game_over;" $gameover
     trap "Game_exit;" $gameexit

     while read -s -n 1 key
     do
      
      case $key in
      [wW]) sign="$up"
      ;;
      [sS]) sign="$down"
      ;;
      [aA]) sign="$left"
      ;;
      [dD]) sign="$right"
      ;;
      [pP]) sign="$pause"
      ;;
      [nN]) New_game over
      ;;
      [qQ]) Game_exit
      ;;
      * ) continue 2
      ;;
      esac
      
      eval displaypid=$(cat $dpath)
      kill -$sign $displaypid

     done
    }


    #------------main----------------
    if [[ "$1" == "display" ]]
    then
     Display
     exit
    else
     bash $0 display&
     Control
     exit
    fi

     


    ##################################################################

    ##################################################################

    ##################################################################

    shell游戏:扫雷

    期待各位帮忙测试,如有BUG,请及时告知,谢谢
    转载请保持版权声明完整,十分感谢

    下载代码:
    http://bitbull.cn/works/mine.sh

    屏幕截图:
    *

    运行环境:
    bash
    80x24终端

    usage:
    $chmod 755 mine.sh
    $./mine.sh

    按1-3选择对应难度,4退出

    方向:
    上:w 下:s 左:a 右:d
    标识地雷:
    FLAG:f
    挖:
    DIG:j
    重玩
    NEWGAME:n
    退出
    EXIT:x 

    规则:
    玩家可用方向键移动指针,按j挖雷,若挖到地雷,则游戏结束.否则,游戏将在你所挖方块内显示该方块周围8个方块内所有地雷数.若怀疑某方块为地雷,可按f键进行标识,以避免误挖.挖开地图内所有非雷方块则获胜.

    代码:

    #!/bin/bash
    #-------------CopyRight-------------
    #   Name:Mine Sweeping
    #   Version Number:1.00
    #   Type:game
    #   Language:bash shell
    #   Date:2005-10-26
    #   Author:BitBull
    #   Email:BitBull.cn(at)gmail.com
    #------------Environment------------
    #   Terminal: column 80 line 24
    #   Linux 2.6.9 i686
    #   GNU Bash 3.00.15
    #-----------------------------------

    #---------------Define--------------
    ECHO="echo -ne"
    ESC="/033["

    OK=0
    FALSE=1
    #--------------Variable--------------
    #ANSI ESC action
    FLASH=5
    REV=7

    #color
    NULL=0
    BLACK=30
    RED=31
    GREEN=32
    ORANGE=33
    BLUE=34
    PURPLE=35
    SBLUE=36
    GREY=37

    #back color
    BBLACK=40
    BRED=41
    BGREEN=42
    BORANGE=43
    BBLUE=44
    BPURPLE=45
    BSBLUE=46
    BGREY=47

    MINE='@'
    FLAG='F'
    NUL=' '
    SHADOW='X'

    X=0
    Y=0
    CurX=1 #cur's X
    CurY=1 #cur's Y
    OCurX=1 #old cur's X
    OCurY=1 #old cur's Y
    MCount=0 #count mine
    FCount=0 #count flag
    SCount=0 #count shadow
    MXYp=0 #MXY Array's ptr
    #---------------Array----------------

    #if ${XY[]} == M { mine }
    #if ${XY[]} == F { flag }
    #if ${XY[]} == N { null }
    #if ${XY[]} == S { shadow }
    #if ${XY[]} == [1-8] { tip_num }
    #${XY[]} init in XYInit(i)

    MXY[0]=""

    #--------------Function--------------

    function SttyInit ()
    {
     stty_save=$(stty -g) #backup stty

     clear
     trap "GameExit;" 2 15
     stty -echo

     $ECHO "${ESC}?25l" #hidden cursor
     
     return $OK
    }

    function GameExit ()
    {
     stty $stty_save
     stty echo
     clear
     trap 2 15
     $ECHO "${ESC}?25h${ESC}0;0H${ESC}0m"

     exit $OK
    }

    #print help
    function Help ()
    {
     msg="Move:w s a d Dig:j Flag:f NewGame:n Exit:x   --CopyRight-- -2005-10-28 BitBull--"
     $ECHO "${ESC}${REV};${RED}m${ESC}24;1H${msg}${ESC}${NULL}m"

     return $OK
    }

    #print dialog window in screen
    function PMsg ()
    {
     local title="$1" content="$2" greeting="$3"

            $ECHO "${ESC}${RED}m"
            $ECHO "${ESC}11;20H ------------------------------------------- "
            $ECHO "${ESC}12;20H|         ======>$title<======           |"
            $ECHO "${ESC}13;20H|         $content          |"
            $ECHO "${ESC}14;20H|         ======>$greeting<======           |"
            $ECHO "${ESC}15;20H ------------------------------------------- "
            $ECHO "${ESC}${NULL}m"

     return $OK
    }

    #print menu and player choose level,then ${X,Y,MCount,FCount,SCount} init
    function Menu ()
    {
     local key

     $ECHO "${ESC}6;1H${ESC}${RED}m"
    cat<<MENUEND
                           +++++++++++++++++++++++++++++
                           +        (1) Easy           +
                           +        (2) Normal         +
                           +        (3) Hardly         +
                           +        (4) Exit           +
                           +++++++++++++++++++++++++++++
    MENUEND
     $ECHO "${ESC}${NULL}m"

     while read -s -n 1 key
     do
      case $key in
      1) X=10;Y=10;MCount=10;FCount=10;SCount=100;break
      ;;
      2) X=20;Y=14;MCount=28;FCount=28;SCount=280;break
      ;;
      3) X=36;Y=18;MCount=65;FCount=65;SCount=648;break
      ;;
      4) GameExit
      ;;
      esac
     done

     return $OK

    #receive CurX CurY,put it into XY[CurX+X*(CurY-1))]
    #if $# == 3;write into XY[]
    #if $# == 2;read from XY[]
    function XYFormat ()
    {
     local XTmp=$1 YTmp=$2

     if [[ $# -eq 3 ]]
     then XY[$XTmp+$X*($YTmp-1)]=$3
     else echo ${XY[$XTmp+$X*($YTmp-1)]}
     fi 
     
     return $OK
    }

    function DrawInit ()
    {
     local DIline DIline2

     DIline=$( for (( i=1; i<$((X*2)); i++ )) do $ECHO '-';done )
     DIline2=$( for (( i=0; i<X; i++ )) do $ECHO "|${ESC}${SBLUE}mX${ESC}${NULL}m";done )

     clear
     Help
     
     $ECHO "${ESC}1;1H+${DIline}+"
     for (( i=0; i<Y; i++ ))
     do
      $ECHO "${ESC}$((i+2));1H${DIline2}|"
     done
     $ECHO "${ESC}$((Y+2));1H+${DIline}+"

     return $OK
    }

    #${XY[*]}=S
    function XYInit ()
    {
     for (( i=1; i<=$X; i++ ))
     do
      for (( j=1; j<=$Y; j++ ))
      do
       XYFormat $i $j S
      done
     done
     return $OK
    }

    #check X Y
    function CheckXY ()
    {
     local XYTmp="$1 $2"

     for(( i=0; i<MXYp; i++ ))
     do
      if [[ "${MXY}" == "$XYTmp" ]]
      then return $FALSE
      fi
     done

     return $OK
    }

    #RANDOM mine's X Y
    function XYRand ()
    {
     local XTmp YTmp

     for(( i=0; i<MCount; i++ ))
     do
      while :
      do
       XTmp=$(( RANDOM % ( X - 1 ) + 1 ))
       YTmp=$(( RANDOM % ( Y - 1 ) + 1 ))
       CheckXY $XTmp $YTmp

       if [[ "$?" == "$OK" ]]
       then
        XYFormat $XTmp $YTmp M
        MXY="$XTmp $YTmp"
        (( ++MXYp ))
        break
       else continue
       fi
      done
     done
     
     return $OK
    }

    #DEBUG
    # print ${XY[*]} into ./mine.tmp
    #you can read mine.tmp to know where is mine,xixi~~:)
    #M is mine
    function DEBUGPXY ()
    {
     rm mine.tmp>/dev/null 2>&1
     for(( i=1; i<=$Y; i++ ))
     do
      for(( j=1; j<=$X; j++))
      do
       $ECHO "$(XYFormat $j $i)">>mine.tmp
      done
      $ECHO "/n">>mine.tmp
     done

     return $OK
    }

    #move cur
    #usage:CurMov [UP|DOWN|LEFT|RIGHT]
    function CurMov ()
    {
     local direction=$1 Xmin=1 Ymin=1 Xmax=$X Ymax=$Y

     OCurX=$CurX
     OCurY=$CurY

     case $direction in
     "UP") if [[ $CurY -gt $Ymin ]];then (( CurY-- ));fi
     ;;
     "DOWN") if [[ $CurY -lt $Ymax ]];then (( CurY++ ));fi
     ;;
     "LEFT") if [[ $CurX -gt $Xmin ]];then (( CurX-- ));fi
     ;;
     "RIGHT")if [[ $CurX -lt $Xmax ]];then (( CurX++ ));fi
     ;;
     esac

     if [[ $CurX != $OCurX || $CurY != $OCurY ]]
     then DrawPoint $CurX $CurY CUR
     fi

     return $OK
    }

    #display point
    #include cur,flag,mine,shadow,nul,tip [1-8]
    function DrawPoint ()
    {
     local TCurX=$(( $1 * 2 )) TCurY=$(( $2 + 1 )) Type=$3
     local TOCurX=$(( OCurX * 2 )) TOCurY=$(( OCurY + 1 ))
     local colr=0 osign=0 sign=0
     
     case $Type in
     "CUR")
      case $(XYFormat $OCurX $OCurY) in
      F) colr=$PURPLE;osign=$FLAG;;
      N) colr=$NULL;osign=$NUL;;
      [1-8]) colr=$ORANGE;osign=$(XYFormat $OCurX $OCurY);;
      [SM]) colr=$SBLUE;osign=$SHADOW;;
      esac

      case $(XYFormat $CurX $CurY) in
      F)      sign=$FLAG;;
      N)      sign=$NUL;;
      [1-8]) sign=$(XYFormat $CurX $CurY);;
      [SM])     sign=$SHADOW;;
      esac

      $ECHO "${ESC}${colr}m${ESC}${TOCurY};${TOCurX}H${osign}${ESC}${NULL}m"
      $ECHO "${ESC}${REV};${FLASH};${ORANGE}m${ESC}${TCurY};${TCurX}H${sign}${ESC}${NULL}m"
     ;;
     "SHADOW")
      $ECHO "${ESC}${SBLUE}m${ESC}${TCurY};${TCurX}H${SHADOW}${ESC}${NULL}m"
     ;;
     "MINE")
      $ECHO "${ESC}${REV};${RED}m${ESC}${TCurY};${TCurX}H${MINE}${ESC}${NULL}m"
     ;;
     "FLAG")
      $ECHO "${ESC}${TCurY};${TCurX}H${ESC}${PURPLE}m${FLAG}${ESC}${NULL}m"
     ;;
     [1-8])
      $ECHO "${ESC}${TCurY};${TCurX}H${ESC}${ORANGE}m${Type}${ESC}${NULL}m"
     ;;
     "NUL")
      $ECHO "${ESC}${TCurY};${TCurX}H${NUL}"
     esac 

     return $OK
    }

    #check xy
    function Loop ()
    {
     local XYTmp="$1 $2"

     for (( i=0; i<MXYp; i++ ))
     do
      if [[ "$XYTmp" == "${MXY}" ]]
      then $ECHO 1
      fi
     done

     return $OK
    }

    #count around mine
    #A B C
    #D X E
    #F G H
    #return mine's number
    function CountM ()
    {
     local Xmin=1 Ymin=1 Xmax=$X Ymax=$Y minecount=0 n=0
    #A
     if [[ ( $CurX -gt $Xmin ) && ( $CurY -gt $Ymin ) ]]
     then
      n=$( Loop $((CurX-1)) $((CurY-1)) )
      (( minecount += n ))
      n=0
     fi
    #B
     if [[ $CurY -gt $Ymin ]]
     then
      n=$( Loop $CurX $((CurY-1)) )
      (( minecount += n ))
      n=0
     fi
    #C
     if [[ ( $CurX -lt $Xmax ) && ( $CurY -gt $Ymin ) ]]
     then
      n=$( Loop $((CurX+1)) $((CurY-1)) )
      (( minecount += n ))
      n=0
     fi
    #D
     if [[ $CurX -gt $Xmin ]]
     then
      n=$( Loop $((CurX-1)) $CurY )
      (( minecount += n ))
      n=0
     fi
    #E
     if [[ $CurX -lt $Xmax ]]
     then
      n=$( Loop $((CurX+1)) $CurY )
      (( minecount += n ))
      n=0
     fi
    #F
     if [[ ( $CurX -gt $Xmin ) && ( $CurY -lt $Ymax ) ]]
     then
      n=$( Loop $((CurX-1)) $((CurY+1)) )
      (( minecount += n ))
      n=0
     fi
    #G
     if [[ $CurY -lt $Ymax ]]
     then
      n=$( Loop $CurX $((CurY+1)) )
      (( minecount += n ))
      n=0
     fi
    #H
     if [[ ( $CurX -lt $Xmax ) && ( $CurY -lt $Ymax ) ]]
     then
      n=$( Loop $((CurX+1)) $((CurY+1)) )
      (( minecount += n ))
      n=0
     fi

     return $minecount
    }

    #dig
    #if mine ,gameover
    #else tip around mine's number
    function Dig ()
    {
     local key minenum=0

     case $(XYFormat $CurX $CurY) in
     M)
      DrawPoint $CurX $CurY MINE
      read -s -n 1 key
      GameOver "Game Over"
     ;;
     S)
      CountM
      minenum=$?
      if [[ $minenum -eq $NULL ]]
      then
       XYFormat $CurX $CurY N
       DrawPoint $CurX $CurY NUL
      else
       XYFormat $CurX $CurY $minenum
       DrawPoint $CurX $CurY $minenum
      fi
     
      (( SCount-- ))
      if [[ $SCount -eq $MCount ]]
      then GameOver "Well Done"
      fi 
     ;;
     esac
     DrawPoint $CurX $CurY CUR

     return $OK
    }

    #draw flag's number
    function DrawFCount ()
    {
     $ECHO "${ESC}22;34H${ESC};${PURPLE}mFLAG=${FCount}  ${ESC}${NULL}m"
    }

    #sign mine
    function Flag ()
    {
     local XYTmp="$CurX $CurY";stat=$FALSE

     case $(XYFormat $CurX $CurY) in
     F)
      for (( i=1; i<MXYp; i++ ))
      do
       if [[ "${MXY}" == "$XYTmp" ]]
       then XYFormat $CurX $CurY M;stat=$OK;break
       fi
      done
      if [[ $stat == $FALSE ]]
      then XYFormat $CurX $CurY S
      fi

      DrawPoint $CurX $CurY SHADOW
      (( FCount++ ))
      DrawFCount
     ;;
     [SM]) 
      if [[ $FCount -eq $NULL ]]
      then return $FALSE
      fi

      DrawPoint $CurX $CurY FLAG
      XYFormat $CurX $CurY F
      (( FCount-- ))
      DrawFCount
     ;;
     esac
     DrawPoint $CurX $CurY CUR

     return $OK
    }

    function GameOver ()
    {
     local key msgtitle=$1

     PMsg "$msgtitle" "Do you want replay?<y/n>" "Thank You"
     while read -s -n 1 key
     do
      case $key in
      [yY]) exec $(dirname $0)/$(basename $0);;
      [nN]) GameExit;;
      *) continue;;
      esac
     done

     return $OK 
    }
     
    #main
    #drawscreen and control
    function Main ()
    {
     local key

     XYInit
     XYRand
    ############################
    # if you enable DEBUGPXY,
    #you can know where is mine
    # DEBUGPXY  #delete this line's #
    #then cat ./mine.tmp
    ############################ 

     DrawPoint $CurX $CurY CUR
     DrawFCount 

     while read -s -n 1 key
     do
      case $key in
      [wW]) CurMov UP;;
      [sS]) CurMov DOWN;;
      [aA]) CurMov LEFT;;
      [dD]) CurMov RIGHT;;
      [jJ]) Dig;;
      [fF]) Flag;;
      [nN]) exec $(dirname $0)/$(basename $0);;
      [xX]) GameExit;;
      esac
     done

     return $OK
    }
    #---------------Main-----------------

    SttyInit
    Menu #X Y MCount FCount SCount OK!
    DrawInit
    Main

     

    展开全文
  • shell游戏:扫雷

    千次阅读 2016-03-30 10:02:14
    shell游戏:扫雷 期待各位帮忙测试,如有BUG,请及时告知,谢谢 转载请保持版权声明完整,十分感谢 下载代码: http://bitbull.cn/works/mine.sh 屏幕截图: * 运行环境: bash 80x24终端 usage: $chm
    http://bbs.chinaunix.net/thread-634271-1-1.html

    shell游戏:扫雷

    期待各位帮忙测试,如有BUG,请及时告知,谢谢
    转载请保持版权声明完整,十分感谢

    下载代码:
    http://bitbull.cn/works/mine.sh

    屏幕截图:
    *

    运行环境:
    bash
    80x24终端

    usage:
    $chmod 755 mine.sh
    $./mine.sh

    按1-3选择对应难度,4退出

    方向:
    上:w 下:s 左:a 右:d
    标识地雷:
    FLAG:f
    挖:
    DIG:j
    重玩
    NEWGAME:n
    退出
    EXIT:x

    规则:
    玩家可用方向键移动指针,按j挖雷,若挖到地雷,则游戏结束.否则,游戏将在你所挖方块内显示该方块周围8个方块内所有地雷数.若怀疑某方块为地雷,可按f键进行标识,以避免误挖.挖开地图内所有非雷方块则获胜.

    代码:
    1. #!/bin/bash
    2. #-------------CopyRight-------------
    3. #   Name:Mine Sweeping
    4. #   Version Number:1.00
    5. #   Type:game
    6. #   Language:bash shell
    7. #   Date:2005-10-26
    8. #   Author:BitBull
    9. #   Email:BitBull.cn(at)gmail.com
    10. #------------Environment------------
    11. #   Terminal: column 80 line 24
    12. #   Linux 2.6.9 i686
    13. #   GNU Bash 3.00.15
    14. #-----------------------------------

    15. #---------------Define--------------
    16. ECHO="echo -ne"
    17. ESC="\033["

    18. OK=0
    19. FALSE=1
    20. #--------------Variable--------------
    21. #ANSI ESC action
    22. FLASH=5
    23. REV=7

    24. #color
    25. NULL=0
    26. BLACK=30
    27. RED=31
    28. GREEN=32
    29. ORANGE=33
    30. BLUE=34
    31. PURPLE=35
    32. SBLUE=36
    33. GREY=37

    34. #back color
    35. BBLACK=40
    36. BRED=41
    37. BGREEN=42
    38. BORANGE=43
    39. BBLUE=44
    40. BPURPLE=45
    41. BSBLUE=46
    42. BGREY=47

    43. MINE='@'
    44. FLAG='F'
    45. NUL=' '
    46. SHADOW='X'

    47. X=0
    48. Y=0
    49. CurX=1 #cur's X
    50. CurY=1 #cur's Y
    51. OCurX=1 #old cur's X
    52. OCurY=1 #old cur's Y
    53. MCount=0 #count mine
    54. FCount=0 #count flag
    55. SCount=0 #count shadow
    56. MXYp=0 #MXY Array's ptr
    57. #---------------Array----------------

    58. #if ${XY[]} == M { mine }
    59. #if ${XY[]} == F { flag }
    60. #if ${XY[]} == N { null }
    61. #if ${XY[]} == S { shadow }
    62. #if ${XY[]} == [1-8] { tip_num }
    63. #${XY[]} init in XYInit(i)

    64. MXY[0]=""

    65. #--------------Function--------------

    66. function SttyInit ()
    67. {
    68.         stty_save=$(stty -g) #backup stty

    69.         clear
    70.         trap "GameExit;" 2 15
    71.         stty -echo

    72.         $ECHO "${ESC}?25l" #hidden cursor
    73.        
    74.         return $OK
    75. }

    76. function GameExit ()
    77. {
    78.         stty $stty_save
    79.         stty echo
    80.         clear
    81.         trap 2 15
    82.         $ECHO "${ESC}?25h${ESC}0;0H${ESC}0m"

    83.         exit $OK
    84. }

    85. #print help
    86. function Help ()
    87. {
    88.         msg="Move:w s a d Dig:j Flag:f NewGame:n Exit:x   --CopyRight-- -2005-10-28 BitBull--"
    89.         $ECHO "${ESC}${REV};${RED}m${ESC}24;1H${msg}${ESC}${NULL}m"

    90.         return $OK
    91. }

    92. #print dialog window in screen
    93. function PMsg ()
    94. {
    95.         local title="$1" content="$2" greeting="$3"

    96.         $ECHO "${ESC}${RED}m"
    97.         $ECHO "${ESC}11;20H ------------------------------------------- "
    98.         $ECHO "${ESC}12;20H|         ======>$title<======           |"
    99.         $ECHO "${ESC}13;20H|         $content          |"
    100.         $ECHO "${ESC}14;20H|         ======>$greeting<======           |"
    101.         $ECHO "${ESC}15;20H ------------------------------------------- "
    102.         $ECHO "${ESC}${NULL}m"

    103.         return $OK
    104. }

    105. #print menu and player choose level,then ${X,Y,MCount,FCount,SCount} init
    106. function Menu ()
    107. {
    108.         local key

    109.         $ECHO "${ESC}6;1H${ESC}${RED}m"
    110. cat<
    111.                        +++++++++++++++++++++++++++++
    112.                        +        (1) Easy           +
    113.                        +        (2) Normal         +
    114.                        +        (3) Hardly         +
    115.                        +        (4) Exit           +
    116.                        +++++++++++++++++++++++++++++
    117. MENUEND
    118.         $ECHO "${ESC}${NULL}m"

    119.         while read -s -n 1 key
    120.         do
    121.                 case $key in
    122.                 1) X=10;Y=10;MCount=10;FCount=10;SCount=100;break
    123.                 ;;
    124.                 2) X=20;Y=14;MCount=28;FCount=28;SCount=280;break
    125.                 ;;
    126.                 3) X=36;Y=18;MCount=65;FCount=65;SCount=648;break
    127.                 ;;
    128.                 4) GameExit
    129.                 ;;
    130.                 esac
    131.         done

    132.         return $OK
    133. }       

    134. #receive CurX CurY,put it into XY[CurX+X*(CurY-1))]
    135. #if $# == 3;write into XY[]
    136. #if $# == 2;read from XY[]
    137. function XYFormat ()
    138. {
    139.         local XTmp=$1 YTmp=$2

    140.         if [[ $# -eq 3 ]]
    141.         then XY[$XTmp+$X*($YTmp-1)]=$3
    142.         else echo ${XY[$XTmp+$X*($YTmp-1)]}
    143.         fi       
    144.        
    145.         return $OK
    146. }

    147. function DrawInit ()
    148. {
    149.         local DIline DIline2

    150.         DIline=$( for (( i=1; i<$((X*2)); i++ )) do $ECHO '-';done )
    151.         DIline2=$( for (( i=0; i

    152.         clear
    153.         Help
    154.        
    155.         $ECHO "${ESC}1;1H+${DIline}+"
    156.         for (( i=0; i
    157.         do
    158.                 $ECHO "${ESC}$((i+2));1H${DIline2}|"
    159.         done
    160.         $ECHO "${ESC}$((Y+2));1H+${DIline}+"

    161.         return $OK
    162. }

    163. #${XY[*]}=S
    164. function XYInit ()
    165. {
    166.         for (( i=1; i<=$X; i++ ))
    167.         do
    168.                 for (( j=1; j<=$Y; j++ ))
    169.                 do
    170.                         XYFormat $i $j S
    171.                 done
    172.         done
    173.         return $OK
    174. }

    175. #check X Y
    176. function CheckXY ()
    177. {
    178.         local XYTmp="$1 $2"

    179.         for(( i=0; i
    180.         do
    181.                 if [[ "${MXY[i]}" == "$XYTmp" ]]
    182.                 then return $FALSE
    183.                 fi
    184.         done

    185.         return $OK
    186. }

    187. #RANDOM mine's X Y
    188. function XYRand ()
    189. {
    190.         local XTmp YTmp

    191.         for(( i=0; i
    192.         do
    193.                 while :
    194.                 do
    195.                         XTmp=$(( RANDOM % ( X - 1 ) + 1 ))
    196.                         YTmp=$(( RANDOM % ( Y - 1 ) + 1 ))
    197.                         CheckXY $XTmp $YTmp

    198.                         if [[ "$?" == "$OK" ]]
    199.                         then
    200.                                 XYFormat $XTmp $YTmp M
    201.                                 MXY[i]="$XTmp $YTmp"
    202.                                 (( ++MXYp ))
    203.                                 break
    204.                         else continue
    205.                         fi
    206.                 done
    207.         done
    208.        
    209.         return $OK
    210. }

    211. #DEBUG
    212. # print ${XY[*]} into ./mine.tmp
    213. #you can read mine.tmp to know where is mine,xixi~~:)
    214. #M is mine
    215. function DEBUGPXY ()
    216. {
    217.         rm mine.tmp>/dev/null 2>&1
    218.         for(( i=1; i<=$Y; i++ ))
    219.         do
    220.                 for(( j=1; j<=$X; j++))
    221.                 do
    222.                         $ECHO "$(XYFormat $j $i)">>mine.tmp
    223.                 done
    224.                 $ECHO "\n">>mine.tmp
    225.         done

    226.         return $OK
    227. }

    228. #move cur
    229. #usage:CurMov [UP|DOWN|LEFT|RIGHT]
    230. function CurMov ()
    231. {
    232.         local direction=$1 Xmin=1 Ymin=1 Xmax=$X Ymax=$Y

    233.         OCurX=$CurX
    234.         OCurY=$CurY

    235.         case $direction        in
    236.         "UP")        if [[ $CurY -gt $Ymin ]];then (( CurY-- ));fi
    237.         ;;
    238.         "DOWN")        if [[ $CurY -lt $Ymax ]];then (( CurY++ ));fi
    239.         ;;
    240.         "LEFT") if [[ $CurX -gt $Xmin ]];then (( CurX-- ));fi
    241.         ;;
    242.         "RIGHT")if [[ $CurX -lt $Xmax ]];then (( CurX++ ));fi
    243.         ;;
    244.         esac

    245.         if [[ $CurX != $OCurX || $CurY != $OCurY ]]
    246.         then DrawPoint $CurX $CurY CUR
    247.         fi

    248.         return $OK
    249. }

    250. #display point
    251. #include cur,flag,mine,shadow,nul,tip [1-8]
    252. function DrawPoint ()
    253. {
    254.         local TCurX=$(( $1 * 2 )) TCurY=$(( $2 + 1 )) Type=$3
    255.         local TOCurX=$(( OCurX * 2 )) TOCurY=$(( OCurY + 1 ))
    256.         local colr=0 osign=0 sign=0
    257.        
    258.         case $Type in
    259.         "CUR")
    260.                 case $(XYFormat $OCurX $OCurY) in
    261.                 F)        colr=$PURPLE;osign=$FLAG;;
    262.                 N)        colr=$NULL;osign=$NUL;;
    263.                 [1-8])        colr=$ORANGE;osign=$(XYFormat $OCurX $OCurY);;
    264.                 [SM])        colr=$SBLUE;osign=$SHADOW;;
    265.                 esac

    266.                 case $(XYFormat $CurX $CurY) in
    267.                 F)      sign=$FLAG;;
    268.                 N)      sign=$NUL;;
    269.                 [1-8])        sign=$(XYFormat $CurX $CurY);;
    270.                 [SM])     sign=$SHADOW;;
    271.                 esac

    272.                 $ECHO "${ESC}${colr}m${ESC}${TOCurY};${TOCurX}H${osign}${ESC}${NULL}m"
    273.                 $ECHO "${ESC}${REV};${FLASH};${ORANGE}m${ESC}${TCurY};${TCurX}H${sign}${ESC}${NULL}m"
    274.         ;;
    275.         "SHADOW")
    276.                 $ECHO "${ESC}${SBLUE}m${ESC}${TCurY};${TCurX}H${SHADOW}${ESC}${NULL}m"
    277.         ;;
    278.         "MINE")
    279.                 $ECHO "${ESC}${REV};${RED}m${ESC}${TCurY};${TCurX}H${MINE}${ESC}${NULL}m"
    280.         ;;
    281.         "FLAG")
    282.                 $ECHO "${ESC}${TCurY};${TCurX}H${ESC}${PURPLE}m${FLAG}${ESC}${NULL}m"
    283.         ;;
    284.         [1-8])
    285.                 $ECHO "${ESC}${TCurY};${TCurX}H${ESC}${ORANGE}m${Type}${ESC}${NULL}m"
    286.         ;;
    287.         "NUL")
    288.                 $ECHO "${ESC}${TCurY};${TCurX}H${NUL}"
    289.         esac       

    290.         return $OK
    291. }

    292. #check xy
    293. function Loop ()
    294. {
    295.         local XYTmp="$1 $2"

    296.         for (( i=0; i
    297.         do
    298.                 if [[ "$XYTmp" == "${MXY[i]}" ]]
    299.                 then $ECHO 1
    300.                 fi
    301.         done

    302.         return $OK
    303. }

    304. #count around mine
    305. #A B C
    306. #D X E
    307. #F G H
    308. #return mine's number
    309. function CountM ()
    310. {
    311.         local Xmin=1 Ymin=1 Xmax=$X Ymax=$Y minecount=0 n=0
    312. #A
    313.         if [[ ( $CurX -gt $Xmin ) && ( $CurY -gt $Ymin ) ]]
    314.         then
    315.                 n=$( Loop $((CurX-1)) $((CurY-1)) )
    316.                 (( minecount += n ))
    317.                 n=0
    318.         fi
    319. #B
    320.         if [[ $CurY -gt $Ymin ]]
    321.         then
    322.                 n=$( Loop $CurX $((CurY-1)) )
    323.                 (( minecount += n ))
    324.                 n=0
    325.         fi
    326. #C
    327.         if [[ ( $CurX -lt $Xmax ) && ( $CurY -gt $Ymin ) ]]
    328.         then
    329.                 n=$( Loop $((CurX+1)) $((CurY-1)) )
    330.                 (( minecount += n ))
    331.                 n=0
    332.         fi
    333. #D
    334.         if [[ $CurX -gt $Xmin ]]
    335.         then
    336.                 n=$( Loop $((CurX-1)) $CurY )
    337.                 (( minecount += n ))
    338.                 n=0
    339.         fi
    340. #E
    341.         if [[ $CurX -lt $Xmax ]]
    342.         then
    343.                 n=$( Loop $((CurX+1)) $CurY )
    344.                 (( minecount += n ))
    345.                 n=0
    346.         fi
    347. #F
    348.         if [[ ( $CurX -gt $Xmin ) && ( $CurY -lt $Ymax ) ]]
    349.         then
    350.                 n=$( Loop $((CurX-1)) $((CurY+1)) )
    351.                 (( minecount += n ))
    352.                 n=0
    353.         fi
    354. #G
    355.         if [[ $CurY -lt $Ymax ]]
    356.         then
    357.                 n=$( Loop $CurX $((CurY+1)) )
    358.                 (( minecount += n ))
    359.                 n=0
    360.         fi
    361. #H
    362.         if [[ ( $CurX -lt $Xmax ) && ( $CurY -lt $Ymax ) ]]
    363.         then
    364.                 n=$( Loop $((CurX+1)) $((CurY+1)) )
    365.                 (( minecount += n ))
    366.                 n=0
    367.         fi

    368.         return $minecount
    369. }

    370. #dig
    371. #if mine ,gameover
    372. #else tip around mine's number
    373. function Dig ()
    374. {
    375.         local key minenum=0

    376.         case $(XYFormat $CurX $CurY) in
    377.         M)
    378.                 DrawPoint $CurX $CurY MINE
    379.                 read -s -n 1 key
    380.                 GameOver "Game Over"
    381.         ;;
    382.         S)
    383.                 CountM
    384.                 minenum=$?
    385.                 if [[ $minenum -eq $NULL ]]
    386.                 then
    387.                         XYFormat $CurX $CurY N
    388.                         DrawPoint $CurX $CurY NUL
    389.                 else
    390.                         XYFormat $CurX $CurY $minenum
    391.                         DrawPoint $CurX $CurY $minenum
    392.                 fi
    393.        
    394.                 (( SCount-- ))
    395.                 if [[ $SCount -eq $MCount ]]
    396.                 then GameOver "Well Done"
    397.                 fi       
    398.         ;;
    399.         esac
    400.         DrawPoint $CurX $CurY CUR

    401.         return $OK
    402. }

    403. #draw flag's number
    404. function DrawFCount ()
    405. {
    406.         $ECHO "${ESC}22;34H${ESC};${PURPLE}mFLAG=${FCount}  ${ESC}${NULL}m"
    407. }

    408. #sign mine
    409. function Flag ()
    410. {
    411.         local XYTmp="$CurX $CurY";stat=$FALSE

    412.         case $(XYFormat $CurX $CurY) in
    413.         F)
    414.                 for (( i=1; i
    415.                 do
    416.                         if [[ "${MXY[i]}" == "$XYTmp" ]]
    417.                         then XYFormat $CurX $CurY M;stat=$OK;break
    418.                         fi
    419.                 done
    420.                 if [[ $stat == $FALSE ]]
    421.                 then XYFormat $CurX $CurY S
    422.                 fi

    423.                 DrawPoint $CurX $CurY SHADOW
    424.                 (( FCount++ ))
    425.                 DrawFCount
    426.         ;;
    427.         [SM])       
    428.                 if [[ $FCount -eq $NULL ]]
    429.                 then return $FALSE
    430.                 fi

    431.                 DrawPoint $CurX $CurY FLAG
    432.                 XYFormat $CurX $CurY F
    433.                 (( FCount-- ))
    434.                 DrawFCount
    435.         ;;
    436.         esac
    437.         DrawPoint $CurX $CurY CUR

    438.         return $OK
    439. }

    440. function GameOver ()
    441. {
    442.         local key msgtitle=$1

    443.         PMsg "$msgtitle" "Do you want replay?" "Thank You"
    444.         while read -s -n 1 key
    445.         do
    446.                 case $key in
    447.                 [yY])        exec $(dirname $0)/$(basename $0);;
    448.                 [nN])        GameExit;;
    449.                 *)        continue;;
    450.                 esac
    451.         done

    452.         return $OK       
    453. }
    454.        
    455. #main
    456. #drawscreen and control
    457. function Main ()
    458. {
    459.         local key

    460.         XYInit
    461.         XYRand
    462. ############################
    463. # if you enable DEBUGPXY,
    464. #you can know where is mine
    465. #        DEBUGPXY  #delete this line's #
    466. #then cat ./mine.tmp
    467. ############################       

    468.         DrawPoint $CurX $CurY CUR
    469.         DrawFCount       

    470.         while read -s -n 1 key
    471.         do
    472.                 case $key in
    473.                 [wW])        CurMov UP;;
    474.                 [sS])        CurMov DOWN;;
    475.                 [aA])        CurMov LEFT;;
    476.                 [dD])        CurMov RIGHT;;
    477.                 [jJ])        Dig;;
    478.                 [fF])        Flag;;
    479.                 [nN])        exec $(dirname $0)/$(basename $0);;
    480.                 [xX])        GameExit;;
    481.                 esac
    482.         done

    483.         return $OK
    484. }
    485. #---------------Main-----------------

    486. SttyInit
    487. Menu #X Y MCount FCount SCount OK!
    488. DrawInit
    489. Main


    ++++++++++++++++++++++++++++++++++++++


    #!/bin/bash
    #-------------CopyRight-------------
    #   Name:Mine Sweeping
    #   Version Number:1.00
    #   Type:game
    #   Language:bash shell
    #   Date:2005-10-26
    #   Author:BitBull
    #   Email:BitBull.cn(at)gmail.com
    #------------Environment------------
    #   Terminal: column 80 line 24
    #   Linux 2.6.9 i686
    #   GNU Bash 3.00.15
    #-----------------------------------

    #---------------Define--------------
    ECHO="echo -ne"
    ESC="\033["

    OK=0
    FALSE=1
    #--------------Variable--------------
    #ANSI ESC action
    FLASH=5
    REV=7

    #color
    NULL=0
    BLACK=30
    RED=31
    GREEN=32
    ORANGE=33
    BLUE=34
    PURPLE=35
    SBLUE=36
    GREY=37

    #back color
    BBLACK=40
    BRED=41
    BGREEN=42
    BORANGE=43
    BBLUE=44
    BPURPLE=45
    BSBLUE=46
    BGREY=47

    MINE='@'
    FLAG='F'
    NUL=' '
    SHADOW='X'

    X=0
    Y=0
    CurX=1 #cur's X
    CurY=1 #cur's Y
    OCurX=1 #old cur's X
    OCurY=1 #old cur's Y
    MCount=0 #count mine
    FCount=0 #count flag
    SCount=0 #count shadow
    MXYp=0 #MXY Array's ptr
    #---------------Array----------------

    #if ${XY[]} == M { mine }
    #if ${XY[]} == F { flag }
    #if ${XY[]} == N { null }
    #if ${XY[]} == S { shadow }
    #if ${XY[]} == [1-8] { tip_num }
    #${XY[]} init in XYInit(i)

    MXY[0]=""

    #--------------Function--------------

    function SttyInit ()
    {
            stty_save=$(stty -g) #backup stty

            clear
            trap "GameExit;" 2 15
            stty -echo

            $ECHO "${ESC}?25l" #hidden cursor
           
            return $OK
    }

    function GameExit ()
    {
            stty $stty_save
            stty echo
            clear
            trap 2 15
            $ECHO "${ESC}?25h${ESC}0;0H${ESC}0m"

            exit $OK
    }

    #print help
    function Help ()
    {
            msg="Move:w s a d Dig:j Flag:f NewGame:n Exit:x   --CopyRight-- -2005-10-28 BitBull--"
            $ECHO "${ESC}${REV};${RED}m${ESC}24;1H${msg}${ESC}${NULL}m"

            return $OK
    }

    #print dialog window in screen
    function PMsg ()
    {
            local title="$1" content="$2" greeting="$3"

            $ECHO "${ESC}${RED}m"
            $ECHO "${ESC}11;20H ------------------------------------------- "
            $ECHO "${ESC}12;20H|         ======>$title<======           |"
            $ECHO "${ESC}13;20H|         $content          |"
            $ECHO "${ESC}14;20H|         ======>$greeting<======           |"
            $ECHO "${ESC}15;20H ------------------------------------------- "
            $ECHO "${ESC}${NULL}m"

            return $OK
    }

    #print menu and player choose level,then ${X,Y,MCount,FCount,SCount} init
    function Menu ()
    {
            local key

            $ECHO "${ESC}6;1H${ESC}${RED}m"
    cat<                        +++++++++++++++++++++++++++++
                           +        (1) Easy           +
                           +        (2) Normal         +
                           +        (3) Hardly         +
                           +        (4) Exit           +
                           +++++++++++++++++++++++++++++
    MENUEND
            $ECHO "${ESC}${NULL}m"

            while read -s -n 1 key
            do
                    case $key in
                    1) X=10;Y=10;MCount=10;FCount=10;SCount=100;break
                    ;;
                    2) X=20;Y=14;MCount=28;FCount=28;SCount=280;break
                    ;;
                    3) X=36;Y=18;MCount=65;FCount=65;SCount=648;break
                    ;;
                    4) GameExit
                    ;;
                    esac
            done

            return $OK
    }       

    #receive CurX CurY,put it into XY[CurX+X*(CurY-1))]
    #if $# == 3;write into XY[]
    #if $# == 2;read from XY[]
    function XYFormat ()
    {
            local XTmp=$1 YTmp=$2

            if [[ $# -eq 3 ]]
            then XY[$XTmp+$X*($YTmp-1)]=$3
            else echo ${XY[$XTmp+$X*($YTmp-1)]}
            fi       
           
            return $OK
    }

    function DrawInit ()
    {
            local DIline DIline2

            DIline=$( for (( i=1; i<$((X*2)); i++ )) do $ECHO '-';done )
            DIline2=$( for (( i=0; i
            clear
            Help
           
            $ECHO "${ESC}1;1H+${DIline}+"
            for (( i=0; i         do
                    $ECHO "${ESC}$((i+2));1H${DIline2}|"
            done
            $ECHO "${ESC}$((Y+2));1H+${DIline}+"

            return $OK
    }

    #${XY[*]}=S
    function XYInit ()
    {
            for (( i=1; i<=$X; i++ ))
            do
                    for (( j=1; j<=$Y; j++ ))
                    do
                            XYFormat $i $j S
                    done
            done
            return $OK
    }

    #check X Y
    function CheckXY ()
    {
            local XYTmp="$1 $2"

            for(( i=0; i         do
                    if [[ "${MXY[i]}" == "$XYTmp" ]]
                    then return $FALSE
                    fi
            done

            return $OK
    }

    #RANDOM mine's X Y
    function XYRand ()
    {
            local XTmp YTmp

            for(( i=0; i         do
                    while :
                    do
                            XTmp=$(( RANDOM % ( X - 1 ) + 1 ))
                            YTmp=$(( RANDOM % ( Y - 1 ) + 1 ))
                            CheckXY $XTmp $YTmp

                            if [[ "$?" == "$OK" ]]
                            then
                                    XYFormat $XTmp $YTmp M
                                    MXY[i]="$XTmp $YTmp"
                                    (( ++MXYp ))
                                    break
                            else continue
                            fi
                    done
            done
           
            return $OK
    }

    #DEBUG
    # print ${XY[*]} into ./mine.tmp
    #you can read mine.tmp to know where is mine,xixi~~:)
    #M is mine
    function DEBUGPXY ()
    {
            rm mine.tmp>/dev/null 2>&1
            for(( i=1; i<=$Y; i++ ))
            do
                    for(( j=1; j<=$X; j++))
                    do
                            $ECHO "$(XYFormat $j $i)">>mine.tmp
                    done
                    $ECHO "\n">>mine.tmp
            done

            return $OK
    }

    #move cur
    #usage:CurMov [UP|DOWN|LEFT|RIGHT]
    function CurMov ()
    {
            local direction=$1 Xmin=1 Ymin=1 Xmax=$X Ymax=$Y

            OCurX=$CurX
            OCurY=$CurY

            case $direction        in
            "UP")        if [[ $CurY -gt $Ymin ]];then (( CurY-- ));fi
            ;;
            "DOWN")        if [[ $CurY -lt $Ymax ]];then (( CurY++ ));fi
            ;;
            "LEFT") if [[ $CurX -gt $Xmin ]];then (( CurX-- ));fi
            ;;
            "RIGHT")if [[ $CurX -lt $Xmax ]];then (( CurX++ ));fi
            ;;
            esac

            if [[ $CurX != $OCurX || $CurY != $OCurY ]]
            then DrawPoint $CurX $CurY CUR
            fi

            return $OK
    }

    #display point
    #include cur,flag,mine,shadow,nul,tip [1-8]
    function DrawPoint ()
    {
            local TCurX=$(( $1 * 2 )) TCurY=$(( $2 + 1 )) Type=$3
            local TOCurX=$(( OCurX * 2 )) TOCurY=$(( OCurY + 1 ))
            local colr=0 osign=0 sign=0
           
            case $Type in
            "CUR")
                    case $(XYFormat $OCurX $OCurY) in
                    F)        colr=$PURPLE;osign=$FLAG;;
                    N)        colr=$NULL;osign=$NUL;;
                    [1-8])        colr=$ORANGE;osign=$(XYFormat $OCurX $OCurY);;
                    [SM])        colr=$SBLUE;osign=$SHADOW;;
                    esac

                    case $(XYFormat $CurX $CurY) in
                    F)      sign=$FLAG;;
                    N)      sign=$NUL;;
                    [1-8])        sign=$(XYFormat $CurX $CurY);;
                    [SM])     sign=$SHADOW;;
                    esac

                    $ECHO "${ESC}${colr}m${ESC}${TOCurY};${TOCurX}H${osign}${ESC}${NULL}m"
                    $ECHO "${ESC}${REV};${FLASH};${ORANGE}m${ESC}${TCurY};${TCurX}H${sign}${ESC}${NULL}m"
            ;;
            "SHADOW")
                    $ECHO "${ESC}${SBLUE}m${ESC}${TCurY};${TCurX}H${SHADOW}${ESC}${NULL}m"
            ;;
            "MINE")
                    $ECHO "${ESC}${REV};${RED}m${ESC}${TCurY};${TCurX}H${MINE}${ESC}${NULL}m"
            ;;
            "FLAG")
                    $ECHO "${ESC}${TCurY};${TCurX}H${ESC}${PURPLE}m${FLAG}${ESC}${NULL}m"
            ;;
            [1-8])
                    $ECHO "${ESC}${TCurY};${TCurX}H${ESC}${ORANGE}m${Type}${ESC}${NULL}m"
            ;;
            "NUL")
                    $ECHO "${ESC}${TCurY};${TCurX}H${NUL}"
            esac       

            return $OK
    }

    #check xy
    function Loop ()
    {
            local XYTmp="$1 $2"

            for (( i=0; i         do
                    if [[ "$XYTmp" == "${MXY[i]}" ]]
                    then $ECHO 1
                    fi
            done

            return $OK
    }

    #count around mine
    #A B C
    #D X E
    #F G H
    #return mine's number
    function CountM ()
    {
            local Xmin=1 Ymin=1 Xmax=$X Ymax=$Y minecount=0 n=0
    #A
            if [[ ( $CurX -gt $Xmin ) && ( $CurY -gt $Ymin ) ]]
            then
                    n=$( Loop $((CurX-1)) $((CurY-1)) )
                    (( minecount += n ))
                    n=0
            fi
    #B
            if [[ $CurY -gt $Ymin ]]
            then
                    n=$( Loop $CurX $((CurY-1)) )
                    (( minecount += n ))
                    n=0
            fi
    #C
            if [[ ( $CurX -lt $Xmax ) && ( $CurY -gt $Ymin ) ]]
            then
                    n=$( Loop $((CurX+1)) $((CurY-1)) )
                    (( minecount += n ))
                    n=0
            fi
    #D
            if [[ $CurX -gt $Xmin ]]
            then
                    n=$( Loop $((CurX-1)) $CurY )
                    (( minecount += n ))
                    n=0
            fi
    #E
            if [[ $CurX -lt $Xmax ]]
            then
                    n=$( Loop $((CurX+1)) $CurY )
                    (( minecount += n ))
                    n=0
            fi
    #F
            if [[ ( $CurX -gt $Xmin ) && ( $CurY -lt $Ymax ) ]]
            then
                    n=$( Loop $((CurX-1)) $((CurY+1)) )
                    (( minecount += n ))
                    n=0
            fi
    #G
            if [[ $CurY -lt $Ymax ]]
            then
                    n=$( Loop $CurX $((CurY+1)) )
                    (( minecount += n ))
                    n=0
            fi
    #H
            if [[ ( $CurX -lt $Xmax ) && ( $CurY -lt $Ymax ) ]]
            then
                    n=$( Loop $((CurX+1)) $((CurY+1)) )
                    (( minecount += n ))
                    n=0
            fi

            return $minecount
    }

    #dig
    #if mine ,gameover
    #else tip around mine's number
    function Dig ()
    {
            local key minenum=0

            case $(XYFormat $CurX $CurY) in
            M)
                    DrawPoint $CurX $CurY MINE
                    read -s -n 1 key
                    GameOver "Game Over"
            ;;
            S)
                    CountM
                    minenum=$?
                    if [[ $minenum -eq $NULL ]]
                    then
                            XYFormat $CurX $CurY N
                            DrawPoint $CurX $CurY NUL
                    else
                            XYFormat $CurX $CurY $minenum
                            DrawPoint $CurX $CurY $minenum
                    fi
           
                    (( SCount-- ))
                    if [[ $SCount -eq $MCount ]]
                    then GameOver "Well Done"
                    fi       
            ;;
            esac
            DrawPoint $CurX $CurY CUR

            return $OK
    }

    #draw flag's number
    function DrawFCount ()
    {
            $ECHO "${ESC}22;34H${ESC};${PURPLE}mFLAG=${FCount}  ${ESC}${NULL}m"
    }

    #sign mine
    function Flag ()
    {
            local XYTmp="$CurX $CurY";stat=$FALSE

            case $(XYFormat $CurX $CurY) in
            F)
                    for (( i=1; i                 do
                            if [[ "${MXY[i]}" == "$XYTmp" ]]
                            then XYFormat $CurX $CurY M;stat=$OK;break
                            fi
                    done
                    if [[ $stat == $FALSE ]]
                    then XYFormat $CurX $CurY S
                    fi

                    DrawPoint $CurX $CurY SHADOW
                    (( FCount++ ))
                    DrawFCount
            ;;
            [SM])       
                    if [[ $FCount -eq $NULL ]]
                    then return $FALSE
                    fi

                    DrawPoint $CurX $CurY FLAG
                    XYFormat $CurX $CurY F
                    (( FCount-- ))
                    DrawFCount
            ;;
            esac
            DrawPoint $CurX $CurY CUR

            return $OK
    }

    function GameOver ()
    {
            local key msgtitle=$1

            PMsg "$msgtitle" "Do you want replay?" "Thank You"
            while read -s -n 1 key
            do
                    case $key in
                    [yY])        exec $(dirname $0)/$(basename $0);;
                    [nN])        GameExit;;
                    *)        continue;;
                    esac
            done

            return $OK       
    }
           
    #main
    #drawscreen and control
    function Main ()
    {
            local key

            XYInit
            XYRand
    ############################
    # if you enable DEBUGPXY,
    #you can know where is mine
    #        DEBUGPXY  #delete this line's #
    #then cat ./mine.tmp
    ############################       

            DrawPoint $CurX $CurY CUR
            DrawFCount       

            while read -s -n 1 key
            do
                    case $key in
                    [wW])        CurMov UP;;
                    [sS])        CurMov DOWN;;
                    [aA])        CurMov LEFT;;
                    [dD])        CurMov RIGHT;;
                    [jJ])        Dig;;
                    [fF])        Flag;;
                    [nN])        exec $(dirname $0)/$(basename $0);;
                    [xX])        GameExit;;
                    esac
            done

            return $OK
    }
    #---------------Main-----------------

    SttyInit
    Menu #X Y MCount FCount SCount OK!
    DrawInit
    Main
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(512) | 评论(0) | 转发(2) |
    给主人留下些什么吧!~~
    评论热议
    展开全文
  • 收集整理的shell游戏

    千次阅读 2017-04-04 10:15:45
    在ubuntu12环境下亲测能够运行的shell游戏脚本,csdn免积分下载,想研究的可以拿回去慢慢看。 扫雷 http://download.csdn.net/detail/andylauren/9803012 俄罗斯方块 ...

    在ubuntu12环境下亲测能够运行的shell游戏脚本,csdn免积分下载,想研究的可以拿回去慢慢看。

    扫雷

    http://download.csdn.net/detail/andylauren/9803012


    俄罗斯方块

    http://download.csdn.net/detail/andylauren/9803011


    猜数字

    http://download.csdn.net/detail/andylauren/9803010



    展开全文
  • 可能是最具美感的shell 游戏--2048.sh

    千次阅读 2019-06-26 17:35:08
    可能是最具美感的shell 游戏–2048.sh github地址

    在这里插入图片描述

    可能是最具美感的shell 游戏–2048.sh

    github地址

    展开全文
  • shell 游戏系列 扫雷

    2013-12-26 15:35:12
    #!/bin/bash #-------------CopyRight------------- # Name:Mine Sweeping # Version Number:1.00 # Type:game # Language:bash shell # Date:2005-10-26 # Author:BitBull # Email:BitBul...
  • Shell 游戏:贪吃蛇

    千次阅读 2014-12-24 15:55:26
    #!/bin/sh ## 区域内部的可活动的范围。此处可修改相应的值 Width=50 # 偶数 Height=15 PadX=10 PadY=5 Speed="2" # 1-9,值越小,速度越快 ## 蛇起始位置 ((SnakeX=PadX+Width/2)) ...((ScoreY=Pa
  • shell 游戏系列 五子棋

    2013-12-26 15:31:39
    #!/bin/bash P1=6;P2=1 P3=20;P4=1 P5=14;P6=48 function Chess_board () { printf "\n\n\n";printf "%32s " " ";printf "\e[33m-----&gt;&gt;...prin
  • #改游戏使用两个进程,一个用于接收输入,一个用于游戏流程和显示界面; #当前者接收到上下左右等按键时,通过向后者发送signal的方式通知后者。 sigRotate=25 sigLeft=26 sigRight=27 sigDown=28 sigAllDown=29 ...
  • Shell游戏-贪吃蛇(牛)

    千次阅读 2011-12-27 20:56:46
    找个时间把这个注释下,可以借机好好学习shell的编程了。在此做个记录 #!/bin/sh ## 区域内部的可活动的范围。此处可修改相应的值 Width=50 # 偶数 Height=15 PadX=10 PadY=5 Speed="2" # 1-9,值越小,...
  • http://bbs.chinaunix.net/thread-3580033-1-1.html  shell游戏收集贴 #!/bin/bash # Tetris Game #APP declaration APP_NAME="${0##*[\\/]}" APP_VERSION="1.0" ...
  • shell写的2048小游戏

    2018-04-28 14:30:15
    shell写的2048小游戏 支持多平台shell写的2048小游戏shell写的2048小游戏
  • shell脚本写一个猜数游戏 最近用shell写的一个猜数游戏,包括4个不同难度,脚本如下: #作者:p_小王 echo '欢迎来到猜数游戏!' while true do read -p '开始/退出(y/n):' x #提示用户输入 if [ -z $x ] #...
  • Shell实现猜数字游戏

    2021-01-20 14:31:04
    本文实例为大家分享了Shell实现猜数字游戏的具体代码,供大家参考,具体内容如下 初学Shell时,需要使用shell完成各种各样的小目标,比如输入一个日期,计算那天是星期几,又或者编写一个小游戏。 首先给大家介绍一...
  • Shell 俄罗斯方块游戏

    2008-05-28 11:30:19
    Shell 下的俄罗斯方块游戏。能把shell用成这样太牛了~!
  • 概述今天主要分享下怎么用shell脚本来实现俄罗斯方块这个小游戏。testris.shtetris是整个游戏的核心,大概500多行,所以这里就不贴脚本了。keytest.shkeytest这个脚本主要是为了获取键盘#!/bin/bash GetKey() { aKey...
  • game-shell, 准备使用 JavaScript shell 进行游戏或者其他交互式演示 游戏外壳用于在JavaScript中创建交互式演示/游戏的通用 shell 。 这将为你提供以下功能:在页面加载时触发的init 事件每个帧或者根据需要调用的...
  • shell之2048小游戏.zip

    2021-01-23 19:47:56
    Linux操作系统下,Shell语言编写2048小游戏
  • shell脚本游戏五子棋

    2019-09-18 06:33:34
    #!/bin/bashP1=6;P2=1P3=20;P4=1P5=14;P6=48 function Chess_board (){printf "\n\n\n";printf "%32s " " ";printf "\e[33m----->>FIVE STONE GAME<<-----\e[0m";...Information:&g...
  • Shell写的贪吃蛇游戏

    2010-12-12 17:33:22
    史上最强shell Shell写的贪吃蛇游戏 超强 游戏键: 上:w, i 下:s, k 左:a, j 右:d, l 退出:q
  • 主要为大家详细介绍了shell实现猜数字小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • 主要为大家详细介绍了Shell脚本实现猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • 11 轻松一刻,玩玩 shell 命令不知不觉已经更新 10 篇文章了,我相信很多人看了都觉得这太基础了吧,还需要说吗?但是再怎么基础的东西,对于新手来说都是需要一步一步慢慢学习的,所以我前面 10 篇文章可谓是手把手...
  • shell不同文件,分步实现俄罗斯方块,每个Shell程序都有详细的注释,可以从简单学习shell编程,里面包含多个shell文件,最后有一个完整的程序可以整体演示。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,364
精华内容 545
关键字:

shell游戏