-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSquare.java
More file actions
139 lines (127 loc) · 3.79 KB
/
MagicSquare.java
File metadata and controls
139 lines (127 loc) · 3.79 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
//******************************************************************************
// Filename: MagicSquare.java
//
// Author: David C. Drake (https://davidcdrake.com)
//
// Description: MagicSquare applet class for creating a colorful square of
// numbers such that the sum of each row, column, and diagonal is
// the same. That sum is also displayed below the square.
//******************************************************************************
import java.awt.*;
import java.applet.Applet;
import java.util.Random;
import java.awt.event.*;
import javax.swing.Timer;
public class MagicSquare extends Applet {
final int NUM_ROWS = 5, NUM_COLS = NUM_ROWS, DELAY = 1000;
Random r = new Random();
Timer timer;
Color special;
boolean done;
int i, j, m, red, green, blue, firstSum, sum;
int[][] square = new int[NUM_ROWS][NUM_COLS];
public void init() {
setBackground(Color.white);
resize(40 * NUM_ROWS, 40 * NUM_COLS);
timer = new Timer(DELAY, new ReboundActionListener());
timer.start();
i = NUM_ROWS - 1;
j = NUM_ROWS / 2;
done = false;
for (m = 1; m <= NUM_ROWS * NUM_COLS; m++) {
square[i][j] = m;
if (++i == NUM_ROWS) {
i = 0;
}
if (++j == NUM_ROWS) {
j = 0;
}
if (square[i][j] != 0 {
if (--j < 0) {
j = NUM_ROWS - 1;
}
if (i <= 1) {
i += NUM_ROWS;
}
i -= 2;
}
}
sum = test();
}
public void paint(Graphics g) {
for (m = 0; m < NUM_ROWS * NUM_COLS; m++) {
red = Math.abs(r.nextInt() % 256);
green = Math.abs(r.nextInt() % 256);
blue = Math.abs(r.nextInt() % 256);
special = new Color(red, green, blue);
g.setColor(special);
i = m / NUM_ROWS;
j = m % NUM_ROWS;
g.drawString("" + square[i][j], 40 + 25 * j, 50 + 25 * i);
if (m == (NUM_ROWS * NUM_COLS) - 1) {
i = NUM_ROWS + 1;
j = 1;
g.drawString("" + sum, 40 + 25 * j, 50 + 25 * i);
}
}
}
public int test() {
//--------------------------------------------------------------------------
// Test the sum of each column.
//--------------------------------------------------------------------------
m = firstSum = 0;
for (i = 0; i < NUM_ROWS; i++) {
sum = 0;
for (j = 0; j < NUM_COLS; j++) {
sum += square[i][j];
}
if (i == 0) {
firstSum = sum;
} else if (sum != firstSum) {
m++;
}
}
if (m > 0) {
System.out.println("Error(s) in column sums: " + m);
}
//--------------------------------------------------------------------------
// Test the sum of each row.
//--------------------------------------------------------------------------
m = 0;
for (j = 0; j < NUM_COLS; j++) {
sum = 0;
for (i = 0; i < NUM_ROWS; i++) {
sum += square[i][j];
}
if (sum != firstSum) {
m++;
}
}
if (m > 0) {
System.out.println("Error(s) in row sums: " + m);
}
//--------------------------------------------------------------------------
// Test the sum of each diagonal.
//--------------------------------------------------------------------------
sum = 0;
for (i = 0, j = 0; i < NUM_ROWS; i++, j++) {
sum += square[i][j];
}
if (sum != firstSum) {
System.out.println("Error in sum of top-left to bottom-right diagonal.");
}
sum = 0;
for (i = NUM_ROWS - 1, j = 0; i >= 0; i--, j++) {
sum += square[i][j];
}
if (sum != firstSum) {
System.out.println("Error in sum of top-right to bottom-left diagonal.");
}
return firstSum;
}
private class ReboundActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
repaint();
}
}
}