-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConway.java
More file actions
176 lines (173 loc) · 4.82 KB
/
Conway.java
File metadata and controls
176 lines (173 loc) · 4.82 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
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
public class Conway implements ActionListener
{
public JFrame frame;
public JButton quit, auto, step, stepTime, noauto, clear;
public JPanel panelBoard;
public BoardButton[][] board;
public Timer timer;
public int dim=150; //edit this to change size of space. default is 150 x 150 boxes.
public int speed=100; //edit this to change animate speed, 100 (fast) is default this is good for long evolutions
// The lower the speed number, the faster.
// If you have a shorter "evolution", increase the number, to see more intermediary steps
public Conway()
{
frame=new JFrame("Conway's Game of Life");
frame.setLayout(new BorderLayout());
panelBoard=new JPanel();
panelBoard.setLayout(new GridLayout(dim,dim));
board=new BoardButton[dim][dim];
for(int y=0; y<dim; y++)
{
for(int x=0; x<dim; x++)
{
BoardButton button=new BoardButton(frame, panelBoard);
panelBoard.add(button);
board[y][x]=button;
button.addActionListener(this);
}
}
quit=new JButton("Exit");
quit.addActionListener(this);
clear=new JButton("clear");
clear.addActionListener(this);
step=new JButton("step");
step.addActionListener(this);
noauto=new JButton("stop animation");
auto=new JButton("animate");
noauto.addActionListener(this);
auto.addActionListener(this);
JPanel menu=new JPanel(new GridLayout(1,5));
menu.add(step);
menu.add(auto);
menu.add(noauto);
menu.add(clear);
menu.add(quit);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(menu, BorderLayout.NORTH);
frame.add(panelBoard,BorderLayout.CENTER);
frame.pack();
frame.setSize(new Dimension(1000,1000));
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==(Object)(quit))
{
frame.dispose();
System.exit(0);
}
else if(e.getSource()==(Object)(step))
{
step();
}
else if(e.getSource()==(Object)(auto))
{
timer=new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
step();
}
}, 10, speed);
}
else if(e.getSource()==(Object)(noauto))
{
timer.cancel();
}
else if(e.getSource()==(Object)(clear))
{
clear();
}
}
public int getNeighbors(int y, int x)
{
int alive=0;
int startingx=x-1;
int startingy=y-1;
if(startingx<0)
{
startingx++;
}
if(startingy<0)
{
startingy++;
}
int startx=startingx;
int starty=startingy;
while(starty<=y+1 && starty<board.length)
{
startx=startingx;
while(startx<=x+1 && startx<board[0].length)
{
if(starty==y && startx ==x)
{
}
else if(board[starty][startx].alive==true)
{
alive++;
}
startx=startx+1;
}
starty=starty+1;
}
return alive;
}
public void getState()
{
for(int y=0; y<board.length; y++)
{
for(int x=0; x<board[0].length; x++)
{
BoardButton square=board[y][x];
square.surround=getNeighbors(y,x);
}
}
}
public void evalState()
{
for(int y=0; y<board.length; y++)
{
for(int x=0; x<board[0].length; x++)
{
BoardButton square=board[y][x];
square.eval();
}
}
}
public void step()
{
getState();
evalState();
frame.revalidate();
frame.repaint();
}
public void clear()
{
for(int y=0; y<board.length; y++)
{
for(int x=0; x<board[0].length; x++)
{
BoardButton square=board[y][x];
square.alive=false;
square.setBackground(Color.WHITE);
}
}
frame.revalidate();
frame.repaint();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new Conway();
}
});
}
}