Can someone help? The thing just outputs jargon right now.
/* Conway's game of life...
a really bad one... just to
bide my boring classtime*/
#include <stdio.h>
int main() {
int oldmap[20,20];
int newmap[20,20];
int a,b,c;
evolve:
while ( a < 21 ){
while ( b < 21 ){
if(oldmap[a,b-1] == 1){c++;}
if(oldmap[a-1,b-1] == 1){c++;}
if(oldmap[a-1,b] == 1){c++;}
if(oldmap[a-1,b+1] == 1){c++;}
if(oldmap[a,b+1] == 1){c++;}
if(oldmap[a+1,b+1] == 1){c++;}
if(oldmap[a+1,b] == 1){c++;}
if(oldmap[a+1,b-1] == 1){c++;}
if (c == 3){ newmap[a,b] = 1; }
if (c != 3){ newmap[a,b] = 0; }
c = 0;
b++;
}
a++;
}
a = 0;b = 0;
while ( a < 21 ){
while ( b < 21 ){
oldmap[a,b] = newmap[a,b];
b++;
}
a++;
}
a = 0;b = 0;
while ( b < 21 ){
printf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n",oldmap[a,b],oldmap[a+1,b],oldmap[a+2,b],oldmap[a+3,b],oldmap[a+4,b],oldmap[a+5,b],oldmap[a+6,b],oldmap[a+7,b],oldmap[a+8,b],oldmap[a+9,b],oldmap[a+10,b],oldmap[a+11,b],oldmap[a+12,b],oldmap[a+13,b],oldmap[a+14,b],oldmap[a+15,b],oldmap[a+16,b],oldmap[a+17,b],oldmap[a+18,b],oldmap[a+19,b]);
b++;
}
goto evolve;
}
aaronk42
08-28-2002, 08:57 PM
Ack! Goto! :eek: I haven't seen one of those in YEARS. :p
bwkaz
08-28-2002, 09:12 PM
Well, I don't know how to really tell you this, but have you ever heard of a for loop?
3m00
08-28-2002, 09:46 PM
fine then. with a nice - pertty for loop. lol.
/* Conway's game of life...
a really bad one... just to
bide my boring classtime*/
#include <stdio.h>
int main() {
int oldmap[20,20];
int newmap[20,20];
int a,b,c,d;
for (d = 0;d < 10000;d++ ) {
while ( a < 21 ){
while ( b < 21 ){
if(oldmap[a,b-1] == 1){c++;}
if(oldmap[a-1,b-1] == 1){c++;}
if(oldmap[a-1,b] == 1){c++;}
if(oldmap[a-1,b+1] == 1){c++;}
if(oldmap[a,b+1] == 1){c++;}
if(oldmap[a+1,b+1] == 1){c++;}
if(oldmap[a+1,b] == 1){c++;}
if(oldmap[a+1,b-1] == 1){c++;}
if (c == 3){ newmap[a,b] = 1; }
if (c != 3){ newmap[a,b] = 0; }
c = 0;
b++;
}
a++;
}
a = 0;b = 0;
while (a=0,a<21,a++){
while (b=0,b<21,b++){
oldmap[a,b] = newmap[a,b];
b++;
}
a++;
}
a = 0;b = 0;
for (a=0;a<21;a++) {
for (b=0;b<21;b++) {
printf("%d",oldmap[a,b]);
}
}
}
}
TacKat
08-28-2002, 10:12 PM
Arrays are zero indexed in C. So there are 20 elements in a[20]: a[0], a[1], ... , a[19]. You need to stop your loops sooner.
bwkaz
08-28-2002, 10:19 PM
OK, where to begin... ;) :p
In C, multidimensional arrays aren't subscripted with commas. You use array[1][1] instead of array[1,1].
Some of your for loops were actually still whiles (I know, I know... :p ).
You need to print a newline at some point -- probably after your inner print loop is done. Otherwise, stdout never flushes itself, and you don't see any output.
You need to initialize your array to something before you start -- life doesn't get generated spontaneously -- at least not by Conway's rules of life ;)
Some of your newly-converted for loops were double-incrementing variables.
main() claims to return an int, but it actually doesn't.
Anyway, I think that was it. Here's something that works (I think... it still doesn't initialize the arrays):
Edit: oh yeah, the stopping sooner thing too. Changed now.
Edit again: You shouldn't just blindly add one to a and b -- you should check to see if they're at the end of the array, or mod them by 20 to wrap around. Also fixed (I mod them). You should also do something to fix if a or b is 0 (you can't subtract 1 from zero and get a valid array offset ;) ), but that's more complicated than a mod. I'm not doing anything with that one here.
/* Conway's game of life...
a really bad one... just to
bide my boring classtime*/
#include <stdio.h>
int main() {
int oldmap[20][20];
int newmap[20][20];
int a,b,c,d;
for (d = 0; d < 10000;d++ ) {
for (a=0; a < 20; a++ ){
for (b=0; b < 20; b++){
if(oldmap[a][b-1] == 1){c++;}
if(oldmap[a-1][b-1] == 1){c++;}
if(oldmap[a-1][ b] == 1){c++;}
if(oldmap[a-1][(b+1)%20] == 1){c++;}
if(oldmap[a][(b+1)%20] == 1){c++;}
if(oldmap[(a+1)%20][(b+1)%20] == 1){c++;}
if(oldmap[(a+1)%20][ b] == 1){c++;}
if(oldmap[(a+1)%20][b-1] == 1){c++;}
if (c == 3){ newmap[a][ b] = 1; }
if (c != 3){ newmap[a][ b] = 0; }
c = 0;
}
}
a = 0;b = 0;
for (a=0;a<20;a++){
for (b=0;b<20;b++){
oldmap[a][ b] = newmap[a][ b];
}
}
for (a=0;a<20;a++) {
for (b=0;b<20;b++) {
printf("%d",oldmap[a][ b]);
}
printf("\n");
}
}
return 0;
}By the way, do you compile with -Wall -pedantic? It really helps you with some of these problems, if you can decipher the warnings.
TacKat
08-28-2002, 10:50 PM
There's more that needs to be done than just change the printf loop. Arrays for one thing. I didn't notice before, but you don't access arrays with a[1,1]. You do it with a[1][1]. You didn't mention what errors you were having so I copied it and started fixing the syntax. While I was at it, I just changed the whole thing around.
This should work alright (I don't actually know how life is supposed to work, but it does what you were trying). It's basically a cleanup and re-organization:
#include <stdio.h>
int test_life(int map[20][20], int x, int y)
{
int c=0;
int i,j;
int main(void)
{
int oldmap[20][20];
int newmap[20][20];
int a,b;
while(1) {
for(a=0; a < 20; a++)
for(b=0; b < 20; b++)
newmap[a][b] = test_life(oldmap, a, b);
for(a=0; a < 20; a++)
for(b=0; b < 20; b++)
oldmap[a][b] = newmap[a][b];
for(a=0; a < 20; a++) {
for(b=0; b < 20; b++)
printf("%d", oldmap[a][b]);
printf("%s", "\n");
}
}
return 0;
}
Edit: Seems somebody else beat me to it. That'll learn me to not refresh. ;)
3m00
08-28-2002, 11:15 PM
I'm not doing anything with that one
Lol, Im not THAT stupid, lol. Thx everybody. Now I can try to impress my friends with something they have all seen visually but now see in basically binary. Hey, at least I can do it over telnet.
Oh, in case anyone wants to know, I made this cuz I couldn't find any clients for linux ppc that compiled. So I made my own. Then again, I did get a lot of help. Thx everybody/
3m00
08-29-2002, 01:05 AM
Okay... on TacKat's code, I was wondering how I can force it into REAL life compilance by making sure that when there are 2 in the vicinity, if the array space = 1 then the newmap[x][y] = 1. This would allow for gliders. (so far, I have gotten A BLINKER! Do you realize how happy I was when I saw it? This is just strange now) lol.....
(p.s. i tried
if (c == 2 && map[x][y] == 1) return 1; but it just outputs jibberish!)
bwkaz
08-29-2002, 08:30 AM
Change this line:
if(c == 3)To this:
if(c == 3 || (c == 2 && map[x][y] == 1))That should work (I hope -- I haven't tried it, but I don't see any reason why it wouldn't...).
Lol, Im not THAT stupid, lol.That was't what I meant -- I just wanted to make sure you (and anyone else reading this thread) knew that I hadn't touched that one.
Seems somebody else beat me to it. That'll learn me to not refresh. ;) Hey, that's OK, your code is better anyway. ;)
3m00
08-29-2002, 07:46 PM
A GLIDER! A GLIDER! IT WORKS! Now, just ./life > life.txt and scroll down to see a glider move to the top left corner. Lol, that was a headrush...
#include <stdio.h>
int main() {
int oldmap[20][20];
int newmap[20][20];
int a,b,c,d;
oldmap[10][10] = 1;
oldmap[11][10] = 1;
oldmap[12][10] = 1;
oldmap[10][11] = 1;
oldmap[11][12] = 1;
for (d = 0; d < 100;d++ ) {
for (a=0; a < 20; a++ ){
for (b=0; b < 20; b++){
if(oldmap[a][b-1] == 1){c++;}
if(oldmap[a-1][b-1] == 1){c++;}
if(oldmap[a-1][ b] == 1){c++;}
if(oldmap[a-1][(b+1)%20] == 1){c++;}
if(oldmap[a][(b+1)%20] == 1){c++;}
if(oldmap[(a+1)%20][(b+1)%20] == 1){c++;}
if(oldmap[(a+1)%20][ b] == 1){c++;}
if(oldmap[(a+1)%20][b-1] == 1){c++;}
if (c == 3 || c == 2 && oldmap[a][b] == 1){ newmap[a][ b] = 1; }
else newmap[a][b] = 0;
c = 0;
}
}
a = 0;b = 0;
for (a=0;a<20;a++){
for (b=0;b<20;b++){
oldmap[a][ b] = newmap[a][ b];
}
}
for (a=0;a<20;a++) {
for (b=0;b<20;b++) {
printf("%d",oldmap[a][ b]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.