Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical session 8 Assignment 3. Game of life A zero-player game. Simulates Evolution, of an infinite two-dimensional matrix’s cells. Each cell can be.

Similar presentations


Presentation on theme: "Practical session 8 Assignment 3. Game of life A zero-player game. Simulates Evolution, of an infinite two-dimensional matrix’s cells. Each cell can be."— Presentation transcript:

1 Practical session 8 Assignment 3

2 Game of life A zero-player game. Simulates Evolution, of an infinite two-dimensional matrix’s cells. Each cell can be alive or dead. A cell’s state in each iteration (generation) is set with accordance to its former state and its neighbors’ states.

3 Our version We’ll simulate a simplified, one-dimensional (finite) array. Each cell is given an initial state on program startup At each “generation”, a cell will determine its next state according to its former state and its neighbors’ states, using a given transition function

4 Implementation Using the co-routine mechanism, we’ll write a small simulator for the game. Running co-routines: – n cell instances – A printer – A scheduler

5 A Cell Can be alive(1) or dead(0) Executes a simple infinite loop: 1.Calculate next state using current state (of “me” and my left and right neighbors). 2.Update new state After each of these two stages, the cell co- routine must resume the scheduler. In other words, it loops (for ever) over: (1)  resume  (2)  resume

6 A Cell Just to be perfectly clear: – cell i ‘s left neighbor is cell i-1. – cell i ‘s right neighbor is cell i+1. – The Array “wraps around”  calculations are %WorldSize

7 A Cell How do we determine a cell’s next step? – One of the program’s parameters is a transition function: A sequence of hexadecimal digits (a nibble of bits) such as “B1E3”. Each digit represents a transition. B  1011 Left neighbor’s state Right neighbor’s state “My” state new state

8 Transition example Given this transition function: B = 1 0 1 1 1 = 0 0 0 1 E = 1 1 1 0 3 = 0 0 1 1 Say a cell is dead, its left neighbor is dead and its right neighbor is alive, then “3” can be applied, and thus the next state is alive (1). When no transition can be applied, no transition is done: Say a cell is alive, its left neighbor is alive and its right neighbor is dead  no transition fits the case (we’d need either “C” or “D”), and thus no change is made.

9 The printer Simply prints the entire “world” (the array), whenever it gets “time”.

10 The scheduler You are to implement a simple round-robin scheduler. void scheduler(int cycles) – Iterate cycles times over all cells – After each K resumes of cell co-routines, resume the printer. – At the end of all cycles, resume the printer once more, and then terminate the process.

11 Program’s flow Invoked using: >ass3 where: – - A series of “0” and “1”. describes the initial state, as well as the amount of cells – - Amount of scheduler cycles through each cell – - Printing frequency – - A series of hexadecimal digits

12 Program’s flow When invoked, and using the co-routines mechanism from class, your application will: – Set up: a state array, WorldSize, K – Initiate all co-routines: Each cell needs to get a parameter i with its index. The scheduler needs to get cycles (t) as a parameter. Initiate the printer – Initiate an array CORS of pointers to: cell 1, cell 2, …, cell WorldSize, scheduler, printer. – Transfer control to scheduler

13 Technicalities You are to submit a single.zip file, using the same directory structure as in previous assignments. src folder will include: – ass3.s (with most of the code) – printer.s (printer function code) – scheduler.s (scheduler function code)

14 Example Let’s run an example using your simple scheduler: >ass3 01010010 5 7 029B46DF Cellleftcurrnext 0001 1010 2101 3010 4100 5001 6010 7100 First 8 “time slices” produce readings and state computations: New 2 4 B 0 1 1 1 0 0 0 0 4 9 2 4 9

15 Example 01010010;after 7 “resumes”. No updates. 00101010;after 14; 6 updates made. 00101001;(21): no second updates yet 10011001;(28): 4 second cycle updates 10010100;(35): 2 nd cycle updated 10010100;(40): last print (done cycling)


Download ppt "Practical session 8 Assignment 3. Game of life A zero-player game. Simulates Evolution, of an infinite two-dimensional matrix’s cells. Each cell can be."

Similar presentations


Ads by Google