CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    1

    Allegro - C++ - Conway's game of life

    Hi, this is my first post here. I've been programming C++ for quite a while, I'm not that good but hey we all try our best right?

    Anyway at the moment I'm writing a kind of copy of Conway's game of life. For those of you not familiar with the game, it is a kind of simulation, where each box behaves acording to the boxes around it. You can Wiki it if you like, its very interesting.

    Anyway I've been writing a version of this, and I can't figure out why it isn't working, can anyone help?

    #include <allegro.h>

    int main(){

    allegro_init();
    install_keyboard();
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

    int Cellnum = 1;
    int Cellcx = -5;
    int Cellcy = 0;
    int Cells [2500];
    int Cellx [2500];
    int Celly [2500];
    int Cellsn [2500];
    int Cellnc;
    Cells[410] = 1;
    Cells[411] = 1;
    Cells[412] = 1;



    while ( !key[KEY_ESC] ){




    for (Cellnum; Cellnum <= 2500; Cellnum++) {

    if (Cells[(Cellnum - 1)] == 1) {
    Cellnc = Cellnc + 1;
    }

    if (Cells[(Cellnum + 1)] == 1) {
    Cellnc = Cellnc + 1;
    }
    if (Cells[(Cellnum - 51)] == 1) {
    Cellnc = Cellnc + 1;
    }
    if (Cells[(Cellnum - 50)] == 1) {
    Cellnc = Cellnc + 1;
    }
    if (Cells[(Cellnum - 49)] == 1) {
    Cellnc = Cellnc + 1;
    }
    if (Cellnc > 3) {
    Cellsn[Cellnum] = 0;
    }
    if (Cellnc < 2) {
    Cellsn[Cellnum] = 0;
    }
    if (Cellnc = 3) {
    Cellsn[Cellnum] = 1;
    }
    Cellnc = 0;
    }

    Cellnum = 1;

    for (Cellnum; Cellnum <= 2500; Cellnum++) {

    Cells[Cellnum] = Cellsn[Cellnum];
    Cellcx = Cellcx + 5;
    if (Cellcx >= 250) {
    Cellcy = Cellcy + 5;
    Cellcx = 0;
    }
    if (Cells[Cellnum] = 1) {
    rectfill( screen, Cellcx, Cellcy, (Cellcx + 5), (Cellcy + 5), makecol( 255, 0, 0));
    }
    if (Cells[Cellnum] = 0) {
    rectfill( screen, Cellcx, Cellcy, (Cellcx + 5), (Cellcy + 5), makecol( 255, 255, 255));
    }}
    Cellnum = 1;
    Cellcx = -5;
    Cellcy = 0;



    }

    return 0;

    }
    END_OF_MAIN();

    Now im more than aware that my code often makes no sense to anyone else, so ask if you don't understand something, but any help anyone could give me would be heartily appreciated.

    Yours, ninetailsgangster

  2. #2
    Join Date
    Aug 2009
    Posts
    10

    Re: Allegro - C++ - Conway's game of life

    Hi Ninetailsgangster,

    As you said, I'm not sure what some of the code is doing but there are a few things that will be stopping your code from working:

    Inside your first loop you do tests such as
    Code:
    if (Cells[(Cellnum - 51)] == 1) {
    Cellnc = Cellnc + 1;
    }
    The first 51 times through the loop you will not be testing a value inside the array so you should always check that fist, for example
    Code:
    if ((Cellnum - 51) > -1 && Cells[(Cellnum - 51)] == 1) {
    Cellnc = Cellnc + 1;
    }
    Secondly, in the second loop you do tests such as
    Code:
    if (Cells[Cellnum] = 1) {
    rectfill( screen, Cellcx, Cellcy, (Cellcx + 5), (Cellcy + 5), makecol( 255, 0, 0));
    }
    Cells[Cellnum] = 1 will always return true because you are setting the value of Cells[Cellnum] to 1 which is not 0 (or true). To make this a test of equality, use the == operator, for example
    Code:
    if (Cells[Cellnum] == 1) {
    rectfill( screen, Cellcx, Cellcy, (Cellcx + 5), (Cellcy + 5), makecol( 255, 0, 0));
    }
    Let me know if that helps, I'm not saying I didn't overlook anything.

  3. #3
    Join Date
    Oct 2009
    Posts
    5

    Smile Re: Allegro - C++ - Conway's game of life

    Hi Ninetailsgangster,

    I tried your code with the Devshed c++ compiler and it worked very well. When I compiled and ran it, I got either a green or orange rectangle.
    However, I made a small change to your code:

    You'd written END_OF_MAIN;

    I removed the semicolon ( after END_OF_MAIN and then it worked perfectly.

    jameson91

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured