-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.cpp
More file actions
125 lines (114 loc) · 2.96 KB
/
grid.cpp
File metadata and controls
125 lines (114 loc) · 2.96 KB
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
/********************************************************************
* Name : Kevin Shaffer
* Student ID : 106069743
* Class : CSCI 2312-001
* HW# : 2
* Due Date : Sep 12th, 2013
* Description : Grid object based on a 2d vector with added
functionality such as ToString and Randomize.
********************************************************************/
#include "grid.h"
#include <vector>
#include <string>
#include <time.h>
#include <stdlib.h>
Grid::Grid(int rows, int cols) :
_vec(rows, std::vector<int>(cols)), _rows(rows), _columns(cols)
{
// seed a RNG with current date time
srand(time(0));
}
Grid::Grid(int rows, int cols, int seed) :
_vec(rows, std::vector<int>(cols)), _rows(rows), _columns(cols)
{
srand(seed);
}
Grid::Grid(std::vector< std::vector<int> > vec) :
_vec(vec)
{
srand(time(0));
}
int Grid::getColumns() const
{
return _columns;
}
int Grid::getRows() const
{
return _rows;
}
void Grid::Randomize()
{
// determine 1/3 of the cells (rounded down)
Randomize(_rows * _columns / 3);
}
void Grid::Randomize(const int numCellsToChange)
{
int cellsChanged = 0;
// loop until 1/3 of the cells are changed
while (cellsChanged < numCellsToChange)
{
// randNum is a random number from 0 to (# of elements in grid) - 1
// randRow is a random vector<int>
// randRow is a random element in the vector<int>
// location is a reference to the randomly selected integer in the grid
int randNum = rand() % (_rows * _columns),
randRow = randNum / _columns,
randCol = randNum % _columns;
int& location = _vec[randRow][randCol];
// if the location hasn't already been set
if (location == 0)
{
location = 1;
cellsChanged++;
}
}
}
std::string Grid::ToString()
{
std::string output;
// loop through the rows in the grid
for (std::vector< std::vector<int> >::iterator it_x = _vec.begin();
it_x != _vec.end();
++it_x)
{
// add new line except on the first row
if (it_x != _vec.begin())
output.append("\n");
// loop through the columns in the grid
for (std::vector<int>::iterator it_y = it_x->begin();
it_y != it_x->end();
++it_y)
{
// add a space (except the first column) and the digit to ouput
if (it_y != it_x->begin())
output += " ";
output += *it_y + 48;
}
}
return output;
}
Grid Grid::operator *(const Grid& grid2) const
{
// if the grids aren't the same size
// then return a blank grid
// !!! THIS COULD BE IMPROVED !!!
if (_rows != grid2.getRows() ||
_columns != grid2.getColumns())
return Grid(0, 0);
// create new vector with same size as the other two
std::vector< std::vector<int> > vec(_rows, std::vector<int>(_columns));
// loop through all elements in the grids
for(int x = 0; x < _rows; x++)
{
for (int y = 0; y < _columns; y++)
{
// assign result from the multiplication to the new vector
vec[x][y] = _vec[x][y] * grid2[x][y];
}
}
return Grid(vec);
}
std::vector<int> Grid::operator [](const int row) const
{
return _vec[row];
}