Program Development: Example - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Program Development: Example

Description:

If occupied, we say cell is ALIVE, otherwise DEAD ... neighbor (loneliness) or 4 or more live ... the number of cells to the left that influence a given cell ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 28
Provided by: johnl8
Category:

less

Transcript and Presenter's Notes

Title: Program Development: Example


1
Program Development Example
  • The mathematician Conway developed a simple
    mathematical model he called the Game of Life
  • In his model, life forms occupy some of the cells
    in a grid
  • The model shows successive generations of the
    life forms
  • According to specified rules, the life form in a
    cell may die or continue to live in the next
    generation
  • Also, cells that do not contain life forms may
    contain a life form in the next generation,
    depending on the rules.
  • The changes in a cell depend on the number of
    live neighbors of the cell

2
Conways Two Dimensional Game of Life
  • Setting is an unbounded rectangular grid
  • Each cell of grid may be occupied by living
    organism
  • If occupied, we say cell is ALIVE, otherwise
    DEAD
  • Configuration changes on each tick of cosmic
    clock and all changes are simultaneous
  • Changes follow rules of Life
  • Change to a cells state is determined by number
    of live neighbors

3
Conways Two Dimensional Game of Life
  • Each cell has eight neighboring cells

4
Conways Two Dimensional Game of Life
  • Rules of Life
  • A live cell becomes dead in the next generation
    if it either has 0 or 1 live neighbor
    (loneliness) or 4 or more live neighbors
    (overcrowding).
  • A live cell with 2 or 3 live neighbors stays
    alive in the next generation.
  • A dead cell become alive in the next generation
    if it has exactly 3 live neighbors, otherwise it
    stays dead.
  • All births and deaths take place simultaneously
    on the clock tick.

5
Examples of Two Dimensional Life
  • Example configuration showing neighbor counts
  • Cells colored green are Alive, all others Dead
  • The configuration below is stable, i.e., it does
    not change when the clock ticks

6
Examples of Two Dimensional Life
  • Example configuration showing neighbor counts
  • Cells colored green are Alive, all others Dead

Bistable Configuration
7
Examples of Two Dimensional Life
  • Another view of the evolution of the previous
    bistable configuration

8
Program Development Example
  • Since we have not discussed two-dimensional
    arrays, we will try to model one-dimensional Life
  • We will consider a general situation where we
    can specify how many cells to the left and how
    many cells to the right are considered to be
    neighbors
  • For example, two to the left and three to the
    right

Cell c
9
Program Development Example
  • We will also set up our model so that the
    transition rules can be specified as well
  • If there are n neighbors of a cell, then for each
    possible value i for the number of live neighbors
    (between 0 and n), we specify whether the cell
  • remains the same in the next generation (STAY)
  • switches state in the next generation (SWITCH)
  • becomes DEAD in the next generation (KILL)
  • becomes ALIVE in the next generation (MAKE_ALIVE)

10
The Design
  • Objects the game itself
  • Attributes
  • the number of cells in the universe
  • the number of cells to the left that influence a
    given cell
  • the number of cells to the right that influence a
    given cell
  • an array to hold the states of the cells (ALIVE
    or DEAD)
  • an array to hold the rules for the game
  • Public methods
  • constructor
  • method to show a given number of generations on
    the screen

11
One-Dimensional Life
  • We will create a class called LifeGame
  • Instances of this class will represent a specific
    game of one-dimensional Life with a fixed
    neighborhood and with fixed rules
  • We will initially print an asterix () for a live
    cell and a dash (-) for a dead cell
  • Interesting patterns will develop when we show
    multiple generations in this way
  • It is interesting to see how the patterns change
    as we vary the neighborhoods and the rules
  • The program will use arrays, enumerated types,
    switch statements,

12
One-Dimensional Life
  • As mentioned above, we will use arrays to hold
    the rules and the current state of the
    one-dimensional universe
  • To describe the state of a cell, we will use an
    enumerated type called State with two possible
    values
  • ALIVE
  • DEAD
  • To describe the rules, we will use an enumerated
    type called Action with four possible values
  • KILL change the cells state to DEAD regardless
    of its current state
  • MAKE_ALIVE change the cells state to ALIVE
    regardless of its current state
  • STAY leave the next state the same as the
    current one
  • SWITCH change ALIVE to DEAD and vice versa

13
One-Dimensional Life
  • We will need some private methods
  • neighborCount, which computes the number of live
    neighbors of a cell
  • showMap, which prints out a line of and
    characters, where denotes a live cell, - a dead
    cell
  • computeNextGeneration, which fills an temporary
    new map with the next generation, then copies the
    information back to the map instance variable

14
LifeGame.java
  • //
    // LifeGame.java
    Author R. Tindell//// Conway's Game of Life,
    one-dimensional version//
  • public class LifeGame
  • public enum State DEAD,ALIVE
  • public enum Action STAY,SWITCH,KILL,MAKE_ALI
    VE
  • private int universeSize, left, right
  • private State map
  • private Action rules

