Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Game of life issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Psilobe   Members   -  Reputation: 193

Like
0Likes
Like

Posted 17 October 2012 - 08:28 AM

I'm trying to implement the game of life in c++ but I've run in to some trouble where I don't get the correct result.
I think the problem is with memcpy() as it seems to mess up the arrays but I'm not sure why. If I just copy the two different arrays using nested for loops I get a different result which is also wrong.

[source lang="cpp"]#include <iostream>#include <conio.h>#include <string>void update();void init();int calcNeighbours(int i, int j);void draw();bool run = true;bool field[20][20];int main(){ init(); draw(); getch(); while(run) { update(); draw(); getch(); } return 0;}void update(){ int temp = 0; int tf[20][20]; memcpy(tf,field,sizeof(tf)); for(int i = 0;i<20;i++) { for(int j = 0;j<20;j++) { temp = calcNeighbours(i,j); if(field[i][j]==true && temp < 2) { tf[i][j] = false; } else if(field[i][j]==true && temp > 3) { tf[i][j] = false; } else if(field[i][j]==true && (temp == 2 || temp == 3)) { tf[i][j] = true; } else if(field[i][j]==false && temp == 3) { tf[i][j] = true; } } } memcpy(field,tf,sizeof(field));}void init(){ for(int i = 0;i<20;i++) { for(int j = 0;j<20;j++) { field[i][j] = false; } } field[3][5] = true; field[4][5] = true; field[5][5] = true; field[4][4] = true; field[4][6] = true;}int calcNeighbours(int i, int j){ int temp = 0; for(int a = -1;a<1;a++) { for(int b = -1;b<1;b++) { if(field[i-a][j-b] == true) temp+=1; } } return temp;}void draw(){ system("cls"); for(int i = 0;i<20;i++) { std::cout<<std::endl; for(int j = 0;j<20;j++) { if(field[i][j]==true) std::cout<<"X"; else std::cout<<" "; } }}[/source]

Sponsor:

#2 apatriarca   Crossbones+   -  Reputation: 1743

Like
3Likes
Like

Posted 17 October 2012 - 08:32 AM

You have defined field as an array of booleans and tf as an array of integers. They have different types and probably also different sizes.

#3 Psilobe   Members   -  Reputation: 193

Like
0Likes
Like

Posted 17 October 2012 - 08:35 AM

Thanks, now the copying works. I still got a problem with the figures being wrong. Somewhere I do something wrong with the rules of game of life or calculating the neighbors but I cant find where.

I found what I did wrong, in calcNeighbors when I loop I wrote <1 instead of <=1.

Edited by Psilobe, 17 October 2012 - 08:41 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS