您的位置:首页 > 其它

HDU 5374 模拟俄罗斯方块

2015-08-11 17:00 281 查看
模拟俄罗斯方块游戏

完全按照俄罗斯方块的规则来做

注意规则即可:

1:每种图形开始出现时绿点均在(4,9)位置

2:先做变换,再下降一格

3:若碰到操作无法被执行的则不执行,依次进行下个操作

#include "stdio.h"
#include "string.h"

struct Type
{
int a,b,x,y;
}type;
char str[1010];
int n,a[1010],ans,flag,mark,now;
int mp[20][20];

void tianchong(int op) // 将方块所在占的位置消除或者填充
{
if (type.a==0)
{
mp[type.x][type.y]=op;
mp[type.x+1][type.y]=op;
mp[type.x][type.y+1]=op;
mp[type.x+1][type.y+1]=op;
}

if (type.a==1)
{
if (type.b==1)
{
mp[type.x][type.y]=op;
mp[type.x][type.y+1]=op;
mp[type.x][type.y+2]=op;
mp[type.x][type.y+3]=op;
}
else
{
mp[type.x][type.y]=op;
mp[type.x+1][type.y]=op;
mp[type.x+2][type.y]=op;
mp[type.x+3][type.y]=op;
}
}

if (type.a==2)
{
if (type.b==1)
mp[type.x][type.y]=mp[type.x][type.y+1]=mp[type.x+1][type.y]=mp[type.x+2][type.y]=op;
if (type.b==2)
mp[type.x][type.y]=mp[type.x][type.y+1]=mp[type.x][type.y+2]=mp[type.x+1][type.y+2]=op;
if (type.b==3)
mp[type.x][type.y+1]=mp[type.x+1][type.y+1]=mp[type.x+2][type.y+1]=mp[type.x+2][type.y]=op;
if (type.b==4)
mp[type.x][type.y]=mp[type.x+1][type.y]=mp[type.x+1][type.y+1]=mp[type.x+1][type.y+2]=op;
}

}

void change()
{
if (type.a==0) return ;

if (type.a==1)
{
if (type.b==1)
{
if (mp[type.x+1][type.y]==0 && mp[type.x+2][type.y]==0 && mp[type.x+3][type.y]==0)
{
tianchong(0);
type.b=2;
tianchong(1);
}
}
else
{
if (mp[type.x][type.y+1]==0 && mp[type.x][type.y+2]==0 && mp[type.x][type.y+3]==0)
{
tianchong(0);
type.b=1;
tianchong(1);
}
}
}

if (type.a==2)
{
if (type.b==1)
{
if (mp[type.x][type.y+2]==0 && mp[type.x+1][type.y+2]==0)
{
tianchong(0);
type.b=2;
tianchong(1);
}
}
else
if (type.b==2)
{
if (mp[type.x+1][type.y+1]==0 && mp[type.x+2][type.y+1]==0 && mp[type.x+2][type.y]==0)
{
tianchong(0);
type.b=3;
tianchong(1);
}
}
else
if (type.b==3)
{
if (mp[type.x][type.y]==0 && mp[type.x+1][type.y]==0 && mp[type.x+1][type.y+2]==0)
{
tianchong(0);
type.b=4;
tianchong(1);
}
}
else
if (type.b==4)
{
if (mp[type.x][type.y+1]==0 && mp[type.x+2][type.y]==0)
{
tianchong(0);
type.b=1;
tianchong(1);

}
}
}
}

void left()
{
if (type.a==0)
{
if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1 ) return ;
tianchong(0);
type.x--;
tianchong(1);
}
if (type.a==1)
{
if (type.b==1)
{
if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1 || mp[type.x-1][type.y+2]==1 || mp[type.x-1][type.y+3]==1) return ;
tianchong(0);
type.x--;
tianchong(1);
}
else
{
if (type.x==1 || mp[type.x-1][type.y]==1) return ;
tianchong(0);
type.x--;
tianchong(1);
}
}
if (type.a==2)
{
if (type.b==1)
{
if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1) return ;
tianchong(0);
type.x--;
tianchong(1);
}
if (type.b==2)
{
if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1 || mp[type.x-1][type.y+2]==1) return ;
tianchong(0);
type.x--;
tianchong(1);
}
if (type.b==3)
{
if (type.x==1 || mp[type.x-1][type.y+1]==1 || mp[type.x+1][type.y]==1) return ;
tianchong(0);
type.x--;
tianchong(1);
}
if (type.b==4)
{
if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x][type.y+1]==1 || mp[type.x][type.y+2]==1) return ;
tianchong (0);
type.x--;
tianchong(1);
}
}
}

