I know this question has already been answered 10 times but while looking at all
ID: 3697238 • Letter: I
Question
I know this question has already been answered 10 times but while looking at all the different results none of them have user input. They also don't show the grid (1 2 3 4)
Ex: System.out.println("Please enter list of (i,j) pairs for populated cells: ")
Question:
You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton discussed in class. See for example
Our simplified version has a 10 x 10 grid, numbered like this:
0 1 2 3 4 5 6 7 8 9
0
1
2
3
4
5
6
7
8
9
The grid is represented by a 10 x 10 2-dimensional integer array. If the grid point (i, j) is “populated”, the array element [i][j] contains 1; otherwise it contains 0. Elements along the edges, i == 0 or 9, or j == 0 or 9, are always unpopulated.
When we display the grid, a populated cell is indicated by a ‘#’; an unpopulated cell is indicated by a space.
What your program should do:
Prompt the user to enter a list of (i,j) pairs (both non-negative integers)
(stop when a negative integer is read for either i or j)
Prompt the user to enter the number of time steps
Initialize the grid based on the (i,j) pairs entered by the user
Display the initial state of the grid
(call the displayGrid() method)
For each time step,
update the grid according to Conway’s rules (call the updateGrid() method)
display the grid (call the displayGrid() method)
We follow Conway’s standard rules for updating the cells.
For a cell that is “populated”, if the cell has <= 1 neighbors, or > = 4 neighbors, it dies (becomes 0). Otherwise, it survives (remains 1).
For a cell that is not populated, if the cell has exactly 3 neighbors, it becomes populated (becomes 1).
Cells on the edge always remain unpopulated (0).
Some sample runs are shown in text files on libra in ~whsu/csc210/P8/.
The displayGrid() method has prototype:
void displayGrid(int mat[][]);
It displays the borders of the grid (see sample runs below), and prints the 10 x 10 grid of cells. Populated cells are indicated with a ‘#’ sign, unpopulated cells with a space.
The updateGrid() method has prototype:
void updateGrid(int mat[][]);
mat is the 2-dimensional array that contains the current state of the grid. The method counts the neighbors in each cell of the grid, updates the cells in the grid according to Conway’s rules, and returns the grid.
Sample runs:
libra% java Life
Please enter list of (i,j) pairs for populated cells
(negative i or j to quit): 6 4 6 5 6 6 6 7 6 8 -1 -1
Enter number of time steps: 5
Initial grid:
0123456789
0
1
2
3
4
5
##### 6
7
8
9
Time step 1
0123456789
0
1
2
3
4
### 5
### 6
### 7
8
9
Time step 2
0123456789
0
1
2
3
# 4
# # 5
# # 6
# # 7
# 8
9
Time step 3
0123456789
0
1
2
3
# 4
### 5
## ## 6
### 7
# 8
9
Time step 4
0123456789
0
1
2
3
### 4
# # 5
# # 6
# # 7
### 8
9
Time step 5
0123456789
0
1
2
# 3
### 4
# # # 5
### ## 6
# # # 7
### 8
9
Explanation / Answer
import javax.swing.*; public class SwingLayoutDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; private JLabel msglabel; public SwingLayoutDemo(){ prepareGUI(); } public static void main(String[] args){ SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo(); swingLayoutDemo.showGridLayoutDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showGridLayoutDemo(){ headerLabel.setText("Layout in action: GridLayout"); JPanel panel = new JPanel(); panel.setBackground(Color.darkGray); panel.setSize(300,300); GridLayout layout = new GridLayout(0,3); layout.setHgap(10); layout.setVgap(10); panel.setLayout(layout); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); panel.add(new JButton("Button 4")); panel.add(new JButton("Button 5")); controlPanel.add(panel); mainFrame.setVisible(true); } }