#include"DxLib.h"
#define blocksize 5
#define blocksize2 10
#define width 10
#define height 20
#define width2 20
#define height2 40
#define wait 500
int nx;
int ny;
int wc;
int nt;
int ot;
int ncolor;
char fcolor;
int rotatetype;
char field[4+height][3+width+3];
char unit01[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,1},{0,0,0,0,0},{0,0,0,0,0}};
char unit02[5][5]={{0,0,0,0,0},{0,1,1,0,0},{0,1,1,0,0},{0,0,0,0,0},{0,0,0,0,0}};
char unit03[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,1,1,0,0},{0,0,1,1,0},{0,0,0,0,0}};
char unit04[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,0,1,1,1},{0,0,1,0,0},{0,0,0,0,0}};
char unit05[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,0},{0,0,1,0,0},{0,0,0,0,0}};
char unit1[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,1},{0,0,0,0,0},{0,0,0,0,0}};
char unit2[5][5]={{0,0,0,0,0},{0,1,1,0,0},{0,1,1,0,0},{0,0,0,0,0},{0,0,0,0,0}};
char unit3[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,1,1,0,0},{0,0,1,1,0},{0,0,0,0,0}};
char unit4[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,0,1,1,1},{0,0,1,0,0},{0,0,0,0,0}};
char unit5[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,0},{0,0,1,0,0},{0,0,0,0,0}};
char unit11[5][5]={{0,0,0,1,0},{0,0,0,1,0},{0,0,0,1,0},{0,0,0,1,0},{0,0,0,0,0}};
char unit12[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,1},{0,0,0,0,0},{0,0,0,0,0}};
char unit1_r1[5][5]={{0,0,1,1,0},{0,1,1,1,0},{0,1,1,1,1},{0,0,0,1,1},{0,0,0,0,0}};
char unit1_r2[5][5]={{0,0,1,1,0},{0,1,1,1,1},{0,1,1,1,1},{0,0,1,1,0},{0,0,0,0,0}};
int red; //1
int yellow; //2
int blue; //3
int green; //4
int unittype;
#define STAGE_X 20
#define STAGE_Y 0
int lock(int unittype,int x,int y);
int eliminate(void);
void gameover(void);
int move(int mx,int my);
int rotate(int unittype,int rotatetype);
int checkrl(int unittype,int nnx,int nny)
{
int i,j,c;
c=0;
if(unittype==1)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit1[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit1[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==2)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit2[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit2[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==3)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit3[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit3[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==4)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit4[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit4[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==5)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit5[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit5[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
}
int checkud(int unittype,int nnx,int nny)
{
int i,j,c;
c=0;
if(unittype==1)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit1[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit1[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==2)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit2[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit2[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==3)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit3[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit3[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==4)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit4[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit4[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
else if(unittype==5)
{
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
if(unit5[i][j]>=1 && field[i+nny][j+nnx]==-1)c=1;
if(unit5[i][j]>=1 && field[i+nny][j+nnx]>=1)c=1;
}
return c;
}
}
void startgame(void)
{
ot=GetNowCount();
nx=3;ny=1;
unittype=GetRand(4)+1; unittype=2;
rotatetype=1;
ncolor=GetRand(3)+1;
int i,j;
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
unit1[i][j]=unit01[i][j];
unit2[i][j]=unit02[i][j];
unit3[i][j]=unit03[i][j];
unit4[i][j]=unit04[i][j];
unit5[i][j]=unit05[i][j];
}
for(i=0; i<=3+width+3-1; i++)for(j=0; j<=2+height-1; j++)field[j][i]=-1;
for(i=2; i<2+width; i++)for(j=0; j<=2+height-3; j++)field[j][i]=0;
}
int key(void)
{
int key;
static int ok;
key=GetJoypadInputState(DX_INPUT_KEY_PAD1);
if((key&~ok)&PAD_INPUT_LEFT&&nx>0)move(-1,0);
if((key&~ok)&PAD_INPUT_RIGHT&&nx<width+2)move(1,0);
if((key&~ok)&PAD_INPUT_DOWN)move(0,1);
if((key&~ok)&KEY_INPUT_UP)rotate(unittype,rotatetype);
ok=key;
return 0;
}
int tf(void)
{
int t;
t=GetNowCount();
nt=t-ot;
ot=t;
wc=wc+nt;
if(wc>wait)
{
move(0,1);
wc=0;
}
return 0;
}
void create(void)
{
int i,j;
unittype=GetRand(4)+1;
ncolor=GetRand(3)+1;
rotatetype=1;
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{
unit1[i][j]=unit01[i][j];
unit2[i][j]=unit02[i][j];
unit3[i][j]=unit03[i][j];
unit4[i][j]=unit04[i][j];
unit5[i][j]=unit05[i][j];
}
}
int move(int mx,int my)
{
int nnx,nny;
nnx=nx+mx;nny=ny+my;
if(mx!=0 && checkrl(unittype,nnx,nny)==1)mx=0;
if(my !=0 && checkud(unittype,nnx,nny)==1){lock(unittype,nx,ny);my=0;}
nx=nx+mx;
ny=ny+my;
return 0;
}
int lock(int unittype,int x,int y)
{
int i,j;
char nncolor;
if(ncolor==1)nncolor=ncolor;
if(ncolor==2)nncolor=ncolor;
if(ncolor==3)nncolor=ncolor;
if(ncolor==4)nncolor=ncolor;
if(unittype==1)
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{if(unit1[j][i]>=1)field[j+ny][i+nx]=nncolor;}
else if(unittype==2)
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{if(unit2[j][i]>=1)field[j+ny][i+nx]=nncolor;}
else if(unittype==3)
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{if(unit3[j][i]>=1)field[j+ny][i+nx]=nncolor;}
else if(unittype==4)
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{if(unit4[j][i]>=1)field[j+ny][i+nx]=nncolor;}
else if(unittype==5)
for(i=0; i<=4; i++)for(j=0; j<=4; j++)
{if(unit5[j][i]>=1)field[j+ny][i+nx]=nncolor;}
eliminate();
nx=3;
ny=1;
create();
if(checkud(unittype,nx,ny)==1)gameover();
wc=0;
WaitTimer(200);
return 0;
}
int eliminate(void)
{
int i,j,k,l;
l=0;
int zero=0;
int zest=0;
for(j=0; j<=2+height-1;j++)
{
l=0;
for(k=1; k<=1+width-1; k++)if(field[j][k]>=1)l++;
if(l==10){zero++; for(k=3; k<=3+width-1; k++)field[j][k]=0;if(zero==1)zest=j;}
}
int count=0;
if(zero>=1)
{
for(j=zest-1; j>=2; j--)
{
count=0;
for(k=1; k<=1+width-1; k++)if(field[j][k]>=1)count++;
if(count>=1)
{
for(k=1; k<=1+width-1; k++)if(field[j][k]>=1){field[j+zero][k]=field[j][k]; field[j][k]=0;}
}
}
}
return 0;
}
int draw(void)
{
int i,j,k;
ClearDrawScreen();
DrawBox( STAGE_X - 10 , STAGE_Y , STAGE_X ,
STAGE_Y + height2 * blocksize ,
GetColor( 255 , 0 , 0 ) , TRUE ) ;
DrawBox( STAGE_X + width2 * blocksize , STAGE_Y ,
STAGE_X + 10 + width2 * blocksize , STAGE_Y + height2 * blocksize ,
GetColor( 255 , 0 , 0 ) , TRUE ) ;
DrawBox( STAGE_X - 10 , STAGE_Y + height2 * blocksize ,
STAGE_X + 10 + width2 * blocksize , STAGE_Y + height2 * blocksize + 10 ,
GetColor( 255 , 0 , 0 ) , TRUE ) ;
int ccolor;
if(ncolor==1)ccolor=GetColor(255,0,0);
else if(ncolor==2)ccolor=GetColor(255,255,0);
else if(ncolor==3)ccolor=GetColor(0,0,255);
else if(ncolor==4)ccolor=GetColor(0,255,0);
//////カレントブロック描画
if(unittype==1)
{
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
{
if(unit1[i][j]>=1)
{
DrawBox((nx+j)*10,(ny+i)*10,(nx+j)*10+8,(ny+i)*10+8,ccolor,TRUE);
}
}
}
else if(unittype==2)
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
{
if(unit2[i][j]>=1)
{
DrawBox((nx+j)*10,(ny+i)*10,(nx+j)*10+8,(ny+i)*10+8,ccolor,TRUE);
}
}
else if(unittype==3)
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
{
if(unit3[i][j]>=1)
{
DrawBox((nx+j)*10,(ny+i)*10,(nx+j)*10+8,(ny+i)*10+8,ccolor,TRUE);
}
}
else if(unittype==4)
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
{
if(unit4[i][j]>=1)
{
DrawBox((nx+j)*10,(ny+i)*10,(nx+j)*10+8,(ny+i)*10+8,ccolor,TRUE);
}
}
else if(unittype==5)
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
{
if(unit5[i][j]>=1)
{
DrawBox((nx+j)*10,(ny+i)*10,(nx+j)*10+8,(ny+i)*10+8,ccolor,TRUE);
}
}
for(i=0; i<=2+height; i++)
for(j=2; j<width+2; j++)
{
if(field[i][j]==1)DrawBox(j*10,i*10,j*10+8,i*10+8,GetColor(255,0,0),TRUE);
else if(field[i][j]==2)DrawBox(j*10,i*10,j*10+8,i*10+8,GetColor(255,255,0),TRUE);
else if(field[i][j]==3)DrawBox(j*10,i*10,j*10+8,i*10+8,GetColor(0,0,255),TRUE);
else if(field[i][j]==4)DrawBox(j*10,i*10,j*10+8,i*10+8,GetColor(0,255,0),TRUE);
}
return 0;
}
void gameover(void)
{
draw() ;
DrawString( 300 , 220 , _T("GameOver") , GetColor( 255 , 255 , 255 ) ) ;
ScreenFlip() ;
WaitKey() ;
DxLib_End() ;
exit( -1 ) ;
}
int rotate(int unittype,int rotatetype)
{
int i,j,c;
c=0;
if(unittype==1 && rotatetype==1)
{
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
if(unit1_r1[i][j]>=1 && (field[i+ny][j+nx]>=1 || field[i+ny][j+nx]==-1))
c=1;
if(c!=1)
{
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
unit1[i][j]=unit11[i][j];
rotatetype=2;
}
}
else if(unittype==1 && rotatetype==2)
{
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
if(unit1_r2[i][j]>=1 && (field[i+ny][j+nx]>=1 || field[i+ny][j+nx]==-1))
c=1;
if(c!=1)
{
for(i=0; i<=4; i++)
for(j=0; j<=4; j++)
unit1[i][j]=unit12[i][j];
rotatetype=1;
}
}
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE ) ;
SetGraphMode( 640 , 480 , 16 ) ;
if( DxLib_Init() == -1 ) // DXライブラリ初期化処理
{
return -1; // エラーが起きたら直ちに終了
}
// ゲームを初期化
startgame() ;
// 描画先画面を裏画面にセット
SetDrawScreen( DX_SCREEN_BACK ) ;
// メインループ
while( ProcessMessage() == 0 )
{
key() ;
tf() ;
draw() ;
ScreenFlip() ;
}
DxLib_End() ;
return 0 ;
}