Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

I have some of this done already I think but I am not sure on the rest. Would someone be able to help me?

I have some of this done already I think but I am not sure on the rest. Would someone be able to help me?

// lab4: simplegame_Inheritance

// <insert your name here>

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

#include <stdio.h>// printf(char*, ...), putchar(int)

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

#include "game.h"

void main()

{

Game game;

do

{

game.draw();

game.setInput(_getch());

game.update();

}

while(game.getState() == Game::RUNNING);

printf("press ESCAPE to quitn");

while(_getch() != 27);

}

// INSTRUCTIONS

// ------------

// Compile this code. You should see a rectangular play field of periods, with

// 3 Entity objects visible on it. The happy-face Entity moves with the "wasd"

// keys, and the club Entity moves with the "ijkl" keys. If the happy-face

// reaches the diamond Entity, the player wins. If the happy-face reaches the

// club, the player loses.

//

// 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) Getting comfortable with the game code

//  a) Implement initialization lists in Vector2, Entity, and Game (setting 

//   object values after a ':', between the constructor signature and body).

//   Have initialization lists set initial values for each member variable.

//  b) Add another Entity to the game that isn't the same location as an

//   existing Entity. Use a heart icon (ASCII code 3). It should display in

//   the game.

//  c) Add logic that makes the club (PLAYER2) win the game if that player

//   reaches the heart Entity. You may want to make new constants, like

//   GOAL2, and WIN2, to follow the existing code convention.

//  d) Make a new private function called "void Game::handleUserInput()", move

//   the user input handling logic from Game::update() into this new

//   function, and call Game::handleUserInput from Game::update.

//  e) Add whitespace to the handleUserInput logic, and comment each line with

//   what you understand it is doing. If you don't understand what the code

//   is doing, experiment with it until you do! Do things like printing

//   variables you are unsure about, and guess what output will look like.

//  f) Implement the prototyped overloaded operators for Vector2. Once they

//   are finished you should be able to use the alternate code for setting

//   up PLAYER2 in Game::Game() in "game.cpp".

// 2) A "BlinkEntity" class

//  a) Create 2 new files in your project: "blinkentity.h", and

//   "blinkentity.cpp"

//  b) Make a BlinkEntity class that extends Entity. Declare the class in

//   "blinkentity.h" and define it's methods in "blinkentity.cpp". Your

//   "blinkentity.h" file should look something like:

//     #pragma once

//

//     #include "entity.h"

//

//     class BlinkEntity : public Entity {

//     };

//  c) Instead of using an Entity for the Entity marked GOAL1 in the Game

//   constructor, use a BlinkEntity. You will need to make a public 

//   BlinkEntity constructor.

//  d) Give BlinkEntity another member variable called "alternateIcon". When

//   BlinkEntity calls it's update function, swap the values of "icon" and

//   "alternateIcon". You won't notice a change during runtime until you add

//   the virtual modifier to Entity::update().

// 3) A "WanderingEntity" class

//  a) Create 2 new files in your project: "wanderingentity.h", and

//   "wanderingentity.cpp"

//  b) Make a WanderingEntity class that extends Entity. Declare the class in

//   "wanderingentity.h" and define it's methods in "wanderingentity.cpp".

//   Your "wanderingentity.h" file should look something like:

//     #pragma once

//

//     #include "entity.h"

//

//     class WanderingEntity : public Entity {

//     };

//  c) Instead of using an Entity for the Entity marked PLAYER2 in the Game

//   constructor, use a WanderingEntity. You will need to make a public

//   WanderingEntity constructor.

//  d) make a new update method for WanderingEntity. Declare it in

//   "wanderingentity.h", and define it in "wanderingentity.cpp". In the

//   WanderingEntity::update() method, set the "howToMoveNext" variable

//   to a random number from 0 to 3. You can use "rand() % 4" to do thiss in

//   "wanderingentity.cpp" if you #include <cstdlib> or <stdlib.h>. After

//   setting the "howToMoveNext" variable in update, call the parent class's

//   updated with "Entity::update()".

//  e) Add at least 2 more WanderingEntity objects in the Game. Add game logic

