-
Notifications
You must be signed in to change notification settings - Fork 0
/
MineSweeper.cpp
99 lines (97 loc) · 3.34 KB
/
MineSweeper.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
#include <iostream>
#include <fstream>
#include <string>
int index(int rowNum, int colNum, int nCols)
{
return (rowNum * nCols + colNum);
}
int main(int argc, char **argv)
{
std::string fileName = argv[1];
std::fstream filePtr(fileName);
std::string line;
while(std::getline(filePtr, line))
{
size_t delimiter_position = line.find(',');
int nrows = std::stoi(line.substr(0, delimiter_position));
line = line.substr(delimiter_position + 1, line.length() - delimiter_position);
delimiter_position = line.find(';');
int ncols = std::stoi(line.substr(0, delimiter_position));
line = line.substr(delimiter_position + 1,line.length() - delimiter_position);
// std::cout << nrows << '\t' << ncols << '\t' << line << std::endl;
for(int i = 0; i < line.length(); i++)
{
if(line[i] == '*')
{
std::cout << line[i];
}
else
{
int rowNum = i / ncols;
int colNum = i % ncols;
int num_Adjacent_Mines = 0;
//Checking Adjacent Locations for Mines
if(rowNum - 1 >= 0) // 1
{
if(line[index(rowNum - 1, colNum, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
if(rowNum - 1 >= 0 && colNum - 1 >= 0) //2
{
if(line[index(rowNum - 1, colNum - 1, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
if(rowNum - 1 >= 0 && colNum + 1 < ncols) //3
{
if(line[index(rowNum - 1, colNum + 1, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
if(rowNum + 1 < nrows) //4
{
if(line[index(rowNum + 1, colNum, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
if(rowNum + 1 < nrows && colNum - 1 >= 0) //5
{
if(line[index(rowNum + 1, colNum - 1, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
if(rowNum + 1 < nrows && colNum + 1 < ncols) //6
{
if(line[index(rowNum + 1, colNum + 1, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
if(colNum + 1 < ncols) //7
{
if(line[index(rowNum, colNum + 1, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
if(colNum - 1 >= 0) //8
{
if(line[index(rowNum, colNum - 1, ncols)] == '*')
{
num_Adjacent_Mines++;
}
}
std::cout << num_Adjacent_Mines;
}
}
std::cout << std::endl;
}
filePtr.close();
return 0;
}