Generating Random black and white spaces repeating problem.

Hi, I am currently attempting to create a 7x7 grid that will be filled randomly with black and white spaces. On top of those spaces are a start and end space marked by a red or blue square.

Basically, so far I have got the code working, the grid is generated, colours added and the int random = rand() % 5; part is doing what it should by giving me different (and random) results.

The problem is that my loop never seems to end or that the board keeps changing the existing black and white spaces without me asking for it.

The odds are i'm missing something stupidly simple but originally I had this code just generate a series of black and white spaces systematically. This had no problems. Since I added the randomizer, the program keeps running endlessly.

The following is a picture of how it looks when run and my code for that particular section, note that all the black and white spaces change around every 3 seconds or so, only the grid lines, the blue and red squares stay the same.

I'm assuming the problem has something to do with my For loops but I just don't know what to try having tried many different ideas from other threads touching on the problem.

The srand(time(NULL)); was added and slowed things gown dramatically, but not completely. Before it was added the program ran at a blur of speed between black and white.

also I realise that this may look like a lot of code, but you can ignore the bulk of it I as the majority is just the code for drawing a shape and not actually related to the randomizer. I am most interested in my use of the int random = rand() % 5; function which I bolded.

http://i50.photobucket.com/albums/f342/liboempire/Capture.png

void OGWindow::drawGrid(GLvoid){

	int squareColour = 1;
	int size = 7; 
	srand(time(NULL));
	
	for (int xValue = 0; xValue < size; xValue++)
	{
		for (int yValue = 0; yValue < size; yValue++)
		{
			if (xValue == 0  && yValue == 0)
			{
					glColor3f(1, 0, 0);
					//start square
					glBegin(GL_QUADS);
					glVertex3f(xValue-0.3, yValue-0.3, 0.0);
					glVertex3f(xValue+0.3, yValue-0.3, 0.0);
					glVertex3f(xValue+0.3, yValue+0.3, 0.0);
					glVertex3f(xValue-0.3, yValue+0.3, 0.0);
					glEnd();

					glColor3f(0, 0, 0);
					//start square outline
					glBegin(GL_LINE_LOOP);
					glVertex3f(xValue-0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue+0.5, 0.0);
					glVertex3f(xValue-0.5, yValue+0.5, 0.0);
					glEnd();

			} else if (xValue == size - 2  && yValue == size - 2){

					glColor3f(0, 0, 1);
					//an end square
					glBegin(GL_QUADS);
					glVertex3f(xValue-0.3, yValue-0.3, 0.0);
					glVertex3f(xValue+0.3, yValue-0.3, 0.0);
					glVertex3f(xValue+0.3, yValue+0.3, 0.0);
					glVertex3f(xValue-0.3, yValue+0.3, 0.0);
					glEnd();

					glColor3f(0, 0, 0);
					//end square outline
					glBegin(GL_LINE_LOOP);
					glVertex3f(xValue-0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue+0.5, 0.0);
					glVertex3f(xValue-0.5, yValue+0.5, 0.0);
					glEnd();

			} else {
				
					glColor3f(0, 0, 0);
					//square outline
					glBegin(GL_LINE_LOOP);
					glVertex3f(xValue-0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue+0.5, 0.0);
					glVertex3f(xValue-0.5, yValue+0.5, 0.0);
					glEnd();

					int random = rand() % 5;
					cout << random << endl;

					if (random < 2){
								squareColour = 0;
					}else{
								squareColour = 1;
					}


					glColor3f(squareColour, squareColour, squareColour);
					//making a square
					glBegin(GL_QUADS);
					glVertex3f(xValue-0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue-0.5, 0.0);
					glVertex3f(xValue+0.5, yValue+0.5, 0.0);
					glVertex3f(xValue-0.5, yValue+0.5, 0.0);
					glEnd();
				
		}

	}
}

}



If there is any advice you can offer it would be much appreciated. I will be regularly looking for your responses. Thanks for your time.
Last edited on
Topic archived. No new replies allowed.