Shopping Cart

GSP 125 Week 1 iLab Simple game Updated

  • GSP 125 Week 1 iLab Simple game Updated
Model:Latest
  • Price : $15.00 | $10.00

This Tutorial Purchased: 8 Times Rating: A

attachments
This Tutorial contains following Attachments:
  • GSP 125 Week 1 ILab.zip

GSP 125 Week 1 iLab Simple game Updated

 

//
//
// 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>
#include <conio.h> // SetConsoleCursorPosition(HANDLE,COORD)
// _getch() struct Vector2 {
int x;
int y;
Vector2() :
x(0), y(0) {
}
Vector2(int x, int y) {
x = x;
y = y;
}
bool is(int a_x, int a_y) {
if (a_x == x && a_y == y) {
return true;
}
} return false; };
class Entity {
public:
Entity(int x, int y, char i) {
pos.x = x;
pos.y = y;
icon = i;
}
void setX(int x) {
pos.x = x;
}
int getX() {
pos.x;
}
void setY(int y) {
pos.y = y;
} int getY() {
pos.y;
}
void setIcon(char i) {
icon = i;
}
char getIcon() {
icon;
}
private:
Vector2 pos;
char icon;
};
enum GameState {
RUNNING, WIN, LOST, USER_QUIT
}; /**
* 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
Entity e(3, 4, 1); // game data
GameState state = RUNNING;
int input;
Vector2 size(20, 15);
Vector2 winPosition(size.x / 2, size.y / 2);
do
{ // draw the game world
moveCursor(0, 0);
for (int row = 0; row < size.x; row++)
{
for (int col = 0; col < size.y; col++)
{
cout << '.';
} cout << '\n';
}
// draw the player
moveCursor(e.getX(), e.getY());
cout << e.getIcon();
// get input from the user (wait for one key press)
input = _getch();
// process input from the user
switch (input)
{
case 'w':
e.setY(e.getY() - 1);
break; // move up
case 'a':
e.setX(e.getX() - 1); break; // move left
case 's':
e.setY(e.getY() + 1); break; // move down
case 'd':
e.setX(e.getX() + 1); break; // move right
case 27:
state = USER_QUIT; break; // quit
}
// show the game state message
moveCursor(0, size.y + 1);
switch (state)
{
case WIN:
cout << "You WON! Congratulations!\n";
break;
case LOST:
cout << "You lost...\n";
break;
}
if (winPosition.is(e.getX(), e.getY()))
{
state = WIN;
}
else
{
state = LOST;
}
} while (state == RUNNING); }; // user must press ESCAPE before closing the program
cout << "press ESCAPE to quit\n";
while (_getch() != 27);
return 0;

 

Write a review

Note: HTML is not translated!
    Bad           Good
Captcha