Community for Gamedevelopment (viral gof 2 Players)

Hello Community,

I´m new in here. I´ll describe in a few words what could be even interesting for you.
I guess Gofl is familiar to everyone. I´m trying to develop a 2 Player version out of it.

Description:

  • Fielddimension various. (rectangle)
  • 1 turn for moving (<= 3 cells in the square (cornered or straight)
  • Set cells active unlimited in a turn (right mouse button) // means the cell would react gofl like with neighbored ones if set active)
  • the evolving generations erase the ones of the other players
  • game start with 10 to 30 cells on the left player 1 and on the right player 2.
  • so first to move the cells to a combination that is reactive (setting them active)
  • only inactive cells can move (means every automatic moving cell is locked and does it´s thing)
  • goal is to place your inactive cells in the startupcells the other player is starting (inactive cells even erase by moving them)

I didn´t get very far in programming it. Here is the startup code
If someone has some freetime and maybe interested in developing on the code
please feel free to post.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdarg.h>
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>

#define dimFieldX 40
#define dimFieldY 34

#define dimQUAD 2
#define colIdx 3
#define zDepth 0
#define CELLS  8

struct gField {
    float ***fld;
    int    **col;
    bool turn[2];
} gf;
struct player1 {
    float ***fld1;
    int    **col1;                
    float     NM[3];
    bool       turn1;
    float ****cells;
} pl1;
struct player2 {
    float ***fld2;
    int    **col2;
    float     NM[3];
    bool    turn2;
    float  ****cells;
} pl2;
void buildFont(void);
void glPrint(const char *fmt, ...);
void initRendering();
void drawScene(void);
void func_setF();
int initGL(void);
void processMouse(int button, int state, int x, int y);
int main(int argc, char *argv[]) {
    char exit = 'q';
    gf.turn[0] = false;
    gf.turn[1] = false;
    //-------------------------------------------------------
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition(10,10);
    glutInitWindowSize(1280,840);
    glutCreateWindow("_");
    initGL();
    func_setF();
    glutMouseFunc(processMouse);
    glutDisplayFunc(drawScene);
    glutMainLoop();
    //-------------------------------------------------------
    free(pl1.fld1);
    free(pl1.col1);
    free(pl2.fld2);
    free(pl2.col2);
    free(pl1.cells);
    free(pl2.cells);
    free(gf.fld);
    free(gf.col);
    return 0;
}
    
GLvoid glPrint( const char *fmt, ... )
{
    int base = glGenLists(96);
    char text[256]; /* Holds our string */
    va_list ap;     /* Pointer to our list of elements */

    /* If there's no text, do nothing */
    if ( fmt == NULL )
        return;

    /* Parses The String For Variables */
    va_start( ap, fmt );
      /* Converts Symbols To Actual Numbers */
      vsprintf( text, fmt, ap );
    va_end( ap );

    /* Pushes the Display List Bits */
    glPushAttrib( GL_LIST_BIT );

    /* Sets base character to 32 */
    glListBase( base - 32 );

    /* Draws the text */
    glCallLists( strlen( text ), GL_UNSIGNED_BYTE, text );

    /* Pops the Display List Bits */
    glPopAttrib( );
}

void buildFont( void )
{
    Display *dpy;          /* Our current X display */
    XFontStruct *fontInfo; /* Our font info */

    /* Sotrage for 96 characters */
    int base = glGenLists( 96 );

    /* Get our current display long enough to get the fonts */
    dpy = XOpenDisplay( NULL );

    /* Get the font information */
    fontInfo = XLoadQueryFont( dpy, "-adobe-helvetica-medium-r-normal--18-*-*-*-p-*-iso8859-1" );

    /* If the above font didn't exist try one that should */
    if ( fontInfo == NULL )
        {
            fontInfo = XLoadQueryFont( dpy, "fixed" );
            /* If that font doesn't exist, something is wrong */
            if ( fontInfo == NULL )
                {
                    fprintf( stderr, "no X font available?
" );
                    exit( 1 );
                }
        }

    /* generate the list */
    glXUseXFont( fontInfo->fid, 32, 96, base );

    /* Recover some memory */
    XFreeFont( dpy, fontInfo );

    /* close the display now that we're done with it */
    XCloseDisplay( dpy );

    return;
}
int initGL()
{

    /* Enable smooth shading */
    glShadeModel( GL_SMOOTH );

    /* Set the background black */
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );

    /* Depth buffer setup */
    glClearDepth( 1.0f );

    /* Enables Depth Testing */
    glEnable( GL_DEPTH_TEST );

    /* The Type Of Depth Test To Do */
    glDepthFunc( GL_LEQUAL );

    /* Really Nice Perspective Calculations */
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

    buildFont( );
    return 1;
}

void drawScene(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST | GL_DEPTH);
    glLoadIdentity();  
    int i_0, i_1, i_c, ctr = 0;
    glRasterPos3f(-0.2f, -0.8f, 0.0f);
    // glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,"Active OpenGL Text - %20.2f");
    glColor3f(1.0f,1.0f,1.0f);
    glTranslatef(-0.4f, -0.4f, 0.0f);
    glPushMatrix();
    for(i_0 = 0; i_0 < dimFieldX - 1; i_0++) {
        for(i_1 = 0; i_1 < dimFieldY - 1; i_1++) {
        glBegin(GL_LINES);
            glColor3f(1.0f,0.0f,0.0f);
            glVertex3f(gf.fld[i_0][i_1][0],((gf.fld[i_0][i_1][1])), (float) zDepth - 0.1f);
            glVertex3f(gf.fld[i_0 + 1][i_1][0],((gf.fld[i_0][i_1][1])), (float) zDepth - 0.1f);

            glVertex3f(gf.fld[i_0 + 1][i_1][0],((gf.fld[i_0][i_1][1])), (float) zDepth - 0.1f);
            glVertex3f(gf.fld[i_0 + 1][i_1][0],((gf.fld[i_0 + 1][i_1 + 1][1])), (float) zDepth - 0.1f);

            glVertex3f(gf.fld[i_0 + 1][i_1][0],((gf.fld[i_0 + 1][i_1 + 1][1])), (float) zDepth - 0.1f);
            glVertex3f(gf.fld[i_0][i_1][0],((gf.fld[i_0][i_1 + 1][1])), (float) zDepth - 0.1f);

            glVertex3f(gf.fld[i_0][i_1][0],((gf.fld[i_0][i_1 + 1][1])), (float) zDepth - 0.1f);
            glVertex3f(gf.fld[i_0][i_1][0],((gf.fld[i_0][i_1][1])), (float) zDepth - 0.1f);
        }
    }
    for(i_c = 0; i_c < CELLS; i_c++) {
        for(i_0 = 0; i_0 < dimFieldX - 1; i_0++) {
            for(i_1 = 0; i_1 < dimFieldY - 1; i_1++) {
                    glColor3f(0.0f,1.0f,0.0f);
                    glBegin(GL_QUAD_STRIP);
                            glVertex3f(pl1.cells[i_c][i_0][i_1][0],pl1.cells[i_c][i_0][i_1][1], (float) zDepth -0.1f);
                            glVertex3f(pl1.cells[i_c][i_0 + 1][i_1][0],pl1.cells[i_c][i_0][i_1][1], (float) zDepth -0.1f);
                            glVertex3f(pl1.cells[i_c][i_0 + 1][i_1][0],pl1.cells[i_c][i_0 + 1][i_1 + 1][1], (float) zDepth -0.1f);
                            glVertex3f(pl1.cells[i_c][i_0][i_1][0],pl1.cells[i_c][i_0][i_1 + 1][1], (float) zDepth -0.1f);
                    glColor3f(0.0f,0.0f,1.0f);
                    glBegin(GL_QUAD_STRIP);
                            glVertex3f(pl2.cells[i_c][i_0][i_1][0],pl2.cells[i_c][i_0][i_1][1], (float) zDepth -0.1f);
                            glVertex3f(pl2.cells[i_c][i_0 + 1][i_1][0],pl2.cells[i_c][i_0][i_1][1], (float) zDepth -0.1f);
                            glVertex3f(pl2.cells[i_c][i_0 + 1][i_1][0],pl2.cells[i_c][i_0 + 1][i_1 + 1][1], (float) zDepth -0.1f);
                            glVertex3f(pl2.cells[i_c][i_0][i_1][0],pl2.cells[i_c][i_0][i_1 + 1][1], (float) zDepth -0.1f);
                }    
        }
    }
    glPopMatrix();
    //-->
    glEnd();
    glutSwapBuffers();
    glFlush();
}
void func_setF() {
    int i_0, i_1, i_c;
    srand(time(NULL));
    
    gf.col = (int **) malloc((dimFieldX*dimFieldY)*sizeof(int *));
    pl1.col1 = (int **) malloc((dimFieldX*dimFieldY)*sizeof(int *));
    pl2.col2 = (int **) malloc((dimFieldX*dimFieldY)*sizeof(int *));
    for(i_0 = 0; i_0 < dimFieldX * dimFieldY; i_0++) {
        gf.col[i_0] = (int *) malloc(colIdx*sizeof(int));
        pl1.col1[i_0] = (int *) malloc((colIdx)*sizeof(int));
        pl2.col2[i_0] = (int *) malloc((colIdx)*sizeof(int));
    }    
    gf.fld = (float ***) malloc(dimFieldX*sizeof(float **));    
    pl1.fld1 = (float ***) malloc(dimFieldX*sizeof(float **));    
    pl2.fld2 = (float ***) malloc(dimFieldX*sizeof(float **));    
    for(i_0 = 0; i_0 < dimFieldX; i_0++) {
        gf.fld[i_0] = (float **) malloc(dimFieldY*sizeof(float *));
        pl1.fld1[i_0] = (float **) malloc(dimFieldY*sizeof(float *));    
        pl2.fld2[i_0] = (float **) malloc(dimFieldY*sizeof(float *));    
    }
    for(i_0 = 0; i_0 < dimFieldX; i_0++) {
        for(i_1 = 0; i_1 < dimFieldY; i_1++) {
            gf.fld[i_0][i_1] = (float *) malloc(dimQUAD*sizeof(float));
            pl1.fld1[i_0][i_1] = (float *) malloc(dimQUAD*sizeof(float));    
            pl2.fld2[i_0][i_1] = (float *) malloc(dimQUAD*sizeof(float));    
        }
    }
    for(i_0 = 1; i_0 <= dimFieldX; i_0++) {
        for(i_1 = 1; i_1 <= dimFieldY; i_1++) {
            gf.fld[i_0 - 1][i_1 - 1][0] =  ((float)i_0 / (float)dimFieldX);
            gf.fld[i_0 - 1][i_1 - 1][1] =  ((float)i_1 / (float)dimFieldY);
        }
    }
    pl1.cells = (float ****) malloc(CELLS*sizeof(float ***));
    pl2.cells = (float ****) malloc(CELLS*sizeof(float ***));
    for(i_c = 0; i_c < CELLS; i_c++) {
        pl1.cells[i_c] = (float ***) malloc(dimFieldX*sizeof(float **));
        pl2.cells[i_c] = (float ***) malloc(dimFieldX*sizeof(float **));            
        for(i_0 = 0; i_0 < dimFieldX; i_0++) {
                pl1.cells[i_c][i_0] = (float **) malloc(dimFieldY*sizeof(float *));
                 pl2.cells[i_c][i_0] = (float **) malloc(dimFieldY*sizeof(float *));
            for(i_1 = 0; i_1 < dimFieldY; i_1++) {
                pl1.cells[i_c][i_0][i_1] = (float *) malloc(3*sizeof(float));
                 pl2.cells[i_c][i_0][i_1] = (float *) malloc(3*sizeof(float));
            }
        }
    }
    for(i_c = 0; i_c < CELLS; i_c++) {
        for(i_0 = 1; i_0 <= dimFieldX; i_0++) {
            for(i_1 = 1; i_1 <= dimFieldY; i_1++) {
                pl1.cells[i_c][i_0 - 1][i_1 - 1][0] = 0.0;
                 pl1.cells[i_c][i_0 - 1][i_1 - 1][1] = ((float) i_1 / (float) dimFieldY);
                pl2.cells[i_c][i_0 - 1][i_1 - 1][0] = (float) dimFieldX;
                 pl2.cells[i_c][i_0 - 1][i_1 - 1][1] = ((float) i_1 / (float) dimFieldY);
                pl1.cells[i_c][i_0 - 1][i_1 - 1][2] = 0.0; // inactive gofcell
                 pl2.cells[i_c][i_0 - 1][i_1 - 1][2] = 0.0;
            }
        }
    }
    pl1.NM[0] = 0.0;
    pl2.NM[0] = 0.0;
    pl1.NM[1] = 0.0;
    pl2.NM[1] = 0.0;    
    pl1.NM[3] = 0.0;
    pl2.NM[3] = 0.0;
}
void processMouse(int button, int state, int x, int y)
{
    int cL, i_0, i_1;
    if(state == GLUT_DOWN)
    {
        if(button == GLUT_LEFT_BUTTON)
        {
            if(pl1.turn1 == true) {
                 if(pl1.NM[0] == x && pl1.NM[1] == y) {
                    for(cL = 0; cL < CELLS; cL++) {
                        for(i_0 = 0; i_0 < dimFieldX; i_0++) {
                            for(i_1 = 0; i_1 < dimFieldY; i_1++) {
                                if(pl1.cells[cL][i_0][i_1][0] == (float) x && pl1.cells[cL][i_0][i_1][1] == (float) y) {
                                    if((pl1.NM[0] - x) + (pl1.NM[1] -y) <= 3 && (pl1.NM[0] - x) + (pl1.NM[1] -y) >= 1) {
                                        pl1.cells[cL][i_0][i_1][0] = pl1.NM[0];
                                        pl1.cells[cL][i_0][i_1][1] = pl1.NM[1];
                                    }                                    
                                }
                            }
                        }
                    }
                }
            }
            if(pl2.turn2 == true) {
                if(pl2.NM[0] == 0 && pl2.NM[1] == 0) {
                    for(cL = 0; cL < CELLS; cL++) {
                        for(i_0 = 0; i_0 < dimFieldX; i_0++) {
                            for(i_1 = 0; i_1 < dimFieldY; i_1++) {
                                if(pl2.cells[cL][i_0][i_1][0] == (float) x && pl2.cells[cL][i_0][i_1][1] == (float) y) {
                                    if((pl2.NM[0] - x) + (pl2.NM[1] -y) <= 3 && (pl2.NM[0] - x) + (pl2.NM[1] -y) >= 1) {
                                        pl2.cells[cL][i_0][i_1][0] = pl2.NM[0];
                                        pl2.cells[cL][i_0][i_1][1] = pl2.NM[1];
                                    }                                    
                                }
                            }
                        }
                    }
                }
            }
        }
        else if(button == GLUT_RIGHT_BUTTON)
        {
            if(pl1.turn1 == true) {
                if(pl1.NM[0] == x && pl1.NM[1] == y) {
                    for(cL = 0; cL < CELLS; cL++) {
                        for(i_0 = 0; i_0 < dimFieldX; i_0++) {
                                for(i_1 = 0; i_1 < dimFieldY; i_1++) {
                                    if(pl1.cells[cL][i_0][i_1][0] == (float) x && pl1.cells[cL][i_0][i_1][1] == (float) y) {
                                        pl1.cells[cL][i_0][i_1][3] = 1.0;
                                    }
                                }
                        }
                    }
                    
                }
            }
            if(pl2.turn2 == true) {
                if(pl2.NM[0] == x && pl2.NM[1] == y) {
                    for(i_0 = 0; i_0 < dimFieldX; i_0++) {
                        for(i_1 = 0; i_1 < dimFieldY; i_1++) {
                            if(pl2.cells[cL][i_0][i_1][0] == (float) x && pl2.cells[cL][i_0][i_1][1] == (float) y) {
                                pl2.cells[cL][i_0][i_1][3] = 1.0;
                            }
                        }
                    }
                    
                }
            }
        }
    }
    else
    {
        //alternate code
    }
}

thx for reading

reference for the missing algorithm: Wonders of Math - The Game of Life

debirius