This repository was archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSquare.java
More file actions
173 lines (147 loc) · 3.92 KB
/
MagicSquare.java
File metadata and controls
173 lines (147 loc) · 3.92 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
import java.util.HashSet;
import java.util.Set;
/**
* Mandatory Assignment 6.15 Due 12/10/2010
* @author Andreas Bok Andersen
*
*/
public class MagicSquare {
// The n/sidelength value of the magicsquare
private int magicNumber;
// Array of integers holding the user's inputs
private int[] inputs;
// Double array containing the magic square
private int[][] magicSquare ;
private int count= 0;
public MagicSquare(int n) {
magicNumber = n;
magicSquare = new int[n][n];
inputs = new int[(int) Math.pow(Double.valueOf(n), 2)];
}
/**
*
* @return the magic constant m using the formula
* m = n * (n^2 + 1) / 2
* ex. n = 3
* m = 3 (3^2 +1) 2
* m = 15
*/
private int computeMagicConstant() {
return (int) (magicNumber *(Math.pow(magicNumber, 2) + 1)/2);
}
/**
*
* @return true if the sum of rows equals magic constant
*/
private boolean validateRows() {
for (int row = 0; row < magicSquare.length; row++) {
int tempSum = 0;
for (int col = 0; col < magicSquare.length; col++) {
tempSum += magicSquare[row][col];
}
if (tempSum != computeMagicConstant()) {
return false;}
}
return true;
}
/**
*
* @return true if the sum of columns equals magic constant
*/
private boolean validateColumns() {
for (int col = 0; col < magicSquare.length; col++) {
int tempSum = 0;
for (int row = 0; row < magicSquare.length; row++) {
tempSum += magicSquare[row][col];
}
if (tempSum != computeMagicConstant()) {
return false;}
}
return true;
}
/**
*
* @return true if sum of diagonals equals magic constant
*/
private boolean validateDiagonals() {
// Check bottom left to top right
for (int cornerRow = getMagicSquare().length -1; cornerRow >= 0; cornerRow--) {
int tempSum = 0;
for (int cornerColumn = 0; cornerColumn < getMagicSquare().length; cornerColumn++) {
tempSum += magicSquare[cornerRow][cornerColumn];
} if (tempSum != computeMagicConstant()) {
return false;
}
}
// Check bottom right to top left
for (int cornerRow = getMagicSquare().length -1; cornerRow >= 0; cornerRow--) {
int tempSum = 0;
for (int cornerColumn = getMagicSquare().length -1; cornerColumn >=0; cornerColumn--) {
tempSum += magicSquare[cornerRow][cornerColumn];
} if (tempSum != computeMagicConstant()) {
return false;
}
}
return true;
}
/**
* Validates if userinput is the full range of numberrs from n to n^2
* Validates if number of inputs equals n^2
* @return
*/
public boolean validateInput() {
// Convert the array to a Set of integers. Duplicates will be eliminated
Set<Integer> setMagicSquare = new HashSet<Integer>();
for (int i = 0; i < inputs.length; i++) {
setMagicSquare.add(inputs[i]);
}
// Check if the number of values equals n^2
if (setMagicSquare.size() != Math.pow(magicNumber, 2)) {
return false;
}
// Check if all values from 1 to n^2 is present
for (int i = 1; i <= Math.pow(magicNumber, 2); i++) {
if (!setMagicSquare.contains(i)) {
return false;
}
}
return true;
}
/**
*
* @param i a value typed in by the user
*/
public void addInt(int i) {
inputs[count] = i;
count++;
}
/**
*
* @return true if the numbers form a magicsquare
*/
public boolean isMagic() {
fillMagicSquare();
return validateInput() && validateRows() &&
validateColumns() &&
validateDiagonals();
}
/**
*
* @return an instance of the magicsquare
*/
public int[][] getMagicSquare() {
return this.magicSquare;
}
/**
* Creates a magicsquare array
*/
private void fillMagicSquare() {
int counter = 0;
for (int row = 0; row < magicNumber; row++) {
for (int col = 0; col < magicNumber; col++ ) {
magicSquare[row][col] = inputs[counter];
counter++;
}
}
}
}