-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridClass.cpp
117 lines (100 loc) · 1.96 KB
/
GridClass.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
#include "GridClass.h"
GridClass::GridClass()
{
for (int i = 0; i < MAXCELLS; ++i)
{
grid[i] = 0;
}
}
void GridClass::getRow(const int rowNum, int row[]) const
{
int startIndex = (rowNum - 1) * 9;
for (int i = 0; i < 9; ++i)
{
row[i] = grid[startIndex + i];
}
}
void GridClass::getCol(const int colNum, int col[]) const
{
for (int i = 0; i < 9; ++i)
{
col[i] = grid[(colNum - 1) + (i * 9)];
}
}
void GridClass::getBox(const int boxNum, int box[]) const
{
int x1 = (boxNum - 1) / 3;
int x2 = (boxNum - 1) % 3;
int startingIndex = 27 * x1 + 3 * x2;
for (int i = 0; i < 9; ++i)
{
box[i] = grid[startingIndex + 9 * (i/3) + (i % 3)];
}
}
int GridClass::getBoxNum(const int row, const int col) const
{
int rowNum = (row-1) / 3;
int colNum = (col-1) / 3;
return rowNum * 3 + colNum + 1;
}
void GridClass::loadGrid(const string fname)
{
ifstream inFile;
string line;
string val;
inFile.open(fname.c_str());
for (int i = 0; i < MAXCELLS; ++i)
{
if (i % 9 == 0)
{
getline(inFile, line);
}
val = line[i % 9];
grid[i] = atoi(val.c_str());
}
inFile.close();
}
void GridClass::writeCell(const int row, const int col, const int val)
{
grid[(row - 1) * 9 + (col - 1)] = val;
}
void GridClass::printGrid()
{
int val;
bool newline = true;
cout << "\t-------------------------" << endl;
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
if (j % 3 == 0)
{
if (newline)
{
cout << "\t";
}
cout << "| ";
newline = false;
}
val = grid[i*9 + j];
if (val != 0)
{
cout << val << " ";
}
else
{
cout << " ";
}
}
cout << "|" << endl;
if (i % 3 == 2)
{
cout << "\t-------------------------" << endl;
}
newline = true;
}
}
int GridClass::readCell(const int row, const int col)
{
return grid[(row - 1) * 9 + (col - 1)];
}