Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java ----> Game of Life (Abstract Class Version) [3000 points total] if i am sat

ID: 638967 • Letter: J

Question

Java ----> Game of Life (Abstract Class Version) [3000 points total]

if i am satisfied with the work i will provide another link for another 1500 points .....total of 3000 points)

===>>> Implement all the functions that I have and make the program working

In this assignment, you will define two member functions of class Square: bool getLiveInfo() and void update() as pure virtual functions by the "=0" syntax. As a result, class Square will become an abstract class. As we studied in class, no concrete objects can be created through an abstract class. That means that class Square will only serve as a parent class of class Cell. Class Square, however, can be used as a pointer or reference data type of child class objects as "Square * _squares[ROWS][COLS]" in class Grid. In addition, those two pure virtual member functions must be overridden by child class.

Rules:

1) Any live cell with fewer than two live neighbours dies, as if caused by under-population.

2) Any live cell with two or three live neighbours lives on to the next generation.

3) Any live cell with more than three live neighbours dies, as if by overcrowding.

4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

__________

Hints for Game of Life

You don't have to build GUI interface for this simulation unless you are confident with that. I expected command output like the one given in the sample output.

initialize() basically sets up cells on the grid initially

run() starts the simulation run by run (generation by generation)

All parameters are specified in class Param.

Make class Square as an abstract one and class Cell as its child class

A couple of methods in class Square (those virtual ones) need to be abstract methods as well.

The newly calculated state of a cell should not be used for updating its neighbors in the present run.

________________________

public class Grid
{
   final class DefineConstants
   {
       public static final int ROWS = 20;
       public static final int COLS = 20;
       public static final int NUM_CELLS = 40;
       public static final char LIVE_IMAGE = 'O';
       public static final char DEAD_IMAGE = '-';
       public static final int NUM_RUNS = 10;
       public static final int NUM_DISPLAY = 1;
  
   }
  
  
  
   /*
   //Predeclarations in C++
   *
   class Square;      
   class Grid;
   class Coordinate;
   class Cell;
   */
  

  
  
   //__________________________________________________________________
  

   public class Coordinate
   {
       //Constructors
       public Coordinate()
       {
           _x = 0;
           _y = 0;
       }
       public Coordinate(int x, int y)
       {
           _x = x;
           _y = y;
       }
       public void dispose()
       {
       }

       public final int getX()
       {
           return _x;
       }
       public final int getY()
       {
           return _y;
       }
       public final void setX(int anX)
       {
           _x = anX;
       }
       public final void setY(int aY)
       {
           _y = aY;
       }

       private int _x;
       private int _y;
   }
  
   //_____________________________________________________________________________
  
  
  
   //Class Square serves as an abstract class from which class Cell inherits.
   //No concrete objects of Square can be created because of the nature of its abstract
   public abstract class Square
   {
  
       /*
      
       Square(Grid aGrid, Coordinate aCoord, sbyte aImage);

       public void dispose();


       sbyte getImage();
  
       Grid getGrid();
  
       void setImage(sbyte NamelessParameter);
  
       Coordinate getCoordinate();
      
      
*/


       //This function will be overwritten by getLiveInfo() in a child class (Cell)
       public abstract boolean getLiveInfo(); //Pure virtual function

  

       //This function will be overwritten by update() in a child class (Cell)
       public abstract void update(); //Pure virtual function
       private Grid _grid; //The grid to which this square belongs
       private Coordinate _coord; //The coordinate of this square on the grid
       private byte _image; //Printing form of this object. For example: live cell: O, dead live: -
   }
  
   //_____________________________________________________________________________
  
   //Child class
   abstract public class Cell extends Square
   {
       /*
      
       //Constructor of Grid. Initializer will be used to initialize the parent part
       Cell(Grid aGrid, Coordinate aCoord, sbyte aImage, int f);

      
       boolean getLiveInfo();
      
               void setLiveInfo(boolean NamelessParameter);

           //Count how many live cells among the eight neighbors using the rules
           int countLiveNeighbors();

           //When a cell becomes dead, _lifeSpan will be reset to zero
           void resetLifeSpan();

           //If a cell continues to be live during next generation, _lifeSpan will increment by one
               void incrementLiveSpan();

           //Based on the number of live neighbor cells, update the state of each cell
           //for next generation (run) using the rules
                   void update();

*/

