[ Completed Project, Thanks for your help ]
[ Completed Project, Thanks for your help ]
Last edited by KBakerSR; 05-18-2008 at 08:23 PM.
Seems to me like you have out-of-bounds memory accesses and you check them after the fact in livingcell and deadcell. Also, the upper bound check in those functions is incorrect.
What would be a lot easier would be to modify boolMatrix.get():
The other thing you have wrong is that you're counting the current cell.Code:bool boolMatrix::get(int row, int col) { if(row < 0 || row >= rows || col < 0 || col >= cols) { return false; } return matrix[row][col]; }
The distinction between deadcell and livingcell is not useful. The functions are too similar.
Also, the loops in those functions are misplaced. Way too complicated for something so simple, since you know they'll never change. Unroll them. Also, you can take advantage of the implicit conversion of bool to int.
Then you do:Code:int truecount = generation.get(row-1, col-1) + generation.get(row-1, col ) + generation.get(row-1, col+1) generation.get(row-1, col ) + 0 + generation.get(row , col+1) generation.get(row-1, col+1) + generation.get(row+1, col ) + generation.get(row+1, col+1)
This is possible because of a shorter reformulation of the rules:Code:switch(truecount) { case 2: newGeneration.set(row, col, generation.get(row, col)); case 3: newGeneration.set(row, col, true); default: newGeneration.set(row, col, false); }
1) If a cell has two living neighbors, it stays the way it is.
2) If a cell has three living neighbors, it will be alive, whether it was now or not.
3) Otherwise, it will be dead, whether it was now or not.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
THANK YOU!! Your way was much more simple to follow and check for errors. The boundries were a little messed up because I just coppied and pasted the contents of one for statement 3 times in each function and forgot to change what all the boundries were reading to fit each for statement. Also, I wasn't even thinking about the current cell being counted which is probably why I wasn't able to get the correct answer no matter how many different ways i wrote this section of code. I've re-written it four times now and yours is by far the best way.
for your reference, there was a very similar problem on the CEMC contest (a Canadian CS contest for high school students, which I participated in)
http://www.cemc.uwaterloo.ca/ccc/2006/senior.pdf
scroll down to S5. The name is Origin of Life, your teacher probably changed it to avoid googling. The problem is much harder, though, because it wants the students to work backwards, from a current generation, generating a tree, and also it's a lot more parameterized. That was the super hard question of the year =).
Actually that game was changed from the original, the original thing is called Conway’s Game of Life which is pretty much the exact program my teacher is having us write. They are almost exactly the same with an exception of the Origin of Life game having to find the garden of eden generation and what you said about having to work backwards to figure out the Origin of Life. Thank you for that link though, it will be an interesting thing to check out.
> [ Completed Project, Thanks for your help ]
Please don't do this - you've basically trashed the thread so no-one else can possibly learn from (or even contribute to) the discussion.
Talk about being selfish - you've got your cookie, and it's "adios suckers".
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Salem, I agree. That should totally be disallowed. Maybe users with fewer than x posts should not be allowed to edit them.
alright, sorry for that. I will post my entire code with comments and everything later tonight or tomarrow. and Salem what is the problem with being urgent about getting an assignment done that was due in one day. I had worked on trying to get the correct answer to come out for hours and couldnt do it so my last resort was to post on two forums that I thought looked like I would get the best answers from, not all over the internet.