Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a java program that cotains the following requirements: The game of Life (

ID: 3757700 • Letter: W

Question

write a java program that cotains the following requirements:

The game of Life (not the board game) is intended to model life in a society of organisms. Given a rectangular grid of cells (i.e. a two dimensional array), a cell (individual square of the grid) may contain an organism. Except for cells on the border of the grid, each cell in the grid has eight neighbors(above, below, right, left, above and to the right, above and to the left, below and to the right, below and to the

left.) Organism are born and die according to the following rules:

a) an organism is born in an empty cell that has exactly three neighbors

b) an organism will die from isolation if it has fewer than two neighbors

c) an organism will die from overcrowding if it has more than three neighbors

The following display shows the first five generations of a particular configuration of organisms:

                                                            *

                        *             * * *            *               * * *

* * *           * * *                           *      *           *    *

     *              * * *          *    *             *               * * *

                                          *               *

1st               2nd             3rd            4th               5th

Write a program to play the game of Life.

Specifics:

1. Use methods.

2.      The grid is 10 by 8.

3.      Create a method of displaying the grid with the cells in their respective location.

4.      Show the user the first generation and ask if they like to see the next generation. Your program should continue until the user decides to quit.

5.      You’ll need to consider boundary areas. Make sure you don’t go outside the bounds of your grid.

Tips:

1.      Use two arrays, one that holds the old grid configuration and one that holds the next (new) generation configuration. Create a method to copy the contents of new configuration to the old.

2.      Get the display method working right away. This way you can tell if the game rules are being applied correctly.

3.      To save time, you may hardcode the initial organism coordinates.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

   void header(void);

   void survivalRule(char [][20], int, int);

   void birthRule(char [][20], int, int);

   void deathRule(char [][20], int, int);

      

    int main(void)

   {

      char Life[20][20];

      int orgs, gens;

      int i, a, b, row, col;

      int count = 0;

      int x = 19;

      int y = 19;

      char ans;

      

      header();

      do{

         printf(" Please enter the number of organisms you would like to live in the first generation: ");

         scanf("%i", &orgs);

     

         srand( (unsigned)time( NULL ) );

             

         for(i = 0; i<orgs; i++)

         {

            row = rand();

            row %= 20;

            col = rand();

            col %= 20;

            Life[row][col] == '*';

         }

       

         for(row = 0; row<20; row++)

         {

            for(col = 0; col<20; col++)

            {

               if(Life[row][col] != '*')

                   Life[row][col] == ' ';

            }

         }

             

         printf(" Now enter the number of generations you would like to simulate: ");

         scanf("%i", &gens);

             

         for(row = 0; row<20; row++)

         {

              for(col = 0; col<20; col++)

              {

                  printf("%s", Life[row][col]);

              }

              puts(" ");

          }

           

         do{               

            birthRule(Life, x, y);

            survivalRule(Life, x, y);

            deathRule(Life, x, y);

                for(row = 0; row<20; row++)

             {

                  for(col = 0; col<20; col++)

                  {

                      printf("%s", Life[row][col]);

                  }

                  puts(" ");

             }

            count++;

         }while(count<gens);

         

         printf("Would you like to simulate another generation? y or n: ");

         

         scanf("%c", &ans);

              

      }while( ans != 'n' && ans != 'N');

      

      return 0;

   }

           

               

    void header(void) /*function for program header*/

   {

      printf(" ..Welcome to the Game of Life.. ");

      printf("Follow the instructions to start your own simulation of life! ");

   }

   

    void survivalRule(char Life[][20], int x, int y)

   {

      int row, col;

      int neighbors = 0;

      for(row = 1; row<19; row++)

      {

         for(col = 1; col<19; col++)

         {

            if(Life[row][col] == '*')

            {

               if(Life[row - 1][col - 1] == '*')

                  ++neighbors;

               if(Life[row - 1][col] == '*')

                  ++neighbors;

               if(Life[row - 1][col + 1] == '*')

                  ++neighbors;

               if(Life[row][col - 1] == '*')

                  ++neighbors;

               if(Life[row][col + 1] == '*')

                  ++neighbors;

               if(Life[row + 1][col - 1] == '*')

                  ++neighbors;

               if(Life[row + 1][col] == '*')

                  ++neighbors;

               if(Life[row + 1][col + 1] == '*')

                  ++neighbors;

               if(neighbors == 2 || neighbors == 3)

               {

                  Life[row][col] == '*';

               }

            }

         }

      }

      return;

   }

   

    void birthRule(char Life[][20], int x, int y)

   {

      int row, col;

      int neighbors = 0;

      for(row = 1; row<19; row++)

      {

         for(col = 1; col<19; col++)

         {

            if(&Life[row][col]== " ")

            {

               if(Life[row - 1][col - 1] == '*')

                  neighbors++;

               if(Life[row - 1][col] == '*')

                  neighbors++;

               if(Life[row - 1][col + 1] == '*')

                  neighbors++;

               if(Life[row][col - 1] == '*')

                  neighbors++;

               if(Life[row][col + 1] == '*')

                  neighbors++;

               if(Life[row + 1][col - 1] == '*')

                  neighbors++;

               if(Life[row + 1][col] == '*')

                  neighbors++;

               if(Life[row + 1][col + 1] == '*')

                  neighbors++;

               if(neighbors == 3)

              {

                   Life[row][col] == '*';

               }

            }

         }

      }

      return;

   }

   

    void deathRule(char Life[][20], int x, int y)

   {

      int row, col;

      int neighbors = 0;

      for(row = 1; row<19; row++)

      {

         for(col = 1; col<19; col++)

         {

            if(Life[row][col] == '*')

            {

               if(Life[row - 1][col - 1] == '*')

                  neighbors++;

               if(Life[row - 1][col] == '*')

                  neighbors++;

               if(Life[row - 1][col + 1] == '*')

                  neighbors++;

               if(Life[row][col - 1] == '*')

                  neighbors++;

               if(Life[row][col + 1] == '*')

                  neighbors++;

               if(Life[row + 1][col - 1] == '*')

                  neighbors++;

               if(Life[row + 1][col] == '*')

                  neighbors++;

               if(Life[row + 1][col + 1] == '*')

                  neighbors++;

               if(neighbors < 2 || neighbors > 4)

               {

                  Life[row][col] == ' ';

               }

            }

         }

      }

      return;

   }