偶也嘗試用shell編個簡單小游戲:代碼很笨拙,還不能人機對抗,但必竟能玩了。
sco、Linux測試通過,linux效果更好些。其它系統未測。
代碼:
#!/bin/sh
# Programname: five
# Author: CLD
# 2004.01
#
L()
{
# 屏幕任意位置
h=$1;l=$2
echo -n "[$h;$l;H"
}
## 界面顏色控制變量
white_b="[47;30m" # 白底黑字
green_b="[42;30m" # 綠底黑字(移動棋字亮塊)
blue_b="[44;37m" # 蘭底白字
gray_b="[40;30;5m" # 灰底黑字
gray_w="[40;37;5m" # 灰底白字
black_w="[37;40;0m" # 黑底白字
# 棋盤換色控制變量
brown_b="[43;30m" # 桔黃底黑棋
brown_w="[43;37m" # 桔黃底白棋
mage_b="[45;30m" # 紫底黑棋
mage_w="[45;37m" # 紫底白棋
lt_blue_b="[44;30;5m" # 淺蘭底黑棋
lt_blue_w="[44;37;5m" # 淺蘭底白棋
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 "┃ ┃"
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)
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
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;;
esac
done
圖:
[myimg]upload/ccc.png[/myimg]
圖:
[myimg]upload/ccc.png[/myimg]