当前位置:Linux教程 - Linux综合 - SCO、Linux下的shell五子棋小游戏

SCO、Linux下的shell五子棋小游戏

  偶也尝试用shell编个简单小游戏:代码很笨拙,还不能人机对抗,但必竟能玩了。 sco、Linux测试通过,linux效果更好些。其它系统未测。 代码: #!/bin/sh # Programname: five # Author: CLD # 2004.01 # L() { # 屏幕任意位置 h=$1;l=$2 echo -n "[$h;$l;H" } ## 界面颜色控制变量 white_b="" # 白底黑字 green_b="" # 绿底黑字(移动棋字亮块) blue_b="" # 兰底白字 gray_b="" # 灰底黑字 gray_w="" # 灰底白字 black_w="" # 黑底白字 # 棋盘换色控制变量 brown_b="" # 桔黄底黑棋 brown_w="" # 桔黄底白棋 mage_b="" # 紫底黑棋 mage_w="" # 紫底白棋 lt_blue_b="" # 浅兰底黑棋 lt_blue_w="" # 浅兰底白棋 kkk() { # 棋盘 L 1 22;echo "$gray_b $black_w" L 2 22;echo "$gray_b ┏┯┯┯┯┯┯┯┯┯┯┯┯┯┓ $black_w" L 3 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 4 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 5 22;echo "$gray_b ┠┼┼╋┼┼┼┼┼┼┼╋┼┼┨ $black_w" L 6 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 7 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 8 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 9 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 10 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 11 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 12 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 13 22;echo "$gray_b ┠┼┼╋┼┼┼┼┼┼┼╋┼┼┨ $black_w" L 14 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 15 22;echo "$gray_b ┠┼┼┼┼┼┼┼┼┼┼┼┼┼┨ $black_w" L 16 22;echo "$gray_b ┗┷┷┷┷┷┷┷┷┷┷┷┷┷┛ $black_w" L 17 22;echo "$gray_b $black_w" } getchar() { key=`stty -g` stty -echo stty raw dd if=/dev/tty bs=1 count=1 2>/dev/null stty -raw stty echo stty $key } clear kkk L 5 34;echo " 五 子 棋 " L 10 24;echo "┌────游戏○设定────┐" L 11 24;echo "│ │" L 12 24;echo "│ 1_白先走 2_黑先走 [ ] │" L 13 24;echo "│ │" L 14 24;echo "└─────────────┘" L 12 48;echo -n goo=`getchar` case $goo in 1) go="白";ss=0;sss=0;; 2) go="黑";ss=1;sss=1;; *) clear;exit;; esac by=0;bs=0;bsy=0;hy=0;hs=0;hsy=0 #初始化输赢局数 tput civis help() { L 16 3;echo "┏━━━━━━━┓" L 16 57;echo "┏━━━━━━━┓" L 17 3;echo "┃ ┃"
[1] [2] [3] [4] [5] 下一页 

