i'm trying to make a game that has a 20X20 grid, it starts with an ant on 1 side, doodlebug on the other. i know i have to use a vector. i'm trying to get it so both the ant and doodle bug can move either up, down, left, or right while adhering to the 20X20 grid structure. also the possibility to add an ant or doodle bug to the left, right, up or down once again adhering to grid dimensions
if you can point me to the references to accomplish that, that would be great.
Try this. I wrote it for a similar game. You may have to adjust it to your liking but I set it to size 20x20. Have fun!!! P.S.- "Doodlebug" cracked me up!
#include <vector>
usingnamespace std;
struct Grid {
enum DirType {up = 1, down, right, left};
// enumerated type for direction
Grid();
//default constructor
bool LoadGrid(string filename);
// loads the the grid with data
// from file passed in
bool Move(int move, int dir);
// moves to a spot on the board
// (move is number of spaces to move)
// returns false if move is out of bounds
string Location();
// returns a string containing a formatted location
int LocationVal();
//returns the val stored in curr location
constint GRID_MAX;
pair<int, int> location;
vector<vector<int> > pos;
};
//*****************************************************************************
Grid::Grid() : GRID_MAX(20), location(0, 0) {
pos.resize(GRID_MAX);
for (int i = 0; i < GRID_MAX; i++)
pos[i].resize(GRID_MAX);
}
//*****************************************************************************
bool Grid::LoadGrid(string filename) {
int input;
ifstream fin;
fin.open(filename.c_str());
if (!fin)
returnfalse;
for (int i = 0; i < GRID_MAX and fin; i++) // since it is 2D vector
for (int j = 0; j < GRID_MAX and fin; j++) { // it loads backwards
fin >> pos[j][i];
}
returntrue;
}
//*****************************************************************************
bool Grid::Move(int move, int dir) {
bool valid = true;
switch(dir) {
case up:
if ((location.second - move) >= 0) // move is in bounds
location.second -= move;
else
valid = false;
break;
case down:
if ((location.second + move) < GRID_MAX) // move is in bounds
location.second += move;
else
valid = false;
break;
case right:
if ((location.first + move) < GRID_MAX) // move is in bounds
location.first += move;
else
valid = false;
break;
case left:
if ((location.first - move) >= 0) // move is in bounds
location.first -= move;
else
valid = false;
break;
}
return valid;
}
//*****************************************************************************
string Grid::Location() {
stringstream ss;
ss << "grid[" << location.second << "][" << location.first << "]"; // gotta love
// string streams!
return ss.str();
}
//*****************************************************************************
int Grid::LocationVal()
{ return pos[location.first][location.second]; }
Try this driver to see how the object actually works (you must put a space between the moves, I don't validate anything else):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int userMove;
int userDir;
cout << "\nEnter move(1-6) & dir(1-up, 2-down, 3-right, 4-left): \n";
cin >> userMove >> userDir;
cin.ignore(256, '\n');
while ((userMove < 1 or userMove > 6) or (userDir < 1 or userDir > 4) // while invalid
or (!grid.Move(userMove, userDir))) {
cout << "\nI'm sorry, you either entered an invalid move or have\nencountered";
cout << " an impassable wilderness. Please try again.\n";
cout << "\nEnter move(1-6) & dir(1-up, 2-down, 3-right, 4-left): \n";
cin >> userMove >> userDir;
cin.ignore(256, '\n');
}
cout << "You entered:" << setw(43) << right << userMove << " " << userDir
<< endl << endl <<endl;