{"id":160829,"date":"2019-10-28T08:00:18","date_gmt":"2019-10-28T00:00:18","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=160829"},"modified":"2019-10-14T14:57:05","modified_gmt":"2019-10-14T06:57:05","slug":"bash-github","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bash-github.html","title":{"rendered":"\u5982\u4f55\u7528 Bash \u7f16\u5199\u4e00\u4e2a\u626b\u96f7\u6e38\u620f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u5728\u7f16\u7a0b\u6559\u5b66\u65b9\u9762\u4e0d\u662f\u4e13\u5bb6\uff0c\u4f46\u5f53\u6211\u60f3\u66f4\u597d\u638c\u63e1\u67d0\u4e00\u6837\u4e1c\u897f\u65f6\uff0c\u4f1a\u8bd5\u7740\u627e\u51fa\u8ba9\u81ea\u5df1\u4e50\u5728\u5176\u4e2d\u7684\u65b9\u6cd5\u3002\u6bd4\u65b9\u8bf4\uff0c\u5f53\u6211\u60f3\u5728 shell \u7f16\u7a0b\u65b9\u9762\u66f4\u8fdb\u4e00\u6b65\u65f6\uff0c\u6211\u51b3\u5b9a\u7528 Bash \u7f16\u5199\u4e00\u4e2a\u626b\u96f7\u6e38\u620f\u6765\u52a0\u4ee5\u7ec3\u4e60\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6211\u5728\u7f16\u7a0b\u6559\u5b66\u65b9\u9762\u4e0d\u662f\u4e13\u5bb6\uff0c\u4f46\u5f53\u6211\u60f3\u66f4\u597d\u638c\u63e1\u67d0\u4e00\u6837\u4e1c\u897f\u65f6\uff0c\u4f1a\u8bd5\u7740\u627e\u51fa\u8ba9\u81ea\u5df1\u4e50\u5728\u5176\u4e2d\u7684\u65b9\u6cd5\u3002\u6bd4\u65b9\u8bf4\uff0c\u5f53\u6211\u60f3\u5728 shell \u7f16\u7a0b\u65b9\u9762\u66f4\u8fdb\u4e00\u6b65\u65f6\uff0c\u6211\u51b3\u5b9a\u7528 Bash \u7f16\u5199\u4e00\u4e2a\u626b\u96f7\u6e38\u620f\u6765\u52a0\u4ee5\u7ec3\u4e60\u3002<\/p>\n

\u5982\u679c\u4f60\u662f\u4e00\u4e2a\u6709\u7ecf\u9a8c\u7684 Bash \u7a0b\u5e8f\u5458\uff0c\u5e0c\u671b\u5728\u63d0\u9ad8\u6280\u5de7\u7684\u540c\u65f6\u4e50\u5728\u5176\u4e2d\uff0c\u90a3\u4e48\u8bf7\u8ddf\u7740\u6211\u7f16\u5199\u4e00\u4e2a\u4f60\u7684\u8fd0\u884c\u5728\u7ec8\u7aef\u4e2d\u7684\u626b\u96f7\u6e38\u620f\u3002\u5b8c\u6574\u4ee3\u7801\u53ef\u4ee5\u5728\u8fd9\u4e2a GitHub \u5b58\u50a8\u5e93\u4e2d\u627e\u5230\u3002<\/p>\n

\u505a\u597d\u51c6\u5907<\/strong><\/div>\n

\u5728\u6211\u7f16\u5199\u4efb\u4f55\u4ee3\u7801\u4e4b\u524d\uff0c\u6211\u5217\u51fa\u4e86\u8be5\u6e38\u620f\u6240\u5fc5\u987b\u7684\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n

    \n
  1. \u663e\u793a\u96f7\u533a<\/li>\n
  2. \u521b\u5efa\u6e38\u620f\u903b\u8f91<\/li>\n
  3. \u521b\u5efa\u5224\u65ad\u5355\u5143\u683c\u662f\u5426\u53ef\u9009\u7684\u903b\u8f91<\/li>\n
  4. \u8bb0\u5f55\u53ef\u7528\u548c\u5df2\u67e5\u660e\uff08\u5df2\u6392\u96f7\uff09\u5355\u5143\u683c\u7684\u4e2a\u6570<\/li>\n
  5. \u521b\u5efa\u6e38\u620f\u7ed3\u675f\u903b\u8f91<\/li>\n<\/ol>\n
    \u663e\u793a\u96f7\u533a<\/strong><\/div>\n

    \u5728\u626b\u96f7\u4e2d\uff0c\u6e38\u620f\u754c\u9762\u662f\u4e00\u4e2a\u7531 2D \u6570\u7ec4\uff08\u5217\u548c\u884c\uff09\u7ec4\u6210\u7684\u4e0d\u900f\u660e\u5c0f\u65b9\u683c\u3002\u6bcf\u4e00\u683c\u4e0b\u90fd\u6709\u53ef\u80fd\u85cf\u6709\u5730\u96f7\u3002\u73a9\u5bb6\u7684\u4efb\u52a1\u5c31\u662f\u627e\u5230\u90a3\u4e9b\u4e0d\u542b\u96f7\u7684\u65b9\u683c\uff0c\u5e76\u4e14\u5728\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\uff0c\u4e0d\u80fd\u70b9\u5230\u5730\u96f7\u3002\u8fd9\u4e2a Bash \u7248\u672c\u7684\u626b\u96f7\u4f7f\u7528 10x10 \u7684\u77e9\u9635\uff0c\u5b9e\u9645\u903b\u8f91\u5219\u7531\u4e00\u4e2a\u7b80\u5355\u7684 Bash \u6570\u7ec4\u6765\u5b8c\u6210\u3002<\/p>\n

    \u9996\u5148\uff0c\u6211\u5148\u751f\u6210\u4e86\u4e00\u4e9b\u968f\u673a\u6570\u5b57\u3002\u8fd9\u5c06\u662f\u5730\u96f7\u5728\u96f7\u533a\u91cc\u7684\u4f4d\u7f6e\u3002\u63a7\u5236\u5730\u96f7\u7684\u6570\u91cf\uff0c\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u8fd9\u4e48\u505a\u4f1a\u5bb9\u6613\u4e00\u4e9b\u3002\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u903b\u8f91\u53ef\u4ee5\u66f4\u597d\uff0c\u4f46\u6211\u8fd9\u4e48\u505a\uff0c\u662f\u4e3a\u4e86\u8ba9\u6e38\u620f\u5b9e\u73b0\u4fdd\u6301\u7b80\u6d01\uff0c\u5e76\u6709\u6539\u8fdb\u7a7a\u95f4\u3002\uff08\u6211\u7f16\u5199\u8fd9\u4e2a\u6e38\u620f\u7eaf\u5c5e\u5a31\u4e50\uff0c\u4f46\u5982\u679c\u4f60\u80fd\u5c06\u5b83\u4fee\u6539\u7684\u66f4\u597d\uff0c\u6211\u4e5f\u662f\u5f88\u4e50\u610f\u7684\u3002\uff09<\/p>\n

    \u4e0b\u9762\u8fd9\u4e9b\u53d8\u91cf\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u662f\u4e0d\u53d8\u7684\uff0c\u58f0\u660e\u5b83\u4eec\u662f\u4e3a\u4e86\u968f\u673a\u751f\u6210\u6570\u5b57\u3002\u5c31\u50cf\u4e0b\u9762\u7684\u00a0a<\/code>\u00a0-\u00a0g<\/code> \u7684\u53d8\u91cf\uff0c\u5b83\u4eec\u4f1a\u88ab\u7528\u6765\u8ba1\u7b97\u53ef\u6392\u9664\u7684\u5730\u96f7\u7684\u503c\uff1a<\/p>\n

    # \u53d8\u91cf\r\nscore=0 # \u4f1a\u7528\u6765\u5b58\u653e\u6e38\u620f\u5206\u6570\r\n# \u4e0b\u9762\u8fd9\u4e9b\u53d8\u91cf\uff0c\u7528\u6765\u968f\u673a\u751f\u6210\u53ef\u6392\u9664\u5730\u96f7\u7684\u5b9e\u9645\u503c\r\na=\"1 10 -10 -1\"\r\nb=\"-1 0 1\"\r\nc=\"0 1\"\r\nd=\"-1 0 1 -2 -3\"\r\ne=\"1 2 20 21 10 0 -10 -20 -23 -2 -1\"\r\nf=\"1 2 3 35 30 20 22 10 0 -10 -20 -25 -30 -35 -3 -2 -1\"\r\ng=\"1 4 6 9 10 15 20 25 30 -30 -24 -11 -10 -9 -8 -7\"\r\n#\r\n# \u58f0\u660e\r\ndeclare -a room # \u58f0\u660e\u4e00\u4e2a room \u6570\u7ec4\uff0c\u5b83\u7528\u6765\u8868\u793a\u96f7\u533a\u7684\u6bcf\u4e00\u683c\u3002<\/pre>\n

    \u63a5\u4e0b\u6765\uff0c\u6211\u4f1a\u7528\u5217\uff080-9\uff09\u548c\u884c\uff08a-j\uff09\u663e\u793a\u51fa\u6e38\u620f\u754c\u9762\uff0c\u5e76\u4e14\u4f7f\u7528\u4e00\u4e2a 10x10 \u77e9\u9635\u4f5c\u4e3a\u96f7\u533a\u3002\uff08M[10][10]\u00a0\u662f\u4e00\u4e2a\u7d22\u5f15\u4ece 0-99\uff0c\u6709 100 \u4e2a\u503c\u7684\u6570\u7ec4\u3002\uff09 \u5982\u60f3\u4e86\u89e3\u66f4\u591a\u5173\u4e8e Bash \u6570\u7ec4\u7684\u5185\u5bb9\uff0c\u8bf7\u9605\u8bfb\u8fd9\u672c\u4e66\u90a3\u4e9b\u5173\u4e8e Bash \u4f60\u6240\u4e0d\u4e86\u89e3\u7684\u4e8b: Bash \u6570\u7ec4\u7b80\u4ecb\u3002<\/p>\n

    \u521b\u5efa\u4e00\u4e2a\u53eb plough \u7684\u51fd\u6570\uff0c\u6211\u4eec\u5148\u5c06\u6807\u9898\u663e\u793a\u51fa\u6765\uff1a\u4e24\u4e2a\u7a7a\u884c\u3001\u5217\u5934\uff0c\u548c\u4e00\u884c -\uff0c\u4ee5\u793a\u610f\u5f80\u4e0b\u662f\u6e38\u620f\u754c\u9762:<\/p>\n

    printf '\\n\\n'\r\nprintf '%s' \" \u00a0 \u00a0 a \u00a0 b \u00a0 c \u00a0 d \u00a0 e \u00a0 f \u00a0 g \u00a0 h \u00a0 i \u00a0 j\"\r\nprintf '\\n \u00a0 %s\\n' \"-----------------------------------------\"<\/pre>\n

    \u7136\u540e\uff0c\u6211\u521d\u59cb\u5316\u4e00\u4e2a\u8ba1\u6570\u5668\u53d8\u91cf\uff0c\u53eb\u00a0r\uff0c\u5b83\u4f1a\u7528\u6765\u8bb0\u5f55\u5df2\u663e\u793a\u591a\u5c11\u6a2a\u884c\u3002\u6ce8\u610f\uff0c\u7a0d\u540e\u5728\u6e38\u620f\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f1a\u7528\u540c\u4e00\u4e2a\u53d8\u91cf\u00a0r\uff0c\u4f5c\u4e3a\u6211\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002 \u5728\u00a0Bash for \u5faa\u73af\u4e2d\uff0c\u7528\u00a0seq\u00a0\u547d\u4ee4\u4ece 0 \u589e\u52a0\u5230 9\u3002\u6211\u7528\u6570\u5b57\uff08d%\uff09\u5360\u4f4d\uff0c\u6765\u663e\u793a\u884c\u53f7\uff08$row\uff0c\u7531\u00a0seq\u00a0\u5b9a\u4e49\uff09\uff1a<\/p>\n

    r=0 # \u8ba1\u6570\u5668\r\nfor row in $(seq 0 9); do\r\nprintf '%d ' \"$row\" # \u663e\u793a \u884c\u6570 0-9<\/pre>\n

    \u5728\u6211\u4eec\u63a5\u7740\u5f80\u4e0b\u505a\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5230\u73b0\u5728\u90fd\u505a\u4e86\u4ec0\u4e48\u3002\u6211\u4eec\u5148\u6a2a\u7740\u663e\u793a\u00a0[a-j]\u00a0\u7136\u540e\u518d\u5c06\u00a0[0-9]\u00a0\u7684\u884c\u53f7\u663e\u793a\u51fa\u6765\uff0c\u6211\u4eec\u4f1a\u7528\u8fd9\u4e24\u4e2a\u8303\u56f4\uff0c\u6765\u786e\u5b9a\u7528\u6237\u6392\u96f7\u7684\u786e\u5207\u4f4d\u7f6e\u3002<\/p>\n

    \u63a5\u7740\uff0c\u5728\u6bcf\u884c\u4e2d\uff0c\u63d2\u5165\u5217\uff0c\u6240\u4ee5\u662f\u65f6\u5019\u5199\u4e00\u4e2a\u65b0\u7684 for \u5faa\u73af\u4e86\u3002\u8fd9\u4e00\u5faa\u73af\u7ba1\u7406\u7740\u6bcf\u4e00\u5217\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b9e\u9645\u4e0a\u662f\u751f\u6210\u6e38\u620f\u754c\u9762\u7684\u6bcf\u4e00\u683c\u3002\u6211\u6dfb\u52a0\u4e86\u4e00\u4e9b\u8f85\u52a9\u51fd\u6570\uff0c\u4f60\u80fd\u5728\u6e90\u7801\u4e2d\u770b\u5230\u5b83\u7684\u5b8c\u6574\u5b9e\u73b0\u3002 \u5bf9\u6bcf\u4e00\u683c\u6765\u8bf4\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u8ba9\u5b83\u770b\u8d77\u6765\u50cf\u5730\u96f7\u7684\u4e1c\u897f\uff0c\u6240\u4ee5\u6211\u4eec\u5148\u7528\u4e00\u4e2a\u70b9\uff08.\uff09\u6765\u521d\u59cb\u5316\u7a7a\u683c\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u60f3\u6cd5\uff0c\u6211\u4eec\u7528\u7684\u662f\u4e00\u4e2a\u53eb is_null_field \u7684\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u540c\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u5b58\u50a8\u6bcf\u4e00\u683c\u5177\u4f53\u503c\u7684\u6570\u7ec4\uff0c\u8fd9\u513f\u4f1a\u7528\u5230\u4e4b\u524d\u5df2\u5b9a\u4e49\u7684\u5168\u5c40\u6570\u7ec4 room , \u5e76\u7528 \u53d8\u91cf r\u4f5c\u4e3a\u7d22\u5f15\u3002\u968f\u7740 r \u7684\u589e\u52a0\uff0c\u904d\u5386\u6240\u6709\u5355\u5143\u683c\uff0c\u5e76\u968f\u673a\u90e8\u7f72\u5730\u96f7\u3002<\/p>\n

    \u00a0 for col in $(seq 0 9); do\r\n((r+=1)) # \u5faa\u73af\u5b8c\u4e00\u5217\u884c\u6570\u52a0\u4e00\r\nis_null_field $r # \u5047\u8bbe\u8fd9\u91cc\u6709\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u68c0\u67e5\u5355\u5143\u683c\u662f\u5426\u4e3a\u7a7a\uff0c\u4e3a\u771f\uff0c\u5219\u6b64\u5355\u5143\u683c\u521d\u59cb\u503c\u4e3a\u70b9\uff08.\uff09\r\nprintf '%s \\e[33m%s\\e[0m ' \"|\" \"${room[$r]}\" # \u6700\u540e\u663e\u793a\u5206\u9694\u7b26\uff0c\u6ce8\u610f\uff0c${room[$r]} \u7684\u7b2c\u4e00\u4e2a\u503c\u4e3a '.'\uff0c\u7b49\u4e8e\u5176\u521d\u59cb\u503c\u3002\r\n#\u7ed3\u675f col \u5faa\u73af\r\ndone<\/pre>\n

    \u6700\u540e\uff0c\u4e3a\u4e86\u4fdd\u6301\u6e38\u620f\u754c\u9762\u6574\u9f50\u597d\u770b\uff0c\u6211\u4f1a\u5728\u6bcf\u884c\u7528\u4e00\u4e2a\u7ad6\u7ebf\u4f5c\u4e3a\u7ed3\u5c3e\uff0c\u5e76\u5728\u6700\u540e\u7ed3\u675f\u884c\u5faa\u73af\uff1a<\/p>\n

    printf '%s\\n' \"|\" # \u663e\u793a\u51fa\u884c\u5206\u9694\u7b26\r\nprintf ' %s\\n' \"-----------------------------------------\"\r\n# \u7ed3\u675f\u884c\u5faa\u73af\r\ndone\r\nprintf '\\n\\n'<\/pre>\n

    \u5b8c\u6574\u7684\u00a0plough\u00a0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

    plough()\r\n{\r\n\u00a0 r=0\r\n\u00a0 printf '\\n\\n'\r\n\u00a0 printf '%s' \" \u00a0 \u00a0 a \u00a0 b \u00a0 c \u00a0 d \u00a0 e \u00a0 f \u00a0 g \u00a0 h \u00a0 i \u00a0 j\"\r\n\u00a0 printf '\\n \u00a0 %s\\n' \"-----------------------------------------\"\r\n\u00a0 for row in $(seq 0 9); do\r\n\u00a0 \u00a0 printf '%d \u00a0' \"$row\"\r\n\u00a0 \u00a0 for col in $(seq 0 9); do\r\n\u00a0 \u00a0 \u00a0 \u00a0((r+=1))\r\n\u00a0 \u00a0 \u00a0 \u00a0is_null_field $r\r\n\u00a0 \u00a0 \u00a0 \u00a0printf '%s \\e[33m%s\\e[0m ' \"|\" \"${room[$r]}\"\r\n\u00a0 \u00a0 done\r\n\u00a0 \u00a0 printf '%s\\n' \"|\"\r\n\u00a0 \u00a0 printf ' \u00a0 %s\\n' \"-----------------------------------------\"\r\n\u00a0 done\r\n\u00a0 printf '\\n\\n'\r\n}<\/pre>\n

    \u6211\u82b1\u4e86\u70b9\u65f6\u95f4\u6765\u601d\u8003\uff0cis_null_field \u7684\u5177\u4f53\u529f\u80fd\u662f\u4ec0\u4e48\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\uff0c\u5b83\u5230\u5e95\u80fd\u505a\u4e9b\u4ec0\u4e48\u3002\u5728\u6700\u5f00\u59cb\uff0c\u6211\u4eec\u9700\u8981\u6e38\u620f\u6709\u4e00\u4e2a\u56fa\u5b9a\u7684\u72b6\u6001\u3002\u4f60\u53ef\u4ee5\u968f\u4fbf\u9009\u62e9\u4e2a\u521d\u59cb\u503c\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u5b57\u6216\u8005\u4efb\u610f\u5b57\u7b26\u3002\u6211\u6700\u540e\u51b3\u5b9a\uff0c\u6240\u6709\u5355\u5143\u683c\u7684\u521d\u59cb\u503c\u4e3a\u4e00\u4e2a\u70b9\uff08.\uff09\uff0c\u56e0\u4e3a\u6211\u89c9\u5f97\uff0c\u8fd9\u6837\u4f1a\u8ba9\u6e38\u620f\u754c\u9762\u66f4\u597d\u770b\u3002\u4e0b\u9762\u5c31\u662f\u8fd9\u4e00\u51fd\u6570\u7684\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n

    is_null_field()\r\n{\r\nlocal e=$1 # \u5728\u6570\u7ec4 room \u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u7528\u8fc7\u5faa\u73af\u53d8\u91cf 'r' \u4e86\uff0c\u8fd9\u6b21\u6211\u4eec\u7528 'e'\r\nif [[ -z \"${room[$e]}\" ]];then\r\nroom[$r]=\".\" #\u8fd9\u91cc\u7528\u70b9\uff08.\uff09\u6765\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u5355\u5143\u683c\r\nfi\r\n}<\/pre>\n

    \u73b0\u5728\uff0c\u6211\u5df2\u7ecf\u521d\u59cb\u5316\u4e86\u6240\u6709\u7684\u683c\u5b50\uff0c\u73b0\u5728\u53ea\u8981\u7528\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u51fd\u6570\u5c31\u80fd\u5f97\u51fa\u5f53\u524d\u6e38\u620f\u4e2d\u8fd8\u6709\u591a\u5c11\u5355\u5143\u683c\u53ef\u4ee5\u64cd\u4f5c\uff1a<\/p>\n

    get_free_fields()\r\n{\r\nfree_fields=0 # \u521d\u59cb\u5316\u53d8\u91cf \r\nfor n in $(seq 1 ${#room[@]}); do\r\nif [[ \"${room[$n]}\" = \".\" ]]; then # \u68c0\u67e5\u5f53\u524d\u5355\u5143\u683c\u662f\u5426\u7b49\u4e8e\u521d\u59cb\u503c\uff08.\uff09\uff0c\u7ed3\u679c\u4e3a\u771f\uff0c\u5219\u8bb0\u4e3a\u7a7a\u4f59\u683c\u5b50\u3002 \r\n((free_fields+=1))\r\n\u00a0 \u00a0 fi\r\n\u00a0 done\r\n}<\/pre>\n

    \u8fd9\u662f\u663e\u793a\u51fa\u6765\u7684\u6e38\u620f\u754c\u9762\uff0c[a-j]\u00a0\u4e3a\u5217\uff0c[0-9]\u00a0\u4e3a\u884c\u3002<\/p>\n

    \"\"<\/p>\n

    Minefield<\/em><\/p>\n

    \u521b\u5efa\u73a9\u5bb6\u903b\u8f91<\/strong><\/div>\n

    \u73a9\u5bb6\u64cd\u4f5c\u80cc\u540e\u7684\u903b\u8f91\u5728\u4e8e\uff0c\u5148\u4ece stdin \u4e2d\u8bfb\u53d6\u6570\u636e\u4f5c\u4e3a\u5750\u6807\uff0c\u7136\u540e\u518d\u627e\u51fa\u5bf9\u5e94\u4f4d\u7f6e\u5b9e\u9645\u5305\u542b\u7684\u503c\u3002\u8fd9\u91cc\u7528\u5230\u4e86 Bash \u7684\u53c2\u6570\u6269\u5c55\uff0c\u6765\u8bbe\u6cd5\u5f97\u5230\u884c\u5217\u6570\u3002\u7136\u540e\u5c06\u4ee3\u8868\u5217\u6570\u7684\u5b57\u6bcd\u4f20\u7ed9\u5206\u652f\u8bed\u53e5\uff0c\u4ece\u800c\u5f97\u5230\u5176\u5bf9\u5e94\u7684\u5217\u6570\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u770b\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf o \u6240\u5bf9\u5e94\u7684\u503c\u3002 \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u73a9\u5bb6\u8f93\u5165\u4e86 c3\uff0c\u8fd9\u65f6 Bash \u5c06\u5176\u5206\u6210\u4e24\u4e2a\u5b57\u7b26\uff1ac \u548c 3\u3002\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u8df3\u8fc7\u4e86\u5982\u4f55\u5904\u7406\u65e0\u6548\u8f93\u5165\u7684\u90e8\u5206\u3002<\/p>\n

    colm=${opt:0:1} # \u5f97\u5230\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff0c\u4e00\u4e2a\u5b57\u6bcd\r\nro=${opt:1:1} # \u5f97\u5230\u7b2c\u4e8c\u4e2a\u5b57\u7b26\uff0c\u4e00\u4e2a\u6574\u6570\r\ncase $colm in\r\na ) o=1;; # \u6700\u540e\uff0c\u901a\u8fc7\u5b57\u6bcd\u5f97\u5230\u5bf9\u5e94\u5217\u6570\u3002\r\nb ) o=2;;\r\n\u00a0 \u00a0 c ) o=3;;\r\n\u00a0 \u00a0 d ) o=4;;\r\n\u00a0 \u00a0 e ) o=5;;\r\n\u00a0 \u00a0 f ) o=6;;\r\n\u00a0 \u00a0 g ) o=7;;\r\n\u00a0 \u00a0 h ) o=8;;\r\n\u00a0 \u00a0 i ) o=9;;\r\n\u00a0 \u00a0 j ) o=10;;\r\n\u00a0 esac<\/pre>\n

    \u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u8ba1\u7b97\u7528\u6237\u6240\u9009\u5355\u5143\u683c\u5b9e\u9645\u5bf9\u5e94\u7684\u6570\u5b57\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u50a8\u5b58\u5728\u53d8\u91cf\u4e2d\u3002<\/p>\n

    \u8fd9\u91cc\u4e5f\u7528\u5230\u4e86\u5f88\u591a\u7684\u00a0shuf\u00a0\u547d\u4ee4\uff0cshuf\u00a0\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u6765\u751f\u6210\u968f\u673a\u5e8f\u5217\u7684\u00a0Linux \u547d\u4ee4\u3002-i\u00a0\u9009\u9879\u540e\u9762\u9700\u8981\u63d0\u4f9b\u9700\u8981\u6253\u4e71\u7684\u6570\u6216\u8005\u8303\u56f4\uff0c-n\u00a0\u9009\u9879\u5219\u89c4\u5b9a\u8f93\u51fa\u7ed3\u679c\u6700\u591a\u9700\u8981\u8fd4\u56de\u51e0\u4e2a\u503c\u3002Bash \u4e2d\uff0c\u53ef\u4ee5\u5728\u4e24\u4e2a\u5706\u62ec\u53f7\u5185\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\uff0c\u8fd9\u91cc\u6211\u4eec\u4f1a\u591a\u6b21\u7528\u5230\u3002<\/p>\n

    \u8fd8\u662f\u6cbf\u7528\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u73a9\u5bb6\u8f93\u5165\u4e86 c3\u3002 \u63a5\u7740\uff0c\u5b83\u88ab\u8f6c\u5316\u6210\u4e86 ro=3 \u548c o=3\u3002 \u4e4b\u540e\uff0c\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u652f\u8bed\u53e5\u4ee3\u7801\uff0c \u5c06 c \u8f6c\u5316\u4e3a\u5bf9\u5e94\u7684\u6574\u6570\uff0c\u5e26\u8fdb\u516c\u5f0f\uff0c\u4ee5\u5f97\u5230\u6700\u7ec8\u7ed3\u679c i \u7684\u503c\u3002<\/p>\n

    i=$(((ro*10)+o)) # \u9075\u5faa\u8fd0\u7b97\u89c4\u5219\uff0c\u7b97\u51fa\u6700\u7ec8\u503c\r\nis_free_field $i $(shuf -i 0-5 -n 1) # \u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5224\u65ad\u5176\u6307\u5411\u7a7a\/\u53ef\u9009\u62e9\u5355\u5143\u683c\u3002<\/pre>\n

    \u4ed4\u7ec6\u89c2\u5bdf\u8fd9\u4e2a\u8ba1\u7b97\u8fc7\u7a0b\uff0c\u770b\u770b\u6700\u7ec8\u7ed3\u679c i \u662f\u5982\u4f55\u8ba1\u7b97\u51fa\u6765\u7684\uff1a<\/p>\n

    i=$(((ro*10)+o))\r\ni=$(((3*10)+3))=$((30+3))=33<\/pre>\n

    \u6700\u540e\u7ed3\u679c\u662f 33\u3002\u5728\u6211\u4eec\u7684\u6e38\u620f\u754c\u9762\u663e\u793a\u51fa\u6765\uff0c\u73a9\u5bb6\u8f93\u5165\u5750\u6807\u6307\u5411\u4e86\u7b2c 33 \u4e2a\u5355\u5143\u683c\uff0c\u4e5f\u5c31\u662f\u5728\u7b2c 3 \u884c\uff08\u4ece 0 \u5f00\u59cb\uff0c\u5426\u5219\u8fd9\u91cc\u53d8\u6210 4\uff09\uff0c\u7b2c 3 \u5217\u3002<\/p>\n

    \u521b\u5efa\u5224\u65ad\u5355\u5143\u683c\u662f\u5426\u53ef\u9009\u7684\u903b\u8f91<\/strong><\/div>\n

    \u4e3a\u4e86\u627e\u5230\u5730\u96f7\uff0c\u5728\u5c06\u5750\u6807\u8f6c\u5316\uff0c\u5e76\u627e\u5230\u5b9e\u9645\u4f4d\u7f6e\u4e4b\u540e\uff0c\u7a0b\u5e8f\u4f1a\u68c0\u67e5\u8fd9\u4e00\u5355\u5143\u683c\u662f\u5426\u53ef\u9009\u3002\u5982\u4e0d\u53ef\u9009\uff0c\u7a0b\u5e8f\u4f1a\u663e\u793a\u4e00\u6761\u8b66\u544a\u4fe1\u606f\uff0c\u5e76\u8981\u6c42\u73a9\u5bb6\u91cd\u65b0\u8f93\u5165\u5750\u6807\u3002<\/p>\n

    \u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5355\u5143\u683c\u662f\u5426\u53ef\u9009\uff0c\u662f\u7531\u6570\u7ec4\u91cc\u5bf9\u5e94\u7684\u503c\u662f\u5426\u4e3a\u70b9\uff08.\uff09\u51b3\u5b9a\u7684\u3002\u5982\u679c\u53ef\u9009\uff0c\u5219\u91cd\u7f6e\u5355\u5143\u683c\u5bf9\u5e94\u7684\u503c\uff0c\u5e76\u66f4\u65b0\u5206\u6570\u3002\u53cd\u4e4b\uff0c\u56e0\u4e3a\u5176\u5bf9\u5e94\u503c\u4e0d\u4e3a\u70b9\uff0c\u5219\u8bbe\u7f6e\u53d8\u91cf\u00a0not_allowed\u3002\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u6e38\u620f\u4e2d\u8b66\u544a\u6d88\u606f\u8fd9\u90e8\u5206\u6e90\u7801\uff0c\u6211\u4f1a\u7559\u7ed9\u8bfb\u8005\u4eec\u81ea\u5df1\u53bb\u63a2\u7d22\u3002<\/p>\n

    is_free_field()\r\n{\r\n\u00a0 local f=$1\r\n\u00a0 local val=$2\r\n\u00a0 not_allowed=0\r\n\u00a0 if [[ \"${room[$f]}\" = \".\" ]]; then\r\n\u00a0 \u00a0 room[$f]=$val\r\n\u00a0 \u00a0 score=$((score+val))\r\n\u00a0 else\r\n\u00a0 \u00a0 not_allowed=1\r\n\u00a0 fi\r\n}<\/pre>\n

    \u5982\u8f93\u5165\u5750\u6807\u6709\u6548\uff0c\u4e14\u5bf9\u5e94\u4f4d\u7f6e\u4e3a\u5730\u96f7\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002\u73a9\u5bb6\u8f93\u5165\u00a0h6\uff0c\u6e38\u620f\u754c\u9762\u4f1a\u51fa\u73b0\u4e00\u4e9b\u968f\u673a\u751f\u6210\u7684\u503c\u3002\u5728\u53d1\u73b0\u5730\u96f7\u540e\uff0c\u8fd9\u4e9b\u503c\u4f1a\u88ab\u52a0\u5165\u7528\u6237\u5f97\u5206\u3002<\/p>\n

    \"\"<\/p>\n

    Extracting mines<\/em><\/p>\n

    \u8fd8\u8bb0\u5f97\u6211\u4eec\u5f00\u5934\u5b9a\u4e49\u7684\u53d8\u91cf\uff0ca\u00a0-\u00a0g\u00a0\u5417\uff0c\u6211\u4f1a\u7528\u5b83\u4eec\u6765\u786e\u5b9a\u968f\u673a\u751f\u6210\u5730\u96f7\u7684\u5177\u4f53\u503c\u3002\u6240\u4ee5\uff0c\u6839\u636e\u73a9\u5bb6\u8f93\u5165\u5750\u6807\uff0c\u7a0b\u5e8f\u4f1a\u6839\u636e\uff08m\uff09\u4e2d\u968f\u673a\u751f\u6210\u7684\u6570\uff0c\u6765\u751f\u6210\u5468\u56f4\u5176\u4ed6\u5355\u5143\u683c\u7684\u503c\uff08\u5982\u4e0a\u56fe\u6240\u793a\uff09\u3002\u4e4b\u540e\u5c06\u6240\u6709\u503c\u548c\u521d\u59cb\u8f93\u5165\u5750\u6807\u76f8\u52a0\uff0c\u6700\u540e\u7ed3\u679c\u653e\u5728\u00a0i\uff08\u8ba1\u7b97\u7ed3\u679c\u5982\u4e0a\uff09\u4e2d\u3002<\/p>\n

    \u8bf7\u6ce8\u610f\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684 X\uff0c\u5b83\u662f\u6211\u4eec\u552f\u4e00\u7684\u6e38\u620f\u7ed3\u675f\u6807\u5fd7\u3002\u6211\u4eec\u5c06\u5b83\u6dfb\u52a0\u5230\u968f\u673a\u5217\u8868\u4e2d\u3002\u5728 shuf \u547d\u4ee4\u7684\u9b54\u529b\u4e0b\uff0cX \u53ef\u4ee5\u5728\u4efb\u610f\u60c5\u51b5\u4e0b\u51fa\u73b0\uff0c\u4f46\u5982\u679c\u4f60\u8db3\u591f\u5e78\u8fd0\u7684\u8bdd\uff0c\u4e5f\u53ef\u80fd\u4e00\u76f4\u4e0d\u4f1a\u51fa\u73b0\u3002<\/p>\n

    m=$(shuf -e a b c d e f g X -n 1) # \u5c06 X \u6dfb\u52a0\u5230\u968f\u673a\u5217\u8868\u4e2d\uff0c\u5f53 m=X\uff0c\u6e38\u620f\u7ed3\u675f\r\nif [[ \"$m\" != \"X\" ]]; then # X \u5c06\u4f1a\u662f\u6211\u4eec\u7206\u70b8\u5730\u96f7\uff08\u6e38\u620f\u7ed3\u675f\uff09\u7684\u89e6\u53d1\u6807\u5fd7\r\nfor limit in ${!m}; do # !m \u4ee3\u8868 m \u53d8\u91cf\u7684\u503c\r\nfield=$(shuf -i 0-5 -n 1) # \u7136\u540e\u518d\u6b21\u83b7\u5f97\u4e00\u4e2a\u968f\u673a\u6570\u5b57\r\nindex=$((i+limit)) # \u5c06 m \u4e2d\u7684\u6bcf\u4e00\u4e2a\u503c\u548c index \u52a0\u8d77\u6765\uff0c\u76f4\u5230\u5217\u8868\u7ed3\u5c3e\r\nis_free_field $index $field\r\n\u00a0 \u00a0 done<\/pre>\n

    \u6211\u60f3\u8981\u6e38\u620f\u754c\u9762\u4e2d\uff0c\u6240\u6709\u968f\u673a\u663e\u793a\u51fa\u6765\u7684\u5355\u5143\u683c\uff0c\u90fd\u9760\u8fd1\u73a9\u5bb6\u9009\u62e9\u7684\u5355\u5143\u683c\u3002<\/p>\n

    \"\"<\/p>\n

    Extracting mines<\/em><\/p>\n

    \u8bb0\u5f55\u5df2\u9009\u62e9\u548c\u53ef\u7528\u5355\u5143\u683c\u7684\u4e2a\u6570<\/strong><\/div>\n

    \u8fd9\u4e2a\u7a0b\u5e8f\u9700\u8981\u8bb0\u5f55\u6e38\u620f\u754c\u9762\u4e2d\u54ea\u4e9b\u5355\u5143\u683c\u662f\u53ef\u9009\u62e9\u7684\u3002\u5426\u5219\uff0c\u7a0b\u5e8f\u4f1a\u4e00\u76f4\u8ba9\u7528\u6237\u8f93\u5165\u6570\u636e\uff0c\u5373\u4f7f\u6240\u6709\u5355\u5143\u683c\u90fd\u88ab\u9009\u4e2d\u8fc7\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u53eb\u00a0free_fields\u00a0\u7684\u53d8\u91cf\uff0c\u521d\u59cb\u503c\u4e3a\u00a00\u3002\u7528\u4e00\u4e2a\u00a0for\u00a0\u5faa\u73af\uff0c\u8bb0\u5f55\u4e0b\u6e38\u620f\u754c\u9762\u4e2d\u53ef\u9009\u62e9\u5355\u5143\u683c\u7684\u6570\u91cf\u3002 \u5982\u679c\u5355\u5143\u683c\u6240\u5bf9\u5e94\u7684\u503c\u4e3a\u70b9\uff08.\uff09\uff0c\u5219\u00a0free_fields\u00a0\u52a0\u4e00\u3002<\/p>\n

    get_free_fields()\r\n{\r\n\u00a0 free_fields=0\r\n\u00a0 for n in $(seq 1 ${#room[@]}); do\r\n\u00a0 \u00a0 if [[ \"${room[$n]}\" = \".\" ]]; then\r\n\u00a0 \u00a0 \u00a0 ((free_fields+=1))\r\n\u00a0 \u00a0 fi\r\n\u00a0 done\r\n}<\/pre>\n

    \u7b49\u4e0b\uff0c\u5982\u679c free_fields=0 \u5462\uff1f \u8fd9\u610f\u5473\u7740\uff0c\u73a9\u5bb6\u5df2\u9009\u62e9\u8fc7\u6240\u6709\u5355\u5143\u683c\u3002\u5982\u679c\u60f3\u66f4\u597d\u7406\u89e3\u8fd9\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u770b\u770b\u8fd9\u91cc\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n

    if [[ $free_fields -eq 0 ]]; then # \u8fd9\u610f\u5473\u7740\u4f60\u5df2\u9009\u62e9\u8fc7\u6240\u6709\u683c\u5b50\r\nprintf '\\n\\n\\t%s: %s %d\\n\\n' \"You Win\" \"you scored\" \"$score\"\r\n\u00a0 \u00a0 \u00a0 exit 0\r\nfi\r\n<\/pre>\n
    \u521b\u5efa\u6e38\u620f\u7ed3\u675f\u903b\u8f91<\/strong><\/div>\n

    \u5bf9\u4e8e\u6e38\u620f\u7ed3\u675f\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e9b\u5f88\u5de7\u5999\u7684\u6280\u5de7\uff0c\u5c06\u7ed3\u679c\u5728\u5c4f\u5e55\u4e2d\u592e\u663e\u793a\u51fa\u6765\u3002\u6211\u628a\u8fd9\u90e8\u5206\u7559\u7ed9\u8bfb\u8005\u670b\u53cb\u4eec\u81ea\u5df1\u53bb\u63a2\u7d22\u3002<\/p>\n

    if [[ \"$m\" = \"X\" ]]; then\r\ng=0 # \u4e3a\u4e86\u5728\u53c2\u6570\u6269\u5c55\u4e2d\u4f7f\u7528\u5b83\r\nroom[$i]=X # \u8986\u76d6\u6b64\u4f4d\u7f6e\u539f\u6709\u7684\u503c\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3aX\r\nfor j in {42..49}; do # \u5728\u6e38\u620f\u754c\u9762\u4e2d\u592e\uff0c\r\nout=\"gameover\"\r\nk=${out:$g:1} # \u5728\u6bcf\u4e00\u683c\u4e2d\u663e\u793a\u4e00\u4e2a\u5b57\u6bcd\r\nroom[$j]=${k^^}\r\n\u00a0 \u00a0 \u00a0 ((g+=1))\r\n\u00a0 \u00a0 done\r\nfi<\/pre>\n

    \u6700\u540e\uff0c\u6211\u4eec\u663e\u793a\u51fa\u73a9\u5bb6\u6700\u5173\u5fc3\u7684\u4e24\u884c\u3002<\/p>\n

    if [[ \"$m\" = \"X\" ]]; then\r\n\u00a0 \u00a0 \u00a0 printf '\\n\\n\\t%s: %s %d\\n' \"GAMEOVER\" \"you scored\" \"$score\"\r\n\u00a0 \u00a0 \u00a0 printf '\\n\\n\\t%s\\n\\n' \"You were just $free_fields mines away.\"\r\n\u00a0 \u00a0 \u00a0 exit 0\r\nfi<\/pre>\n

    \u6587\u7ae0\u5230\u8fd9\u91cc\u5c31\u7ed3\u675f\u4e86\uff0c\u670b\u53cb\u4eec\uff01\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\uff0c\u5177\u4f53\u53ef\u4ee5\u67e5\u770b\u6211\u7684\u00a0GitHub \u5b58\u50a8\u5e93\uff0c\u90a3\u513f\u6709\u8fd9\u4e2a\u626b\u96f7\u6e38\u620f\u7684\u6e90\u4ee3\u7801\uff0c\u5e76\u4e14\u4f60\u8fd8\u80fd\u627e\u5230\u66f4\u591a\u7528 Bash \u7f16\u5199\u7684\u6e38\u620f\u3002 \u6211\u5e0c\u671b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u80fd\u6fc0\u8d77\u4f60\u5b66\u4e60 Bash \u7684\u5174\u8da3\uff0c\u5e76\u4e50\u5728\u5176\u4e2d\u3002<\/p>\n


    \n

    via:\u00a0https:\/\/opensource.com\/article\/19\/9\/advanced-bash-building-minesweeper<\/a><\/p>\n

    \u4f5c\u8005\uff1aAbhishek Tamrakar<\/a>\u00a0\u9009\u9898\uff1alujun9972<\/a>\u00a0\u8bd1\u8005\uff1awenwensnow<\/a>\u00a0\u6821\u5bf9\uff1awxy<\/a><\/p>\n

    \u672c\u6587\u7531\u00a0LCTT<\/a>\u00a0\u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd<\/a>\u00a0\u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5982\u679c\u4f60\u662f\u4e00\u4e2a\u6709\u7ecf\u9a8c\u7684 Bash \u7a0b\u5e8f\u5458\uff0c\u5e0c\u671b\u5728\u63d0\u9ad8\u6280\u5de7\u7684\u540c\u65f6\u4e50\u5728\u5176\u4e2d\uff0c\u90a3\u4e48\u8bf7\u8ddf\u7740\u6211\u7f16\u5199\u4e00\u4e2a\u4f60\u7684\u8fd0\u884c\u5728\u7ec8\u7aef\u4e2d\u7684\u626b\u96f7\u6e38\u620f\u3002\u5b8c\u6574\u4ee3\u7801\u53ef\u4ee5\u5728\u8fd9\u4e2a GitHub \u5b58\u50a8\u5e93\u4e2d\u627e\u5230\u3002<\/p>\n","protected":false},"author":323,"featured_media":30226,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-160829","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=160829"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160829\/revisions"}],"predecessor-version":[{"id":161232,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160829\/revisions\/161232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/30226"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=160829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=160829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=160829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}