L 17 57;echo "┃ ┃" L 18 3;echo "┃ ┃ ┃ ┃" L 19 3;echo "┃ 赢盘数: ┃┏━━━━━"$gray_b" m---和为贵 "$black_w"━━━━━┓┃ 赢盘数: ┃" L 20 3;echo "┃ 输盘数: ┃┃ 友 比 ┃┃ 输盘数: ┃" L 21 3;echo "┃ ┃┃ 谊 "$white_b"Press c change color"$black_w" 赛 ┃┃ ┃" L 22 3;echo "┃ ┃┃ 第 第 ┃┃ ┃" L 23 3;echo "┃ 最后成绩: ┃┃ 一 二 ┃┃ 最后成绩: ┃" L 24 3;echo "┗━━━━━━━┛┗━━━━━━━━━━━━━━━━┛┗━━━━━━━┛" } init_(){ # 继续棋局初始化 kkk >abc a1=2 a2=38 b1=52 b2=16 write=$green_b$go$black_w cat abc L $a1 $a2;echo $write # 棋子初始位置 ccc=1 #改变颜色次数 color1=`eXPr substr $gray_b 3 8`;color2=`expr substr $gray_w 3 8` #默认棋子颜色 L 19 13;echo $by;L 19 68;echo $hy L 20 13;echo $bs;L 20 68;echo $hs L 23 15;echo $bsy" ";L 23 70;echo $hsy" " } init_ help ## 棋子移动函数 left(){ ##左移 if [ $a2 -eq 24 ];then a2=24;else a2=`expr $a2 - 2`;fi sort -t"[" -n +1 abc L $a1 $a2;echo $write } right(){ ##右移 if [ $a2 -eq $b1 ];then a2=$b1;else a2=`expr $a2 + 2`;fi sort -t"[" -n +1 abc L $a1 $a2;echo $write } down(){ ##下移 if [ $a1 -eq $b2 ];then a1=$b2;else a1=`expr $a1 + 1`;fi sort -t"[" -n +1 abc L $a1 $a2;echo $write } up(){ ##上移 if [ $a1 -eq 2 ];then a1=2;else a1=`expr $a1 - 1`;fi sort -t"[" -n +1 abc L $a1 $a2;echo $write } sy(){ # 继续棋局输方先走,统计输赢结果 if [ $qs -eq 7 ];then go="黑" #如果白棋赢则下一盘黑先走 run="白" bs=`expr $bs + 0`;by=`expr $by + 1` bsy=`expr $by - $bs` hy=$bs;hs=$by;hsy=`expr $hy - $hs` else go="白" #如果黑棋赢则下一盘白先走 run="黑" hs=`expr $hs + 0`;hy=`expr $hy + 1` hsy=`expr $hy - $hs` by=$hs;bs=$hy;bsy=`expr $by - $bs` fi } sy1(){ # 显示输赢结果,统计棋局,询问是否继续 while true;do L 19 13;echo $by;L 19 68;echo $hy L 20 13;echo $bs;L 20 68;echo $hs L 23 15;echo $bsy" ";L 23 70;echo $hsy" " tput bel;tput bel L 6 28;echo $white_b" "$black_w L 7 28;echo $white_b" "$run$1"!!! "$black_w" "$black_w L 8 28;echo $white_b" 再来一盘否[yn]? "$black_w" "$black_w L 9 28;echo $white_b" "$black_w" "$black_w L 10 30;echo " " key=`getchar` case $key in y) init_ count=`expr $count + 1` L 23 36;echo "第 $count 局" break;; n)
上一页 [1] [2] [3] [4] [5] 下一页 

clear;cat abc L 7 30;echo $blue_b" "$black_w L 8 30;echo $blue_b" "!!!再见!!!" "$black_w" "$black_w L 9 30;echo $blue_b" "$black_w" "$black_w L 10 30;echo $blue_b" "Author: c1l2d3" "$black_w" "$black_w L 11 30;echo $blue_b" "$black_w" "$black_w L 12 32;echo $black_w" "$black_w tput cnorm rm yyy;exit;; *);; esac done } award() #判断输赢函数 { cat abcsed -n 18,"$"pawk -F";" '{print $1,$2,$4}'sed 's/^..//' \ awk '{print $1,$2,substr($3,2,1)}' >yyy # 计算输赢临时文件 grep "$qs$" yyysort -r -n +0 +1awk '$2+=(NR*2)' \ uniq -cawk '{print $1}'grep '[5-9]' >/dev/null 2>&1 if [ $? -eq 0 ];then sy;sy1 "棋赢啦! ";fi # 横连子>=5 grep "$qs$" yyysort -r -n +1awk '$1+=NR' \ uniq -cawk '{print $1}'grep '[5-9]' >/dev/null 2>&1 if [ $? -eq 0 ];then sy;sy1 "棋赢啦! " ;fi # 竖连子>=5 grep "$qs$" yyysort -r -n +0 +1awk '$1=$1+$2/2'sort -r -n \ awk '$2+=(NR*2)'uniq -cawk '{print $1}'grep '[5-9]' >/dev/null 2>&1 if [ $? -eq 0 ];then sy;sy1 "棋赢啦! ";fi # 右斜连子>=5 grep "$qs$" yyysort -r -n +0 +1awk '$1=$1-$2/2'sort -r -n \ awk '$2+=(NR*2)'uniq -cawk '{print $1}'grep '[5-9]' >/dev/null 2>&1 if [ $? -eq 0 ];then sy;sy1 "棋赢啦! ";fi # 左斜连子>=5 } count=1 #局数 while true do L 1 4;echo $white_b" 【执白方】 "$black_w L 4 5;echo $gray_b" a---左移 "$black_w L 6 5;echo $gray_b" d---右移 "$black_w L 8 5;echo $gray_b" w---上移 "$black_w L 10 5;echo $gray_b" s---下移 "$black_w L 12 5;echo $gray_b" p---落子 "$black_w L 14 5;echo $gray_b" ]---投降 "$black_w L 1 62;echo $white_b" 【执黑方】 "$black_w L 4 63;echo $gray_b" 4---左移 "$black_w L 6 63;echo $gray_b" 6---右移 "$black_w L 8 63;echo $gray_b" 8---上移 "$black_w L 10 63;echo $gray_b" 5---下移 "$black_w L 12 63;echo $gray_b" 9---落子 "$black_w L 14 63;echo $gray_b" 0---投降 "$black_w L 22 1;echo -n L 23 36;echo "第 $count 局" cc=`getchar` case $cc in a4) if [ $ss -eq 0 ] && [ "$cc" = "a" ];then left;fi if [ $ss -eq 1 ] && [ "$cc" = "4" ];then left;fi;; d6) if [ $ss -eq 0 ] && [ "$cc" = "d" ];then right;fi if [ $ss -eq 1 ] && [ "$cc" = "6" ];then right;fi;; s5) if [ $ss -eq 0 ] && [ "$cc" = "s" ];then down;fi if [ $ss -eq 1 ] && [ "$cc" = "5" ];then down;fi;; w8) if [ $ss -eq 0 ] && [ "$cc" = "w" ];then up;fi if [ $ss -eq 1 ] && [ "$cc" = "8" ];then up;fi;; m) run="此局和棋! ";sy1;; p9) if [ $ss -eq 0 ] && [ "$cc" = "p" ];then
上一页 [1] [2] [3] [4] [5] 下一页 

