-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
180 lines (145 loc) · 3.92 KB
/
grid.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "grid.h"
#include "constants.h"
#include "iostream"
#include "testcreature.h"
#include "walls.h"
#include "prey.h"
#include "predator.h"
#include "random.h"
#include <iostream>
using namespace std;
grid::grid()
{
intializeGrid(); //sets EVERYTHING to NULL.
placeWalls();
for (int i=0;i<AMOUNT_OF_PREDATORS;i++)
{
placeCreature(new predator);
}
for (int i=0;i<AMOUNT_OF_PREY;i++)
{
placeCreature(new prey);
}
}
grid::~grid()
{
}
void grid::intializeGrid() //currently points EVERYTHING to null
{
for (int i=0;i<ROWS;i++)
{
for (int j=0;j<COLLS;j++)
{
if (_board[i][j]!=NULL)
{
_board[i][j]=NULL; //has not taken walls into account
}
}
}
}
/***********************************
* Function:placeWalls
* Purpose: places the walls
* Parameters:
* Returns:
* Summary:places a wall creature around the boarders
* Notes:
************************************/
void grid::placeWalls()
{
for (int i=0;i<ROWS;i++)
{
_board[0][i]=new walls(0,i);
_board[ROWS-1][i]=new walls(ROWS-1,i);
}
for (int i=0;i<COLLS;i++)
{
_board[i][0]=new walls(i,0);
_board[i][COLLS-1]=new walls(i,COLLS-1);
}
}
void grid::placeCreature(creature* critter) //places creature in a random empty spot
{
//random numbers
if (AMOUNT_OF_CREATURES<(ROWS-2)*(COLLS-2))
{
random ran1(0,ROWS-1);
random ran2(0,COLLS-1);
if (DEBUG)
{
cout<<"ran1: ";ran1.printRandom();cout<<endl;
cout<<"ran2: ";ran2.printRandom();cout<<endl<<endl;
}
bool tryAgain=true;
while (tryAgain==true) //keeps trying to place critter in a random place until success
{
if (_board[ran1.getRandomNumber()][ran2.getRandomNumber()]==NULL) //if the spot is empty...
{
_board[ran1.getRandomNumber()][ran2.getRandomNumber()]=critter; //put a critter.
critter->set_position(ran1.getRandomNumber(),ran2.getRandomNumber()); //sets new coords
if(DEBUG)
{
cout<<"PLACED! "<<endl;
cout<<"ran1: ";ran1.printRandom();cout<<endl;
cout<<"ran2: ";ran2.printRandom();cout<<endl;
critter->get_position().print_xy(); cout<<endl;
}
tryAgain=false;
}
else
{
ran1.newRandom(); //get new random coordinates...
ran2.newRandom();
// if (DEBUG)
// {
// cout<<"Trying again..."<<endl;
// cout<<"ran1: ";ran1.printRandom();
// cout<<" ran2: ";ran2.printRandom();cout<<endl;
// }
tryAgain=true;
}
}
}
else
{
cout<<"TOO MANY CRITTERS!!!"<<endl;
exit(0);
}
}
void grid::step()
{
for (int i=1;i<ROWS-1;i++)
{
for (int j=1;j<COLLS-1;j++)
{
if (_board[i][j]!=NULL)
{
_board[i][j]->move(_board);//<---@@@@@ problem
//_board[i][j]->get_position().print_xy();cout<<endl;
}
else
{
//cout<<"grid step empty : "<<endl;
}
}
}
}
void grid::print()
{
for (int i=0;i<ROWS;i++)
{
for (int j=0;j<COLLS;j++)
{
if (_board[i][j]!=NULL)//if the slot is taken print that creature's type
{
cout<<_board[i][j]->get_creatureType()<<" "; //currently printing NULL.
}
else
{
cout<<EMPTY<<" "; //prints a empty slot
}
//@@@@@@ want set and print out the wall next
}
cout<<endl;
}
}