I can't figure out how to make this more efficient!

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!

main.cpp


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include "bearly.h"

int main()
{
int doorX, doorY, playerX, playerY, bearX, bearY;
BearlyGame game, x, y;

// Door always starts in the same place and stays fixed
doorX = game.xDoor();
doorY = game.yDoor();

while(!game.isOver())
{
// Position of Player Piece, Bear
playerX = game.xPlayer();
playerY = game.yPlayer();
bearX = game.xBear();
bearY = game.yBear();

/* if (playerX < bearX-2) {
game.turn(RIGHT);
}
else {
game.turn(LEFT);
}
if (playerY < bearY-2) {
game.turn(DOWN);
}
else {
game.turn(UP);
} */

// Take Turn (this should be surrounded by some logic
game.turn(LEFT/* LEFT, RIGHT, UP, DOWN, NONE */);
}
return 0;
}




Bearly.cpp

#include "bearly.h"
#include <cstdlib>
#include <ctime>

Coord::Coord(int x, int y)
{
this->x = x;
this->y = y;
}

Coord::Coord()
{
this->x = 0;
this->y = 0;
}

bool Coord::operator==(const Coord &t) const
{
if(this->x == t.x && this->y == t.y)
return true;
return false;
}

// Constructor
BearlyGame::BearlyGame():boardWidth(10),boardHeight(10),bearSense(2)
{
srand((unsigned int)time(NULL));
this->end = false;
this->angry = false;

// 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;
}

this->printBoard();
this->delay(2);

return;
}

// Pause Execution of Game
void BearlyGame::delay(int sec)
{
time_t startTime, curTime;
time(&startTime);
curTime = startTime;
while(difftime(curTime, startTime) < 1)
time(&curTime);
}

// Display Board to the Console
void BearlyGame::printBoard()
{
int i, j;
this->clearBoard();

// Set Tokens
this->board[this->player.y][this->player.x] = 'X';
this->board[this->door.y][this->door.x] = 'D';
this->board[this->bear.y][this->bear.x] = 'B';

// Print Board
cout << "Display Board: " << endl;
for(i = 0; i < boardWidth; i++)
{
for (j = 0; j < 2 * boardWidth; j++)
cout << "-";
cout << endl;

for(j = 0; j < boardWidth; j++)
{
cout << "|" << board[i][j];
}
cout << "|";
cout << endl;
}
for (j = 0; j < 2 * boardWidth; j++)
cout << "-";
cout << endl;
}

void BearlyGame::clearBoard()
{
int i, j;
for(i = 0; i < boardWidth; i++)
for(j = 0; j < boardHeight; j++)
this->board[i][j] = ' ';
}

// Determine if the move is within the bounds of the board
bool BearlyGame::isValid(const Coord &piece, Dir direction)
{
if(direction == LEFT)
{
if(piece.x <= 0)
return false;
}
else if(direction == RIGHT)
{
if(piece.x >= (this->boardWidth - 1))
return false;
}
else if(direction == UP)
{
if(piece.y <= 0)
return false;
}
else if(direction == DOWN)
{
if(piece.y >= (this->boardHeight - 1))
return false;
}
else if(direction == NONE)
return true;
else
return false;

return true;
}

// Translate Move Into a Direction
void BearlyGame::move(Coord &piece, Dir direction)
{
if(!this->isValid(piece, direction))
{
cout << "Attempted invalid move" << endl;
return;
}
if(direction == LEFT)
piece.x--;
else if(direction == RIGHT)
piece.x++;
else if(direction == UP)
piece.y--;
else if(direction == DOWN)
piece.y++;
return;
}

void BearlyGame::moveBear()
{
// Calculate Manhatten Distance
int distX = this->player.x - this->bear.x;
int distY = this->player.y - this->bear.y;
Coord originalBear = this->bear;

// 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();

private:
const int boardWidth;
const int boardHeight;
const int bearSense;
bool isValid(const Coord &, Dir);
void move(Coord &, Dir);
void delay(int);
void moveBear();
void clearBoard();
char board[10][10];
Coord bear;
Coord door;
Coord player;
bool angry;
bool end;
};


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.
Last edited on
Topic archived. No new replies allowed.