熄灯游戏
游戏规则: 在一个 3×3 的方格里,放着九盏灯。他们开始时都是关闭的。当我们拉动这盏灯,它和它相邻的几盏灯状态都会被取反。如果你可以把他们全部关闭,你就赢了。
你需要输入你拉动灯的坐标 x( 1−3 ), y ( 1−3 )。接着发挥想象,赢得游戏吧!
代码(亲测无BUG)
#include <bits/stdc++.h>
using namespace std;
int a[5][5];
bool f()
{
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
if(a[i][j]==1)
return false;
return true;
}
int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
a[i][j]=1;
do
{
system("cls");
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
int x,y;
cin>>x>>y;
if(x<1||y<1)
continue;
a[x][y]=!a[x][y];
a[x][y-1]=!a[x][y-1];
a[x][y+1]=!a[x][y+1];
a[x-1][y]=!a[x-1][y];
a[x+1][y]=!a[x+1][y];
}while(!f());
system("cls");
cout<<"You Win";
return 0;
}
温馨提示:
1.游戏中 0 表示关, 1 表示开;
2.只有上、下、左、右和自己的状态会被取反,斜边 不会
3.注意 x 、 y 的范围(均为 1−3 ),超出无效
求关注qwq