-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_square.cpp
189 lines (161 loc) · 4.71 KB
/
magic_square.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
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
#include <iostream>
#include <utility>
#include <algorithm>
#include "magic_square.h"
#include "main.h"
Magic_Square::Magic_Square(const int size) : order(size), board(order, slice_t(order)) {
if (order % 2 == 0) {
throw std::logic_error("Order cannot be even.");
}
if (order > 15) {
throw std::logic_error("Order cannot be above 15.");
}
if (order < 3) {
throw std::logic_error("Order cannot be below 3.");
}
construct();
}
void Magic_Square::print() {
for (const auto& slice : board) {
for (const auto& element : slice) {
if (element < 10) {
std::cout << " " << element << " ";
} else if(element < 100) {
std::cout << " " << element << " ";
} else {
std::cout << element << " ";
}
}
std::cout << "\n";
}
}
// Summing
// Checks if all of the sums of the array are equal
void check_sum(const slice_t& sums) {
for (unsigned int i = 0; i < sums.size() - 1; i++) {
if (sums[i] != sums[i + 1]) {
throw std::logic_error("Not a magic square!");
}
}
}
// Get the sum of an array
int get_sum(const slice_t& slice) {
int sum = 0;
for (const auto& element : slice) {
sum += element;
}
return sum;
}
slice_t Magic_Square::get_row(const int row) {
return board[row];
}
slice_t Magic_Square::sum_row() {
slice_t sums;
for (int i = 0; i != order; i++) {
sums.push_back(get_sum(get_row(i)));
}
check_sum(sums);
return sums;
}
slice_t Magic_Square::get_col(const int col) {
slice_t out_col;
for (const auto& slice : board) {
out_col.push_back((slice)[col]);
}
return out_col;
}
slice_t Magic_Square::sum_col() {
slice_t sums;
for (int i = 0; i != order; i++) {
sums.push_back(get_sum(get_col(i)));
}
check_sum(sums);
return sums;
}
slice_t Magic_Square::get_diag_left_right() {
slice_t diag;
for (int row = 0, col = 0; row != order || col != order; row++, col++) {
diag.push_back(board[row][col]);
}
return diag;
}
slice_t Magic_Square::get_diag_right_left() {
slice_t diag;
for (int row = order - 1, col = order - 1; row >= 0 || col >= 0; row--, col--) {
diag.push_back(board[row][col]);
}
return diag;
}
slice_t Magic_Square::sum_diag() {
slice_t sums;
sums.push_back(get_sum(get_diag_left_right()));
sums.push_back(get_sum(get_diag_right_left()));
check_sum(sums);
return sums;
}
// Creating the magic square
// Generates the magic square
void Magic_Square::construct() {
// Start in middle top of the square
auto point = point_t(0, order / 2);
for (int i = 1; i <= order*order; i++) {
write(point, i);
point = move(point);
}
}
// Checks if the point hasn't been write in yet
bool Magic_Square::is_empty(const point_t& point) {
return board[point.first][point.second] == 0;
}
void Magic_Square::write(const point_t& point, const int element) {
if (element < 0) {
throw std::logic_error("Cannot write negative numbers to board!");
}
board[point.first][point.second] = element;
}
// Move in according to the magic square pattern
point_t Magic_Square::move(const point_t point) {
// Move new point up right.
auto point_new = point_t(point.first - 1, point.second + 1);
point_new = wrap(point_new);
// If point is occupied then move point one down.
if (!is_empty(point_new)) {
// The (row + 2, col - 1) is to negate the first move.
// So the result is (row + 1, col), below the original position.
point_new = point_t(point_new.first + 2, point_new.second - 1);
point_new = wrap(point_new);
}
return point_new;
}
// Move the point if it is outside of the bounds of the square
point_t Magic_Square::wrap(const point_t point) {
int coord_x = point.first;
int coord_y = point.second;
// If coord_x is beyond the right edge, then move to left edge
if (coord_x > order - 1) {
coord_x -= order;
// If coord_x is beyond the left edge, then move to right edge
} else if (coord_x < 0) {
coord_x += order;
}
//Same logic as above
if (coord_y > order - 1) {
coord_y -= order;
} else if (coord_y < 0) {
coord_y += order;
}
return point_t(coord_x, coord_y);
}
// Rotate the magic square to generate the other cases
void Magic_Square::rotate() {
// Transpose matrix
for (int i = 0; i != order - 1; i++) {
for (int j = i + 1; j != order; j++) {
std::swap(board[i][j], board[j][i]);
}
}
// Flip rows
for (int i = 0; i != order/2; i++) {
swap(board[i], board[order-1-i]);
}
}