I'm supposed to make the most efficient function to win this game. Only the main.cpp can be edited. I've included all of the needed code. In the main.cpp, I've commented out the part that I have come up with so far. My problem is that the player piece moves blindly while maintaining that 2 space distance from the bear. In the end, it ends up hitting a wall while the bear gets closer. I'm at wits end on this :/ any insight would be greatly appreciated!
// Set Player Position
this->player = Coord(0, 0);
// Set Bear Position
this->bear = Coord(7, 7);
// Set Door Position, Static
this->door = Coord(9, 9);
}
bool BearlyGame::isOver()
{
return this->end;
}
int BearlyGame::xBear()
{
return this->bear.x;
}
int BearlyGame::yBear()
{
return this->bear.y;
}
int BearlyGame::xPlayer()
{
return this->player.x;
}
int BearlyGame::yPlayer()
{
return this->player.y;
}
int BearlyGame::xDoor()
{
return this->door.x;
}
int BearlyGame::yDoor()
{
return this->door.y;
}
bool BearlyGame::angryBear()
{
return angry;
}
void BearlyGame::turn(Dir direction)
{
// Try to Move Player Piece
if(this->isValid(player, direction))
this->move(player, direction);
else
{
if(direction == LEFT)
cout << "Left is invalid move" << endl;
else if(direction == RIGHT)
cout << "Right is invalid move" << endl;
else if(direction == UP)
cout << "Up is invalid move" << endl;
else if(direction == DOWN)
cout << "Down is invalid move" << endl;
else
cout << direction << " is invalid move" << endl;
}
this->printBoard();
this->delay(2);
// Check for a Win
if(this->player == this->door)
{
this->end = true;
cout << "Winner!" << endl;
return;
}
// Check for a loss
if(this->player == this->bear)
{
this->end = true;
cout << "You ran into the bear. Your bold move didn't pan out" << endl;
cout << "You Lost." << endl;
return;
}
// Move The Bear
this->moveBear();
// Check for a loss
if(this->player == this->bear)
{
this->end = true;
cout << "Some bearly bad news. The bear caught up to you." << endl;
cout << "You Lost." << endl;
return;
}
// Should the bear be enraged?
if(abs(distX) <= bearSense && abs(distY) <= bearSense)
this->angry = true;
// Check for Anger
if(this->angry)
{
// Take Shortest Path
if(distX < 0)
this->move(this->bear, LEFT);
else if(distX > 0)
this->move(this->bear, RIGHT);
else if(distY < 0)
this->move(this->bear, UP);
else if(distY > 0)
this->move(this->bear, DOWN);
}
else
{
// Meander
bool invalid = true;
while(invalid)
{
// Pick a random direction
Dir direction = (Dir)(rand() % 4);
if(this->isValid(this->bear, direction))
{
this->move(this->bear, direction);
// Make sure bear does not overlap door
if(this->bear == this->door)
this->bear = originalBear;
else
invalid = false;
}
}
}
// Should the bear be enraged?
if(abs(distX) <= bearSense && abs(distY) <= bearSense)
this->angry = true;
return;
}
Bearly.h
#include <iostream>
using namespace std;
enum Dir {LEFT, RIGHT, UP, DOWN, NONE};
struct Coord
{
int x, y;
Coord();
Coord(int x, int y);
bool operator==(const Coord &c) const;
};
class BearlyGame
{
public:
BearlyGame();
void turn(Dir);
int xBear();
int yBear();
int xDoor();
int yDoor();
int xPlayer();
int yPlayer();
bool angryBear();
bool isOver();
void printBoard();
Welcome to the forum.
If you want our help, start by helping us help you.
You'll want to edit your original post, and put code-tags around all your code.
This link describes how to use code-tags: http://www.cplusplus.com/articles/jEywvCM9/
Code-tags make your code easier to read, and will make it more likely that someone will help you.
You'll probably want to use three separate code-tags, one for each .cpp file.
Also, nobody is going to go through all your code just to figure out what the rules of the game are. Give us a detailed description of what the game rules are, and how you've attempted to implement those rules.