{"id":39926,"date":"2025-05-26T13:57:01","date_gmt":"2025-05-26T05:57:01","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/39926.html"},"modified":"2025-05-26T13:57:01","modified_gmt":"2025-05-26T05:57:01","slug":"%e3%80%90c%e3%80%91-%e6%88%91%e7%9a%84%e4%b8%96%e7%95%8c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/39926.html","title":{"rendered":"\u3010c++\u3011 \u6211\u7684\u4e16\u754c"},"content":{"rendered":"<p>\u592a\u4e45\u6ca1\u66f4\u65b0\u5c0f\u6e38\u620f\u4e86<\/p>\n<p>\u7ed9\u4e2a\u8d5e\u548c\u6536\u85cf\u5427&#xff0c;\u6c42\u6c42\u4e86<\/p>\n<p>\u8981\u6e38\u620f\u7684\u8bf7\u79c1\u804a\u6211<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e16\u754c\u5927\u5c0f<br \/>\nconst int WORLD_WIDTH &#061; 20;<br \/>\nconst int WORLD_HEIGHT &#061; 10;<\/p>\n<p>\/\/ \u5b9a\u4e49\u65b9\u5757\u7c7b\u578b<br \/>\nenum BlockType {<br \/>\n    AIR,<br \/>\n    GRASS,<br \/>\n    DIRT,<br \/>\n    STONE<br \/>\n};<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e16\u754c<br \/>\nstd::vector&lt;std::vector&lt;BlockType&gt;&gt; world(WORLD_HEIGHT, std::vector&lt;BlockType&gt;(WORLD_WIDTH, AIR));<\/p>\n<p>\/\/ \u751f\u6210\u5730\u5f62<br \/>\nvoid generateTerrain() {<br \/>\n    for (int x &#061; 0; x &lt; WORLD_WIDTH; &#043;&#043;x) {<br \/>\n        int groundLevel &#061; WORLD_HEIGHT \/ 2;<br \/>\n        world[groundLevel][x] &#061; GRASS;<br \/>\n        for (int y &#061; groundLevel &#043; 1; y &lt; WORLD_HEIGHT; &#043;&#043;y) {<br \/>\n            if (y &lt; groundLevel &#043; 3) {<br \/>\n                world[y][x] &#061; DIRT;<br \/>\n            } else {<br \/>\n                world[y][x] &#061; STONE;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6253\u5370\u4e16\u754c<br \/>\nvoid printWorld(int playerX, int playerY) {<br \/>\n    for (int y &#061; 0; y &lt; WORLD_HEIGHT; &#043;&#043;y) {<br \/>\n        for (int x &#061; 0; x &lt; WORLD_WIDTH; &#043;&#043;x) {<br \/>\n            if (x &#061;&#061; playerX &amp;&amp; y &#061;&#061; playerY) {<br \/>\n                std::cout &lt;&lt; &#039;P&#039;;<br \/>\n            } else {<br \/>\n                switch (world[y][x]) {<br \/>\n                    case AIR:<br \/>\n                        std::cout &lt;&lt; &#039; &#039;;<br \/>\n                        break;<br \/>\n                    case GRASS:<br \/>\n                        std::cout &lt;&lt; &#039;G&#039;;<br \/>\n                        break;<br \/>\n                    case DIRT:<br \/>\n                        std::cout &lt;&lt; &#039;D&#039;;<br \/>\n                        break;<br \/>\n                    case STONE:<br \/>\n                        std::cout &lt;&lt; &#039;S&#039;;<br \/>\n                        break;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        std::cout &lt;&lt; std::endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    generateTerrain();<\/p>\n<p>    int playerX &#061; WORLD_WIDTH \/ 2;<br \/>\n    int playerY &#061; WORLD_HEIGHT \/ 2 &#8211; 1;<\/p>\n<p>    while (true) {<br \/>\n        system(&#034;cls&#034;); \/\/ \u6e05\u5c4f&#xff0c;\u5728 Linux \u7cfb\u7edf\u4e2d\u4f7f\u7528 system(&#034;clear&#034;);<br \/>\n        printWorld(playerX, playerY);<\/p>\n<p>        std::cout &lt;&lt; &#034;\u4f7f\u7528 w(\u4e0a), s(\u4e0b), a(\u5de6), d(\u53f3) \u79fb\u52a8&#xff0c;q \u9000\u51fa: &#034;;<br \/>\n        char input;<br \/>\n        std::cin &gt;&gt; input;<\/p>\n<p>        if (input &#061;&#061; &#039;q&#039;) {<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        int newX &#061; playerX;<br \/>\n        int newY &#061; playerY;<\/p>\n<p>        switch (input) {<br \/>\n            case &#039;w&#039;:<br \/>\n                newY&#8211;;<br \/>\n                break;<br \/>\n            case &#039;s&#039;:<br \/>\n                newY&#043;&#043;;<br \/>\n                break;<br \/>\n            case &#039;a&#039;:<br \/>\n                newX&#8211;;<br \/>\n                break;<br \/>\n            case &#039;d&#039;:<br \/>\n                newX&#043;&#043;;<br \/>\n                break;<br \/>\n        }<\/p>\n<p>        \/\/ \u68c0\u67e5\u65b0\u4f4d\u7f6e\u662f\u5426\u5408\u6cd5<br \/>\n        if (newX &gt;&#061; 0 &amp;&amp; newX &lt; WORLD_WIDTH &amp;&amp; newY &gt;&#061; 0 &amp;&amp; newY &lt; WORLD_HEIGHT) {<br \/>\n            playerX &#061; newX;<br \/>\n            playerY &#061; newY;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}     <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.3k\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf21\u6b21\u3002\u9605\u8bfb\u91cf\u8d85\u8fc71000\uff0c\u7ee7\u7eed\u66f4\u65b0_mc\u4ee3\u7801c++<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55],"topic":[],"class_list":["post-39926","post","type-post","status-publish","format-standard","hentry","category-server","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010c++\u3011 \u6211\u7684\u4e16\u754c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/39926.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010c++\u3011 \u6211\u7684\u4e16\u754c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.3k\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf21\u6b21\u3002\u9605\u8bfb\u91cf\u8d85\u8fc71000\uff0c\u7ee7\u7eed\u66f4\u65b0_mc\u4ee3\u7801c++\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/39926.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-26T05:57:01+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39926.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/39926.html\",\"name\":\"\u3010c++\u3011 \u6211\u7684\u4e16\u754c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-26T05:57:01+00:00\",\"dateModified\":\"2025-05-26T05:57:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/39926.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/39926.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39926.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010c++\u3011 \u6211\u7684\u4e16\u754c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010c++\u3011 \u6211\u7684\u4e16\u754c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/39926.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010c++\u3011 \u6211\u7684\u4e16\u754c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.3k\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf21\u6b21\u3002\u9605\u8bfb\u91cf\u8d85\u8fc71000\uff0c\u7ee7\u7eed\u66f4\u65b0_mc\u4ee3\u7801c++","og_url":"https:\/\/www.wsisp.com\/helps\/39926.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-26T05:57:01+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/39926.html","url":"https:\/\/www.wsisp.com\/helps\/39926.html","name":"\u3010c++\u3011 \u6211\u7684\u4e16\u754c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-26T05:57:01+00:00","dateModified":"2025-05-26T05:57:01+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/39926.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/39926.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/39926.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010c++\u3011 \u6211\u7684\u4e16\u754c"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=39926"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39926\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=39926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=39926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=39926"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=39926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}