Question
Please help with doing this program! This is what i have so far: import java.util.Scanner; public class Milestone1 { public static void main(String[] args) {
Please help with doing this program!
This is what i have so far:
import java.util.Scanner;
public class Milestone1 {
public static void main(String[] args) {
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" +
" 5)Random 6)Custom or 7)Exit" +
" Choose a pattern: ";
// Scanner to get user input
Scanner in = new Scanner(System.in);
int userChoice = 0;
// Initialize world array
boolean[][] world = {{false, false, false, false, false, false, false, false, false, false},
{false, false, true, true, false, false, false, false, false, false},
{false, true, false, false, true, false, false, false, false, false},
{false, false, true, true, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false}};
// Display welcome message
System.out.println(welcomeMessage);
do {
// Display menu
System.out.print(optionList);
// Get user choice
while (true) {
if (in.hasNextInt()) {
userChoice = in.nextInt();
if ((1 <= userChoice) && (userChoice <= 7))
break;
}
// If user enters anything other than a value between 1-7
System.out.print("Enter a number between 1 and 7: ");
// Consume the input
in.nextLine();
}
// Clear keyboard buffer
in.nextLine();
if (userChoice != 7) {
String stringChoice = "";
int generationNum = 0;
do {
int aliveCount = 0;
// Display generation
System.out.println(" Generation: " + generationNum);
for (int i = 0; i < world.length; i++) {
for (int j = 0; j < world[i].length; j++) {
System.out.print(world[i][j] ? Config.ALIVE : Config.DEAD);
if (world[i][j])
aliveCount += 1;
}
System.out.println();
}
// 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;
} while (!stringChoice.equalsIgnoreCase("end"));
}
} while (userChoice != 7);
// Display exit message
System.out.println(exitMessage);
// Close scanner
in.close();
}
}
Here is the next part that i would like help with. Thank you!
1. In this milestone you will refactor the code within the main method into methods and then extend the program with additional methods. Refactoring changes the structure of your code, but keeps the same output. You will move much of the code from the main method into various supporting methods and then call those methods from the main method. In addition, in this milestone, create additional methods to initialize the world with various patterns of life.
2. Download the GameOfLife.java file that contains various methods. In this milestone and then next 2 milestones, you write the code for these methods. Key learning objectives of this project are to learn to pass parameters and handle return types appropriately. Automated testing code can test many of these methods separately but the methods must have the exact same parameters and return types. For full credit, keep all method signatures as defined in the GameOfLife.java file.
GamOfLife.java file to download into the program, i have copied below:
//TODO: file header import java.util.Random; import java.util.Scanner; public class GameOfLife { public static void main(String[] args){ } /** * Prints out the world showing each cell as alive or dead. * * Loops through every cell of the world. If a cell is alive, print out * the Config.ALIVE character, otherwise print out the Config.DEAD * character. * * Counts how many cells are alive and prints out the number of cells * alive. For 2 or more cells alive, for 1 cell alive and 0 cells alive * the following messages are printed: * 5 cells are alive. * 1 cell is alive. * No cells are alive. * * @param world The array representation of the current world. */ public static void printWorld( boolean[][] world) { //TODO: implement method } /** * This method clears the world by setting all the cells in the * world to false (dead). This method uses array lengths, * not constants, to determine the size of the world. * * @param world the world to clear */ public static void clearWorld( boolean[][]world) { //TODO: implement method } /** * This method expects an integer to be entered by the user * between and including the values of min and max. If the value * entered is not an integer or is an integer but less than * min or greater than max the following message is displayed: * Enter a number between 1 and 5: * Assuming that min was 1 and max was 5 when this method was * called. * * @param input The Scanner instance for reading from System.in. * @param min The minimum acceptable integer. * @param max The maximum acceptable integer. * @return An integer between and including min and max. */ public static int getIntChoice(Scanner input, int min, int max) { //TODO: implement method return -1; //TODO replace with valid user input } /** * Initializes the world to the Glider pattern. ** .......... * .@........ * ..@@...... * .@@....... * .......... * .......... * .......... * .......... ** * The world may have any pattern within it when passed into this * method. After this method call, the only living cells in the * world is the Glider pattern as shown. * * @param world the existing double dimension array that will be * reinitialized to the Glider pattern. */ public static void initializeGliderWorld(boolean[][]world) { //TODO: implement method } /** * Initializes the world to the Beacon pattern. ** .......... * .@@....... * .@........ * ....@..... * ...@@..... * .......... * .......... * .......... ** * The world may have any pattern within it when passed into this * method. After this method call, the only living cells in the * world is the Beacon pattern as shown. * * @param world the existing 2-dimension array that will be * reinitialized to the Beacon pattern. */ public static void initializeBeaconWorld(boolean[][] world) { //TODO: implement method } /** * Initializes the world to the Beehive pattern. ** .......... * ..@@...... * .@..@..... * ..@@...... * .......... * .......... * .......... * .......... ** * The world may have any pattern within it when passed into this * method. After this method call, the only living cells in the * world is the Beehive pattern as shown. * * @param world the existing double dimension array that will be * reinitialized to the Beehive pattern. */ public static void initializeBeehiveWorld(boolean[][] world) { //TODO: implement method } /** * Initializes the world to the R-pentomino pattern. ** .......... * ..@@...... * .@@....... * ..@....... * .......... * .......... * .......... * .......... ** * The world may have any pattern within it when passed into this * method. After this method call, the only living cells in the * world is the R-pentomino pattern as shown. * * @param world the existing double dimension array that will be * reinitialized to the R-pentomino pattern. */ public static void initializeRpentominoWorld(boolean[][] world) { //TODO: implement method } /** * Initialize the GameOfLife world with a random selection of * cells alive. * * For testing purposes, implementations should * use the Config.CHANCE_ALIVE constant and Config.SEED. * Create an instance of the java.util.Random class, setting * the seed to the SEED constant. For each cell, if the * returned value of the nextDouble() method is less than * Config.CHANCE_ALIVE then the cell is alive. * * @param world the existing double dimension array that will be * reinitialized to a Random pattern. */ public static void initializeRandomWorld(boolean[][] world){ //TODO: implement method } /** * Prompt for a pattern of cells. Each line of input corresponds * to one row in the world. Continue reading lines until * 'END' is entered on a line of its own. Ignore case and * leading and trailing spaces when comparing to 'END'. (See String * methods such as trim() method.) * * The maximum size is the size of the world passed into this method. * Any additional characters typed by the user are ignored. * When interpreting the characters typed in, only the Config.ALIVE * character is used to determine which cells are alive. All other * characters are interpreted as dead cells. * * @param input The Scanner instance that reads from System.in. * @param world The world array that is filled with the pattern the * user enters. */ public static void initializeCustomWorld(Scanner input, boolean [][]world) { //TODO: implement method } /** * Checks whether a specific cell is alive or dead. * * Note that cellRow and cellColumn may not be valid indexes into * the world array. Return false for any cell outside the * world array. Checks the values of cellRow and cellColumn to make sure * they are valid prior to looking in the world array. Does not use * try-catch blocks or other exception handling mechanisms. * * @param world The current world. * @param cellRow The row of the cell which we are wanting to know * whether it is alive. * @param cellColumn The column of the cell which we are wanting * to know whether it is alive. * * @return Whether the specified cell is alive. */ public static boolean isCellAlive(boolean [][]world, int cellRow, int cellColumn) { //TODO: implement method return false; //TODO return valid result. } /** * Counts the number of neighbors that are currently living around the * specified cell. * * A cell has eight neighbors. The neighbors are the cells that are * horizontally, vertically and diagonally adjacent. * * Calls the isCellAlive method to determine whether any specific cell * is alive. * * @param world The current world. * @param row The row of the cell for which we are looking for living * neighbors. * @param column The column of the cell for which we are looking for * living neighbors. * * @return The number of neighbor cells that are currently living. */ public static int numNeighborsAlive(boolean[][] world, int row, int column) { //TODO: implement method return -1; //TODO return valid result. } /** * Whether a cell is living in the next generation of the game. * * The rules of the game are as follows: * 1) Any live cell with fewer than two live neighbors dies, as if caused * by under-population. * 2) Any live cell with two or three live neighbors lives on to the next * generation. * 3) Any live cell with more than three live neighbors dies, as if by * overcrowding. * 4) Any dead cell with exactly three live neighbors becomes a live cell, * as if by reproduction. * * @param numLivingNeighbors The number of neighbors that are currently * living. * @param cellCurrentlyLiving Whether the cell is currently living. * * @return true if this cell is living in the next generation, otherwise * false. */ public static boolean isCellLivingInNextGeneration( int numLivingNeighbors, boolean cellCurrentlyLiving) { //TODO: implement method return false; //TODO return valid result. } /** * Determines the cells living in the next generation of the world. * The next generation is created by applying the 4 rules simultaneously * to every cell in the previous generation. Births and deaths occur * simultaneously. In other words, look only at whether cells are living * in the current generation to determine whether a cell lives in the new * generation. Don't look at other cells in the new generation. * * For each cell in the current generation, determine whether the cell * at the same coordinates is living in the next generation using the * numNeighborsAlive and the isCellLivingInNextGeneration methods. * * @param currentWorld The world currently shown. * @param newWorld The new world based on the rules of life. */ public static void nextGeneration(boolean[][] currentWorld, boolean[][] newWorld) { //TODO: implement method } /** * This shows each generation of the simulation starting with * generation 0. The display of the world is by calling the * printWorld method and then prompting the user for whether to * calculate and show the next generation. * Then, for any input other then 'end', this calculates the next * generation using the nextGeneration method and shows it. Any case * of 'end' is acceptable, and also ignore leading and trailing * whitespace. (See String trim() method.) * * Note that any number of generations are possible to implement * with only the world passed as the parameter and one other * 2-dimensional array the same size. Create the second world * the same size as the world passed in, by using the length * attributes rather than using constant values. The world * passed in will be rectangular and not irregular. * * @param input The Scanner object used to read from System.in. * @param world The initialized world to show as generation 0. */ public static void runSimulation(Scanner input, boolean[][] world) { //TODO: implement method } }
End of file to copy.
3. First, copy your main method code from your Milestone1.java file to the GameOfLife.java and make sure it works. Now, lets refactor the GameOfLife.java main method.
4.
In the GameOfLife.java file, move the code that gets and verifies user menu input from the main method to the getIntChoice method:
static int getIntChoice(Scanner input, int min, int max){ //move code to get user integer input }
Then call the getIntChoice method from the main method:
getIntChoice( input, 1, 7);
The input argument (actual parameter) is the single Scanner instance created in your program and the 1 and 7 are the minimum and maximum valid menu choices. Note that getIntChoice returns a valid menu choice that your main method will eventually need in order to initialize the chosen pattern or exit when the user chooses.
5. Verify your program still produces the same output as it did for Milestone 1(the code at the top) but now calls the getIntChoice method.
6.
Next, move your code that prints out the alive and dead cells in the world into the GameOfLife printWorld method. The printWorld method also prints out the number of cells that are alive message.
static void printWorld(boolean[][] world) { }
From within the main method, call the printWorld method by passing the reference to the world array.
7. Verify your program still produces the same output as it did for Milestone 1. (the code at the top)
8. Next move the code that initializes the Beehive pattern into the initializeBeehiveWorld method. Call the initializeBeehiveWorld method from the main method and make sure the output is as before.
9. In the method header to initializeBeehiveWorld, the description indicates that no matter what pattern was previously in the world, only the Beehive pattern should remain after calling the initializeBeehiveWorld method. One way to achieve this is to clear the world before setting the pattern. Write the code for the clearWorld method. Call the clearWorld method from within initializeBeehiveWorld, before setting the Beehive pattern. Make sure your code works as before.
10. Next, extend the GameOfLife simulation to include other patterns. Write the code for the initializeGliderWorld, initializeBeaconWorld and initializeRpentominoWorld. They are all very similar to initializeBeehiveWorld.
11.
Extend your main method to call the initialize method for the corresponding user choice. Some examples to help verify your program is working are shown next. Note the bold text is input typed by the user.
Welcome to Conway's Game Of Life -------------------------------- 1)Glider 2)Beacon 3)Beehive 4)R-pentomino 5)Random 6)Custom or 7)Exit Choose a pattern: 1 Generation: 0 .......... .@........ ..@@...... .@@....... .......... .......... .......... .......... 5 cells are alive. Press Enter for next generation, 'end' to stop: end 1)Glider 2)Beacon 3)Beehive 4)R-pentomino 5)Random 6)Custom or 7)Exit Choose a pattern: 2 Generation: 0 .......... .@@....... .@........ ....@..... ...@@..... .......... .......... .......... 6 cells are alive. Press Enter for next generation, 'end' to stop: end 1)Glider 2)Beacon 3)Beehive 4)R-pentomino 5)Random 6)Custom or 7)Exit Choose a pattern: 3 Generation: 0 .......... ..@@...... .@..@..... ..@@...... .......... .......... .......... .......... 6 cells are alive. Press Enter for next generation, 'end' to stop: end 1)Glider 2)Beacon 3)Beehive 4)R-pentomino 5)Random 6)Custom or 7)Exit Choose a pattern: 4 Generation: 0 .......... ..@@...... .@@....... ..@....... .......... .......... .......... .......... 5 cells are alive. Press Enter for next generation, 'end' to stop: end 1)Glider 2)Beacon 3)Beehive 4)R-pentomino 5)Random 6)Custom or 7)Exit Choose a pattern: 7 ---------------------------- End of Conway's Game Of Life
12.
Implement and test the initializeRandomWorld method following its method header description. Call from the main method, when chosen. The following example output was generated with SEED for Random set to 428 and CHANCE_ALIVE set to 0.25. The world array was initialized starting with row 0, column 0, initializing all columns in that row and then continuing row by row.
Welcome to Conway's Game Of Life -------------------------------- 1)Glider 2)Beacon 3)Beehive 4)R-pentomino 5)Random 6)Custom or 7)Exit Choose a pattern: 5 Generation: 0 .......... ..@.@....@ ...@...... ..@@.....@ ...@...@.. ...@@..... .@@@..@..@ .....@@... 18 cells are alive. Press Enter for next generation, 'end' to stop: end 1)Glider 2)Beacon 3)Beehive 4)R-pentomino 5)Random 6)Custom or 7)Exit Choose a pattern: 7 ---------------------------- End of Conway's Game Of Life
Thanks so much and I apologize for the length!
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