well this is just the prototype, not the best method i guess(this is the 20th day with c) yet i cant manage to find whats wrong... the blocks just stop "randomly" in the air, please help!
Code:
//headers declaration
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
//var declaration
char choice=NULL,name[4];
int board[16][10],tbm[4][2],score=0,loop1,loop2,check,checkfar[4],i,p,level;
time_t now,later;
FILE *fp=NULL;
//function declaration
void main();
int tetris();
void genblock();
void disp();
void moveblock();
void blockdown();
void clrln();
int gameisover();
void main()
{
// highscore rules tetris game and exit here
tetris();
}
int tetris()
{
// gets:name,level. give:score
tbm[0][0]=20;
for(loop1=0;loop1<16;loop1++)
for(loop2=0;loop2<10;loop2++)
board[loop1][loop2]=0;
while(1)
{
if(tbm[0][0]==20)
genblock();
disp();
while(1)
{
now=time(0);
later=time(0);
while(difftime(later,now)<1)
{
if(kbhit())
moveblock();
later=time(0);
}
blockdown();
clrln();
score+=10;
disp();
if(tbm[0][0]==20)
break;
}
if(gameisover())
break;
}
return 0;
}
void genblock()
{
now=time(0);
now%=7;
switch(now)
{
case 0:
tbm[0][0]=15;
tbm[0][1]=4;
tbm[1][0]=15;
tbm[1][1]=5;
tbm[2][0]=14;
tbm[2][1]=4;
tbm[3][0]=14;
tbm[3][1]=5;
break;
case 1:
tbm[0][0]=14;
tbm[0][1]=3;
tbm[1][0]=14;
tbm[1][1]=4;
tbm[2][0]=14;
tbm[2][1]=5;
tbm[3][0]=14;
tbm[3][1]=6;
break;
case 2:
tbm[0][0]=15;
tbm[0][1]=4;
tbm[1][0]=14;
tbm[1][1]=4;
tbm[2][0]=14;
tbm[2][1]=5;
tbm[3][0]=14;
tbm[3][1]=6;
break;
case 3:
tbm[0][0]=15;
tbm[0][1]=5;
tbm[1][0]=14;
tbm[1][1]=4;
tbm[2][0]=14;
tbm[2][1]=5;
tbm[3][0]=14;
tbm[3][1]=6;
break;
case 4:
tbm[0][0]=15;
tbm[0][1]=6;
tbm[1][0]=14;
tbm[1][1]=4;
tbm[2][0]=14;
tbm[2][1]=5;
tbm[3][0]=14;
tbm[3][1]=6;
break;
case 5:
tbm[0][0]=15;
tbm[0][1]=4;
tbm[1][0]=15;
tbm[1][1]=5;
tbm[2][0]=14;
tbm[2][1]=5;
tbm[3][0]=14;
tbm[3][1]=6;
break;
case 6:
tbm[0][0]=15;
tbm[0][1]=6;
tbm[1][0]=15;
tbm[1][1]=5;
tbm[2][0]=14;
tbm[2][1]=5;
tbm[3][0]=14;
tbm[3][1]=4;
break;
}
for(loop1=0;loop1<4;loop1++)
board[tbm[loop1][0]][tbm[loop1][1]]=1;
}
void disp()
{
system("cls");
for(loop1=14;loop1>=0;loop1--)
{
putchar('|');
for(loop2=0;loop2<10;loop2++)
{
if(board[loop1][loop2])
putchar('x');
else
putchar(' ');
}
putchar('|');
putchar(10);
}
puts(" ==========");
}
void moveblock()
{
choice=getch();
switch(choice)
{
case 'a':
check=0;
checkfar[0]=20;
for(loop1=0;loop1<4;loop1++)
{
if(tbm[loop1][1]<checkfar[0])
checkfar[check]=tbm[loop1][1];
else if (tbm[loop1][1]==checkfar[0])
{
checkfar[check]=checkfar[check+1];
check++;
}
}
check++;
i=0;
p=0;
while(check>i)
{
for(loop1=0;loop1<4;loop1++)
if(board[tbm[loop1][0]][checkfar[i]-1]==1 || tbm[loop1][1]==0)
p=1;
i++;
}
if(p!=1)
{
for(loop1=0;loop1<4;loop1++)
{
board[tbm[loop1][0]][tbm[loop1][1]]=0;
tbm[loop1][1]-=1;
board[tbm[loop1][0]][tbm[loop1][1]]=1;
}
disp();
}
break;
case 's':
blockdown();
break;
case 'd':
check=0;
checkfar[0]=0;
for(loop1=0;loop1<4;loop1++)
{
if(tbm[loop1][1]>checkfar[0])
checkfar[check]=tbm[loop1][1];
else if (tbm[loop1][1]==checkfar[0])
{
checkfar[check]=checkfar[check+1];
check++;
}
}
check++;
i=0;
p=0;
while(check>i)
{
for(loop1=0;loop1<4;loop1++)
if(board[tbm[loop1][0]][checkfar[i]+1]==1 || tbm[loop1][1]==9)
p=1;
i++;
}
if(p!=1)
{
for(loop1=0;loop1<4;loop1++)
{
board[tbm[loop1][0]][tbm[loop1][1]]=0;
tbm[loop1][1]+=1;
board[tbm[loop1][0]][tbm[loop1][1]]=1;
}
disp();
}
break;
default:
break;
}
}
void blockdown()
{
check=0;
checkfar[0]=20;
for(loop1=0;loop1<4;loop1++)
{
if(tbm[loop1][0]<checkfar[0])
checkfar[check]=tbm[loop1][1];
else if (tbm[loop1][1]==checkfar[0])
{
checkfar[check]=checkfar[check+1];
check++;
}
}
check++;
i=0;
while(check>i)
{
for(loop1=0;loop1<4;loop1++)
if(board[checkfar[i]-1][tbm[loop1][1]]==1 || tbm[loop1][0]==0)
tbm[0][0]=20;
i++;
}
if(tbm[0][0]!=20)
{
for(loop1=0;loop1<4;loop1++)
{
board[tbm[loop1][0]][tbm[loop1][1]]=0;
tbm[loop1][0]-=1;
board[tbm[loop1][0]][tbm[loop1][1]]=1;
}
disp();
}
}
void clrln()
{
check=0;
for(loop1=0;loop1<15;loop1++)
{
for(loop2=0;loop2<10;loop2++)
if(board[loop1][loop2])
check+=1;
if(check==10)
break;
}
if(check==10)
{
while(loop1<14)
{
for(loop2=0;loop2<10;loop2++)
board[loop1][loop2]=board[loop1+1][loop2];
loop1++;
}
for(loop2=0;loop2<10;loop2++)
board[loop1][loop2]=0;
disp();
}
}
int gameisover()
{
for(loop2=0;loop2<10;loop2++)
{
if(board[14][loop2]==1 && board[13][loop2]==1 && board[12][loop2]==1 && board[11][loop2]==1 && board[10][loop2]==1)
{
puts("you lose press enter something to continue");
scanf("%d",&check);
return 1;
}
}
return 0;
}