2023.8.18更新:有人给我建议把它放到github上,于是我就这么做了。项目地址在 https://github.com/Kide-Lee/Bash-Minesweeper ,欢迎各位给我提issue或给我星星哦。
怎么玩
载入脚本后,用WASD键控制光标移动,按空格挖开地块,挖到的数字是地块周围的地雷数量,挖到地雷后游戏失败;
按F标记有地雷的地块,按E表示可能有地雷。已挖开的地块无法被标记。将所有地雷标记完毕后游戏胜利。
按Q键退出游戏。无论如何退出游戏,脚本都会总结扫到雷的数量和本局游戏的时间。
在CentOS 7上运行脚本
CentOS 7上的bash版本太低,无法解释脚本中的某些语法。因此我们需要升级CenOS 7上的bash解释器。具体而言,我们需要依次执行如下命令:
1 2 3 4
| wget http://ftp.gnu.org/gnu/bash/bash-5.2.15.tar.gz tar zxvf bash-5.2.15.tar.gz cd bash-5.2.15 ./configure && make && make install
|
假如电脑上没有C语言编译器,最后一条命令会报错;此时我们只要先yum install gcc,再去执行那条命令就好。
编译结束后,重启电脑以使新bash生效。
最后我们还要在/bin目录下添加新bash的软链接,然后重启,才能使我们的bash命令焕然一新:
1 2 3
| mv /bin/bash /bin/bash.bak ln -s /usr/local/bin/bash /bin/bash reboot
|
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
| #!/bin/bash
width=9 height=9 mineCount=10 mine="#" title="MineSweeper"
function ReadTable() { local x=$2 local y=$3
case "$1" in matrix) local info=("${matrix[@]}") ;; field) local info=("${field[@]}") ;; esac
local result=${info[$(("$y" * "$width" + "$x"))]} if [ -z "$result" ]; then return 0; elif [ "$x" -lt 0 ]; then return 0; elif [ "$y" -lt 0 ]; then return 0; elif [ "$x" -ge $width ]; then return 0; elif [ "$y" -ge $height ]; then return 0; else return "$result"; fi } function WriteTable() { local x=$2 local y=$3 local new=$4
if [ "$x" -lt 0 ]; then return 0; elif [ "$y" -lt 0 ]; then return 0; elif [ "$x" -ge $width ]; then return 0; elif [ "$y" -ge $height ]; then return 0; fi if [ "$1" == "matrix" ]; then matrix["$y" * "$width" + "$x"]=$new elif [ "$1" == "field" ]; then field["$y" * "$width" + "$x"]=$new fi }
function StartGame() {
X=$(("$width" / 2)) Y=$(("$height" / 2)) old_X=$X old_Y=$Y size=$(("$width" * "$height")) tput civis
for ((i = 0; i < "$size"; i++)); do matrix+=("0") done
for ((i = 0; i < "$size"; i++)); do field+=("10") done DrawField
while [ -z "$checkFirst" ]; do DrawFocus $X $Y
IFS_back=$IFS IFS= read -srn1 input IFS=$IFS_back case "$input" in w|W) MoveUp ;; a|A) MoveLeft ;; s|S) MoveDown ;; d|D) MoveRight ;; f|F) MarkMatrix $X $Y 11 ;; e|E) MarkMatrix $X $Y 12 ;; q|Q) return 1 ;; " ") CreateMine CreateNumber CheckMatrix $X $Y local checkFirst="yes" startTime=$(date +%s) ;; esac done }
function DrawField() {
tput clear local fieldWidth=$(("$width" * 3 + 2)) local titleWidth=${#title} if [ "$fieldWidth" -gt "$titleWidth" ]; then tput cup 0 $(("$fieldWidth" / 2 - "$titleWidth" / 2)) echo $title else tput cup 0 0 echo $title fi
echo -n + for ((i = 0; i < "$width"; i++)); do echo -n "---" done echo +
for ((i = 0; i < "$height"; i++)); do echo -n "|" for ((j = 0; j < "$width"; j++)); do echo -n "[ ]" done echo "|" done
echo -n + for ((i = 0; i < "$width"; i++)); do echo -n "---" done echo +
echo "move: WASD check: Space" echo "mark: F question: E quit: Q" }
function CreateMine() {
local safeFieldList=() for x in $X-1 $X $X+1; do for y in $Y-1 $Y $Y+1; do x=$(("$x")) y=$(("$y")) local safeNum=$(("$y" * "$width" + "$x")) if [ $safeNum -ge 0 ] || [ $safeNum -lt "$size" ]; then safeFieldList+=("$safeNum") fi done done
local mineList=() while [ ${#mineList[*]} -lt $mineCount ]; do local random=$(("$RANDOM" % "$size")) local isRepeat=false for i in "${mineList[@]}" "${safeFieldList[@]}"; do if [ $random == "$i" ]; then isRepeat=true break fi done if [ $isRepeat == false ]; then mineList+=("$random") fi done
for i in "${mineList[@]}"; do matrix["$i"]=9 done }
function CreateNumber() { function PutOne() { local x=$1 local y=$2 for i in $x-1 $x $x+1; do for j in $y-1 $y $y+1; do local i=$(("$i")) local j=$(("$j")) ReadTable matrix $i $j tmpNum=$? if [ $tmpNum != 9 ]; then WriteTable matrix $i $j $(("$tmpNum" + 1)) fi done done }
for ((y = 0; y < "$height"; y++)); do for ((x = 0; x < "$width"; x++)); do ReadTable matrix $x $y if [ $? == 9 ]; then PutOne $x $y fi done done }
function PlayGame() { while true; do DrawFocus $X $Y
IFS_back=$IFS IFS= read -srn1 input IFS=$IFS_back case "$input" in w|W) MoveUp ;; a|A) MoveLeft ;; s|S) MoveDown ;; d|D) MoveRight ;; f|F) MarkMatrix $X $Y 11 ;; e|E) MarkMatrix $X $Y 12 ;; q|Q) return 1 ;; " ") CheckMatrix $X $Y if [ $? == 9 ]; then tput bel return 2 fi ;; esac Judging if [ $? == 2 ]; then return 3 fi done }
function CheckCell() { local x=$1 local y=$2 ReadTable field "$x" "$y" local result=$?
if [ $result -ge 10 ]; then ReadTable matrix "$x" "$y" local result=$? WriteTable field "$x" "$y" "$result" Draw "$x" "$y" fi return "$result" }
function CheckMatrix() { local x_1=$1 local y_1=$2
local edgeList=("$x_1" "$y_1") local edgeLength=2 while [ "$edgeLength" -gt 0 ]; do
local x=${edgeList[$edgeLength - 2]} local y=${edgeList[$edgeLength - 1]} unset "edgeList[$edgeLength-2]" unset "edgeList[$edgeLength-1]"
CheckCell "$x" "$y" local result=$? if [ $result == 0 ]; then for i in $x-1 $x $x+1; do for j in $y-1 $y $y+1; do local i=$(("$i")) local j=$(("$j")) ReadTable field $i $j if [ $? -gt 9 ]; then edgeList+=("$i" "$j") fi done done fi
local edgeLength=${#edgeList[@]} done
ReadTable field "$x_1" "$y_1" return $? }
function Draw() { local x=$1 local y=$2
ReadTable field "$x" "$y" local result=$? case "$result" in 0) display=" " ;; 9) display="$(tput setaf 1) ${mine} $(tput sgr0)" ;; 10) display="[ ]" ;; 11) display="$(tput setaf 2)[F]$(tput sgr0)" ;; 12) display="$(tput setaf 3)[?]$(tput sgr0)" ;; *) display="$(tput setaf $((8 - "$result"))) ${result} $(tput sgr0)" ;; esac
tput cup $(("$y" + 2)) $(("$x" * 3 + 1)) echo -n "$(tput sgr0)${display}" }
function DrawFocus() { local x=$1 local y=$2
Draw "$old_X" "$old_Y"
Draw "$x" "$y" tput cup $(("$y" + 2)) $(("$x" * 3 + 1)) echo -n "$(tput rev)${display}"
old_X=$1 old_Y=$2 }
function MarkMatrix() { local x=$1 local y=$2 local mark=$3 ReadTable field "$x" "$y" local fieldChar=$?
if [ $fieldChar == "$mark" ]; then WriteTable field "$x" "$y" 10
elif [ $fieldChar -gt 9 ]; then WriteTable field "$x" "$y" "$mark" fi }
function MoveUp() { Y=$(("$Y" - 1)) if [ $Y -lt 0 ]; then Y=$(("$Y" + "$height")) fi } function MoveDown() { Y=$(("$Y" + 1)) if [ $Y -ge $height ]; then Y=$(("$Y" - "$height")) fi } function MoveLeft() { X=$(("$X" - 1)) if [ $X -lt 0 ]; then X=$(("$X" + "$width")) fi } function MoveRight() { X=$(("$X" + 1)) if [ $X -ge $width ]; then X=$(("$X" - "$width")) fi }
function Judging() { local markNum=0 for i in "${field[@]}"; do if [ "$i" == 10 ] || [ "$i" == 12 ]; then return 1 elif [ "$i" == 11 ]; then markNum=$(("$markNum"+1)) fi done if [ "$markNum" == "$mineCount" ]; then return 2 fi }
function ClearGame() {
endTime=$(date +%s) if [ "$startTime" ]; then time=$(("$endTime" - "$startTime")) else time=0 fi
Draw $X $Y
mineSweeper=0 if [ $result == 1 ]; then for ((i = 0; i < ${#matrix[@]}; i++)); do x=$(("$i" % "$width")) y=$(("$i" / "$height")) if [ "${matrix[$i]}" == 9 ]; then
if [ "${field[$i]}" == 11 ]; then mineSweeper=$(("$mineSweeper" + 1)) Draw "$x" "$y" tput cup $(("$y" + 2)) $(("$x" * 3 + 1)) echo -n "$(tput rev)$(tput setaf 2)[F]$(tput sgr0)"
else Draw "$x" "$y" tput cup $(("$y" + 2)) $(("$x" * 3 + 1)) echo -n "[${mine}]" fi fi done clearInfo="Game Exited.\nYou swept $mineSweeper mines in $time seconds." elif [ $result == 2 ]; then for ((i = 0; i < ${#matrix[@]}; i++)); do x=$(("$i" % "$width")) y=$(("$i" / "$height")) if [ "${matrix[$i]}" == 9 ]; then
if [ "${field[$i]}" == 11 ]; then mineSweeper=$(("$mineSweeper" + 1)) Draw "$x" "$y" tput cup $(("$y" + 2)) $(("$x" * 3 + 1)) echo -n "$(tput rev)$(tput setaf 2) ${mine} $(tput sgr0)"
else Draw "$x" "$y" tput cup $(("$y" + 2)) $(("$x" * 3 + 1)) echo -n "$(tput rev)$(tput setaf 1) ${mine} $(tput sgr0)" fi fi done clearInfo="$(tput setaf 1)The Game Failed. \nYou swept $mineSweeper mines in $time seconds." elif [ $result == 3 ]; then clearInfo="$(tput setaf 2)You Win!\nYou swept all mines in $time seconds." fi
tput cnorm tput cup $(("$height" + 3)) 0 tput el echo -e "$(tput rev)${clearInfo}$(tput sgr0)" }
StartGame result=$? if [ $result == 0 ]; then PlayGame result=$? fi ClearGame
|