Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Endomembrane system wikipedia , lookup
Tissue engineering wikipedia , lookup
Extracellular matrix wikipedia , lookup
Programmed cell death wikipedia , lookup
Cell encapsulation wikipedia , lookup
Cytokinesis wikipedia , lookup
Cell growth wikipedia , lookup
Cellular differentiation wikipedia , lookup
Organ-on-a-chip wikipedia , lookup
Program #4: Life 2-Dimensional Arrays, files, functions Problem Description The "game" of Life is a simple simulation that was first described in Scientific American many years ago. It's not really what we think of when we hear the word "game" but it's an interesting problem that uses 2-dimensional arrays. Life simulates a simple population through many generations until the population either (1) stabilizes or (2) dies out. The population is represented on a two-dimensional grid (like a computer monitor). Each cell on the grid is either "alive" (occupied) or "dead" (unoccupied). Whether a cell is alive or dead in the current generation depends on whether its neighbors were alive or dead in the previous generation (so each generation determines the next generation). After starting with some initial configuration of cells, your program should produce and display all succeeding generations according to the following rules: If a cell has 0 or 1 neighbors, it will be dead in the next generation (it dies from isolation). If a cell has 4, 5, 6, 7, or 8 neighbors, it will be dead in the next generation (it dies from overpopulation). If a cell has exactly 2 neighbors, it will remain in the same state in the next generation (live cells remain alive, dead cells remain dead). If a cell has exactly 3 neighbors, it will be alive in the next generation regardless of its state in the current generation (note that this is the only way of creating new life). Every cell has 8 neighbors: 1 2 3 4 * 5 6 7 8 Input Input will be from a file with 48 lines of 80 characters (which corresponds nicely with our 480x800 display). Create an array of "cells" that has 48 rows and 80 columns and read the data from the file into this array. This will represent an initial population configuration. Input will be either periods (dead cells) or asterisks (live cells). Make both the live cell character and the dead cell character named constants. Make the 48 and the 80 named constants. Because you know that there are 48 lines with 80 characters per line, you may use for loops instead of while loops to read the data. Sample files are: Life2.txt, Life3.txt, Life4.txt, and Life5x5.txt. The Life5x5 file is a file that is only 5 rows with 5 characters per row. You may find it helpful when testing and debugging your program to work with an array that has 25 cells as opposed to an array that has 3840 cells (48x80). Here is what the Life2 file should do. Processing and Output On this assignment, you must use methods and you must pass parameters to those methods. Create a new structure called CellStruct that holds two fields: (1) a String representing the value in the cell ("*" for alive, " " for dead), and (2) a Rectangle representing where the cell is to be drawn on the screen. You will need two 2-dimensional arrays of CellStruct: one for the current generation (e.g. currentGen), and one for the next generation (e.g. nextGen). You will need to write methods to do (at least) the following tasks: void InitializeBoard. Pass in a 2-dimensional array as an argument to the method. Set all of the strings to the dead value (blanks) and determine the X and Y coordinates of the cell on the screen. Also determine the width and height of the cell on the screen (which will be the same for all cells). Note that both boards (currentGen and nextGen) need to be initialized, and so the method must be called twice. void ReadBoard. Pass in the 2-dimensional array (currentGen) as an argument to the method. Read the 48 lines one at a time, and extract each of the 80 characters per string into the array using the Substring method. void DisplayBoard. Pass in the 2-dimensional array (currentGen) as an argument to the method. Display each array element. Display the board on every tick of the clock. void UpdateBoard. Pass in both of the 2-dimensional arrays as arguments to the method. Step through each cell in the current generation and count its neighbors. You only need to update that part of the population between (and including) rows 1 and 46 (skipping row 0 and row 47) and between columns 1 and 78 (skipping column 0 and column 79). That is, assume that there is a border around the grid (rows 0 and 47, columns 0 and 79). This will make your programming significantly easier because all cells will have eight neighbors (no special cases for top row, bottom row, side rows, and four corners!). Generate and display a new generation once every 8 ticks. To determine the value of the cell for the next generation you must count the cell's neighbors in the current generation. Then put the appropriate value in the next generation array. When you have looked at every cell in the array, copy all of the cells from the next generation array into your current generation array. This method is only called once every 8 ticks of the clock. This number should be a variable that you can change to speed up or slow down your screen updates for debugging purposes. int NeighborCount. Pass in the 2-dimensional array, and the row and column of the cell whose neighbors you want to count. The function should look at all 8 neighbors of the cell at the given row and column and return the number of live neighbors (an int). Game states This game will have two states: Paused and Playing. When the player presses the Start button or the Enter key, the program will toggle states. When the program begins, the game is in the Paused state and will remain in the Paused state until the player presses the Start button or the Enter key. Then the program will remain in the Playing state until the player presses the Start button or the Enter key. Note that we will only change states when the button/key was released/up on the previous tick and is pressed/down on the current tick. Note that it is possible to go back and forth between the Playing and Paused states indefinitely. Use the entire display to display your cells.