vector game

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <vector>

using namespace 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

        const int 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)
		return false;

	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];
		}
	 
	return true;
}	
//*****************************************************************************
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;


Last edited on
Topic archived. No new replies allowed.