void right()
{
if (type.a==0)
{
if (type.x==8 || mp[type.x+2][type.y]==1 || mp[type.x+2][type.y+1]==1) return ;
tianchong(0);
type.x++;
tianchong(1);
}

if (type.a==1)
{
if (type.b==1)
{
if (type.x==9 || mp[type.x+1][type.y]==1 || mp[type.x+1][type.y+1]==1 || mp[type.x+1][type.y+2]==1 || mp[type.x+1][type.y+3]==1) return ;
tianchong(0);
type.x++;
tianchong(1);
}
else
{
if (type.x==6 || mp[type.x+4][type.y]==1) return ;
tianchong(0);
type.x++;
tianchong(1);
}
}

if (type.a==2)
{
if (type.b==1)
{
if (type.x==7 || mp[type.x+1][type.y+1]==1 || mp[type.x+3][type.y]==1) return ;
tianchong(0);
type.x++;
tianchong(1);
}
if (type.b==2)
{
if (type.x==8 || mp[type.x+1][type.y]==1 || mp[type.x+1][type.y+1]==1 || mp[type.x+2][type.y+2]==1) return ;
tianchong(0);
type.x++;
tianchong(1);
}
if (type.b==3)
{
if (type.x==7 || mp[type.x+3][type.y]==1 || mp[type.x+3][type.y+1]==1) return ;
tianchong(0);
type.x++;
tianchong(1);
}
if (type.b==4)
{
if (type.x==8 || mp[type.x+2][type.y]==1 || mp[type.x+2][type.y+1]==1 || mp[type.x+2][type.y+2]==1) return ;
tianchong(0);
type.x++;
tianchong(1);
}
}
}

void down()
{
if (type.y==1) {flag=1; return ;}

if (type.a==0 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1) ) { flag=1; return ;}

if (type.a==1)
{
if (type.b==1 && mp[type.x][type.y-1]==1) {flag=1; return ;}
if (type.b==2 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1 || mp[type.x+2][type.y-1]==1 || mp[type.x+3][type.y-1]==1)  )
{
flag=1;
return ;
}
}

if (type.a==2)
{
if (type.b==1 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1 || mp[type.x+2][type.y-1]==1 ) )
{
flag=1;
return ;
}
if (type.b==2 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y+1]==1)  )
{
flag=1;
return ;
}
if (type.b==3 && (mp[type.x][type.y]==1 || mp[type.x+1][type.y]==1 || mp[type.x+2][type.y-1]) )
{
flag=1;
return ;
}
if (type.b==4 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1))
{
flag=1;
return ;
}
}
tianchong(0);
type.y--;
tianchong(1);
}

void check_xiao()
{

int i,j,xiao,k;
i=0;
while (i<12)
{
i++;
xiao=1;
for (j=1;j<=9;j++)
if (mp[j][i]==0) {xiao=0; break;}
if (xiao==1)
{
ans++;
for (j=i+1;j<=12;j++)
for (k=1;k<=9;k++)
mp[k][j-1]=mp[k][j];
i--;
}
}
}

void bfs()
{
int now,i,j,len;
memset(mp,0,sizeof(mp));
ans=0;
now=0; // 记录当前处理到哪个动作
len=strlen(str);
mark=1; // 记录当前处理到哪个方块
type.a=a[1]; type.b=1; type.x=4; type.y=9; // a记录形状,b记录角度,x,y记录绿点坐标;
tianchong(1);

while (now<len)
{
flag=0;
if (str[now]=='w') change(); // 变换
if (str[now]=='a') left(); // 左移
if (str[now]=='d') right(); // 右移
if (str[now]=='s') down(); // 下降
/* for (i=12;i>=1;i--)
{
for (j=1;j<=9;j++)
printf("%d",mp[j][i]);
printf("\n");
}
printf("\n");*/

now++;

if (flag==1)
{
check_xiao(); // 检查是否有可以消除的行
mark++;
type.a=a[mark]; type.b=1; type.x=4; type.y=9;
}
else
{
down();
if (flag==1)
{
check_xiao();
mark++;
type.a=a[mark]; type.b=1; type.x=4; type.y=9;
}
}
}
}
int main()
{
int t,ii,i;
scanf("%d",&t);
for (ii=1;ii<=t;ii++)
{
scanf("%d",&n);
scanf("%s",str);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);

bfs();

printf("Case %d: %d\n",ii,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: