Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// INSTRUCTIONS // ------------ // Compile this code. You should see a happy-fac

ID: 3604303 • Letter: #

Question

// INSTRUCTIONS
// ------------
// Compile this code. You should see a happy-face character on a field of
// periods. You can move the character with the 'w', 'a', 's', and 'd' keys.
//
// Read through this code! Try to understand it before starting the assignment.
// Comment confusing lines with what you think code is doing, and experiment
// with existing code to test your understanding.
// Once you feel comfortable with this code, accomplish each of the following,
// and make sure your code compiles and runs after each step is completed.
//
// 1) Object Oriented Refactoring
// a) Write a class called Entity to store two public integers named x and y,
// and a char named icon (the player data).
// b) Remove x, y, and icon (the player data) from main(), create an instance
// of the Entity class (named whatever you like) in main(), and use its
// members as replacements for the x, y, and icon variables that were
// removed.
// c) Write a parameterized constructor for the Entity class that sets x, y,
// and icon, and use it when creating the instance.
// d) Make x, y, and icon private variables of Entity, and create Accessor
// and Mutator (or Getter and Setter) functions to use them in main().
// (hint: "player.x++" could be "player.setX(player.getX()+1);" )
// e) Write a struct called Vector2, which has two int variables, x and y.
// f) Write a default constructor for Vector2, which sets x and y to 0.
// g) Write a parameterized constructor for Vector2, which sets x and y.
// h) Remove x, and y from Entity, add an instance of the Vector2 structure
// named "pos" to the Entity class, and use pos's members as replacements
// for the x, and y variables that were removed.
// i) Remove height and width (in the game data) from main(), create an
// instance of the Vector2 structure named "size", and use size's x member
// as a replacement for width, and size's y member as a replacement for
// height.
// j) Write a method in Vector2 with the signature
// "bool is(int a_x, int a_y)". "is" should return true if a_x is equal to
// that instance's x, and a_y is equal that instance's y.
// k) Instantiate a new object of class Vector2 called "winPosition", and set
// it's x, y value to size.x/2, size.y/2.
// 2) Add Game Logic
// a) Add code to the while-loop so that when the player reaches
// "winPosition", which should be determined by using the "is" method, the
// "state" variable should be set to WIN, ending the game.
// b) Add code to the while-loop so that the state variable is set to to LOST
// if the player leaves the play field (ending the game).
// 3) Using enums
// a) Create an enum called "GameState" with the possible values "RUNNING",
// "WIN", "LOST", and "USER_QUIT".
// b) Replace the state variable with an isntance of the GameState enum.

// lab1: simplegame_OOP

// <insert your name here>

// read main.cpp, and follow the instructions at the bottom of main.cpp

#include <iostream> // std::cout

using namespace std;

#include <windows.h> // SetConsoleCursorPosition(HANDLE,COORD)

#include <conio.h> // _getch()

/**

* moves the console cursor to the given x/y coordinate

* @param x

* @param y

*/

void moveCursor(int x, int y)

{

COORD c = {x,y};

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);

}

int main()

{

// player data

int x = 3, y = 4;

char icon = 1;

// game state constants

const int RUNNING = 1, WIN = 2, LOST = 3, USER_QUIT = -1;

// game data

int width = 20, height = 15, input, state = RUNNING;

do

{

// draw the game world

moveCursor(0,0);

for(int row = 0; row < height; row++)

{

for(int col = 0; col < width; col++)

{

cout << '.';

}

cout << ' ';

}

// draw the player

moveCursor(x, y);

cout << icon;

// get input from the user (wait for one key press)

input = _getch();

// process input from the user

switch(input)

{

case 'w': y--; break; // move up

case 'a': x--; break; // move left

case 's': y++; break; // move down

case 'd': x++; break; // move right

case 27: state = USER_QUIT; break; // quit

}

// show the game state message

moveCursor(0, height+1);

switch(state)

{

case WIN: cout << "You WON! Congratulations! "; break;

case LOST: cout << "You lost... "; break;

}

}

while(state == RUNNING);

// user must press ESCAPE before closing the program

cout << "press ESCAPE to quit ";

while(_getch() != 27);

return 0;

}

Explanation / Answer

#include <windows.h> // SetConsoleCursorPosition(HANDLE,COORD)

#include <conio.h> // _getch()

/**

* moves the console cursor to the given x/y coordinate

* @param x

* @param y

*/

void moveCursor(int x, int y)

{

COORD c = {x,y};

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);

}

int main()

{

// player data

int x = 3, y = 4;

char icon = 1;

// game state constants

const int RUNNING = 1, WIN = 2, LOST = 3, USER_QUIT = -1;

// game data

int width = 20, height = 15, input, state = RUNNING;

do

{

// draw the game world

moveCursor(0,0);

for(int row = 0; row < height; row++)

{

for(int col = 0; col < width; col++)

{

cout << '.';

}

cout << ' ';

}

// draw the player

moveCursor(x, y);

cout << icon;

// get input from the user (wait for one key press)

input = _getch();

// process input from the user

switch(input)

{

case 'w': y--; break; // move up

case 'a': x--; break; // move left

case 's': y++; break; // move down

case 'd': x++; break; // move right

case 27: state = USER_QUIT; break; // quit

}

// show the game state message

moveCursor(0, height+1);

switch(state)

{

case WIN: cout << "You WON! Congratulations! "; break;

case LOST: cout << "You lost... "; break;

}

}

while(state == RUNNING);

// user must press ESCAPE before closing the program

cout << "press ESCAPE to quit ";

while(_getch() != 27);

return 0;

}