{"id":5276,"date":"2017-02-10T04:00:53","date_gmt":"2017-02-10T04:00:53","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=5276"},"modified":"2023-01-09T13:19:34","modified_gmt":"2023-01-09T13:19:34","slug":"c-programming-samples-snake-game","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/c-programming-samples-snake-game\/","title":{"rendered":"C# Programming Samples: Snake Game"},"content":{"rendered":"<p>Long ago, when monitors were black and green and computers had 64 kilobytes RAM, the legendary game was created. It was called \u201cSnake\u201d and it still has a lot of clones on different devices: from desktop to smartphones and tablets. But the original textual implementation hasn\u2019t been used for years. So in this guide, I want to show you the canonical implementation of the legendary game.<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5290\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-1.png\" alt=\"skake-game-in-c-1\" width=\"600\" height=\"304\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-1.png 677w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-1-300x152.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><!--more--><\/p>\n<p style=\"text-align: center;\">How to play<\/p>\n<p>Snake\u2019s head looks like this: \u201c@\u201d. You can control the snake using the arrows buttons. It can\u2019t move backward. The snake shouldn\u2019t come across the sides and itself. You should feed the snake with (X). When the snake eats X\u2019s it grows. If the snake comes across the side or itself the game is over and you see the message informing about it.<\/p>\n<p>End of the game:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5292\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-2.png\" alt=\"skake-game-in-c-2\" width=\"600\" height=\"304\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-2.png 677w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/02\/Skake-game-in-C-2-300x152.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">License<\/p>\n<p>GNU GPL. It is a widely used\u00a0free software license, which guarantees\u00a0end users\u00a0(individuals, organizations, companies) the freedoms to run, study, share (copy), and modify the software.<\/p>\n<p style=\"text-align: center;\">Code listing<\/p>\n<p>[code language=&#8221;csharp&#8221;]<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;windows.h&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>struct position {<br \/>\n\tint x, y;<br \/>\n};<\/p>\n<p>class field_clear {<br \/>\n\tstatic const int height;<br \/>\n\tstatic const int width;<br \/>\n\tchar ** field;<br \/>\n\tfield_clear(const field_clear &amp;);<br \/>\n\tfield_clear operator=(const field_clear &amp;);<br \/>\npublic:<br \/>\n\tfield_clear() {<br \/>\n\t\tfield = new char*[field_clear::height];<br \/>\n\t\tfor (int c = 0; c &lt; field_clear::height; ++c) {<br \/>\n\t\t\tfield[\u0441] = new char[field_clear::width];<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\t~field_clear() {<br \/>\n\t\tfor (int c = 0; c &lt; field_clear::height; ++c) {<br \/>\n\t\t\tdelete[] field[\u0441];<br \/>\n\t\t}<br \/>\n\t\tdelete[] field;<br \/>\n\t}<\/p>\n<p>\tvoid output() {<br \/>\n\t\tfor (int c = 0; c &lt; height; ++c) {<br \/>\n\t\t\tfor (int r = 0; r &lt; width; ++r) {<br \/>\n\t\t\t\tcout &lt;&lt; field[\u0441][r];<br \/>\n\t\t\t}<br \/>\n\t\t\tcout &lt;&lt; endl;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tvoid clear() {<br \/>\n\t\tfor (int c = 0; c &lt; height; ++c) {<br \/>\n\t\t\tfor (int r = 0; r &lt; width; ++r) {<br \/>\n\t\t\t\tfield[\u0441][r] = &#8216; &#8216;;<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tint get_width() const { return width; }<br \/>\n\tint get_height() const { return height; }<\/p>\n<p>\tvoid draw(int y, int x, char what) {<br \/>\n\t\t\/\/y = (y &lt; 0) ? 0 : (y &gt;= height ? height &#8211; 1 : y);<br \/>\n\t\t\/\/x = (x &lt; 0) ? 0 : (x &gt;= width ? width &#8211; 1 : x);<\/p>\n<p>\t\tfield[y][x] = what;<br \/>\n\t}<\/p>\n<p>} field;<\/p>\n<p>class food_clear {<br \/>\n\tposition pos;<br \/>\n\tchar symbol;<br \/>\npublic:<br \/>\n\tfood_clear() : symbol(&#8216;X&#8217;), pos() {<br \/>\n\t\tpos.x = pos.y = -1;<br \/>\n\t}<\/p>\n<p>\tvoid set_pos(int x, int y) {<br \/>\n\t\tpos.x = x;<br \/>\n\t\tpos.y = y;<br \/>\n\t}<\/p>\n<p>\tvoid reposition(const field_clear &amp; field) {<br \/>\n\t\tpos.x = rand() % field.get_width();<br \/>\n\t\tpos.y = rand() % field.get_height();<br \/>\n\t}<\/p>\n<p>\tint get_x() const { return pos.x; }<br \/>\n\tint get_y() const { return pos.y; }<br \/>\n\tchar get_symbol() const { return symbol; }<br \/>\n} food;<\/p>\n<p>class snake_clear {<br \/>\n\tenum { UP, DOWN, LEFT, RIGHT } dir;<br \/>\n\tchar symbol, head_symbol;<br \/>\n\tposition pos[100];<br \/>\n\tposition &amp; head;<br \/>\n\tint speed;<br \/>\n\tint size;<br \/>\n\tbool can_turn;<br \/>\npublic:<br \/>\n\tsnake_clear(int x, int y) :<br \/>\n\t\tsymbol(&#8216;#&#8217;), head_symbol(&#8216;@&#8217;), pos(),<br \/>\n\t\tspeed(1), size(1), dir(RIGHT),<br \/>\n\t\thead(pos[0]), can_turn(true)<br \/>\n\t{<br \/>\n\t\tpos[0].x = x;<br \/>\n\t\tpos[0].y = y;<br \/>\n\t}<\/p>\n<p>\tbool check_food(const food_clear &amp; food) {<br \/>\n\t\tif (food.get_x() == head.x &amp;&amp; food.get_y() == head.y) {<br \/>\n\t\t\tsize += 1;<br \/>\n\t\t\treturn true;<br \/>\n\t\t}<br \/>\n\t\treturn false;<br \/>\n\t}<\/p>\n<p>\tvoid get_input(const field_clear &amp; field) {<br \/>\n\t\tif (GetAsyncKeyState(VK_UP) &amp;&amp; dir != DOWN) {<br \/>\n\t\t\tdir = UP;<br \/>\n\t\t}<br \/>\n\t\tif (GetAsyncKeyState(VK_DOWN) &amp;&amp; dir != UP) {<br \/>\n\t\t\tdir = DOWN;<br \/>\n\t\t}<br \/>\n\t\tif (GetAsyncKeyState(VK_LEFT) &amp;&amp; dir != RIGHT) {<br \/>\n\t\t\tdir = LEFT;<br \/>\n\t\t}<br \/>\n\t\tif (GetAsyncKeyState(VK_RIGHT) &amp;&amp; dir != LEFT) {<br \/>\n\t\t\tdir = RIGHT;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tvoid move(const field_clear &amp; field) {<br \/>\n\t\tposition next = { 0, 0 };<br \/>\n\t\tswitch (dir) {<br \/>\n\t\tcase UP:<br \/>\n\t\t\tnext.y = -speed;<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase DOWN:<br \/>\n\t\t\tnext.y = speed;<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase LEFT:<br \/>\n\t\t\tnext.x = -speed;<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase RIGHT:<br \/>\n\t\t\tnext.x = speed;<br \/>\n\t\t}<br \/>\n\t\tfor (int c = size &#8211; 1; c &gt; 0; &#8211;c) {<br \/>\n\t\t\tpos[\u0441] = pos[c-1];<br \/>\n\t\t}<br \/>\n\t\thead.x += next.x;<br \/>\n\t\thead.y += next.y;<\/p>\n<p>\t\tif (head.x &lt; 0 || head.y &lt; 0 || head.x &gt;= field.get_width() || head.y &gt;= field.get_height()) {<br \/>\n\t\t\tthrow &quot;Game over&quot;;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tvoid draw(field_clear &amp; field) {<br \/>\n\t\tfor (int c = 0; c &lt; size; ++c) {<br \/>\n\t\t\tif (c == 0) {<br \/>\n\t\t\t\tfield.draw(pos[\u0441].y, pos[\u0441].x, head_symbol);<br \/>\n\t\t\t}<br \/>\n\t\t\telse {<br \/>\n\t\t\t\tfield.draw(pos[\u0441].y, pos[\u0441].x, symbol);<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tint get_x() const { return head.x; }<br \/>\n\tint get_y() const { return head.y; }<br \/>\n\tchar get_symbol() const { return symbol; }<br \/>\n} snake(1, 1);<\/p>\n<p>const int field_clear::height = 24;<br \/>\nconst int field_clear::width = 79;<\/p>\n<p>int main() {<br \/>\n\tfield.clear();<\/p>\n<p>\tfood.set_pos(5, 5);<\/p>\n<p>\twhile (1) {<br \/>\n\t\tfield.clear();<\/p>\n<p>\t\tsnake.get_input(field);<br \/>\n\t\ttry {<br \/>\n\t\t\tsnake.move(field);<br \/>\n\t\t}<br \/>\n\t\tcatch (const char * er) {<br \/>\n\t\t\tfield.clear();<br \/>\n\t\t\tcerr &lt;&lt; er &lt;&lt; endl;<br \/>\n\t\t\tsystem(&quot;pause&quot;);<br \/>\n\t\t\treturn -1;<br \/>\n\t\t}<br \/>\n\t\tsnake.draw(field);<br \/>\n\t\tfield.draw(food.get_y(), food.get_x(), food.get_symbol());<\/p>\n<p>\t\tif (snake.check_food(food)) {<br \/>\n\t\t\tfood.reposition(field);<br \/>\n\t\t}<\/p>\n<p>\t\tfield.output();<\/p>\n<p>\t\tSleep(1000 \/ 30);<br \/>\n\t\tsystem(&quot;cls&quot;);<br \/>\n\t}<\/p>\n<p>\treturn 0;<br \/>\n}<br \/>\n[\/code]<\/p>\n<h2>Programming Homework Assignments Help<\/h2>\n<blockquote><p><em>During reading one of our C# programming samples, you can get some ideas to deal with your own assignment &#8211; the one published above, or one on one-dimensional arrays. Unfortunately, not all students can handle homework on their own. If you are one of them, we suggest you to use <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">help with assignments<\/a> of AssignmentShark.com. <\/em><\/p>\n<p><em>Once your order is placed on our site, a professional expert starts to work on it. All your requirements will be taken into account in order to satisfy your needs in <a href=\"https:\/\/assignmentshark.com\/c-homework-help.html\" target=\"_blank\" rel=\"noopener\">C homework help<\/a>. We are available 24\/7, so that you can contact us any time you want. Moreover, you can contact an expert directly via chat if you have any questions. So, what are you waiting for? Place an order right now!<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Long ago, when monitors were black and green and computers had 64 kilobytes RAM, the legendary game was created. It was called \u201cSnake\u201d and it still has a lot of clones on different devices: from desktop to smartphones and tablets. But the original textual implementation hasn\u2019t been used for years. So in this guide, I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-5276","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/5276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=5276"}],"version-history":[{"count":20,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/5276\/revisions"}],"predecessor-version":[{"id":13481,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/5276\/revisions\/13481"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=5276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=5276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=5276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}