-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8rainhas.cpp
More file actions
256 lines (249 loc) · 7.07 KB
/
8rainhas.cpp
File metadata and controls
256 lines (249 loc) · 7.07 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <iostream>
#include <fstream>
#include <string>
#include "8rainhas.hpp"
using namespace std;
/**
* @brief Converts the file content into an 8x8 integer matrix.
*
* This function reads a file and creates a dynamically allocated 8x8 integer matrix based on the file content.
* Each line in the file represents a row in the matrix, and each character (0 or 1) represents a column entry.
*
* @param filename The name of the file to read.
* @return A pointer to a dynamically allocated integer matrix.
* Returns nullptr if the file is invalid or if there are errors in reading the file content.
*/
int** convert(const char* filename) {
ifstream file(filename);
string current;
// create the pointer for matrix
int** matrix = new int*[8];
for (int i = 0; i < 8; ++i) {
matrix[i] = new int[8];
}
int i = 0;
while (getline(file, current)) {
string line = "";
int counter = 0;
for (char ch : current) {
// take just the numbers of the file reading
if (ch == '0' || ch == '1') {
line += ch;
if (ch == '1') {
counter++;
}
}
}
if (i >= 8 || line.length() != 8 || counter < 1) {
// clean up the memory if invalid case: (row or column)
for (int k = 0; k < 8; ++k) {
delete[] matrix[k];
}
delete[] matrix;
return nullptr;
}
int j = 0;
for (char ch : line) {
// adding a equivalent int to pointer
matrix[i][j] = ch - '0';
++j;
}
++i;
} return matrix;
}
/**
* @brief Finds horizontal attacks in the matrix.
*
* This function identifies and returns the locations of queens that can attack horizontally.
*
* @param matrix Pointer to the 8x8 integer matrix.
* @return A string representing the positions of attacking queens in horizontal direction.
*/
string horizontal(int** matrix) {
string loc = "";
for (int i = 0; i < 8; ++i) {
string add = "";
int counter = 0;
for (int j = 0; j < 8; ++j) {
if (matrix[i][j] == 1) {
add += to_string(i+1) + "," + to_string(j+1) + " ";
counter++;
}
if (j == 7 && counter == 1) {
add = "";
}
if (j == 7 && add != "") {
if (loc != "") {
loc += "\n";
}
loc += add;
}
}
}
return loc;
}
/**
* @brief Finds vertical attacks in the matrix.
*
* This function identifies and returns the locations of queens that can attack vertically.
*
* @param matrix Pointer to the 8x8 integer matrix.
* @return A string representing the positions of attacking queens in vertical direction.
*/
string vertical(int** matrix) {
string loc = "";
for (int j = 0; j < 8; ++j) {
string add = "";
int counter = 0;
for (int i = 0; i < 8; ++i) {
if (matrix[i][j] == 1) {
add += to_string(i+1) + "," + to_string(j+1) + " ";
counter++;
}
if (i == 7 && counter == 1) {
add = "";
}
if (i == 7 && add != "") {
if (loc != "") {
loc += "\n";
}
loc += add;
}
}
}
return loc;
}
/**
* @brief Finds diagonal attacks in the matrix.
*
* This function identifies and returns the locations of queens that can attack diagonally.
*
* @param matrix Pointer to the 8x8 integer matrix.
* @return A string representing the positions of attacking queens in diagonal direction.
*/
string diagonal(int** matrix) {
string loc;
for (int j = 0; j < 8; ++j) {
string add = "";
int i = 0, counter = 0, k = j;
while (i < 8 && k >= 0) {
if (matrix[i][k] == 1) {
add += to_string(i+1) + "," + to_string(k+1) + " ";
counter++;
}
if (k == 0 && counter == 1) {
add = "";
}
if (k == 0 && add != "") {
if (loc != "") {
loc += "\n";
}
loc += add;
}
i++, k--;
}
}
return loc;
}
/**
* @brief Finds opposite diagonal attacks in the matrix.
*
* This function identifies and returns the locations of queens that can attack along the opposite diagonal.
*
* @param matrix Pointer to the 8x8 integer matrix.
* @return A string representing the positions of attacking queens in opposite diagonal direction.
*/
string di_opo(int** matrix) {
string loc;
for (int j = 0; j < 8; ++j) {
string add = "";
int i = 0, counter = 0, k = j;
while (i < 8 && k < 8) {
if (matrix[i][k] == 1) {
add += to_string(i+1) + "," + to_string(k+1) + " ";
counter++;
}
if (k == 0 && counter == 1) {
add = "";
}
if (k == 0 && add != "") {
if (loc != "") {
loc += "\n";
}
loc += add;
}
i++, k++;
}
}
return loc;
}
/**
* @brief Combines all attack directions.
*
* This function combines the results of horizontal, vertical, diagonal, and opposite diagonal attack detections.
*
* @param matrix Pointer to the 8x8 integer matrix.
* @return A string representing the combined positions of attacking queens in all directions.
*/
string attack(int** matrix) {
string h = horizontal(matrix);
string v = vertical(matrix);
string d = diagonal(matrix);
string o = di_opo(matrix);
string sum;
if (h != "") {
sum = h;
}
if (v != "") {
if (sum != "") {
sum += "\n" + v;
} else {
sum = v;
}
}
if (d != "") {
if (sum != "") {
sum += "\n" + d;
} else {
sum = d;
}
}
if (o != "") {
if (sum != "") {
sum += "\n" + o;
} else {
sum = o;
}
}
return sum;
}
/**
* @brief Main function in charge of the execution of the program.
*
* This function calls the convert function to read the file and process its content.
* It also deallocates memory used by the matrix returned by convert and writes the output to "ataques.txt".
*
* @param filename The name of the file to read.
* @return 0 if the program executes successfully, -1 if an error occurs.
*/
int body(const char* filename) {
int output = 0;
int** matrix = convert(filename);
if (matrix != nullptr) {
string loc = attack(matrix);
if (loc == "") {
output = 1;
} else {
ofstream outfile("ataques.txt");
outfile << loc << endl;
outfile.close();
}
for (int j = 0; j < 8; ++j) {
delete[] matrix[j];
}
delete[] matrix;
} else {
output = -1;
}
return output;
}