Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In java, we are tasked to program the game of life, the instructions are shown below, we have 5 files to test, it needs to

image text in transcribedIn java, we are tasked to program the game of life, the instructions are shown below, we have 5 files to test, it needs to be able to have a different file put in every time, the cells are represented with 0's and 1's.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

25 77 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000001010001010000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 The Game of Life consists of a two-dimensional array of cells, each of which can either be alive or dead. The cells go through a (specified or infinite) number of generations. In each generation, the value of a cell is calculated by looking at the values of its neighboring cells in the previous generation. Each non-edge cell has eight neighbors, as shown in Fig 1 below (X's neighbors are the cells immediately adjacent to it, including diagonally). For those cells on the edge, you may consider those neighbors dead', as shown in the Fig 2 below (Fig 2 only shows ONE possible edge combination with X at 0,0). Fig 1 1 2 3 4 X 5 6 7 8 Fig 2 (all other neighbors considered 'dead) X1 23 The rules for determining if a cell lives or dies in the next generation are as follows: 1) Any live cell with fewer than two live neighbors dies of loneliness 2) Any live cell with more than three live neighbors dies of overcrowding 3) Any dead cell with exactly three live neighbors comes to life 4) Any live cell with two or three neighbors stays alive 5) Any dead cell that does not have exactly three live neighbors remains dead These generations can be calculated infinitely. Depending on the initial states of the cells, the entire grid may completely die out, oscillate between several states indefinitely, or reach some stable point that is never altered in later generations. Assignment Description: You are to implement the Game of Life and compute generations infinitely. The user will supply the name of an input file that holds the initial configuration via a command-line argument. Assume the user will include .txt on the name of the file. (For testing purposes, I might suggest you hardcode the filename within the code, then comment out when ready to test with a command-line argument. Make sure and submit the correct version that gets the filename from the command-line or you will be penalized points.) You should then graphically display the generations of the cells in the input file until the user manually stops the program (Ctrl-C from the command prompt or the red stop square in Eclipse). The display should update every 300 milliseconds to display the next generation. (This is one instance where an endless loop is used while (true) since user input is not used and Ctrl-C is used to end the program.) Here are several screenshots of the program based on the life2.txt input file, where an asterisk (*) indicates a live cell and a period (.) indicates a dead cell. (The generations will appear to be updated in place, although they will really print one after the other.) 3. Command Prompt - java Proj5 life2.txt x 11 Pausing: In order to animate the game, you will want to pause between drawing each iteration of the board. Here is how to pause for 500 milliseconds in Java (increase or decrease the value, as applicable): try { Thread.sleep (500); } catch (InterruptedException e) {} Animation: There is no universal way to "delete things printed to a console in Java or to move the cursor to a previous line. You will instead print each animation one after the other it will only appear to be animated. General Requirements/Tips: For this project, you are NOT allowed to use class (i.e. global) variables in your program (which are variables declared OUTSIDE any method). Only variables local to methods are allowed. This forces students to correctly pass values back and forth between the methods, which is a very important concept. In addition, you are NOT allowed to use an ArrayList at any point in your program. Although ArrayLists might make things easier, it is important that all students are comfortable working with standard arrays. We will use ArrayLists later in the semester. Significant point reduction if either are found in your code (maximum score of 12 credit). Lastly, as with Projects 1-4, no use of Programmer defined objects or Packages, which can cause compiling/execution errors. There are five sample input files posted on Canvas (lifel.txt, life2.txt, life3.txt, life4.txt, life5.txt). Review these files to determine their format. In particular, the first two lines of the file contain the number of rows and number of columns in the grid of cells. DO NOT assume that these numbers will always be the same in input files. In the remaining lines, 0 indicates a dead cell and 1 indicates a live cell. Since the intent of this project is the creation and implementation of methods, you must include working implementations of at least 3 of the 4 methods listed below (not counting main) and call/use those methods in your program to be considered for partial credit. Program with less than 3 methods will NOT be considered for any credit. This is the minimum number of methods required - you can have more. It is suggested you use the divide and conquer approach - get one method working before you start another. Your main method should basically be an outline of method calls to the methods defined below. To get full credit, you must use the method names given (to simplify grading) 1) readBoard: This method reads in data from a specified input file and loads into a 2D array. 2) boardDisplay: This method returns a single String representing the current state of the cells array (so that it would print as a grid, if printed). I would suggest the use of StringBuilder to create this String. (You can also use simple String concatenation, i.e., st=...) 3) update: This method returns a new 2D array containing the next generation of cells. 4) neighbors: This method returns the number of live neighbors that a certain position (row,col) has in the 2D cells array. (Might want to call this method from the update method) The main method will control the flow of the program, calling the methods defined above. I realize there are many ways of solving this project, but you MUST include and use the methods above to receive full credit. (Additional methods are allowed) Before you code, I strongly recommend that you (at a minimum) decide on what you want to happen within each method, how it will happen, and determine the method signature for each of the methods described. FYI: Some methods may also require that you add throws FileIOException to get it to compile. Running Requirements: This project will contain ONE file (called Proj5) containing a single class. Your program MUST COMPILE by command-line with the statement: javac Proj5.java It must then RUN by command-line with the command: java Proj5 filename.txt (where filename.txt is the name of an input file that contains an initial configuration of cells.) If the user does not supply a command- line argument, display an appropriate error message and exit the program. 25 77 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000001010001010000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 The Game of Life consists of a two-dimensional array of cells, each of which can either be alive or dead. The cells go through a (specified or infinite) number of generations. In each generation, the value of a cell is calculated by looking at the values of its neighboring cells in the previous generation. Each non-edge cell has eight neighbors, as shown in Fig 1 below (X's neighbors are the cells immediately adjacent to it, including diagonally). For those cells on the edge, you may consider those neighbors dead', as shown in the Fig 2 below (Fig 2 only shows ONE possible edge combination with X at 0,0). Fig 1 1 2 3 4 X 5 6 7 8 Fig 2 (all other neighbors considered 'dead) X1 23 The rules for determining if a cell lives or dies in the next generation are as follows: 1) Any live cell with fewer than two live neighbors dies of loneliness 2) Any live cell with more than three live neighbors dies of overcrowding 3) Any dead cell with exactly three live neighbors comes to life 4) Any live cell with two or three neighbors stays alive 5) Any dead cell that does not have exactly three live neighbors remains dead These generations can be calculated infinitely. Depending on the initial states of the cells, the entire grid may completely die out, oscillate between several states indefinitely, or reach some stable point that is never altered in later generations. Assignment Description: You are to implement the Game of Life and compute generations infinitely. The user will supply the name of an input file that holds the initial configuration via a command-line argument. Assume the user will include .txt on the name of the file. (For testing purposes, I might suggest you hardcode the filename within the code, then comment out when ready to test with a command-line argument. Make sure and submit the correct version that gets the filename from the command-line or you will be penalized points.) You should then graphically display the generations of the cells in the input file until the user manually stops the program (Ctrl-C from the command prompt or the red stop square in Eclipse). The display should update every 300 milliseconds to display the next generation. (This is one instance where an endless loop is used while (true) since user input is not used and Ctrl-C is used to end the program.) Here are several screenshots of the program based on the life2.txt input file, where an asterisk (*) indicates a live cell and a period (.) indicates a dead cell. (The generations will appear to be updated in place, although they will really print one after the other.) 3. Command Prompt - java Proj5 life2.txt x 11 Pausing: In order to animate the game, you will want to pause between drawing each iteration of the board. Here is how to pause for 500 milliseconds in Java (increase or decrease the value, as applicable): try { Thread.sleep (500); } catch (InterruptedException e) {} Animation: There is no universal way to "delete things printed to a console in Java or to move the cursor to a previous line. You will instead print each animation one after the other it will only appear to be animated. General Requirements/Tips: For this project, you are NOT allowed to use class (i.e. global) variables in your program (which are variables declared OUTSIDE any method). Only variables local to methods are allowed. This forces students to correctly pass values back and forth between the methods, which is a very important concept. In addition, you are NOT allowed to use an ArrayList at any point in your program. Although ArrayLists might make things easier, it is important that all students are comfortable working with standard arrays. We will use ArrayLists later in the semester. Significant point reduction if either are found in your code (maximum score of 12 credit). Lastly, as with Projects 1-4, no use of Programmer defined objects or Packages, which can cause compiling/execution errors. There are five sample input files posted on Canvas (lifel.txt, life2.txt, life3.txt, life4.txt, life5.txt). Review these files to determine their format. In particular, the first two lines of the file contain the number of rows and number of columns in the grid of cells. DO NOT assume that these numbers will always be the same in input files. In the remaining lines, 0 indicates a dead cell and 1 indicates a live cell. Since the intent of this project is the creation and implementation of methods, you must include working implementations of at least 3 of the 4 methods listed below (not counting main) and call/use those methods in your program to be considered for partial credit. Program with less than 3 methods will NOT be considered for any credit. This is the minimum number of methods required - you can have more. It is suggested you use the divide and conquer approach - get one method working before you start another. Your main method should basically be an outline of method calls to the methods defined below. To get full credit, you must use the method names given (to simplify grading) 1) readBoard: This method reads in data from a specified input file and loads into a 2D array. 2) boardDisplay: This method returns a single String representing the current state of the cells array (so that it would print as a grid, if printed). I would suggest the use of StringBuilder to create this String. (You can also use simple String concatenation, i.e., st=...) 3) update: This method returns a new 2D array containing the next generation of cells. 4) neighbors: This method returns the number of live neighbors that a certain position (row,col) has in the 2D cells array. (Might want to call this method from the update method) The main method will control the flow of the program, calling the methods defined above. I realize there are many ways of solving this project, but you MUST include and use the methods above to receive full credit. (Additional methods are allowed) Before you code, I strongly recommend that you (at a minimum) decide on what you want to happen within each method, how it will happen, and determine the method signature for each of the methods described. FYI: Some methods may also require that you add throws FileIOException to get it to compile. Running Requirements: This project will contain ONE file (called Proj5) containing a single class. Your program MUST COMPILE by command-line with the statement: javac Proj5.java It must then RUN by command-line with the command: java Proj5 filename.txt (where filename.txt is the name of an input file that contains an initial configuration of cells.) If the user does not supply a command- line argument, display an appropriate error message and exit the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions

Question

Solve the system, if possible. 5x +4y = -3 3x - = -6 3

Answered: 1 week ago