5 Replies - 520 Views - Last Post: 06 July 2013 - 07:26 AM Rate Topic: -----

#1 jellyman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-July 13

Game of Life

Posted 05 July 2013 - 07:47 PM

Alright, so I've been trying to make a simple Game of Life program, but when it enters the next generation it never shows anything for it just blanks.

Anyone care to help? :bigsmile:/>

#include <iostream>

using namespace std;

#include <conio.h>	
#include <memory.h>
#include <stdlib.h>			

void main ()
	{
		const	int		maxc	(60);
		const	int		maxr	(60);
		
			bool	bContinue;
			bool	Board [maxr + 2] [maxc + 2];
			bool	NextBoard [maxr + 2] [maxc + 2];
			int		Col;
			int		Row;
			int		Generation;
		memset (Board, false, (maxr + 2) * (maxc + 2) * sizeof (bool));	
		memset (NextBoard, false, (maxr + 2) * (maxc + 2) * sizeof (bool));

		cout << "Row numbers must go from 1 to " << maxr << ", Column numbers from 1 to " << maxc << endl;
		cout << "Enter a zero for both values to stop entering cells and go to generations" << endl;

		do	{
		bContinue = true;
		cout << "Enter a row and col pair: ";
		cin >> Row >> Col;
		if ((Row == 0) && (Col == 0))
				bContinue = false;

			else
						Board [Row] [Col] = true;

		} while (bContinue);

		for (Generation = 0; ; Generation++)
		{
		cout << "Generation " << Generation << endl;
		for (Row = 1; Row <= maxr; Row++)
			{
			for (Col = 1; Col <= maxc; Col++)
				cout << (Board [Row] [Col] ? '*' : ' ');
			cout << endl;
		}
		{
			int numneighbor;
			numneighbor = 0;

			if (Board [Row] [Col-1] == '*' )
			numneighbor+=1;

			if (Board [Row] [Col+1] == '*' )
			numneighbor+=1;

			if (Board [Row-1] [Col] == '*' )
			numneighbor+=1;

			if (Board [Row+1] [Col] == '*' )
			numneighbor+=1;

			if (Board [Row+1] [Col+1] == '*' )
			numneighbor+=1;

			if (Board [Row-1] [Col-1] == '*' )
			numneighbor+=1;

			if (Board [Row-1] [Col+1] == '*' )
			numneighbor+=1;

			if (Board [Row+1] [Col-1] == '*' )
			numneighbor+=1;


		    if (Board [Row] [Col] == '*' && numneighbor <= 1)
				NextBoard [Row] [Col] = ' ';

			else if (Board [Row] [Col] == '*' && numneighbor >= 4)
				Board [Row] [Col] = ' ';

			else if (Board [Row] [Col] == '*' && numneighbor == 3)
				Board [Row] [Col] = '*';
			else if (Board [Row] [Col] == ' ' && numneighbor == 3)
				Board [Row] [Col] == '*';
		}
			
		memcpy (Board, NextBoard, (maxr + 2) * (maxc + 2) * sizeof (bool));
		while (!_kbhit ());
		_getch ();
		
		}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Game of Life

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Game of Life

Posted 05 July 2013 - 07:57 PM

Look closely at your lines 76-85. Why does line 77 use NextBoard, but lines 80, 83, and 85 use Board?
Was This Post Helpful? 0
  • +
  • -

#3 jellyman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-July 13

Re: Game of Life

Posted 05 July 2013 - 08:11 PM

View PostSkydiver, on 05 July 2013 - 07:57 PM, said:

Look closely at your lines 76-85. Why does line 77 use NextBoard, but lines 80, 83, and 85 use Board?


I think I was trying everything because of frustration and forgot to undo the whole thing.
Even if I use Nextboard it still displays a blank next generation.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Game of Life

Posted 05 July 2013 - 09:14 PM

Next, look closely at your loops. You are not iterating over all the rows and columns to compute the next generation. Additionally, the limits for your loops look suspicious because arrays in C have indices running from 0 to n-1, but your loops look like they are going from 1 to n.
Was This Post Helpful? 0
  • +
  • -

#5 jellyman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-July 13

Re: Game of Life

Posted 05 July 2013 - 09:26 PM

View PostSkydiver, on 05 July 2013 - 09:14 PM, said:

Next, look closely at your loops. You are not iterating over all the rows and columns to compute the next generation. Additionally, the limits for your loops look suspicious because arrays in C have indices running from 0 to n-1, but your loops look like they are going from 1 to n.


Sorry, but I don't understand anything you just said. Were you referring to this loop?
int numneighbor;
			numneighbor = 0;

			if (Board [Row] [Col-1] == '*' )
			numneighbor+=1;

			if (Board [Row] [Col+1] == '*' )
			numneighbor+=1;

			if (Board [Row-1] [Col] == '*' )
			numneighbor+=1;

			if (Board [Row+1] [Col] == '*' )
			numneighbor+=1;

			if (Board [Row+1] [Col+1] == '*' )
			numneighbor+=1;

			if (Board [Row-1] [Col-1] == '*' )
			numneighbor+=1;

			if (Board [Row-1] [Col+1] == '*' )
			numneighbor+=1;

			if (Board [Row+1] [Col-1] == '*' )
			numneighbor+=1;


			else; 

		   if (Board [Row] [Col] = '*' && numneighbor <= 1)
				NextBoard [Row] [Col] = ' ';

		   if (Board [Row] [Col] = '*' && numneighbor >= 4)
				NextBoard [Row] [Col] = ' ';

		   if (Board [Row] [Col] = '*' && numneighbor == 3)
				NextBoard [Row] [Col] = '*';


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Game of Life

Posted 06 July 2013 - 07:26 AM

Yes, that is part of it... or more accurately... it is not part of a loop that computes all the rows and columns.

If you took time to apply a consistent indent style, it would be visually obvious:

#include <iostream>

using namespace std;

#include <conio.h>	
#include <memory.h>
#include <stdlib.h>			

void main()
{
    const	int		maxc(60);
    const	int		maxr(60);

    bool	bContinue;
    bool	Board[maxr + 2][maxc + 2];
    bool	NextBoard[maxr + 2][maxc + 2];
    int		Col;
    int		Row;
    int		Generation;

    memset(Board, false, (maxr + 2) * (maxc + 2) * sizeof (bool));	
    memset(NextBoard, false, (maxr + 2) * (maxc + 2) * sizeof (bool));

    cout << "Row numbers must go from 1 to " << maxr << ", Column numbers from 1 to " << maxc << endl;
    cout << "Enter a zero for both values to stop entering cells and go to generations" << endl;

    do	
    {
        bContinue = true;
        cout << "Enter a row and col pair: ";
        cin >> Row >> Col;
        if ((Row == 0) && (Col == 0))
            bContinue = false;
        else
            Board[Row][Col] = true;

    } while (bContinue);

    for (Generation = 0; ; Generation++)
    {
        cout << "Generation " << Generation << endl;
        for (Row = 1; Row <= maxr; Row++)
        {
            for (Col = 1; Col <= maxc; Col++)
                cout << (Board[Row][Col] ? '*' : ' ');
            cout << endl;
        }
        {
            int numneighbor;
            numneighbor = 0;

            if (Board[Row][Col-1] == '*')
                numneighbor+=1;

            if (Board[Row][Col+1] == '*')
                numneighbor+=1;

            if (Board[Row-1][Col] == '*')
                numneighbor+=1;

            if (Board[Row+1][Col] == '*')
                numneighbor+=1;

            if (Board[Row+1][Col+1] == '*')
                numneighbor+=1;

            if (Board[Row-1][Col-1] == '*')
                numneighbor+=1;

            if (Board[Row-1][Col+1] == '*')
                numneighbor+=1;

            if (Board[Row+1][Col-1] == '*')
                numneighbor+=1;


            if (Board[Row][Col] == '*' && numneighbor <= 1)
                NextBoard[Row][Col] = ' ';
            else if (Board[Row][Col] == '*' && numneighbor >= 4)
                Board[Row][Col] = ' ';
            else if (Board[Row] [Col] == '*' && numneighbor == 3)
                Board[Row][Col] = '*';
            else if (Board[Row] [Col] == ' ' && numneighbor == 3)
                Board[Row][Col] == '*';
        }

        memcpy(Board, NextBoard, (maxr + 2) * (maxc + 2) * sizeof(bool));

        while (!_kbhit ())
            ;
        _getch();
    }
}


This post has been edited by Skydiver: 06 July 2013 - 07:33 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1