1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.

Slides:



Advertisements
Similar presentations
Data Structures Chapter 1 Programming Principles Andreas Savva.
Advertisements

Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Game of Life Rules and Games Linh Tran ECE 573. What is Life? Life is just one example of a cellular automaton, which is any system in which rules are.
1 The Game of Life Supplement 2. 2 Background The Game of Life was devised by the British mathematician John Horton Conway in More sophisticated.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
CELLULAR AUTOMATON Presented by Rajini Singh.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter1 PROGRAMMING PRINCIPLES. Problems of Large Programs 1.The patchwork approach --- Disadvantage: if any change must be made, it will have unpredictable.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
ARRAYS EXAMPLE: THE GAME OF LIFE CITS Scope of this lecture The Game of Life Implementation Performance Issues References:
1 Storage Classes, Scope, and Recursion Lecture 6.
1 Chapter 1 Data structure and Programming Principles.
Chapter 1 PROGRAMMING PRINCIPLES 1. Introduction : Problems with large programs 2.The Game of Life (a continuing example) 3. Programming style 4. Coding,
1 Data Structures CSCI 132, Spring 2014 Lecture 3 Programming Principles and Life Read Ch. 1.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Recursion: Backtracking
The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts.
Review Recursion Call Stack. Two-dimensional Arrays Visualized as a grid int[][] grays = {{0, 20, 40}, {60, 80, 100}, {120, 140, 160}, {180, 200, 220}};
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Fundamental Programming Fundamental Programming for Loops.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
1 Data Structures CSCI 132, Spring 2014 Lecture 18 Recursion and Look-Ahead.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 A Brief Introduction to the Game of Life. 2 Example demonstration ——game of life  Definition  It takes place on an unbounded rectangular grid in which.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Practical Session 9 Computer Architecture and Assembly Language.
Computer Programming -1-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Review 1.
Programming Fundamental
CS 1430: Programming in C++.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
לולאות קרן כליף.
Control Structures Part 1
Looping III (do … while statement)
Alternate Version of STARTING OUT WITH C++ 4th Edition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life

2 Rules of Life The neighbors of a given cell are the eight cells that touch it. Every cell is either living or dead. A living cell stays alive in the next generation if it has either 2 or 3 living neighbors; otherwise it dies (of loneliness or overcrowding) A dead cell becomes alive in the next generation if it has exactly 3 neighboring cells that are already alive. All births and deaths take place at exactly the same time for each generation.

3 Specifications What do we want the program to do?

4 Specifications We want the program to: List instructions Allow user to initialize grid Print out grid at each generation Update next generation Repeat until user says stop

5 Design Class (noun) The Life game itself Methods or functions (What methods or functions should we have?) Print instructions Initialize Print Update

6 The Client Code #include "utility.h" #include "life.h" int main( ) { Life configuration; instructions( ); configuration.initialize( ); configuration.print( ); cout << " Continue viewing new generations? "<<endl; while (user_says_yes( )) { configuration.update( ); configuration.print( ); cout << " Continue viewing new generations? "<<endl; } }

7 Testing with Stubs void instructions( ) { } bool user_says_yes( ) { return(true); } class Life { // In file life.h public: void initialize( ); void print( ); void update( ); }; void Life ::initialize( ) { } // In file life.cc void Life ::print( ) { } void Life ::update( ) { }

8 Class Specification const int maxrow = 20, maxcol = 60;// grid dimensions class Life { public: void initialize( ); void print( ); void update( ); private: int grid[maxrow +2][maxcol +2]; // allows for two extra rows and columns int neighbor_count(int row, int col); };

9 Class implementation int Life ::neighbor_count(int row, int col) { int i, j; int count = 0; //we will fill this in return count; }

10 Class implementation int Life ::neighbor_count(int row, int col) { int i, j; int count = 0; for (i = row - 1; i <= row +1; i ++) { for (j = col - 1; j <= col +1; j ++) { count += grid[i][j];//Increase the count if neighbor is alive. } count -=grid[row][col];// Reduce count, since cell is not its own neighbor. return count; }

11 Using a Driver int main ( ) // driver for neighbor_count( ) { Life configuration; configuration.initialize( ); //we will fill this in. }

12 Using a Driver int main ( ) // driver for neighbor_count( ) { Life configuration; configuration.initialize( ); for (row = 1; row <= maxrow; row ++){ for (col = 1; col <= maxrow; col ++) { cout << configuration.neighbor_count(row, col) << " " ; } cout << endl; }

int Life ::update( ) { int row, col; int new_grid[maxrow+2][maxcol+2]; //we will fill this in for (row = 1; row <= maxrow; row++) { for (col = 1; col <= maxcol; col++) { grid[row][col] = new_grid[row][col]; } //end inner for loop } //end outer for loop } //end update()

int Life ::update( ) { int row, col; int new_grid[maxrow+2][maxcol+2]; for (row = 1; row <= maxrow; row ++) { for (col = 1; col <= maxcol; col ++) { switch (neighbor_count(row, col)) { case 2: new_grid[row][col] = grid[row][col]; break; case 3: new_grid[row][col] = 1; break; default: new_grid[row][col] = 0; } //end switch } //end inner for loop } //end outer for loop for (row = 1; row <= maxrow; row++) { for (col = 1; col <= maxcol; col++) { grid[row][col] = new_grid[row][col]; } //end inner for loop } //end outer for loop } //end update()

int Life ::initialize( ) { int row, col; for (row = 0; row <= maxrow+1; row++) { for (col = 0; col <= maxcol+1; col++) { grid[row][col] = 0; } //end inner for loop } //end outer for loop cout << "List the coordinates for the living cells." << endl; cout << "Terminate the list with the pair " << endl; cin >> row >> col; // we will fill this in } //end initialize()

int Life ::initialize( ) { int row, col; for (row = 0; row <= maxrow+1; row++) { for (col = 0; col <= maxcol+1; col++) { grid[row][col] = 0; } //end inner for loop } //end outer for loop cout << "List the coordinates for the living cells." << endl; cout << "Terminate the list with the pair " << endl; cin >> row >> col; while (row != -1 || col != -1) { if (row >= 1 && row <= maxrow) { if ( col >= 1 && col <= maxcol ) { grid[row][col] = 1; } else { cout << "Column " << col << " is out of range." << endl; } // end inner if-else } else { cout << "Row " << row << " is out of range." << endl; } //end outer if - else cin >> row >> col; } //end while loop } //end initialize()

17 Order of Development 1. initialize( ) 2. print( ) 3. neighbor_count ( ) 4. update( )