Question: JAVA!!! I really need help with this problem. I don't have any ideas how to save and recall the state of the Game of Life.
JAVA!!! I really need help with this problem. I don't have any ideas how to save and recall the state of the Game of Life.
Here's the instruction:
Now, the state information is what's stored in the "two-dimensional" matrix you created in for Part I. What you need to do is serialize the data for storage into a file. You may find the notes (documentation) for java.io.Seralizable to be helpful.
Now, this serialized data (yes, it just means that it's been arranged into a single string of bytes) needs to be stored. Recall that java.io is structured around input and output streams. So, you will need to create an output stream (with an associated file open for writing) to save the state of the game. The serialized data can be stored using that output stream. Similarly, to restore the stored state of the game, you will need to create an input stream (with an associated file open for reading). Some research on your part will make this operation relatively trivial.
The question may arise about user interface for saving and restoring the state information. For example, you could have a textbox in your user interface, with Save and Restore buttons (or other similar captions). This is a minimal user interface, which is rather straight-forward to code. The issue becomes one of making sure that the user is providing reasonable file and folder information. To help the user give folder information, one could use javax.swing.JFileChooser. This class supports File Open and File Save As options. This circumvents a number of the potential problems with opening files for reading or writing, since the user will be 'guided' to give reasonable directory information. (There is an extra-credit option for filtering the files displayed by the File Open, and possibly amending the file name for File Save.)
Note: no matter how you support the user providing filename information, it is reasonable for the application to 'ask' before it overwrites a file. With this sort of game is not typical that a user will want to open a saved game, let it run for a while, then save the updated state (overwriting the formerly saved state). The initial state is of greater interest than the resultant state, since the resultant state is derivable in a mechanical way from the starting point. So, one of the requirements for full credit is to check for the existence of the output file and confirm overwrite before saving, even if it is the file that was read to give this starting state. (A simple confirm dialog box is supported by JOptionPane.)
Here is what I've done so far:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*;
public class runMain extends JFrame implements ActionListener, Runnable{ // Label the number of generation private JLabel generationLabel; // Label the start/stop/reset buttons private JButton start; private JButton stop; private JButton restart; // The inital number of generation private int generation = 0; // Thread clas object private Thread t; private boolean flag = true; // Label the cells private MarkLabel[][] mark; // Declare state of the game private boolean gameLife = false; /** * Construct game of life with user interface */ public runMain(int ROW, int COL){ // Set up window super("Life - Controller"); setLocation(25,25); setSize(450,450); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creating the labels for cells on board game mark = new MarkLabel[ROW + 2][COL + 2]; for (int row = 0; row < ROW + 2; row++) { for (int col = 0; col < COL + 2; col++) { mark[row][col] = new MarkLabel(); } } // Set size of horizontal and vertical lines and set color for lines JPanel boardPanel = new JPanel(new GridLayout(ROW, COL, 1, 1)); // Set background color boardPanel.setBackground(Color.LIGHT_GRAY); // Adding each label to the board panel and add to each of them to its neigbors for(int row = 1; row class MarkLabel extends JLabel implements MouseListener { // Declare the color for gameboard and clicked cells static final Color[] color = {Color.GRAY, Color.RED}; // Declare state of the game and number of neighbor cells private int state, newState, nCell; // Set up the array private MarkLabel[] neighborCell = new MarkLabel[8]; MarkLabel() { // State of the game DEAD state = newState = 0; // Show up color setOpaque(true); setBackground(color[0]); // To select new ALIVE cells addMouseListener(this); } // Adding neighbor cell void nextAdd(MarkLabel n) { neighborCell[nCell++] = n; } // Check cells whether it should stay ALIVE or DEAD void checkState() { int Alive = 0; for(int i = 0; i < nCell; i++) Alive += neighborCell[i].state; // If state is alive: if(state == 1) { // Cell with fewer 2 LIVE neighbor dies if(Alive < 2) newState = 0; // Cell with more than 3 neigbor dies if(Alive > 3) newState = 0; } // If DEAD cells with 3 LIVE neighbors will become ALIVE else { if(Alive == 3) newState = 1; } } // Update the state void updateState() { if(state != newState) { state = newState; setBackground(color[state]); } } // Call when the game is reseted void restart() { if(state == 1 || newState == 1) { state = newState = 0; setBackground(color[state]); } } /** * This method takes action from clicking mouse from the user */ // If mouse is click on a cell, it become ALIVE @Override public void mouseClicked(MouseEvent e) { state = newState = 1; setBackground(color[1]); } // If click on a ALIVE cell, hold and drag that cell, it will become DEAD @Override public void mouseReleased(MouseEvent e) { state = newState = 0; setBackground(color[0]); } // Do nothing for all of these below public void mousePressed(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