//   will cause the player to lose if the player shares a location with any

//   WanderingEntity object.

// When finished:

// 1) Make sure your name is at the top of this source file

// 2) Submit this project online

//  a) Right-click on the .cpp file's name within visual studio, and select

//   "Open Containing Folder"

//  b) Close Visual Studio (you may re-open this .cpp file by right-clicking

//   on it in the file system, and slecting edit)

//  c) Make sure the following files are DELETED from the project's file

//   structure:

//   * Any file with the extension: ".ncb", ".sdf"

//   * Any folder named: "Debug" or "ipch"

//   * If Visual Studio is open, you will not be able to delete some files

//   * If you do not see file extensions, press Alt in the file explorer,

//    select "Tools"->"Folder options..."->"View", and uncheck

//    "Hide extensions for known file types".

//  d) zip the file structure (the project), which is now missing the

//   temporary files

//   * Select all of the files in the project folder

//   * If the resulting zip file is more than 1mb, you have included

//    temporary files mentioned above. Delete temporary files (that don't

//    have a .cpp or .h extension) and try again.

THIS IS WHAT I HAVE GAME.CPP

#include "game.h"

#include "consoleutil.h"

#include "blinkentity.h"

#include "wanderingentity.h"

#include <stdio.h>

// moves for players. each list in order defined by Entity::Move_XXXX variables

char * moveCommands[] = { "wasd", "ijkl" };

const int NUMBER_OF_PLAYERS = sizeof(moveCommands) / sizeof(moveCommands[0]);

Game::Game()

{

state = RUNNING;

size.init(20, 15);

// allocate each entity

list[PLAYER1] = new Entity(4, 3, 1);

list[GOAL1] = new BlinkEntity(10, 7, 4);

list[GOAL2] = new Entity(1, 1, 3);

Vector2 g1 = list[GOAL1]->getPosition();

Vector2 p1 = list[PLAYER1]->getPosition();

list[PLAYER2] = new WanderingEntity(

(g1.x + p1.x) / 2,

(g1.y + p1.y) / 2, 5);

// add 2 more wandering entities

list[WANDERER2] = new WanderingEntity(

(g1.x + p1.x + 4) / 2,

(g1.y + p1.y + 4) / 3, 232);

list[WANDERER3] = new WanderingEntity(

(g1.x + p1.x + 6) / 2,

(g1.y + p1.y) / 2, 178);

// TODO overload operators below to replace logic above

//Vector2 p2 = (g1 + p1) / 2;

//list[PLAYER2] = new Entity(p2.x, p2.y, 5);

}

void Game::draw()

{

// draw the game world

moveCursor(0, 0);

for (int row = 0; row < size.y; row++) {

for (int col = 0; col < size.x; col++) {

putchar('.');

}

putchar('n');

}

// draw players in the game

for (int i = 0; i < LIST_SIZE; ++i) {

list[i]->draw();

}

// inform player how to play

for (int i = 0; i < NUMBER_OF_PLAYERS; i++) {

moveCursor(0, size.y + i);

printf("use "%s" to move %cn",

moveCommands[i], list[i]->getIcon());

}

}

void Game::update()

