engr cs 101 cs session lecture 11
play

ENGR/CS 101 CS Session Lecture 11 Log into Windows (reboot if in - PowerPoint PPT Presentation

ENGR/CS 101 CS Session Lecture 11 Log into Windows (reboot if in Linux) Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f11-courses/engrcs101/cs.html Right-click on Inclass11.zip link. Save link/target


  1. ENGR/CS 101 CS Session Lecture 11  Log into Windows (reboot if in Linux)  Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f11-courses/engrcs101/cs.html  Right-click on Inclass11.zip link. Save link/target to Visual Studio 2010 projects folder.  Browse to Inclass11.zip, right-click on it and select Extract All... and extract project folder  Double-click into the ConwaysLife folder, then double-click on the Visual Studio Solution file. Lecture 11 ENGR/CS 101 Computer Science Session 1

  2. Outline  Cellular automata  Conway's Game of Life  C# GUI implementation of the game  Two-dimensional arrays Lecture 11 ENGR/CS 101 Computer Science Session 2

  3. Cellular Automata  A cellular automaton is used in discrete modeling. It consists of a regular grid of cells , each of which can be in one of a finite number of states .  Time is discrete, so the state of a cell at time t +1 is a function of the state of a finite number of neighboring cells at time t .  A rule (or function) is applied to every cell at each time step and all updates happen at once creating a new generation . Lecture 11 ENGR/CS 101 Computer Science Session 3

  4. Conway's Game of Life  Conway's Game of Life is one well-known example of a two-state, two-dimensional cellular automaton.  The cells are square, and the neighborhood is the 8 cells immediately adjacent (i.e., including diagonally) to a cell.  At each generation, a cell may be live (represented by a '*' in the cell) or dead (represented a space cell). Lecture 11 ENGR/CS 101 Computer Science Session 4

  5. Conway's Game of Life  The original rules posed by Conway are:  If a live cell has fewer than 2 live neighbors, the cell dies of loneliness  If a live cell has more than 3 live neighbors, the cell dies of overcrowding  If a dead cell has exactly 3 live neighbors, the cell becomes alive by birth  Otherwise, the cell state is unchanged Lecture 11 ENGR/CS 101 Computer Science Session 5

  6. Conway's Game of Life  Here is an example of how the rule works using the interface mockup for a GUI implementation of Conway's.  An initial condition is shown below. Lecture 11 ENGR/CS 101 Computer Science Session 6

  7. Conway's Game of Life  The figure below shows the number of live neighbors each cell has in the initial condition. Lecture 11 ENGR/CS 101 Computer Science Session 7

  8. Conway's Game of Life  Applying the rules, the end live cells have only one neighbor, so they die. Two new cells are born above and below the center live cell as they have 3 live neighbors. The center live cell has two neighbors so it continues to live. The resulting state is shown below. Lecture 11 ENGR/CS 101 Computer Science Session 8

  9. Interface Mockup  The interface mockup shown consists of  Two textboxes, one for the number of generations to be computed when the Next button is clicked and one for the time between generation updates.  Two buttons, one to start the next generation updates and one to reset the simulation state back to the way it is when the program first starts.  A grid of labels that represent the cells of the grid that are dynamically created when the application starts. Lecture 11 ENGR/CS 101 Computer Science Session 9

  10. Implementation  All of the interface and most of the code for the game has been provided to you. The parts of the code (press F7 to see it) that you need to understand to finish the implementation are as follows. Lecture 11 ENGR/CS 101 Computer Science Session 10

  11. Implementation  Data Items:  genCount – an integer that keeps track of the number of generations that have been simulated. The value of this integer is displayed in generationCount.Text  grid – a 2-D array of Labels currently set to 10 rows and 20 columns that are displayed by the GUI.  nextGrid – a 2-D array of strings the same size as grid used to hold the state of the cell in the next generation. Lecture 11 ENGR/CS 101 Computer Science Session 11

  12. Two-dimensional Arrays  A two-dimensional (2-D) array is a data structure that represents a 2-D grid of elements. Generally, the first dimension index is called the row index and the second dimension index is called the column index. [0] [1] [2] [3] [4] [5] Column Indexes [0] Row [1] Indexes [2] [3] Lecture 11 ENGR/CS 101 Computer Science Session 12

  13. Two-dimensional Arrays  An individual element is accessed by giving an index for each dimension. In C#, the indexes are given as a list of indexes separated by commas. E.g. grid[0,0] is the element with row index of 0 and column index of 0, i.e., the upper left corner element. Lecture 11 ENGR/CS 101 Computer Science Session 13

  14. Two-dimensional Arrays  Often we want to access every element in a 2-D array, e.g. to initialized the elements to some value like 0 or a space. This is usually done by nesting two for-loops to count each index. In C#, the code for this would look like: int rowMax = grid.GetUpperBound(0), colMax = grid.GetUpperBound(1); for (int i=0; i < rowMax; i++) for (int j=0; j < colMax; j++) { // do something with grid[i,j] } Lecture 11 ENGR/CS 101 Computer Science Session 14

  15. Implementation  Algorithm elements  Function CountNeighbors receives the row and column indexes of a grid element (called i and j) and returns the number of live neighbors that surround grid[i,j] . The neighboring elements have indexes that are one more and one less for both the row and column index, but the function also must check that the resulting index is still a valid one. Lecture 11 ENGR/CS 101 Computer Science Session 15

  16. Implementation  We will implement the functions whose stubs are at the bottom of the code file.  grid_Click - handler for when a grid label is clicked  clear_Click - handler for the Clear button  ComputeGenerations - a function that implements the game rules Lecture 11 ENGR/CS 101 Computer Science Session 16

  17. grid_Click  grid_Click is the handler for the grid labels. We want to alternate between "*" and a space. The only tricky part is we need to tell the compiler to treat the sender argument as a Label like so: Label cell = (Label) sender;  Otherwise the algorithm is simple 1. If cell.Text is a "*" then 1.1 Set cell.Text to a space (" ") Else (cell is a space) 1.2 Set cell.Text to "*"  Try running the program and clicking in a cell. Lecture 11 ENGR/CS 101 Computer Science Session 17

  18. clear_Click  clear_Click is the handler for the Clear button. It simply sets all of the grid cells to a space and resets the generation counters to 0. 1. Declare and initialize rowMax to grid.GetUpperBound(0) 2. Declare and initialize colMax to grid.GetUpperBound(1) 3. Loop index i from 0 to rowMax by 1 3.1 Loop index j from 0 to colMax by 1 3.1.1 Set grid[i,j].Text to a space 4. Set genCount to 0 5. Set generationCount.Text to "0"  Try running the program and clicking the Clear button after clicking in some grid cells. Lecture 11 ENGR/CS 101 Computer Science Session 18

  19. ComputeGenerations  ComputeGenerations is the function that embodies the game rules. It uses the CountNeighbors function to determine the number of neighbors a cell has, then applies the rules to compute the cell state in the next generation.  Since all of the next states must be computed before a cell can be changed, the auxiliary grid nextGrid is used to hold the results. These are transfered back to grid in the DisplayGeneration function. Lecture 11 ENGR/CS 101 Computer Science Session 19

  20. ComputeGenerations  Implement the steps shown in bold : 1. Declare and initialize rowMax to grid.GetUpperBound(0) 2. Declare and initialize colMax to grid.GetUpperBound(1) 3. Loop index i from 0 to rowMax by 1 3.1 Loop index j from 0 to colMax by 1 3.1.1 Compute neighbors = CountNeighbors (i, j) 3.1.2 If grid[i,j].Text is "*" and neighbors is less than 2 or greater than 3 then 3.1.2.1 Set nextGrid[i,j] to a space (i.e., a death) 3.1.3 Else if grid[i,j].Text is " " and neighbors is 3 then 3.1.3.1 Set newGrid[i,j] to "*" (i.e., a birth) 3.1.4 Else (no change) 3.1.4.1 Set newGrid[i,j] to grid[i,j].Text 3.2 Increment genCount Lecture 11 ENGR/CS 101 Computer Science Session 20

  21. Check This Out!  Try the following initial condition. Lecture 11 ENGR/CS 101 Computer Science Session 21

Recommend


More recommend