sed -n 18,"$"p abcgrep "\[$a1;$a2" 2>&1 if [ $? -eq 0 ];then L 10 32;echo " 此处已有棋...";tput bel;sleep 1 write=$green_b白$black_w sss=0 else mm=`L $a1 $a2`[$color2●$black_w echo $mm>>abc write=$green_b黑$black_w sss=1 fi sort -t"[" -n +1 abc qs=7 award;tput bel fi if [ $ss -eq 1 ] && [ "$cc" = "9" ];then sed -n 18,"$"p abcgrep "\[$a1;$a2" 2>&1 if [ $? -eq 0 ];then L 10 32;echo " 此处已有棋...";tput bel;sleep 1 write=$green_b黑$black_w sss=1 else mm=`L $a1 $a2`"[$color1●$black_w" echo $mm>>abc write=$green_b白$black_w sss=0 fi sort -t"[" -n +1 abc qs=0 award;tput bel fi ss=$sss;; # 落子方不允许再走,等待对方走棋 0]) if [ "$cc" = "]" ];then go="白";run="白";bs=`expr $bs + 1` by=`expr $by + 0` bsy=`expr $by - $bs` hy=$bs;hs=$by;hsy=`expr $hy - $hs` sss=0 else go="黑";run="黑";hs=`expr $hs + 1`;hy=`expr $hy + 0` hsy=`expr $hy - $hs` by=$hs;bs=$hy;bsy=`expr $by - $bs` sss=1 fi ss=$sss #谁投降谁下一局先走 sy1 "棋投降啦";; c) md=$ccc # 变换棋盘颜色 if [ $md -eq 1 ];then def1=`expr substr $gray_b 3 8`;def2=`expr substr $gray_w 3 8` color1=`expr substr $brown_b 3 6`;color2=`expr substr $brown_w 3 6` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc + 1` fi # 桔黄色 if [ $md -eq 2 ];then def1=`expr substr $brown_b 3 6`;def2=`expr substr $brown_w 3 6` color1=`expr substr $lt_blue_b 3 8`;color2=`expr substr $lt_blue_w 3 8` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc + 1` fi # 浅蓝色 if [ $md -eq 3 ];then def1=`expr substr $lt_blue_b 3 8`;def2=`expr substr $lt_blue_w 3 8` color1=`expr substr $mage_b 3 6`;color2=`expr substr $mage_w 3 6` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc - 3` fi # 紫色 if [ $md -eq 0 ];then def1=`expr substr $mage_b 3 6`;def2=`expr substr $mage_w 3 6` color1=`expr substr $gray_b 3 8`;color2=`expr substr $gray_w 3 8` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc + 1` fi # 灰色 L 2 38;echo $write;;
上一页 [1] [2] [3] [4] [5] 下一页 

esac done 图: [myimg]upload/ccc.png[/myimg]

(出处:http://www.sheup.com)


上一页 [1] [2] [3] [4] [5] 

by=`expr $by + 0` bsy=`expr $by - $bs` hy=$bs;hs=$by;hsy=`expr $hy - $hs` sss=0 else go="黑";run="黑";hs=`expr $hs + 1`;hy=`expr $hy + 0` hsy=`expr $hy - $hs` by=$hs;bs=$hy;bsy=`expr $by - $bs` sss=1 fi ss=$sss #谁投降谁下一局先走 sy1 "棋投降啦";; c) md=$ccc # 变换棋盘颜色 if [ $md -eq 1 ];then def1=`expr substr $gray_b 3 8`;def2=`expr substr $gray_w 3 8` color1=`expr substr $brown_b 3 6`;color2=`expr substr $brown_w 3 6` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc + 1` fi # 桔黄色 if [ $md -eq 2 ];then def1=`expr substr $brown_b 3 6`;def2=`expr substr $brown_w 3 6` color1=`expr substr $lt_blue_b 3 8`;color2=`expr substr $lt_blue_w 3 8` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc + 1` fi # 浅蓝色 if [ $md -eq 3 ];then def1=`expr substr $lt_blue_b 3 8`;def2=`expr substr $lt_blue_w 3 8` color1=`expr substr $mage_b 3 6`;color2=`expr substr $mage_w 3 6` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc - 3` fi # 紫色 if [ $md -eq 0 ];then def1=`expr substr $mage_b 3 6`;def2=`expr substr $mage_w 3 6` color1=`expr substr $gray_b 3 8`;color2=`expr substr $gray_w 3 8` sed 's/'$def1'/'$color1'/g' abcsed 's/'$def2'/'$color2'/g' >abc.tmp mv abc.tmp abc cat abc ccc=`expr $ccc + 1` fi # 灰色 L 2 38;echo $write;; esac done
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页 

图: [myimg]upload/ccc.png[/myimg]

(出处:http://www.sheup.com/)


上一页 [1] [2] [3] [4] [5] [6] [7]