写一个c语言消消乐程序
时间: 2023-03-09 15:43:52 浏览: 314
C#消消乐程序
4星 · 用户满意度95%
我可以为您提供一个示例程序:#include <stdio.h> int main()
{
int row, col, i, j;
int row_size, col_size; // 输入行列
printf("请输入行和列的大小:");
scanf("%d %d", &row_size, &col_size); int board[row_size][col_size]; // 输入棋盘
printf("请输入棋盘:\n");
for (i = 0; i < row_size; i++)
{
for (j = 0; j < col_size; j++)
{
scanf("%d", &board[i][j]);
}
} // 消除棋子
while (1)
{
int remove = 0;
for (i = 0; i < row_size; i++)
{
for (j = 0; j < col_size; j++)
{
if (board[i][j] != 0)
{
if ((i+2 < row_size && board[i][j] == board[i+1][j] &&
board[i][j] == board[i+2][j]) ||
(j+2 < col_size && board[i][j] == board[i][j+1] &&
board[i][j] == board[i][j+2]))
{
board[i][j] = board[i+1][j] = board[i+2][j] = 0;
remove = 1;
}
}
}
} // 棋子下落
if (remove)
{
// 棋子下落
for (i = row_size - 1; i >= 0; i--)
{
for (j = 0; j < col_size; j++)
{
if (board[i][j] == 0)
{
int k = i;
while (k >= 0 && board[k][j] == 0)
k--;
if (k >= 0)
{
board[i][j] = board[k][j];
board[k][j] = 0;
}
}
}
}
}
else
break;
} // 输出棋盘
printf("消除棋子后的棋盘:\n");
for (i = 0; i < row_size; i++)
{
for (j = 0; j < col_size; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
} return 0;
}
阅读全文