shell编程–推箱子第一关
废话不多说,直接上代码
#!/bin/bashdeclare -i numdeclare -i sa=(0 0 1 1 1 0 0 0 9 0 0 1 4 1 0 0 0 9 0 0 1 3 1 1 1 1 9 1 1 1 0 0 3 4 1 9 1 4 0 3 2 1 1 1 9 1 1 1 1 3 1 0 0 9 0 0 0 1 4 1 0 0 9 0 0 0 1 1 1 0 0 9)main(){while truedoclearecho \"===================推箱子==================\"echo \"====游戏规则:将星星全部推到终结符处,即为胜利====\"echo \"==w:向上推 s:向下推 a:向左推 d:向右推 输入字符后回车执行==\"showmovenp=0for n in ${!a[@]}doif [ ${a[$n]} -eq 3 ]; thennp=$(( np+1 ))fidoneif [ $np -gt 0 ]; thenecho \"continue\"elsebreakfidoneecho \"you are win!\"}show(){for data in \"${a[@]}\";docase $data in[0])printf \" \"#空白;;[1])printf \"■\"#墙;;[2])printf \"♀\"#人;;[3])printf \"☆\"#箱子;;[4])printf \"◎\"#终点;;[5])printf \"♂\"#人+终点;;[6])printf \"★\"#箱子+终点;;[9])printf \"\\n\";;esacdone}function move(){for i in ${!a[@]}doif [ ${a[$i]} -eq 2 -o ${a[$i]} -eq 5 ]; then#找到自己num=$ibreakfidoneread dircase $dir in\"w\")#上if [ \"${a[num-9]}\" -eq 0 ]; thena[num-9]=2a[num]=0elif [ \"${a[num-9]}\" -eq 3 ]; thenif [ \"${a[num-18]}\" -eq 0 ]; thena[num-9]=2a[num]=0a[num-18]=3elif [ \"${a[num-18]}\" -eq 4 ]; thena[num-9]=2a[num]=0a[num-18]=6fielif [ \"${a[num-9]}\" -eq 4 ]; thena[num-9]=5a[num]=0fi;;\"a\")#左if [ \"${a[num-1]}\" -eq 0 ]; thena[num-1]=2a[num]=0elif [ \"${a[num-1]}\" -eq 3 ]; thenif [ \"${a[num-2]}\" -eq 0 ]; thena[num-1]=2a[num]=0a[num-2]=3elif [ \"${a[num-2]}\" -eq 4 ]; thena[num-1]=2a[num]=0a[num-2]=6fielif [ \"${a[num-1]}\" -eq 4 ]; thena[num-1]=5a[num]=0fi;;\"d\")#右if [ \"${a[num+1]}\" -eq 0 ]; thena[num+1]=2a[num]=0elif [ \"${a[num+1]}\" -eq 3 ]; thenif [ \"${a[num+2]}\" -eq 0 ]; thena[num+1]=2a[num]=0a[num+2]=3elif [ \"${a[num+2]}\" -eq 4 ]; thena[num+1]=2a[num]=0a[num+2]=6fielif [ \"${a[num+1]}\" -eq 4 ]; thena[num+1]=5a[num]=0fi;;\"s\")#下if [ \"${a[num+9]}\" -eq 0 ]; thena[num+9]=2a[num]=0elif [ \"${a[num+9]}\" -eq 3 ]; thenif [ \"${a[num+18]}\" -eq 0 ]; thena[num+9]=2a[num]=0a[num+18]=3elif [ \"${a[num+18]}\" -eq 4 ]; thena[num+9]=2a[num]=0a[num+18]=6fielif [ \"${a[num+9]}\" -eq 4 ]; thena[num+9]=5a[num]=0fi;;esac}main