Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me on activity 2&3,c# Activity 1: Menu and Initial Board Setup For the project, you should implement the following classes (but you may

Please help me on activity 2&3,c#

image text in transcribedimage text in transcribed

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

Activity 1: Menu and Initial Board Setup For the project, you should implement the following classes (but you may choose to have more) Program.cs - the Main method Game.cs - this class will control the game/simulation Cell.cs- this class represents what is stored in the game board Step 1: Create menu and start Game class Game Create these two placeholders for the GenerateBoard and DisplayBoard methods in the Game class. At this stage, these two methods should simply display a message on the screen describing the work they do (see below). Main() When your program starts, bring up a welcome message along with the following three options: Generate a random board Display board Load board (make a placeholder for this option; it won't do anything in Activity 1) Quit When the user selects either the "generate" or "display" options, the program calls the corresponding method from the Game class to do the work. If the user selects the "quit" option, end the program. At this point, your program should look something like this when it is run: Welcome to the Game of Life! - Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit - Your choice: 1 Placeholder for board generation. Your choice: 3 Will implement load in activity 2. Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit Options: 1 - Generate a random board 2 - Display board - 3 - Load initial board from file 4 - Quit Your choice: 4 Goodbye! Your choice: 2 Placeholder for board generation. Once you are satisfied that your menus are working as above, move on to the next step, which is generating the initial game board. (If you haven't already, now is a good time to commit your changes and push to the server!) Step 2: Cell class The only thing the Cell class should do at this point is override the ToString method so that the returned String contains only a single character. Pick any character you would like for the time being ("*" or "O" or "x" or "@" or anything you want!) At this point, the Cell class will have no fields, properties, or other methods. You'll add those later. Step 3: Game class Declare a field to hold the board (a 2D array of Cell objects). This field will be initialized in one of the methods below, rather than in the constructor. In the Generate method, remove the testing text that is being printed to the console. In this method, create the 2D array itself (using new) and give it a reasonable width and height. Now, for each "spot" in the 2D array you just created, create a brand new Cell object and store it in that "spot". Afterward, print a message about the size of the board being generated. Next, implement the Display method. Again, remove the testing text. It should now print the contents of the game board to the console, such that it looks like a grid. See the sample output below. Step 4: Testing Now, it's time to test what you've done. Run the program. Generate a board and display the result. The sample output below shows a 20 x 10 board (wxh)), although yours might be a different size. Welcome to the Game of Life! - 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit Your choice: 1 A 10x20 board was generated. - Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit Your choice: 2 Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit Your choice: 3 Will implement load in activity 2. Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit Your choice: 4 Goodbye! Step 5: Make changes to Cell and Game classes & Test again Once you have the Generate and Display methods working, make the following modifications to the board creation: The size of the board should be randomized each time the create method is called. Experiment with different bounds on the dimensions. The board DOES NOT have to be a square, but boards that are too small are not going to be interesting for the rest of the program. o Remember that you should have only one Random object per Game! Cells have the ability to be alive or dead. To illustrate this, you should have the Cell display a different symbol depending on if it is alive or dead. You will need to create functionality in the Cell class to do this. o Cell objects should be able to be created with a starting state of alive (true) or dead (false). Save this state appropriately within the class and update ToString accordingly. o Check that this is working by first creating a board where all the Cell objects are alive and then by creating one where all the Cell objects are dead. Each time the generate board method is called, each Cell on the board has a 30% chance of being alive. Welcome to the Game of Life! Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file Quit 4 - Your choice: 1 A 8x20 board was generated. Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit Your choice: 2 XXOXXXOXXXOOXXXXXXOO XXOOXXXXXOXXOOXOOXXX OXXOXXXXOXXXXXXOXOXO OXXXXOOOXXXXXXXXXXXO xxxxoxo xxxxxxxxxxxxxxxxxxx xxxooxxooxoxxxxoxxxx XXXOXXXXXXOOXXXXXXXO Activity 2: Incorporating Files Modify the game so that the initial board state is read from a file, and the current board could be written to a file if the user chooses. Step 1: Load a file and create the board Update the load option in the main menu to prompt for the name of the file. Create a method in Game that accepts the filename and, instead of generating a random board, creates and fills the board 2d array based on the data in the file. The format of the file to be read in should be as follows: 6,4 Row 1 contains the width of the board and the height of the board Row 2 contains the symbol for alive cells followed by the symbol for dead cells 000x00 This is the board configuration, XXX000 o represents a living cell, x represents a dead cell XXOOXX 00 Once the board is read in, it should be displayed. To test your program, download the starting Board.txt file and place it into your bin/Debug folder. When used, your output should look like the following (do not proceed if it doesn't!). Welcome to the Game of Life! Options: 1 - Generate a random board 2 - Display board 3 - Load initial board from file 4 - Quit @- -@------- -------- -------- ---@@-- ----@@---- -------- -------- -@------- Your choice: 3 Filename? startingBoard.txt Activity 3: Advancing the Simulation Now that you can generate random boards and load known boards, we will work on advancing the simulation and displaying the changes to the board. This game is a simulation of cells in a world. After some amount of time, the status of the Cells and status of the board changes and we will enable the progression of board and the Cells over time. Second board array In order to correctly advance the simulation, you will need to analyze each Cell in the array in relation to its neighboring cells, and then update its state (whether it is alive or dead) based on several rules. However, if you update each Cell as you loop through the array, you will potentially end up checking neighbors that have already been changed earlier in the loop. This leads to an inaccurate simulation. Instead, you need to look at the entire board in its current (unaltered) state. To properly implement the functionality of advancing the board, you will need to create A SECOND board (another 2D array of cell objects called the future board). As you loop through and check the current board, you'll be updating the corresponding cell in the future board. This ensures that the simulation remains accurate as if each and every Cell was updated at once rather than one at a time. After doing this for every cell, the future board will hold the new state of the simulation. You will then need to copy all of that data back into the current board, overwriting the state of each cell. To prepare for this, do the following: Create a second field to hold the future board (another 2D array of Cells) Inside the Generate method: Initialize the game board as before (this is the current board) Also initialize the future board array with the same width and height as the current board Simulation steps The overall flow of one "step" of the simulation is as follows: Analyze all of the cells of the current board by looking at their neighbors o Use this information to update the corresponding cell in the future board Copy the state of each future board cell back into the current board (to prepare for the next simulation step) o X o o x X o o o x x x x x xoxo o ox x x Current board x x xox x|o| x | x | 0 Xoxo o 000 x x 0 0 x x x Future board: Derived from looking at neighbors in the current board, then changing cell's life inside the future board. Xx| xo|x xoxo xoxo 000 x o o xxx Current board: Copied from future board. Rules for Cell Birth and Death When analyzing the cells of the current board (the results of which are stored in the future board), follow these rules: Any live cell with fewer than two live neighbors dies, as if caused by under population. Any live cell with two or three living neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. Not so secret tip: Write a method that determines how many live neighbors a given cell has. This means that you need a way to determine if a given Cell is alive or dead. (Perhaps a property could work?) Assume the eight cardinal directions for neighbors. You will need to think about how close a given cell is to the edge of the board so that you don't cause an IndexOutOfRange Exception when looking at the neighbors. If a cell's neighbor doesn't exist (since it's on the edge), simply disregard that neighbor. Interior Cell Border Cell Neighbor Neighbor Neighbor Neighbor Neighbor Cell Neighbor Neighbor Cell Neighbor Neighbor Neighbor Neighbor Neighbor Neighbor Final copy Copying the state of each cell from the future board back to the current board also sounds like a great candidate for a method. Implement this method, and call it at the end of the Advance method. Note: Only copy the state of whether or not the cell is alive (i.e. cellone.Alive = cellTwo.Alive), NOT the entire cell (i.e. cellone = cellTwo). Copying the cell actually just makes one variable refer to the object of another. This means that you'd no longer have two distinct boards. Instead, you'd have two arrays that all refer to the same set of Cell objects

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

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago