Question

In: Computer Science

Write a program in C that implements Conway's Game of Life. You will be expected to...

Write a program in C that implements Conway's Game of Life. You will be expected to apply the principles of Design by Contract to your code.

The rules for the Game of Life are simple. The universe consists of a two-dimensional matrix of cells with each cell being alive or dead. For each generation every cell determines its next phase of life as follows:

  • If the cell is alive:
    1. it dies if it has 0, 1, 4 or more living neighbours (starvation), or
    2. it lives if it has 2 or 3 living neighbours (balance).
  • If the cell is dead:
    1. it springs to life if it has exactly 3 neighbours (procreation).

A cycle occurs as soon as the state of the universe of the latest generation is the same as a previous generation. You should be able to see that once this occurs the Game of Life will repeat the intermediate generations forever.

Implementation Requirements

All of the following conditions must be met by your solution.

  • Your program must read the initial state from standard input (redirecting files on the command line is the way to go!). Here is a sample input file to use for testing. You will be expected to adequately test your program -- I will not be providing the files used by your marker. All input will conform to the following format:
    • The input will consist of starting universes for multiple games. Play each game completely, in the order they appear in the file.
    • The first line of the game starts with an asterisk and contains the game title.
    • The second line of the game contains two numbers, separated by a space, which indicate the number of rows and columns for the 2-D matrix.
    • Then there is one line for each row in the matrix:
      • A blank character represents a dead cell.
      • An 'X' represents an alive cell.
    • The next game starts on the next line, until EOF.
    • There are no errors in the input file format.
  • For each game, your program will first print the game title as read from the file. Print the starting universe as read in. Then, after each generation is calculated your program will print the current state of the universe. All printing goes to standard output, and each universe must be preceded by a label indicating the generation number (where the initial state is generation 0). Here is a sample of the output expected. You must include a border, use '.' for dead cells and '*' for alive cells.
  • Each game will run for 250 generations or until a cycle is detected, whichever comes first. If the game ends because of a cycle, print the numbers of the two generations that were duplicated.
  • Your program must include appropriate pre and post conditions.
  • Printing all generations may be great for debugging purposes (and should prove mesmerizing) but the release version should only print the initial universe, and the last 10 (or less) generations. When your program is compiled with -DNDEBUG, the behaviour must change from printing all generations to only printing the last 10. The sample output linked above is run with -DNDEBUG.

SAMPLE INPUT

*Case #1
10 20
                    
                    
                    
       X            
        X           
      XXX           
                    
                    
                    
                    
*Case #2: R-Pentomino
7 7
       
       
   XX  
  XX   
   X   
       
       
*Case #3: Trans-boat and dock getting blown up by a lightweight spaceship
8 33
    X                        X  X
   X X                      X    
    XX                      X   X
                            XXXX 
    XXXX                         
   X    X                        
   XX  XX                        
                                 
*Case #4: Glider Gun
20 36
                        X           
                      X X           
            XX      XX            XX
           X   X    XX            XX
XX        X     X   XX              
XX        X   X XX    X X           
          X     X       X           
           X   X                    
            XX                      
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                          

SAMPLE OUTPUT

*Case #1
Generation 0:
+--------------------+
|....................|
|....................|
|....................|
|.......*............|
|........*...........|
|......***...........|
|....................|
|....................|
|....................|
|....................|
+--------------------+
Found a cycle between generation 19 and generation 20
Generation 11:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|.........*..........|
|..........**........|
|.........**.........|
|....................|
+--------------------+
Generation 12:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|..........*.........|
|...........*........|
|.........***........|
|....................|
+--------------------+

Solutions

Expert Solution

PROGRAM :

#include <iostream>
#include <cstdlib>


const int gridsize = 75; //Making this a global constant to avoid array issues.

void Display(bool grid[gridsize+1][gridsize+1]){
for(int a = 1; a < gridsize; a++){
for(int b = 1; b < gridsize; b++){
if(grid[a][b] == true){
std::cout << " *";
}
else{
std::cout << " ";
}
if(b == gridsize-1){
std::cout << std::endl;
}
}
}
}
//This copy's the grid for comparision purposes.
void CopyGrid (bool grid[gridsize+1][gridsize+1],bool grid2[gridsize+1][gridsize+1]){
for(int a =0; a < gridsize; a++){
for(int b = 0; b < gridsize; b++){grid2[a][b] = grid[a][b];}
}
}
//Calculates Life or Death
void liveOrDie(bool grid[gridsize+1][gridsize+1]){
bool grid2[gridsize+1][gridsize+1] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < gridsize; a++){
for(int b = 1; b < gridsize; b++){
int life = 0;
for(int c = -1; c < 2; c++){
for(int d = -1; d < 2; d++){
if(!(c == 0 && d == 0)){
if(grid2[a+c][b+d]) {++life;}
}
}
}
if(life < 2) {grid[a][b] = false;}
else if(life == 3){grid[a][b] = true;}
else if(life > 3){grid[a][b] = false;}
}
}
}

int main(){

//const int gridsize = 50;
bool grid[gridsize+1][gridsize+1] = {};

//Still have to manually enter the starting cells.
grid[gridsize/2][gridsize/2] = true;
grid[gridsize/2-1][gridsize/2] = true;
grid[gridsize/2][gridsize/2+1] = true;
grid[gridsize/2][gridsize/2-1] = true;
grid[gridsize/2+1][gridsize/2+1] = true;

while (true){
//The following copies our grid.

Display(grid); //This is our display.
liveOrDie(grid); //calculate if it lives or dies.
system("CLS");
}
}


Related Solutions

Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Python3  Please add comments Write a program that implements the word guessing game - There is a...
Python3  Please add comments Write a program that implements the word guessing game - There is a secret word - The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess. If the user reveals all the letters in the word before getting too many wrong guesses then they win! Otherwise, they lose. 1 - define secret word 2 - create a revealed letter list that...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT