#!/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=0FALSE=1#--------------Variable--------------#ANSI ESC actionFLASH=5REV=7#colorNULL=0BLACK=30RED=31GREEN=32ORANGE=33BLUE=34PURPLE=35SBLUE=36GREY=37#back colorBBLACK=40BRED=41BGREEN=42BORANGE=43BBLUE=44BPURPLE=45BSBLUE=46BGREY=47MINE='@'FLAG='F'NUL=' 'SHADOW='X'X=0Y=0CurX=1 #cur's XCurY=1 #cur's YOCurX=1 #old cur's XOCurY=1 #old cur's YMCount=0 #count mineFCount=0 #count flagSCount=0 #count shadowMXYp=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 helpfunction 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 screenfunction 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} initfunction Menu (){ local key $ECHO "${ESC}6;1H${ESC}${RED}m"cat</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 xyfunction Loop (){ local XYTmp="$1 $2" for (( i=0; i " "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 controlfunction 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-----------------SttyInitMenu #X Y MCount FCount SCount OK!DrawInitMain