{

// if the user pressed the escape key

if (userInput == 27) {

state = USER_QUIT;

return;// quit

}

// move the players, based on what key was pressed

int whatMove = Entity::MOVE_NONE, whosTurnItIs;

for (int player = 0; player < NUMBER_OF_PLAYERS; ++player) {

for (int i = 0; moveCommands[player][i] != ''; ++i) {

if (moveCommands[player][i] == userInput) {

whosTurnItIs = player;

whatMove = i;

break;

}

}

if (whatMove != Entity::MOVE_NONE) {

break;

}

}

if (whatMove != Entity::MOVE_NONE) {

list[whosTurnItIs]->move(whatMove);

}

// game logic

for (int i = 0; i < LIST_SIZE; ++i) {

list[i]->update();

}

Entity * player1 = list[PLAYER1];

Entity * player2 = list[PLAYER2];

Entity * goal = list[GOAL1];

Entity * wanderer2 = list[WANDERER2];

Entity * wanderer3 = list[WANDERER3];

if (goal->getPosition().is(player1->getPosition())) {

state = WIN;

}

if (!player1->getPosition().isWithin(0, 0, size.x, size.y)) {

state = LOST;

}

if (player2->getPosition().is(player1->getPosition())) {

state = DEFEAT;

}

// add game logic the will cause the player to lose if the player shares a location with any

// wanderingEntity object 

if (player1->getPosition().is(player2->getPosition()) || player1->getPosition().is(wanderer2->getPosition())

|| player1->getPosition().is(wanderer3->getPosition())){

state = LOST;

}

//show the end-of-game state message when the game ends

if (state != Game::RUNNING) {

moveCursor(0, size.y + NUMBER_OF_PLAYERS + 1);

char * message;

switch (state)

{

case WIN:message = "Goal ACHIEVED!";break;

case LOST:message = "Player lost...";break;

case DEFEAT:message = "Player was CLUBBED";break;

default:message = "unknown state occured";break;

}

printf("%snn", message);

}

}

Game::~Game()

{

// de-allocate each entity

for (int i = 0; i < LIST_SIZE; ++i)

delete list[i];

}

GAME.H

#pragma once

#include "entity.h"

#include "vector2.h"

class Game

{

private:

static const int LIST_SIZE = 6;

Entity* list[LIST_SIZE];

/** labels for which Entity in 'list' is what*/

static const int

PLAYER1 = 0,

PLAYER2 = 1,

GOAL1 = 2,

GOAL2 = 3,

WANDERER2 = 4,

WANDERER3 = 5;

/** width/height of the map */

Vector2 size;

int userInput;

int state;

public:

void setInput(int input){ userInput = input; }

int getState(){ return state; }

/** game state constants */

static const int

RUNNING = 1,

WIN = 2,

LOST = 3,

DEFEAT = 4,

USER_QUIT = -1;

Game();

void draw();

void update();

~Game();

};

VECTOR2.H

#pragma once

/**

 * Object Oriented implementation of a 2 dimensional (math) vector

 */

struct Vector2

{

/** the x, y coordinates */

float x, y;

/** initializes the Vector2 */

Vector2(float x, float y):x(x),y(y){}

/** default constructor - sets x,y to 0,0 */

Vector2():x(0),y(0){}

/** @return true if the given x, y coordinate mathes this Vector2's data */

bool is(float x, float y) const;

/** @return true if the given x, y coordinate mathes this Vector2's data */

bool is(Vector2 xy) const ;

/** @return true if the Vector2 is within the given rectangular boundary */

bool isWithin(float minx, float miny, float maxx, float maxy);

/** re-initialize */

void init(float x, float y);

/** @return a random Vector2 within the specified boundary */

static Vector2 random(Vector2 min, Vector2 max);

/** @return pythagorean distance from the origin */

float magnitude();

Vector2 operator+(Vector2 const & v) const;

Vector2 operator-(Vector2 const & v) const;

Vector2 operator*(float value) const;

Vector2 operator/(float value) const;

Vector2 & operator+=(Vector2& v);

Vector2 & operator-=(Vector2& v);

Vector2 & operator*=(float value);

Vector2 & operator/=(float value);

bool operator==(Vector2 const & v) const { return is(v); }

bool operator!=(Vector2 const & v) const { return !is(v); }

/**

* @param A,B line 1

* @param C,D line 2

* @param point __OUT to the intersection of line AB and CD

* @param dist __OUT the distance along line AB to the intersection

* @return true if intersection occurs between the lines

*/

static bool lineIntersection(const Vector2 & A, const Vector2 & B, 

               const Vector2 & C, const Vector2 & D, 

               float & dist, Vector2 & point);

/**

* @return positive if v2 is clockwise of this vector

* (assume Y points down, X to right)

*/

float sign(const Vector2 & v) const;

/** @return true if this point is inside the given triangle */

bool isInsideTriangle(Vector2 const & a, Vector2 const & b, 

           Vector2 const & c) const;

void limitMagnitude(float max);

};

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question