{"id":217636,"date":"2018-09-15T22:59:19","date_gmt":"2018-09-15T22:59:19","guid":{"rendered":"http:\/\/localhost\/wordpress\/index.php\/2018\/09\/15\/gsp-125-week-1-ilab-simple-game\/"},"modified":"2018-10-24T09:00:59","modified_gmt":"2018-10-24T09:00:59","slug":"gsp-125-week-1-ilab-simple-game","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/gsp-125-week-1-ilab-simple-game\/","title":{"rendered":"GSP 125 Week 1 iLab Simple game"},"content":{"rendered":"<p>GSP 125 GSP125 GSP 125 \u00a0Week 1 iLab\t\u00a0\t\/\/\t\/\/\t\/\/ INSTRUCTIONS\t&#8212;&#8212;&#8212;&#8211;Compile this code. You should see a happy-face character on a field of\tperiods. You can move the character with the &#8216;w&#8217;, &#8216;a&#8217;, &#8216;s&#8217;, and &#8216;d&#8217; keys.\tRead through this code! Try to understand it before starting the assignment.\tComment confusing lines with what you think code is doing, and experiment\twith existing code to test your understanding.\tOnce you feel comfortable with this code, accomplish each of the following,\tand make sure your code compiles and runs after each step is completed.\t1) Object Oriented Refactoring\ta) Write a class called Entity to store two public integers named x and y,\tand a char named icon (the player data).\tb) Remove x, y, and icon (the player data) from main(), create an instance\tof the Entity class (named whatever you like) in main(), and use its\tmembers as replacements for the x, y, and icon variables that were\tremoved.\tc) Write a parameterized constructor for the Entity class that sets x, y,\tand icon, and use it when creating the instance.\td) Make x, y, and icon private variables of Entity, and create Accessor\tand Mutator (or Getter and Setter) functions to use them in main().\t(hint: &quot;player.x++&quot; could be &quot;player.setX(player.getX()+1);&quot; )\te) Write a struct called Vector2, which has two int variables, x and y.\tf) Write a default constructor for Vector2, which sets x and y to 0.\tg) Write a parameterized constructor for Vector2, which sets x and y.\th) Remove x, and y from Entity, add an instance of the Vector2 structure\tnamed &quot;pos&quot; to the Entity class, and use pos&#8217;s members as replacements\tfor the x, and y variables that were removed.\ti) Remove height and width (in the game data) from main(), create an\tinstance of the Vector2 structure named &quot;size&quot;, and use size&#8217;s x member\tas a replacement for width, and size&#8217;s y member as a replacement for\theight.\tj) Write a method in Vector2 with the signature\t&quot;bool is(int a_x, int a_y)&quot;. &quot;is&quot; should return true if a_x is equal to\tthat instance&#8217;s x, and a_y is equal that instance&#8217;s y.\tk) Instantiate a new object of class Vector2 called &quot;winPosition&quot;, and set\tit&#8217;s x, y value to size.x\/2, size.y\/2.\t2) Add Game Logic\ta) Add code to the while-loop so that when the player reaches\t&quot;winPosition&quot;, which should be determined by using the &quot;is&quot; method, the\t&quot;state&quot; variable should be set to WIN, ending the game.\tb) Add code to the while-loop so that the state variable is set to to LOST\tif the player leaves the play field (ending the game).\t3) Using enums\ta) Create an enum called &quot;GameState&quot; with the possible values &quot;RUNNING&quot;,\t&quot;WIN&quot;, &quot;LOST&quot;, and &quot;USER_QUIT&quot;.\tb) Replace the state variable with an isntance of the GameState enum. \/\/ lab1: simplegame_OOP\t\/\/ &lt;insert your name here&gt;\t\/\/ read main.cpp, and follow the instructions at the bottom of main.cpp\t#include &lt;iostream&gt; \/\/ std::cout using namespace std;\t#include &lt;windows.h&gt;\t#include &lt;conio.h&gt; \/\/ SetConsoleCursorPosition(HANDLE,COORD)\t\/\/ _getch() struct Vector2 {\tint x;\tint y;\tVector2() :\tx(0), y(0) {\t}\tVector2(int x, int y) {\tx = x;\ty = y;\t}\tbool is(int a_x, int a_y) {\tif (a_x == x &amp;&amp; a_y == y) {\treturn true;\t}\t} return false; };\tclass Entity {\tpublic:\tEntity(int x, int y, char i) {\tpos.x = x;\tpos.y = y;\ticon = i;\t}\tvoid setX(int x) {\tpos.x = x;\t}\tint getX() {\tpos.x;\t}\tvoid setY(int y) {\tpos.y = y;\t} int getY() {\tpos.y;\t}\tvoid setIcon(char i) {\ticon = i;\t}\tchar getIcon() {\ticon;\t}\tprivate:\tVector2 pos;\tchar icon;\t};\tenum GameState {\tRUNNING, WIN, LOST, USER_QUIT\t}; \/**\t* moves the console cursor to the given x\/y coordinate\t* @param x\t* @param y\t*\/\tvoid moveCursor(int x, int y)\t{\tCOORD c = { x,y };\tSetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);\t}\tint main()\t{\t\/\/ player data\tEntity e(3, 4, 1); \/\/ game data\tGameState state = RUNNING;\tint input;\tVector2 size(20, 15);\tVector2 winPosition(size.x \/ 2, size.y \/ 2);\tdo\t{ \/\/ draw the game world\tmoveCursor(0, 0);\tfor (int row = 0; row &lt; size.x; row++)\t{\tfor (int col = 0; col &lt; size.y; col++)\t{\tcout &lt;&lt; &#8216;.&#8217;;\t} cout &lt;&lt; &#8216;n&#8217;;\t}\t\/\/ draw the player\tmoveCursor(e.getX(), e.getY());\tcout &lt;&lt; e.getIcon();\t\/\/ get input from the user (wait for one key press)\tinput = _getch();\t\/\/ process input from the user\tswitch (input)\t{\tcase &#8216;w&#8217;:\te.setY(e.getY() &#8211; 1);\tbreak; \/\/ move up\tcase &#8216;a&#8217;:\te.setX(e.getX() &#8211; 1); break; \/\/ move left\tcase &#8216;s&#8217;:\te.setY(e.getY() + 1); break; \/\/ move down\tcase &#8216;d&#8217;:\te.setX(e.getX() + 1); break; \/\/ move right\tcase 27:\tstate = USER_QUIT; break; \/\/ quit\t}\t\/\/ show the game state message\tmoveCursor(0, size.y + 1);\tswitch (state)\t{\tcase WIN:\tcout &lt;&lt; &quot;You WON! Congratulations!n&quot;;\tbreak;\tcase LOST:\tcout &lt;&lt; &quot;You lost&#8230;n&quot;;\tbreak;\t}\tif (winPosition.is(e.getX(), e.getY()))\t{\tstate = WIN;\t}\telse\t{\tstate = LOST;\t}\t} while (state == RUNNING); }; \/\/ user must press ESCAPE before closing the program\tcout &lt;&lt; &quot;press ESCAPE to quitn&quot;;\twhile (_getch() != 27);\treturn 0;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GSP 125 GSP125 GSP 125 \u00a0Week 1 iLab \u00a0 \/\/ \/\/ \/\/ INSTRUCTIONS &#8212;&#8212;&#8212;&#8211;Compile this code. You should see a happy-face character on a field of periods. You can move the character with the &#8216;w&#8217;, &#8216;a&#8217;, &#8216;s&#8217;, and &#8216;d&#8217; keys. <a href=\"https:\/\/www.benedictsol.com\/blogs\/gsp-125-week-1-ilab-simple-game\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-217636","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=217636"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217636\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=217636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=217636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=217636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}