Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I pasted my GameOfLife (Which is referred to as part 1 in the instructions) at the bottom of this question. Use JAVA to create a
I pasted my GameOfLife (Which is referred to as "part 1" in the instructions) at the bottom of this question. Use JAVA to create a solution, which is at least 2 java files. One for the game of life class, at least one for the display component class, and one for frame class (the frame class and the display component class can be in the same .java file, but need to be separate classes).
~~~~~ GAME OF LIFE (part 1)~~~~~
import java.util.Random; import java.util.Scanner; public class GameOfLife { //Creates string values for each cell alive or dead final static String ALIVE = "O", DEAD = "."; final static int CHANCE_ALIVE = 6, SEED = 19; boolean[][] world; public GameOfLife() { this.world = new boolean[19][19]; } public void run() { //starting menu and option messages final String welcomeMessage = "Welcome to Conway's Game Of Life"; final String exitMessage = "End of Conway's Game Of Life"; final String optionList = "1)Glider 2)Beacon 3)Beehive 4)R-pentomino" + "or 5)Exit. " + "Choose a pattern: "; // Scanner to get user input Scanner in = new Scanner(System.in); int userChoice = 0; clearWorld(world); // Display Starter menu System.out.println(welcomeMessage); do { // Starter menu System.out.print(optionList); //Setting range for menu userChoice = getIntChoice(in, 1, 5); //directs user to their chosen gameplay if (userChoice != 5) { if (userChoice == 1) { initializeGliderWorld(world); } else if (userChoice == 2) { initializeBeaconWorld(world); } else if (userChoice == 3) { initializeBeehiveWorld(world); } else if (userChoice == 4) { initializeRpentominoWorld(world); } String stringChoice = ""; int generationNum = 0; do { // Which Generation game is in System.out.println("Generation: " + generationNum); //prints the gameboard; System.out.println(this); int aliveCount = noOfAliveCells(world); // Display no. of alive cells if (aliveCount == 0) System.out.println("No cells are alive."); else if (aliveCount == 1) System.out.println("1 cell is alive."); else System.out.println(aliveCount + " cells are alive."); // Check if the user wants to continue System.out .print("Press Enter for next generation, 'end' to stop: "); stringChoice = in.nextLine().trim(); // Increase generation generationNum += 1; boolean[][] newWorld= new boolean[19][19]; nextGeneration(world, newWorld); world = newWorld; } while (!stringChoice.equalsIgnoreCase("end")); } } while (userChoice != 5); // Display exit message System.out.println(exitMessage); // Close scanner in.close(); } /** * Prints out the world showing each cell as alive or dead. */ public void printWorld(boolean[][] world) { for (int i = 0; i = 0 && cellColumn = 0) { if (world[cellRow][cellColumn]) { return true; } } return false; } /** * Counts the number of neighbors that are currently living around the * specified cell begining at zero * @param col scanning through board columns to locate cells give directions * @param row scanning through board to rows locate cells give directions * @param world accessing board * @return the number of alive cells */ public static int numNeighborsAlive(boolean[][] world, int row, int col) { int activeNeighbours = 0; if (isCellAlive(world, row - 1, col - 1)) activeNeighbours++; if (isCellAlive(world, row - 1, col)) activeNeighbours++; if (isCellAlive(world, row - 1, col + 1)) activeNeighbours++; if (isCellAlive(world, row, col - 1)) activeNeighbours++; if (isCellAlive(world, row, col + 1)) activeNeighbours++; if (isCellAlive(world, row + 1, col - 1)) activeNeighbours++; if (isCellAlive(world, row + 1, col)) activeNeighbours++; if (isCellAlive(world, row + 1, col + 1)) activeNeighbours++; return activeNeighbours; } /** * Whether a cell stays living in the next generation of the game. * @return true if this cell is living in the next generation, otherwise * false. * @param cellCurrentlyLiving number of cells nearby to decide their behavior */ public static boolean isCellLivingInNextGeneration(int numLivingNeighbors, boolean cellCurrentlyLiving) { if (cellCurrentlyLiving) { if (numLivingNeighbors == 3 || numLivingNeighbors == 2) { return true; } } else { if (numLivingNeighbors == 3) { return true; } } return false; } /** * Determines the cells living in the next generation of the game. * @param currentWorld game board with values from current generation * @param newWorld game board with values from next level of game */ public static void nextGeneration(boolean[][] currentWorld, boolean[][] newWorld) { for (int i = 0; i}
~~~~~~~END GAME OF LIFE (part 1)~~~~~~~~~
Objectives Render (draw) the contents of a two-dimensional array. . Use JOptionPane to control the processing of an application. Instructions Window-Based Game of Life. In Part I, you created an implementation of Life. The Part I implementation uses a two-dimensional array to represent the cells in the game. It used System.out to present the Life board to the user Now, in Part II, you are going to replace the textual output with a graphical output. 1. Create an output class for this application. Implement this in a separate class. (There may be other support classes that you will need to create.) 2. This class can extend java.awt.Component or javax.swing.JComponent. 3. This instance will need access to the two-dimensional array representing the cells. You can either pass the array as a parameter to the constructor, or you can pass the array each time you update the contents of the display. 4. This class should not be a Frame or JFrame. We will add this to a frame for display Create a frame to display your output class. This should be a JFrame. The layout of the frame is up to you In the application method, display the starting point for your Life implementation in the window, using your display class Design Decisions There are a number of design decisions that are part of this assignment. The primary component that you are creating renders the 19x19 grid of cells which is the complete universe (board) for the Game of Life. However, each of the 381 individual cells can use the same code to display itself. There is a related design decision here. Is the size of the cell fixed? Or can it grow / shrink based on the size of the environment (the board) This might suggest that the cells are themselves graphical components, with the board being some sort of container which holds the cells. Alternately, there could be a simple method (or two) of your board component that would take a Graphics object and a location (either int x, int y or a java.awt.Point) (and also, potentially a size) and then draw the appropriate graphical image at the given location. Next Generation Now, calculate the next generation. Display the next generation within the output class in the window. To make it possible to examine the different generations, use javax.swing.JOptionPane to pause the application method. See the example code, JOptionDemo. The code compiles and is complete. The display component is in a separate class from the frame (JFrame / top-level window). The display component is in a separate class from the two-dimensional array (Part I of the assignment). The display component accurately renders the state of the game as represented within the array The display component is not responsible for initiating or calculating the next generation of the game. The display component uses java.awt.Graphics to render the state of the game. (Using drawString or drawChars does not count.) The display component arranges the cells of the game in a square. (You may assume that a pixels is a true square.) The use of color is laudable. The use of shapes is laudable. The display of the boundries of each cell, whether dead or alive, is laudable. Objectives Render (draw) the contents of a two-dimensional array. . Use JOptionPane to control the processing of an application. Instructions Window-Based Game of Life. In Part I, you created an implementation of Life. The Part I implementation uses a two-dimensional array to represent the cells in the game. It used System.out to present the Life board to the user Now, in Part II, you are going to replace the textual output with a graphical output. 1. Create an output class for this application. Implement this in a separate class. (There may be other support classes that you will need to create.) 2. This class can extend java.awt.Component or javax.swing.JComponent. 3. This instance will need access to the two-dimensional array representing the cells. You can either pass the array as a parameter to the constructor, or you can pass the array each time you update the contents of the display. 4. This class should not be a Frame or JFrame. We will add this to a frame for display Create a frame to display your output class. This should be a JFrame. The layout of the frame is up to you In the application method, display the starting point for your Life implementation in the window, using your display class Design Decisions There are a number of design decisions that are part of this assignment. The primary component that you are creating renders the 19x19 grid of cells which is the complete universe (board) for the Game of Life. However, each of the 381 individual cells can use the same code to display itself. There is a related design decision here. Is the size of the cell fixed? Or can it grow / shrink based on the size of the environment (the board) This might suggest that the cells are themselves graphical components, with the board being some sort of container which holds the cells. Alternately, there could be a simple method (or two) of your board component that would take a Graphics object and a location (either int x, int y or a java.awt.Point) (and also, potentially a size) and then draw the appropriate graphical image at the given location. Next Generation Now, calculate the next generation. Display the next generation within the output class in the window. To make it possible to examine the different generations, use javax.swing.JOptionPane to pause the application method. See the example code, JOptionDemo. The code compiles and is complete. The display component is in a separate class from the frame (JFrame / top-level window). The display component is in a separate class from the two-dimensional array (Part I of the assignment). The display component accurately renders the state of the game as represented within the array The display component is not responsible for initiating or calculating the next generation of the game. The display component uses java.awt.Graphics to render the state of the game. (Using drawString or drawChars does not count.) The display component arranges the cells of the game in a square. (You may assume that a pixels is a true square.) The use of color is laudable. The use of shapes is laudable. The display of the boundries of each cell, whether dead or alive, is laudable
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started