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!
Explanation / Answer
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Life extends JPanel implements ActionListener, MouseListener, MouseMotionListener { public static void main(String[] args) { JFrame f = new JFrame("Life"); JPanel p = new Life(); f.setContentPane(p); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocation(100,50); f.setVisible(true); } private final int GRID_SIZE = 100; // Number of squares along each side of the board // (Should probably not be less than 10 or more than 200,) private boolean[][] alive; // Represents the board. alive[r][c] is true if the cell in row r, column c is alive. private MosaicPanel display; // Displays the game to the user.. White squares are alive; black squares are dead. private Timer timer; // Drives the game when the user presses the "Start" button. private JButton stopGoButton; // Button for starting and stopping the running of the game. private JButton nextButton; // Button for computing just the next generation. private JButton randomButton; // Button for filling the board randomly with each cell having a 25% chance of being alive. private JButton clearButton; // Button for clearing the board, that is setting all the cells to "dead". private JButton quitButton; // Button for ending the program. /** * Create a life game board, initially empty. The number of cells on each side of the grid is GRID_SIZE. */ public Life() { alive = new boolean[GRID_SIZE][GRID_SIZE]; setLayout(new BorderLayout(3,3)); setBackground(Color.GRAY); setBorder(BorderFactory.createLineBorder(Color.GRAY,3)); int cellSize = 600/GRID_SIZE; // Aim for about a 600-by-600 pixel board. display = new MosaicPanel(GRID_SIZE,GRID_SIZE,cellSize,cellSize); if (cellSize < 5) display.setGroutingColor(null); display.setUse3D(false); add(display,BorderLayout.CENTER); JPanel bottom = new JPanel(); add(bottom,BorderLayout.SOUTH); clearButton = new JButton("Clear"); stopGoButton = new JButton("Start"); quitButton = new JButton("Quit"); nextButton = new JButton("One Step"); randomButton = new JButton("Random Fill"); bottom.add(stopGoButton); bottom.add(nextButton); bottom.add(randomButton); bottom.add(clearButton); bottom.add(quitButton); stopGoButton.addActionListener(this); clearButton.addActionListener(this); quitButton.addActionListener(this); randomButton.addActionListener(this); nextButton.addActionListener(this); display.addMouseListener(this); display.addMouseMotionListener(this); timer = new Timer(50,this); } private void doFrame() { // Compute the new state of the Life board. boolean[][] newboard = new boolean[GRID_SIZE][GRID_SIZE]; for ( int r = 0; r 0 ? r-1 : GRID_SIZE-1; below = rRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.