Please help with doing this program! This is what i have so far: import java.uti
ID: 3815007 • Letter: P
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) {
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:
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, let’s 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:
Then call the getIntChoice method from the main method:
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.
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.
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.
Thanks so much and I apologize for the length!