关灯游戏,谁有简单的代码啊
int light[4][4] = {
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
};
int x = 0, y = 0, sum = 0;
for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 4; j ++) {
printf("%d ", light[i][j]);
sum += light[i][j];
}
printf("\n");
}
while (sum != 0){
scanf("%d, %d", &x, &y);
if (x > 4 || y > 4 || x <= 0 || y <= 0) {
printf("请重新输入坐标:");
scanf("%d, %d", &x, &y);
}
x --;
y --;
light[x][y] = (light[x][y] + 1) % 2;
if (x > 0 && x < 3) {
light[x - 1][y] = (light[x - 1][y] + 1) % 2;
light[x + 1][y] = (light[x + 1][y] + 1) % 2;
}
if (x == 0) {
light[x + 1][y] = (light[x + 1][y] + 1) % 2;
}
if (x == 3) {
light[x - 1][y] = (light[x - 1][y] + 1) % 2;
}
if (y > 0 && y < 3) {
light[x][y - 1] = (light[x][y - 1] + 1) % 2;
light[x][y + 1] = (light[x][y + 1] + 1) % 2;
}
if (y == 0) {
light[x][y + 1] = (light[x][y + 1] + 1) % 2;
}
if (y == 3) {
light[x][y - 1] = (light[x][y - 1] + 1) % 2;
}
for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 4; j ++) {
printf("%d ", light[i][j]);
sum += light[i][j];
}
printf("\n");
}
}
printf("恭喜您,终于玩完了!");