C语言写俄罗斯方块旋转算法
- 提问者网友:謫仙
- 2021-08-19 22:37
- 五星知识达人网友:独行浪子会拥风
- 2021-08-19 23:42
问题描述不完整,给你我作业里德旋转函数好了,
//...
#define LONG_SIZE 15
#define WIDE_SIZE 10
//...
char img[LONG_SIZE][WIDE_SIZE];
int x_[4],y_[4],x,y;
//...
void rotate()
{
int tmx[4],tmy[4];
int tf=1;
int i;
int k;
if((x_[0]==x_[1])&&(x_[2]==x_[3])&&(y_[0]==y_[2])&&(y_[1]==y_[3]))
return;
for(i=0;i<4;i++)
img[x_[i]][y_[i]]=0;
for(i=0;i<4;i++)
{
tmx[i]=x+(y_[i]-y);
tmy[i]=y-(x_[i]-x);
if(tmx[i]<0||tmx[i]>LONG_SIZE-1)
{
tf=0;
break;
}
if(tmy[i]<0||tmy[i]>WIDE_SIZE-1)
{
tf=0;
break;
}
if((tmx[i]==x_[i])||(tmy[i]==y_[i]))
{
if(tmx[i]==x_[i])
{ if(img[tmx[i]][tmy[i]]==1||img[tmx[i]][y]==1)
{
tf=0;
break;
}
}
else if(tmy[i]==y_[i])
{
if(img[tmx[i]][tmy[i]]==1||img[x][tmy[i]]==1)
{
tf=0;
break;
}
}
else continue;
}
if((tmx[i]<x_[i]&&tmy[i]>y_[i])||(tmx[i]>x_[i]&&tmy[i]<y_[i]))
k=1;
else k=0;
while(tmx[i]!=x_[i]||tmy[i]!=y_[i])
{
if(img[tmx[i]][tmy[i]]==1)
{
tf=0;
break;
}
if(k%2==0)
tmx[i]<x_[i]?tmx[i]++:tmx[i]--;
else
tmy[i]<y_[i]?tmy[i]++:tmy[i]--;
k++;
}
}
if(tf)
{
for(i=0;i<4;i++)
{
tmx[i]=x+(y_[i]-y);
tmy[i]=y-(x_[i]-x);
x_[i]=tmx[i];
y_[i]=tmy[i];
}
}
for(i=0;i<4;i++)
img[x_[i]][y_[i]]=1;
}
几个月前写的,可能因为之前的水平原因代码有点乱(现在懒的编),不过是可行的..
- 1楼网友:纵马山川剑自提
- 2021-08-20 01:20