Game of life in C .plz check

assignment to write game of life in C . plz crossecheck and give feedb

Page 1 of 1

1 Replies - 2298 Views - Last Post: 06 April 2008 - 08:05 AM Rate Topic: -----

#1 akash_9105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-April 08

Game of life in C .plz check

Posted 06 April 2008 - 07:46 AM

hello evryone

well i have gt an assignment C programming, i have written the code i just want your feedback and plz tell me if its good..

The Problem: The Game of Life
The game of life is a simple cellular automaton where the world is a 2D grid of cells which have two states:
alive or dead. At each iteration, the new state of a cell is determined by the state of its neighbors at the
previous iteration. This includes both the nearest neighbors and the diagonal neighbors, i.e., like a 9 point
stencil without using the value of the cell itself as input. The rules for the evolution of the system are:
• If a cell has exactly two alive neighbors, it maintains state.
• If a cell has exactly three alive neighbors, it is alive.
• Otherwise, the cell is dead.
Your code will need to:
1. Initialize the gameboard
2. Start loop:
(a) Print the gameboard (temporarily for correctness checking)
(B) Calculate number of live neighbors
© If (live neighbors = 3) then live
(d) If (live neighbors < 2) or (live neighbors > 3) then die
3. End loop
4. Print final gameboard (again temporarily for correctness checking)



The Assignment

3.1 The Basic Sequential Program:
Details of each part follow:

1. Initialization: Take the value of n (the dimension of the board) as an argument to your MPI program.
Use array syntax to initialize a board (an n x n INTEGER array with value 1 for a live cell and 0 for a
dead one) with the following pattern: let n = 8, and set the row and column nearest the center (n/2)
to be alive with all other cells dead. For n = 8, that means that the live cells will be all of row 4 and
all of column 4.

2. Print board: The board can be printed to the display as lines of 0’s and 1’s. or as a series of bitmaps
which can be animated using xv, by having the board be printed in pgm format (look on the internet
for more information on pgm format if not familiar with it).

3. Update: Declare a count array the same size as the board (n x n) to contain the number of live
neighbors for each point. Include all nearest neighbors, including diagonal neighbors.

4. Test: Compile and run the code on a single processor for n=8 and 10 iterations of evolution to check
that it produces a set of correct state configurations. Use xv to view the resulting arrays to check that
your code is working. After checking correctness, you can comment out the print statements inside the
loop and keep the final print of the final board.

And in my C syllabus i have only covered these topics so it has to be done according to these cahpters knowledge..

Lecture 1 - Datatypes, input, output
Lecture 3 - Operators, Preprocessors,
Conditions
Lecture 4 - Introduction to Functions
Lecture 5 - More on Loops and Functions,
Type conversion
Lecture 6 – Arrays
Lecture 7 - String, Enumeration
Lecture 8 - File manipulation


My pice of code is..

#include <stdio.h>

int min(int a, int b)
{
  if ( a < b )
	return a;
  return b;
}

int max(int a, int b)
{
  if ( a > b )
	return a;
  return b;
}


int game_board[11][11] = {
/*  1  2  3  4  5  6  7  8  9  0  1 */
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
  { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
  { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 },
  { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0 },
  { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 },
  { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
  { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
  };
  
int count_alive_neighbors(int board_width, int board_height, int x, int y)
{
  int start_x, start_y;
  int end_x, end_y;
  int i, j;
  int alive_neighbors = 0;
  
  /*
   * XXXXXXXXX
   * XXXXXXXXX
   * XXXXXXXXX
   * XXXXXXXXX
   * XXXX0XXXX
   * XXXXXXXXX
   * XXXXXXXXX  
   * XXXXXXXXX
   * XXXXXXXXX
  */	
  
  start_x = max(x-4, 0);
  start_y = max(y-4, 0);
  end_x   = min(x+4, board_width-1);
  end_y   = min(y+4, board_height-1);
  
  for(i = start_x; i < end_x; i++) {
	for(j = start_y; j < end_y; j++) {
	  if ( i != x && j != y )
		alive_neighbors += (game_board[i][j] == 1);
	}
  } 
  
  return alive_neighbors;
}

void update_cells_life(int board_width, int board_height)
{
  int tmp_board[11][11];
  int i, j;
  int nb_alive_neighbors = 1;

  for(i = 0; i < board_width; i++) {
	for(j = 0; j < board_height; j++) {  
	  tmp_board[i][j] = game_board[i][j];
	}
  }
  
  for(i = 0; i < board_width; i++) {
	for(j = 0; j < board_height; j++) {
	  nb_alive_neighbors = count_alive_neighbors(board_width, board_height, i, j);
	  if ( nb_alive_neighbors == 3 )
		tmp_board[i][j] = 1;
	  else if ( nb_alive_neighbors != 2 )
		tmp_board[i][j] = 0;
	}	
  }
  
  for(i = 0; i < board_width; i++) {
	for(j = 0; j < board_height; j++) {  
	  game_board[i][j] = tmp_board[i][j];
	}
  }
}


void display_board(int board_width, int board_height)
{
  int i, j;
  
  printf("\n");
  for(j = 0; j < board_height; j++) {
	for(i = 0; i < board_width; i++) {
	  if ( game_board[i][j] == 1 ) {
		printf("X");
	  } else {
		printf(" ");
	  }			
	}
	printf("\n");
  }
  
}

int main()
{
  int iteration = 0;
  
  for(;;) {
	printf("Iteration %d:\n", iteration++);
	display_board(11, 11);
	getchar();   
	update_cells_life(11, 11);
  }
 
}



just tell me if its ok..??

And a friend told me i can do it using PREPROCESSORS.. can anyone tell me how and wat code to put ?? thanxx

Is This A Good Question/Topic? 0
  • +

Replies To: Game of life in C .plz check

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4884
  • View blog
  • Posts: 11,279
  • Joined: 16-October 07

Re: Game of life in C .plz check

Posted 06 April 2008 - 08:05 AM

You should reasonably be able to tell if it's ok if you run it and it works. :P

Still, one that jumped out ( everything else looks fine at first glance ) was this:
start_x = max(x-4, 0);
start_y = max(y-4, 0);
end_x   = min(x+4, board_width-1);
end_y   = min(y+4, board_height-1);



What's with the "4"? Shouldn't it be "1"?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1