-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridGame.cpp
More file actions
188 lines (160 loc) · 3.8 KB
/
Copy pathGridGame.cpp
File metadata and controls
188 lines (160 loc) · 3.8 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
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
181
182
183
184
185
186
187
188
//-----------------------------------------------------------
//Program Name: Fill It!
//Author: Nischaal Cooray
//Desciption: Simulates the effect of the "Bucket" tool from Paint by filling a shape defined by the user.
//-----------------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "GridGame.h"
using namespace std;
//----------------Default Constructor------------
GridGame::GridGame()
{
for (int i = 0; i < 4; i++)
{
vector<int> gridRow;
for (int j = 0; j < 4; j++)
{
gridRow.push_back(1);
}
grid_.push_back(gridRow);
}
}
//-----------------------------------------------
//--------------Alternate Constructor------------
GridGame::GridGame(int maxRow, int maxCol)
{
maxrow_ = maxRow;
maxcol_ = maxCol;
for (int i = 0; i < maxrow_; i++)
{
vector<int> gridRow;
for (int j = 0; j < maxcol_; j++)
{
gridRow.push_back(0);
}
grid_.push_back(gridRow);
}
}
//-----------------------------------------------
//--------------Alternate Constructor------------
GridGame::GridGame(string fileName)
{
ifstream inFile;
inFile.open(fileName);
string lineRead;
int rows = 0, columns = 0;
while (!inFile.eof())
{
inFile >> lineRead;
vector<int> gridRow;
for (int j = 0; j < lineRead.length(); j++)
{
if ((lineRead.substr(j, 1)) == "1")
{
gridRow.push_back(1);
}
else if ((lineRead.substr(j, 1)) == "0")
{
gridRow.push_back(0);
}
else
{
cout << "Error encountered in File!";
}
columns = j + 1;
}
rows++;
grid_.push_back(gridRow);
}
maxrow_ = rows;
maxcol_ = columns;
}
//-----------------------------------------------
//----------------Destructor---------------------
GridGame::~GridGame()
{
}
//-----------------------------------------------
//---------Method to check if a plot is legal----
bool GridGame::isLegalPlot(int startPosRow, int startPosColumn)
{
if ((getVal(startPosRow, startPosColumn) == 0) && (startPosRow < maxrow_) && (startPosColumn < maxcol_))
return true;
else
return false;
}
//-----------------------------------------------
//---------Method to return the value stored at an index----
int GridGame::getVal(int row, int col)
{
return grid_[row][col];
}
//----------------------------------------------------------
//---------Method to check the max number of rows-----------
int GridGame::getRow()
{
return maxrow_;
}
//----------------------------------------------------------
//---------Method to check the max numbers of columns-------
int GridGame::getCol()
{
return maxcol_;
}
//----------------------------------------------------------
//---------Method to update the value at an index-----------
void GridGame::setVal(int row, int col, int value)
{
grid_[row][col] = value;
}
//----------------------------------------------------------
//---Set the start position defined by the user----
void GridGame::startPosition(int startPosRow, int startPosColumn)
{
if (grid_[startPosRow][startPosColumn] != 1)
{
grid_[startPosRow][startPosColumn] = 1;
}
}
//--------------------------------------------------
//----Facilitator to output shape-------------------
void GridGame::output(ostream& out)
{
out << " ";
for (int k = 0; k < maxcol_; k++)
{
if (k > 10)
out << " " << k;
else
out << " " << k;
}
out << endl << " ";
for (int k = 0; k < maxcol_; k++)
{
out << "---";
}
out << endl;
for (int i = 0; i < maxrow_; i++)
{
out << i << "| ";
for (int j = 0; j < maxcol_; j++)
{
if (grid_[i][j] == 1)
out << " 1";
else
out << " 0";
}
out << endl;
}
}
//-----------------------------------------------
//--Method for overloading output operator-------
ostream& operator<<(ostream& out, GridGame& displayGrid)
{
displayGrid.output(out);
return out;
}
//-----------------------------------------------