每一次移动前判断每个方块位置下面是否已有方块,直到不能移动,将该数组更新到原来的图上
#include<iostream>
using namespace std;
int main()
{
int board[20][15], block[4][2];
int left;
for (int i = 1; i <= 15; i++)
{
for (int j = 1; j <= 10; j++)
{
cin >> board[i][j];
}
}
int num = 0;
for (int i = 1; i <= 4; i++)
{//题目已经说明是标准俄罗斯方块,一共是4块,用四个二维数组存储,其中j:横坐标,i:纵坐标
for (int j = 1; j <= 4; j++)
{
int temp;
cin >> temp;
if (temp)
{
block[num][0] = i;
block[num][1] = j;
num++;
}
}
}
cin >> left;//调整方块在整体中的位置
for (int i = 0; i < 4; i++)
{
block[i][1] += left - 1;//调整的是纵坐标,因为把题目中原来从0开始,修正为从1开始,故left需要减一
}
bool flag = true;
while (flag)
{
for (int i = 0; i < 4; i++)
{//检查四个方块的状态
if (board[block[i][0] + 1][block[i][1]])//注意1;这里时block[i][0] + 1,时提前预判
flag = false;
}
for (int i = 0; i < 4 && flag; i++)
{
block[i][0]++;//纵坐标下移
}
}
for (int i = 0; i < 4; i++) {
board[block[i][0]][block[i][1]] = 1;
}
for (int i = 1; i <= 15; i++)
{
for (int j = 1; j <= 10; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
return 0;
}