So I am working on a Tetris game as a school project in C. I got a little problem with the following situation. In Tetris when one completes a horizontal row, the row should be deleted and everything above the row should move down one place.The weird thing about my code is that when I place a block on the left or center of the board,the block stays there like it should. But when I place a block on the right side of the board, it is immediately deleted. I put my source code below so you can see what I am talking about.
Function checkfory (int ycheckup,int yp)
if(ycheckup==1)
{
yp++;
return yp;
}
else if(ycheckup==0)
{
yp=0;
return yp;
}
Main:
int Block_O[2][2] = {{1,1},{1,1}};
int printb[8][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int saveb[8][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int x;
int y;
int i;
int xp = 0;
int yp = 0;
int ycheckup = 0;
int ch1;
int ch2;
int ytemp;
int ycheck = 0;
int xcheck = 2;
while(1)
{
for(y=0;y<8;y++)
{
for(x=0;x<4;x++)
{
if(y==ycheck && x==xcheck || y==ycheck+1 && x==xcheck || y==ycheck && x==xcheck+1 || y==ycheck+1 && x==xcheck+1)//Looks for place to put block in
{
//printf(" %d ",xp);
printb[y][x] = Block_O[yp][xp];
printf("%d", printb[y][x]);
ycheckup=1;
xp++;
}
else
{
printb[y][x] = saveb[y][x];
printf("%d", printb[y][x]);
ycheckup=0;
}
}
//checkycor(ycheck,printb);
yp = checkfory(ycheckup, yp);
xp=0;
printf("\n");
}
for(i=0; i<30000000; i++) //Delay
{
;
}
ycheck++;
system("cls");
if(ycheck==7 || printb[ycheck+1][xcheck] == 1 || printb[ycheck+1][xcheck+1] == 1 ) //Checks for correlation
{
for(y=0;y<8;y++)
{
for(x=0;x<4;x++)
{
saveb[y][x] = printb[y][x];
if((printb[y][x] == 1) && (printb[y][x+1] == 1) && (printb[y][x+2] == 1) && (printb[y][x+3] == 1)) //Checks for full row
{
ytemp = y;
for(y=ytemp;y>0;y--)
{
for(x=0;x<4;x++)
{
if(y>0)
{
saveb[y][x] = printb[y-1][x]; //moves everything above deleted row, one row down
}
else
{
saveb[y][x] = 0;
}
printb[y][x] = saveb[y][x];
}
}
}
}
}
ycheck=0;
}
if (kbhit()!=0)
{
ch1 = getch();
ch2 = 0;
ch2 = getch();
switch(ch2)
{
case 75:
xcheck--;
break; //left
case 77:
xcheck++;
break;
default:
;
break;
};
}
}
I'm not sure why you shared checkfory(), that doesn't seem to be related to the logic of clearing a row. A couple comments wouldn't hurt, so we'd easily know what part is for moving the block coming down, and what is for clearing a row once the piece is in its final position.
I do see what seems to be an error in the if statement that looks for a full row - it depends on x, but it's inside a loop incrementing x. It appears to me that check will only work correctly on the first loop, and is reading into the next line of blocks after that. So a block placed on the right side will do a compare on that block and the remaining 3 checks are against blocks on the next row.
I think you have a misplaced end-brace, and the first for-x loop in the middle section should just be the one line that makes a shadow copy of the current row (to be moved down if needed).