15
LifeGame.java
  • public LifeGame(int usize, int back, int forward,
    State theMap, Action theRules)
    universeSize usize
  • left back
  • right forward
  • map theMap
  • rules theRules
  • if (map.length lt universeSize
    rules.length ! leftright1

  • leftright1 gt universeSize)
  • System.out.println("Error Incompatible
    parameters for creation " "of a
    LifeGame object\n")
  • System.exit(1)

16
LifeGame.java
  • private void showMap() int j
  • System.out.println("\n")
  • for (j 0 j lt universeSize j) if
    (mapj State.ALIVE) System.out.print("")
  • else System.out.print("-")

17
LifeGame.java
  • private int neighborCount(int c) int i,
    count 0 int leftLimit, rightLimit
  • leftLimit (c-left lt 0 ? 0 c-left)
    rightLimit (universeSize lt cright ?
    universeSize cright)
  • for (i leftLimit i lt c i) if
    (mapi State.ALIVE) count
  • for (i c1 i lt rightLimit i) if
    (mapi State.ALIVE) count
  • return count

Min of 0 and c-left
Max of universeSize and cright
18
LifeGame.java
  • private void computeNextGeneration( )
    int c, count State newMap new
    StateuniverseSize2
  • for (c 1 c lt universeSize c)
  • count neighborCount(c)
  • switch (rulescount)
  • case MAKE_ALIVE newMapc State.ALIVE
    break
  • case SWITCH if (mapc
    State.ALIVE) newMapc State.DEAD
  • else newMapc
    State.ALIVE break
  • case KILL newMapc State.DEAD
    break
  • case STAY newMapc mapc

19
LifeGame.java
  • for (c 1 c lt universeSize c)
  • mapc newMapc
  • public void showGenerations(int n)
  • int i
  • for (i 0 i lt n i) showMap()
  • computeNextGeneration()
  • System.out.println()

20
The Driver Program
  • Finally, we create an application to initialize
    and run a LifeGame object
  • The program will prompt the user for the values
    needed to construct the LifeGame object as well
    as the number of generations to be shown
  • It will then have the object show the desired
    number of generations.

21
Life1.java
  • import java.util.Scanner
  • public class Life1
  • public static void main(String args)
    Scanner scan new Scanner(System.in)
  • int usize, back, forward, i, aRule, col,
    GenerationCount
  • LifeGame.State map
  • LifeGame.Action rules
  • System.out.println( "\nThis program will do
    a simulation "of the 1-dimensional game
    of Life.")

22
Life1.java
  • System.out.print("\nPlease enter the number of
    cells in your universe ")usize
    scan.nextInt()
  • System.out.print("\nPlease enter the number
    of neighbors to the left ") back
    scan.nextInt()
  • System.out.print("\nPlease enter the number
    of neighbors to the right ") forward
    scan.nextInt()
  • map new LifeGame.Stateusize2
  • rules new LifeGame.Actionbackforward1
  • System.out.println("\nPlease enter the state
    change rules. "For each possible
    value, ")
  • System.out.println("enter 0 to make dead, 1
    to make alive, "2 to stay same, 3 to
    switch")

23
Life1.java
  • System.out.print("\nPlease enter the number of
    cells in your universe ")usize
    scan.nextInt()
  • System.out.print("\nPlease enter the number
    of neighbors to the left ") back
    scan.nextInt()
  • System.out.print("\nPlease enter the number
    of neighbors to the right ") forward
    scan.nextInt()
  • map new LifeGame.Stateusize2
  • rules new LifeGame.Actionbackforward1

24
Life1.java
  • for (i 0 i lt backforward i)
  • System.out.print("\nRule for neighbor count "
    i " ")
  • aRule scan.nextInt()
  • switch (aRule)
  • case 0 rulesi LifeGame.Action.KILL b
    reak
  • case 1 rulesi LifeGame.Action.MAKE_ALIVE
    break
  • case 2 rulesi LifeGame.Action.STAY b
    reak
  • case 3 rulesi LifeGame.Action.SWITCH

25
Life1.java
  • for (col 0 col lt usize 1 col)
  • mapcol LifeGame.State.DEAD //
    Set all cells empty,
  • System.out.println("\n\nOn each line give a
    coordinate for a living" "
    cell.\nTerminate the list with the special value
    0.\n")
  • col scan.nextInt()
  • while (col ! 0) / Check termination
    condition. /
  • if ( col gt 1 col lt usize)
  • mapcol LifeGame.State.ALIVE
  • else
  • System.out.println("Value is not within
    range.\n")
  • col scan.nextInt()

26
Life1.java
  • LifeGame G new LifeGame(usize,back,forward,map
    ,rules)
  • G.showRules()
  • System.out.println()
  • System.out.print("Enter the desired number
    of generations to view ")
  • GenerationCount scan.nextInt()
  • G.showGenerations(GenerationCount)

27
Life1.java
  • LifeGame G new LifeGame(usize,back,forward,map,r
    ules)
  • G.showRules()
  • System.out.println()
  • System.out.print("Enter the desired number of
    generations to view ")
  • GenerationCount scan.nextInt()
  • G.showGenerations(GenerationCount)
Write a Comment
User Comments (0)
About PowerShow.com