Conway's Game of Life: boundaries (12)

1 Name: #!/usr/bin/anonymous : 2008-01-29 20:37 ID:lDzXSQQq

'kay, so I made Conway's Game of Life Applet for my Comp. Sci. 1 class. Unfortunately, my program currently assumes all cells outside the array are dead. I read on Wikipedia that this is a common problem. It says that one common solution is making the grid wrap around, so a cell that goes off the left side comes back on the right side.

Being an amateur, I am unsure if my current program could be modified to accommodate a solution of this kind (or any kind) without having to rethink the entire program. Right now the code just checks every cell in a 2-d array from [1][1] to [size-1][size-1] with a nested for loop and then updates all the cells based on the Life rules. The lines of cells below and above these values are all assumed to be dead. Do you think it would be feasible for me to change this simple model?

Or, do you have any other ideas or suggestions?

http://pistonsource.iiichan.net/agargara/life/Life.html
http://pistonsource.iiichan.net/agargara/life/
..for the program and the source code respectively.

(By the way, I'm not just asking you to do my homework for me. In fact, as far as the teacher is concerned, I've already finished this project, so I'm just expanding it on my own. Also, I know I need to add a scrollbar but I haven't gotten around to looking up how to implement them yet.)

2 Name: #!/usr/bin/anonymous : 2008-01-29 22:42 ID:UJfHP0Q5

Quick solution off the top of my head (and not having actually read the code) would be when accessing an array element, instead just writing [x], write [(x+size)%size]. The %size will take care of positive wraparound, and adding size would wrap from the lower boundary. (Relatedly, I'm guessing you're going from [1] to [size-1] to deal with edge cases -- with this modulus arithmetic you wouldn't have to do that.)

3 Name: #!/usr/bin/anonymous : 2008-01-31 18:28 ID:5bWQpjOD

>>2
My god, you're a fucking genius. This worked perfectly. I would never have thought I could solve this problem with modulus arithmetic; guess I still have a lot to learn.

4 Name: #!/usr/bin/anonymous : 2008-01-31 18:32 ID:5bWQpjOD

PS: I updated the link above so you can see >>2's solution in action.

5 Name: #!/usr/bin/anonymous : 2008-01-31 20:09 ID:t1IlEEE2

> guess I still have a lot to learn.

Yes, this is a pretty standard trick in many, many situations. But it is one that you don't really think of until you've seen it once. After that, though, it becomes quite natural.

A further thing to realize about this trick: x%y is the same as x&(y-1) if y is a power of two, and is much faster. Now you know why so much graphics code uses power-of-2 sizes.

6 Name: #!/usr/bin/anonymous : 2008-02-01 13:23 ID:Heaven

Part of it is also that many graphics cards only support things in power of 2 sizes, and thus if you choose to have a 100x100 texture then it still takes up 128x128 in the card. Thus people fill it completely to get higher quality without penalty.

Also, I wish computer modular arithmetic worked like real modular arithmetic. Then you wouldn't need to say (x+size)%size, you could just say x%size which would be much cleaner to look at. But sadly, -1%3 equals -1, not 2, when doing this stuff on a computer. I ended up writing an alternative Math routine class to fill in this kind of hole...

7 Name: #!/usr/bin/anonymous : 2008-02-01 16:44 ID:t1IlEEE2

>>6

Well, if -1%3 equaled 2, then -1/3 would have to equal -1, and I'm not sure if that's any better.

8 Name: #!/usr/bin/anonymous : 2008-02-01 23:52 ID:Heaven

ADDITIONAL PLUGINS ARE REQUIRED TO VIEW ALL THE MEDIA ON THIS PAGE.

CLICK HERE TO DOWNLOAD PLUGIN.

9 Name: #!/usr/bin/anonymous : 2008-02-02 12:26 ID:LygL6FAG

>>7

No it wouldn't, because what I'm saying is that % should be a modulus and not a remainder.

And anyway, -1/3 being -1 would be consistent, it would just mean that division always rounds down. At least it's better than having 5 different values round down to 0 when divided by 3...

10 Name: #!/usr/bin/anonymous : 2008-02-03 02:13 ID:Heaven

11 Name: #!/usr/bin/anonymous : 2008-02-03 03:23 ID:Heaven

>>9

"Modulus" really doesn't have a good definition that I am aware of. I'd say the most important thing is to be consistent between the division and remainder operators, whichever way you do it.

The ideal would be to have both available, but of course most processors only implement one.

12 Name: #!/usr/bin/anonymous : 2008-02-03 12:36 ID:Heaven

Modular arithmetic generally has it that if working in mod 5, you can't get any number outside of 0..4 as a result of calculations.

This thread has been closed. You cannot post in this thread any longer.