#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define HEIGHT 20
#define WEIGHT 20
#define NEXTSTARTX WEIGHT/2-1
#define NEXTSTARTY 2
#define STARTX NEXTSTARTX
#define STARTY 0
char g_Board[HEIGHT][WEIGHT]; //배경..배열...
char g_Block[7][4][4][4] = { //어떤모양 //회전수//4*4
0, 0, 0, 0,
2, 2, 2, 2,
0, 0, 0, 0,
0, 0, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
0, 0, 0, 0,
2, 2, 2, 2,
0, 0, 0, 0,
0, 0, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
////////////////[0][][][]/////////
2, 0, 0, 0,
2, 2, 2, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 2, 0, 0,
2, 0, 0, 0,
2, 0, 0, 0,
0, 0, 0, 0,
2, 2, 2, 0,
0, 0, 2, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
2, 2, 0, 0,
0, 0, 0, 0,
////////////////[1][][][]/////////
0, 0, 2, 0,
2, 2, 2, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 0, 0, 0,
2, 0, 0, 0,
2, 2, 0, 0,
0, 0, 0, 0,
2, 2, 2, 0,
2, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 2, 0, 0,
0, 2, 0, 0,
0, 2, 0, 0,
0, 0, 0, 0,
////////////////[2][][][]/////////
2, 2, 0, 0,
0, 2, 2, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 2, 0, 0,
2, 2, 0, 0,
2, 0, 0, 0,
0, 0, 0, 0,
2, 2, 0, 0,
0, 2, 2, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 2, 0, 0,
2, 2, 0, 0,
2, 0, 0, 0,
0, 0, 0, 0,
////////////////[3][][][]/////////
0, 2, 2, 0,
2, 2, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 0, 0, 0,
2, 2, 0, 0,
0, 2, 0, 0,
0, 0, 0, 0,
0, 2, 2, 0,
2, 2, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 0, 0, 0,
2, 2, 0, 0,
0, 2, 0, 0,
0, 0, 0, 0,
////////////////[5][][][]/////////
2, 2, 2, 0,
0, 2, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 2, 0, 0,
2, 2, 0, 0,
0, 2, 0, 0,
0, 0, 0, 0,
0, 2, 0, 0,
2, 2, 2, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 0, 0, 0,
2, 2, 0, 0,
2, 0, 0, 0,
0, 0, 0, 0,
////////////////[6][][][]/////////
2, 2, 0, 0,
2, 2, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 2, 0, 0,
2, 2, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 2, 0, 0,
2, 2, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
2, 2, 0, 0,
2, 2, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
////////////////[7][][][]/////////
};
int g_blocktype = 5;
int g_rotate = 1;
int g_blocky = 0;
int g_blockx = STARTX;
int g_Temp = 0;
DWORD FTime = GetTickCount();
DWORD LTime = NULL;
BOOL g_Flag = FALSE;
void AllPrint();
void Back();
void Rotate();
void Left();
void Right();
void Down();
void CreateBolck();
int Game();
int Check();
int Check2(int x, int y, int rotate);
void SetBlock();
void Remove();
void CheckAllLine();
int g_check=0;
void main()
{
AllPrint();
while(1)
{
Back();
LTime = GetTickCount();
Game();
printf("rotate:%d",g_check);
system("cls");
} //while문의 끝...임...
}
void AllPrint() //배경..찍어줄 함수.
{
for(int i=0;i<HEIGHT;i++)
{
for(int j=0;j<WEIGHT;j++)
{
if(j==0 || j==WEIGHT-1) // 배경 가로 크기 벽을 1로 만들어줌그래서 벽처럼 보이게
g_Board[i][j] = 1;
else
g_Board[i][j] = 0; // 벽이 아니면 0으로 채워줌,
if(i == HEIGHT-1)
g_Board[i][j] = 1; // 마지막 부분은 ..1로 채워서 보여줌.
}
}
}
void Back()
{
for(int i=0;i<HEIGHT;i++)
{
for(int j=0;j<WEIGHT;j++)
{
if(j==0 || j==WEIGHT-1) // 배경 가로 크기 벽을 1로 만들어줌그래서 벽처럼 보이게
g_Board[i][j] = 1;
if(i == HEIGHT-1)
g_Board[i][j] = 1; // 마지막 부분은 ..1로 채워서 보여줌.
}
}
}
int Game()
{
int xx;
int key=0;
Down();
if(kbhit())//키입력함수
key=getch();
switch(key)
{
case 'q':
case 'Q':
// g_gameflag=0;//바닥 체크변수/////
break;
case '5'://돌리는 키..
{
xx=g_rotate;
xx++;
if( xx > 3 )
xx=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(g_Block[g_blocktype][g_rotate][i][j]==2)
g_Board[g_blocky+i][g_blockx+j]=0;
}
}
if(Check2(g_blockx,g_blocky,xx) == 0)
Rotate();
}
break;
case '4':
Left();
break;
case '6':
Right();
break;
case '2':
// Down();
break;
}//키입력 받는 것의 끝임...
for(int i=0;i<HEIGHT;i++)
{
for(int j=0; j<WEIGHT; j++)
{
if(g_Board[i][j] == 1)
printf("@");
if(g_Board[i][j] == 0)
printf(".");
if(g_Board[i][j] == 2)
printf("#");
}
printf("\n");
}
return 1;
}
void CreateBolck()
{
g_blocky=0;
g_blockx=STARTX;
g_blocktype = rand()%7;
g_blocktype=5;
}
void Rotate()
{
g_rotate = (g_rotate+1)%4;
SetBlock();
}
void Left()
{
Remove();
g_blockx--; //블럭 내리기 위해...*/
if(Check()==0)
g_blockx++;
SetBlock();
}
void Right()
{
Remove();
g_blockx++; //블럭 내리기 위해...
if(Check()==0)
g_blockx--;
SetBlock();
}
void Down()
{
Remove();
if(LTime - FTime >= 300)
{
// g_blocky++; //블럭 내리기 위해...
// if(Check()==0)
// {
// g_blocky--;
// SetBlock(); 6
// CreateBolck();
// }
if(Check2(g_blockx,g_blocky+1,g_rotate) == 0)
{
g_blocky++;
}
else
{
SetBlock();
CreateBolck();
}
FTime = LTime;
}
CheckAllLine();
SetBlock();
}
int Check()
{ //여기서 g_board블럭에 있는 i,j의 위치가 참 중요하다...xdd
for(int i=0; i < 4; i++) // 충돌을 검사한다. 벽, 바닥, 다른블럭과의 충돌을 검사한다.
for(int j=0; j < 4; j++)
if ( g_Board[g_blocky+i][g_blockx+j] > 0 && g_Block[g_blocktype][g_rotate][i][j] > 0 )
return 0;
// g_Board[x][y] <- 이 값이 0보다 크면 벽이든, 블럭이 있다는 것이다.]
return 1;
}
int Check2(int x, int y, int rotate)
{ //여기서 g_board블럭에 있는 i,j의 위치가 참 중요하다...xdd
int result =0;
for(int i=0; i < 4; i++) // 충돌을 검사한다. 벽, 바닥, 다른블럭과의 충돌을 검사한다.
for(int j=0; j < 4; j++)
{
if( g_Block[g_blocktype][rotate][i][j] == 2 )
{
if ( g_Board[y+i][x+j] == 1 )
result = 1;
if( g_Board[y+i][x+j] == 2 )
result = 2;
}
}
// g_Board[x][y] <- 이 값이 0보다 크면 벽이든, 블럭이 있다는 것이다.]
return result;
}
void SetBlock() // 그려주는 블럭.
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(g_Block[g_blocktype][g_rotate][i][j] ==2)
g_Board[g_blocky+i][g_blockx+j]=2;
}
}
void Remove() //지워 주는 블럭..사이즈 만큼...
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(g_Block[g_blocktype][g_rotate][j][i] ==2)
g_Board[g_blocky+j][g_blockx+i]=0;
}
}
void CheckAllLine()
{
int sum;
for(int i=0;i<HEIGHT;i++)
{
sum=0; //다시 초기화 안시켜 주면 안걸린다...오라~~
for(int j=0;j<WEIGHT;j++)
{
if(g_Board[i][j] == 2)
sum++;
}
if(sum == WEIGHT-2)
{
for(int k=i;k>0;k--)
{
for(int j=0;j<WEIGHT;j++)
{
g_Board[k][j] = g_Board[k-1][j];//그위의 생긴 블럭들을,.현재에 넣어주는거지!
}
}
}
}
}