       private int _lifeSpan; //The number of generations this cell has lived
       private boolean _toBeLive; //True if this cell will be live during next generation
   }
  
  
   //_____________________________________________________________________________
  
   abstract public class grid
   {
  
       /*
      
       Grid();
  
   public void dispose();

  
       void initialize();
  
   void run();
  
   void display();
  
   void getStats();
  
   Square getSquare(int x, int y);
  
   void addCells();
  
  
   */
      

       private Square[][] _squares = new Square[DefineConstants.ROWS][DefineConstants.COLS]; //A 2D array of pointers to squares (parent class of cells)
   }

_________

//GameOfLife.java

public class GameOfLife
{
   public static int Main()
   {
       Grid g = new Grid();

       g.initialize();
       g.run();

       if (g != null)
       g.dispose();
// return 0;

   }
}


========================================

Output:

  

Explanation / Answer

Java Program for Game of life : ?import java.awt.*; import java.awt.event.*; import java.net.URI; import java.util.ArrayList; import java.util.ConcurrentModificationException; import javax.swing.*; /** * Conway's game of life is a cellular automaton devised by the * mathematician John Conway. */ public class ConwaysGameOfLife extends JFrame implements ActionListener { private static final Dimension DEFAULT_WINDOW_SIZE = new Dimension(800, 600); private static final Dimension MINIMUM_WINDOW_SIZE = new Dimension(400, 400); private static final int BLOCK_SIZE = 10; private JMenuBar mb_menu; private JMenu m_file, m_game, m_help; private JMenuItem mi_file_options, mi_file_exit; private JMenuItem mi_game_autofill, mi_game_play, mi_game_stop, mi_game_reset; private JMenuItem mi_help_about, mi_help_source; private int i_movesPerSecond = 3; private GameBoard gb_gameBoard; private Thread game; public static void main(String[] args) { // Setup the swing specifics JFrame game = new ConwaysGameOfLife(); game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.setTitle("Conway's Game of Life"); game.setIconImage(new ImageIcon(ConwaysGameOfLife.class.getResource("/images/logo.png")).getImage()); game.setSize(DEFAULT_WINDOW_SIZE); game.setMinimumSize(MINIMUM_WINDOW_SIZE); game.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - game.getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height - game.getHeight())/2); game.setVisible(true); } public ConwaysGameOfLife() { // Setup menu mb_menu = new JMenuBar(); setJMenuBar(mb_menu); m_file = new JMenu("File"); mb_menu.add(m_file); m_game = new JMenu("Game"); mb_menu.add(m_game); m_help = new JMenu("Help"); mb_menu.add(m_help); mi_file_options = new JMenuItem("Options"); mi_file_options.addActionListener(this); mi_file_exit = new JMenuItem("Exit"); mi_file_exit.addActionListener(this); m_file.add(mi_file_options); m_file.add(new JSeparator()); m_file.add(mi_file_exit); mi_game_autofill = new JMenuItem("Autofill"); mi_game_autofill.addActionListener(this); mi_game_play = new JMenuItem("Play"); mi_game_play.addActionListener(this); mi_game_stop = new JMenuItem("Stop"); mi_game_stop.setEnabled(false); mi_game_stop.addActionListener(this); mi_game_reset = new JMenuItem("Reset"); mi_game_reset.addActionListener(this); m_game.add(mi_game_autofill); m_game.add(new JSeparator()); m_game.add(mi_game_play); m_game.add(mi_game_stop); m_game.add(mi_game_reset); mi_help_about = new JMenuItem("About"); mi_help_about.addActionListener(this); mi_help_source = new JMenuItem("Source"); mi_help_source.addActionListener(this); m_help.add(mi_help_about); m_help.add(mi_help_source); // Setup game board gb_gameBoard = new GameBoard(); add(gb_gameBoard); } public void setGameBeingPlayed(boolean isBeingPlayed) { if (isBeingPlayed) { mi_game_play.setEnabled(false); mi_game_stop.setEnabled(true); game = new Thread(gb_gameBoard); game.start(); } else { mi_game_play.setEnabled(true); mi_game_stop.setEnabled(false); game.interrupt(); } } @Override public void actionPerformed(ActionEvent ae) { if (ae.getSource().equals(mi_file_exit)) { // Exit the game System.exit(0); } else if (ae.getSource().equals(mi_file_options)) { // Put up an options panel to change the number of moves per second final JFrame f_options = new JFrame(); f_options.setTitle("Options"); f_options.setSize(300,60); f_options.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - f_options.getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height - f_options.getHeight())/2); f_options.setResizable(false); JPanel p_options = new JPanel(); p_options.setOpaque(false); f_options.add(p_options); p_options.add(new JLabel("Number of moves per second:")); Integer[] secondOptions = {1,2,3,4,5,10,15,20}; final JComboBox cb_seconds = new JComboBox(secondOptions); p_options.add(cb_seconds); cb_seconds.setSelectedItem(i_movesPerSecond); cb_seconds.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { i_movesPerSecond = (Integer)cb_seconds.getSelectedItem(); f_options.dispose(); } }); f_options.setVisible(true); } else if (ae.getSource().equals(mi_game_autofill)) { final JFrame f_autoFill = new JFrame(); f_autoFill.setTitle("Autofill"); f_autoFill.setSize(360, 60); f_autoFill.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - f_autoFill.getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height - f_autoFill.getHeight())/2); f_autoFill.setResizable(false); JPanel p_autoFill = new JPanel(); p_autoFill.setOpaque(false); f_autoFill.add(p_autoFill); p_autoFill.add(new JLabel("What percentage should be filled? ")); Object[] percentageOptions = {"Select",5,10,15,20,25,30,40,50,60,70,80,90,95}; final JComboBox cb_percent = new JComboBox(percentageOptions); p_autoFill.add(cb_percent); cb_percent.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (cb_percent.getSelectedIndex() > 0) { gb_gameBoard.resetBoard(); gb_gameBoard.randomlyFillBoard((Integer)cb_percent.getSelectedItem()); f_autoFill.dispose(); } } }); f_autoFill.setVisible(true); } else if (ae.getSource().equals(mi_game_reset)) { gb_gameBoard.resetBoard(); gb_gameBoard.repaint(); } else if (ae.getSource().equals(mi_game_play)) { setGameBeingPlayed(true); } else if (ae.getSource().equals(mi_game_stop)) { setGameBeingPlayed(false); } else if (ae.getSource().equals(mi_help_source)) { Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; try { desktop.browse(new URI("https://github.com/Burke9077/Conway-s-Game-of-Life")); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Source is available on GitHub at: https://github.com/Burke9077/Conway-s-Game-of-Life", "Source", JOptionPane.INFORMATION_MESSAGE); } } else if (ae.getSource().equals(mi_help_about)) { JOptionPane.showMessageDialog(null, "Conway's game of life was a cellular animation devised by the mathematician John Conway. This Java, swing based implementation was created by Matthew Burke. http://burke9077.com Burke9077@gmail.com @burke9077 Creative Commons Attribution 4.0 International"); } } private class GameBoard extends JPanel implements ComponentListener, MouseListener, MouseMotionListener, Runnable { private Dimension d_gameBoardSize = null; private ArrayList point = new ArrayList(0); public GameBoard() { // Add resizing listener addComponentListener(this); addMouseListener(this); addMouseMotionListener(this); } private void updateArraySize() { ArrayList removeList = new ArrayList(0); for (Point current : point) { if ((current.x > d_gameBoardSize.width-1) || (current.y > d_gameBoardSize.height-1)) { removeList.add(current); } } point.removeAll(removeList); repaint(); } public void addPoint(int x, int y) { if (!point.contains(new Point(x,y))) { point.add(new Point(x,y)); } repaint(); } public void addPoint(MouseEvent me) { int x = me.getPoint().x/10-1; int y = me.getPoint().y/10-1; if ((x >= 0) && (x = 0) && (y
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote