if statements don't work very well what am i doing wrong?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

if statements don't work very well what am i doing wrong?

Post by Ghoti »

Hi folks,

I have this function with some if statement see below:

Code: Select all

int GameEvents(void){

		int test = 0;
		if (BlockStatus[3] == 0 || BlockStatus[4] == 0){
			//sprintf(sBuffer, "Begin: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
			//DebugOutput(sBuffer);
			
			if (iQuadPosY == 11) {
				sprintf(sBuffer, "ELF IS GEKOZEN");
				DebugOutput(sBuffer);
				// if no blocks are changed yet then the quad is in its whole on the bottom
				if (BlockStatus[3] == 0 && BlockStatus[4] == 0) {
					iField[iQuadPosX + 1][12] = iCurrentQuad[3];
					BlockStatus[3] = 1;
					iField[iQuadPosX + 1][11] = iCurrentQuad[2];
					BlockStatus[2] = 1;
					iField[iQuadPosX][12] = iCurrentQuad[4];
					BlockStatus[4] = 1;
					iField[iQuadPosX][11] = iCurrentQuad[1];
					BlockStatus[1] = 1;
					CheckSurroundingField(iQuadPosX + 1, 12, iCurrentQuad[3]);
					CheckSurroundingField(iQuadPosX + 1, 11, iCurrentQuad[2]);
					CheckSurroundingField(iQuadPosX, 12, iCurrentQuad[4]);
					CheckSurroundingField(iQuadPosX, 11, iCurrentQuad[1]);
					
					sprintf(sBuffer, "(11) BEIDE 0: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
					
				}
				// only the left part is on the bottom
				else if (BlockStatus[4] == 0 && BlockStatus[3] == 1) {
					iField[iQuadPosX][12] = iCurrentQuad[4];
					BlockStatus[4] = 1;
					iField[iQuadPosX][11] = iCurrentQuad[1];
					BlockStatus[1] = 1;
					CheckSurroundingField(iQuadPosX, 12, iCurrentQuad[4]);
					CheckSurroundingField(iQuadPosX, 11, iCurrentQuad[1]);

					sprintf(sBuffer, "(11) BLOK 4: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
				}
				// only the right part is on the bottom
				else if (BlockStatus[4] == 1 && BlockStatus[3] == 0) {
					iField[iQuadPosX + 1][12] = iCurrentQuad[3];
					BlockStatus[3] = 1;
					iField[iQuadPosX + 1][11] = iCurrentQuad[2];
					BlockStatus[2] = 1;
					CheckSurroundingField(iQuadPosX + 1, 12, iCurrentQuad[3]);
					CheckSurroundingField(iQuadPosX + 1, 11, iCurrentQuad[2]);
					sprintf(sBuffer, "(11) BLOK 3: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
				}
				iControllable = 1;
			}
			else {
				//sprintf(sBuffer, "NIET ELF GEKOZEN: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
				//DebugOutput(sBuffer);
				if (iField[iQuadPosX + 1][iQuadPosY + 2] > 0 && iField[iQuadPosX][iQuadPosY + 2] > 0 && BlockStatus[3] == 0 && BlockStatus[4] == 0){
					iField[iQuadPosX + 1][iQuadPosY + 1] = iCurrentQuad[3];
					BlockStatus[3] = 1;
					iField[iQuadPosX + 1][iQuadPosY] = iCurrentQuad[2];
					BlockStatus[2] = 1;
					iField[iQuadPosX][iQuadPosY + 1] = iCurrentQuad[4];
					BlockStatus[4] = 1;
					iField[iQuadPosX][iQuadPosY] = iCurrentQuad[1];
					BlockStatus[1] = 1;
					CheckSurroundingField(iQuadPosX + 1, iQuadPosY + 1, iCurrentQuad[3]);
					CheckSurroundingField(iQuadPosX + 1, iQuadPosY, iCurrentQuad[2]);
					CheckSurroundingField(iQuadPosX, iQuadPosY + 1, iCurrentQuad[4]);
					CheckSurroundingField(iQuadPosX, iQuadPosY, iCurrentQuad[1]);
					iControllable = 1;
					sprintf(sBuffer, "(?) BEIDE 0: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
				}
				else if (iField[iQuadPosX + 1][iQuadPosY + 2] > 0 && iField[iQuadPosX][iQuadPosY + 2] == 0 && BlockStatus[3] == 0 && BlockStatus[4] == 0) {
					iField[iQuadPosX + 1][iQuadPosY + 1] = iCurrentQuad[3];
					BlockStatus[3] = 1;
					iField[iQuadPosX + 1][iQuadPosY] = iCurrentQuad[2];
					BlockStatus[2] = 1;
					CheckSurroundingField(iQuadPosX + 1, iQuadPosY + 1, iCurrentQuad[3]);
					CheckSurroundingField(iQuadPosX + 1, iQuadPosY, iCurrentQuad[2]);
					iControllable = 1;
					sprintf(sBuffer, "(?1) BLOK 3: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
					test = 1;
				}
				else if (iField[iQuadPosX + 1][iQuadPosY + 2] == 0 && iField[iQuadPosX][iQuadPosY + 2] > 0 && BlockStatus[3] == 0 && BlockStatus[4] == 0) {
					iField[iQuadPosX][iQuadPosY + 1] = iCurrentQuad[4];
					BlockStatus[4] = 1;
					iField[iQuadPosX][iQuadPosY] = iCurrentQuad[1];
					BlockStatus[1] = 1;
					CheckSurroundingField(iQuadPosX, iQuadPosY + 1, iCurrentQuad[4]);
					CheckSurroundingField(iQuadPosX, iQuadPosY, iCurrentQuad[1]);
					iControllable = 1;
					sprintf(sBuffer, "(?1) BLOK 4: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
				}
				else if (iField[iQuadPosX][iQuadPosY + 2] > 0 && BlockStatus[3] == 1 && BlockStatus[4] == 0) {
					iField[iQuadPosX][iQuadPosY + 1] = iCurrentQuad[4];
					BlockStatus[4] = 1;
					iField[iQuadPosX][iQuadPosY] = iCurrentQuad[1];
					BlockStatus[1] = 1;
					CheckSurroundingField(iQuadPosX, iQuadPosY + 1, iCurrentQuad[4]);
					CheckSurroundingField(iQuadPosX, iQuadPosY, iCurrentQuad[1]);
					iControllable = 1;
					sprintf(sBuffer, "(?2) BLOK 4: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
				}
				else if (iField[iQuadPosX + 1][iQuadPosY + 2] > 0 && BlockStatus[3] == 0 && BlockStatus[4] == 1) {
					iField[iQuadPosX + 1][iQuadPosY + 1] = iCurrentQuad[3];
					BlockStatus[3] = 1;
					iField[iQuadPosX + 1][iQuadPosY] = iCurrentQuad[2];
					BlockStatus[2] = 1;
					CheckSurroundingField(iQuadPosX + 1, iQuadPosY + 1, iCurrentQuad[3]);
					CheckSurroundingField(iQuadPosX + 1, iQuadPosY, iCurrentQuad[2]);
					iControllable = 1;
					sprintf(sBuffer, "(?2) BLOK 3: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
				}
				else {}
				
				if (test == 1) {
					sprintf(sBuffer, "NO ELF2: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
					DebugOutput(sBuffer);
				}

				if (iControllable == 0) {
					if (iDropSpeed == 40){
						iQuadPosY += 1;
						iDropSpeed = 0;

					}
					else { iDropSpeed += 1;}
				}
				else {
					if (iDropSpeed == 10){
						iQuadPosY += 1;
						iDropSpeed = 0;

					}
					else { iDropSpeed += 1;}
					
				}
				if (test == 1) {
				sprintf(sBuffer, "DROP GEDAAN: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
				DebugOutput(sBuffer);
				}
			}
		}
		else {
			sprintf(sBuffer, "CHECK: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
			DebugOutput(sBuffer);
			if (BlockStatus[3] == 1) {
				if (BlockStatus[2] == 1) {
					if (BlockStatus[1] == 1) {
						if (BlockStatus[4] == 1) {
							sprintf(sBuffer, "CHECK OKAY: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
							DebugOutput(sBuffer);
							BlockStatus[1] = 0;
							BlockStatus[2] = 0;
							BlockStatus[3] = 0;
							BlockStatus[4] = 0;
							GameCreateQuad();
							return 0;
						}
					}
				}
			}
		}
		if (test == 1) {
			sprintf(sBuffer, "EINDE FUNCTIE: %i,%i,%i,%i", BlockStatus[1], BlockStatus[2], BlockStatus[3], BlockStatus[4]);
			DebugOutput(sBuffer);
		}
		/*if (iMessageShow == 1){
				if &#40;iMessageDuration < 120&#41; &#123;
					iMessageDuration += 1;
				&#125;
				else &#123; iMessageShow = 0; iMessageDuration = 0;&#125;
		&#125;*/
		return 0;
	&#125;
if you look at the code i give somewhere the test integer the value 1.
BlockStatus[4] is at this time 0, when this happens it goes to the if statement where the "NO ELF2" is passed to the debugfunction then suddenly BlockStatus[4] == 1 why is this?
PlayfulPuppy
Posts: 22
Joined: Fri May 05, 2006 12:04 am

Post by PlayfulPuppy »

I can think of 2 possible reasons:

First of all, by the looks of it you're using your arrays as 1 based, when in C/C++ they're zero based. For example, if you do 'char buffer[4]', the valid indicies to buffer are 0 to 3. 4 is going past the end of the array (So you'd be reading/writing the next element in memory past the array). This is because it's elements, because 0 counts as an element.

The other option is that sBuffer isn't large enough and you're writing past the end of array, which is writing over some of the data in your buffers.
Post Reply