Member Avatar for Jenniferting

Hi guys, im currently doing Conway's game of life and i'm almost there. The variable and all is in German since im studying here in Germany but i think one can still understand the code. There's one thing I need help with, which is with the Glider (Gleiter).

When i run it, the output is that the Glider wil move diagonally from bottom left to upper right. As it reaches the end of the field, i am required to make that it will appear again from bottom right moving to upper left. (Moves in a way that form an 'X')

Need help on how to make it appear again at the bottom left after it reaches the end of the field.

(Sorry for the crappy explanation..)

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include "Console.h"
#include "Console.cpp"

#define MAXX 78
#define MAXY 22


int hauptmenu()
{
    int wahl1;
     printf("\n\t\t\t\t######******######\n");
     printf("\t\t\t\t@= GAME OF LIFE =@\n");
     printf("\t\t\t\t######******######\n");
     printf("\n\t\t\t    -Willkommen in Game of Life-\n");
     printf("\nSpielregel:\n\n");
     printf("1. Eine lebende Zelle ueberlebt in der naechsten Generation, wenn sie zwei oder\n");
     printf("   drei Nachbarn hat. Sind es weniger bzw. mehr, so stirbt sie an Vereinsamung\n");
     printf("   bzw. Uberbevoelkerung\n");
     printf("2. Eine tote Zelle wird immer dann in der naechsten Generation zum\n");
     printf("   Leben erweckt, wenn sie genau drei lebendige Nachbarn hat, ansonsten bleibt\n");
     printf("   sie tot.\n");
     printf("\nGeben Sie Ihre Wahl ein.\n\n");
     printf("\t\t\t\t :- Hauptmenu -:\n\n");
     printf("\t1.Game of Life starten\n");
     printf("\t2.Beenden\n\n");
     printf("\tIhre Wahl: ");
         scanf ("%d", &wahl1);
     return wahl1;  
}

int ausgabe(int feld[MAXX][MAXY], int generation){
    printf("\t\t\t |*|^|*| GENERATION:%d |*|^|*|", generation);
    printf("\n\n");
    int x=0, y=0;
    while (y<MAXY){
        while (x<MAXX){
            if (feld[x][y]==1) printf(" ");
            if (feld[x][y]==2) printf("X");
            x++;
        }
    printf("\n");
    y++;
    x=0;
    }
return 0;
}

int main()
{
    int generation=1;
    int feld[MAXX][MAXY];
    int x=1, y=1;

    while (y<MAXY){
                   while (x<MAXX){
                   feld[x][y]=1;
                   x++;
                   }
    printf("\n");
    y++;
    x=0;
    }
    int wahl1;
    do{

    wahl1=hauptmenu();

    if(wahl1==1){
    int feldneu[MAXX][MAXY];
    int wahl2;

    printf("\n\n1)Blinker\n");
    printf("2)Block\n");
    printf("3)Bienenstock\n");
    printf("4)Leuchtfeuer\n");
    printf("5)Gleiter\n");   
    printf("Bitte waehlen sie die Ausgangsmuster: ");
    scanf("%d", &wahl2);
    switch(wahl2){
                  case 1:                       //Blinker
                  feld[38][11]=2;
                  feld[39][11]=2;
                  feld[40][11]=2;
                  break;
                  case 2:                       //Block
                  feld[38][11]=2;
                  feld[38][12]=2;
                  feld[39][11]=2;
                  feld[39][12]=2;
                  break;
                  case 3:                       //Bienenstock
                  feld[37][11]=2;
                  feld[36][12]=2;
                  feld[38][12]=2;
                  feld[36][13]=2;
                  feld[38][13]=2;
                  feld[37][14]=2;
                  break;
                  case 4:                       //Leuchtfeuer
                  feld[38][11]=2;
                  feld[39][11]=2;
                  feld[39][12]=2;
                  feld[36][13]=2;
                  feld[36][14]=2;
                  feld[37][14]=2;
                  break;
                  case 5:                       //Gleiter
                  feld[39][11]=2;
                  feld[40][11]=2;
                  feld[38][12]=2;
                  feld[40][12]=2;
                  feld[40][13]=2;
                  break;
                 }

    ausgabe(feld, generation);
    Sleep(1000);
    cls();

    while (generation){
                 int nachbarn=0;
                 x=1, y=1;
                 while (y<MAXY){
                       while (x<MAXX){
                             if (feld[x][y-1]==2) nachbarn++;
                             if (feld[x-1][y-1]==2) nachbarn++;
                             if (feld[x-1][y]==2) nachbarn++;
                             if (feld[x-1][y+1]==2) nachbarn++;
                             if (feld[x][y+1]==2) nachbarn++;
                             if (feld[x+1][y+1]==2) nachbarn++;
                             if (feld[x+1][y]==2) nachbarn++;
                             if (feld[x+1][y-1]==2) nachbarn++;

                             if(feld[x][y]==2){
                             if (nachbarn<2 || nachbarn >3) feldneu[x][y]=1;
                             else feldneu[x][y]=2;
                             }
                             if (feld[x][y]==1){
                             if (nachbarn==3) feldneu[x][y]=2;
                             else feldneu[x][y]=1;
                             }
                             nachbarn=0;
                             x++;
                             }
                 y++;
                 x=0;
                 }

           x=1, y=1;       
           while (y<MAXY){
                 while (x<MAXX){
                       feld[x][y] = feldneu[x][y];   
                       x++;
                       }
                 y++;
                 x=0;
          }
    generation++;
    Sleep(500);   
    cls();
    ausgabe(feld, generation);
    }
    }//End if loop

    else if(wahl1==2) break;
    else printf("\tUngueltige Eingabe");

    }while(wahl1>2);//End do loop

    system("PAUSE");
    return 0;
}       

First of all you're accessing your array out of bounds when x is MAXX - 1 (and thus x + 1 is MAXX) or y is MAXY - 1 (and thus y + 1 is MAXY), so your program is invoking undefined behaviour.

I think you started your loop at 1 instead of 0 because you wanted to avoid issues with x - 1 and y - 1 being -1 and thus out of bounds by simply considering the fields on the border as dead space. If so, you should apply the same logic to MAXX - 1 and MAXY - 1 and thus adjust your loop bounds to exclude those values, too.

Except don't because the way to make the board wrap-around like you want is to not exclude the fields on the border, but rather consider the opposite border fields as their neighbours. I.e. if x is 0 then you should consider fields with MAXX - 1 instead of x-1 as the top neighbours and if x is MAXX - 1, you should consider fields with 0 as its bottom neighbours. Same with y and the left/right neighbours.

Member Avatar for Jenniferting

Sorry, i don't quite get what u meant by the last paragraph there..

I meant that if, for example, you're looking at the field with the coordinates (MAXX-1, MAXY-1) (i.e. (77,21) you should consider its neighbours to be:

(76, 20), (77, 20), ( 0, 20)
(76, 21),           ( 0, 21)
(76,  0), (77,  0), ( 0, 0)

And similarly the neighbours of (0, 0) should be:

(77, 21), ( 0, 21), ( 1, 21)
(77,  0),           ( 1,  0)
(77,  1), ( 0,  1), ( 1,  1)

And likewise for every coordinate that's on the edge (i.e. every coordinate where x is 0 or 77 or y is 0 or 21). That way any shape that walks off on one edge will re-appear on the opposite edge, which is what you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.