HDU 5374 模拟俄罗斯方块

模拟俄罗斯方块游戏

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

注意规则即可:

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值