C++#include<bits/stdc++.h>
#include<windows.h>
#define KeyDown(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000) ? 1:0)
using namespace std;
int M[50][50];
mt19937 mrand(time(0));
HWND hWnd=GetConsoleWindow();
HDC hdc=GetDC(hWnd);
HPEN white=CreatePen(PS_SOLID,1,RGB(255,255,255));
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE),In=GetStdHandle(STD_INPUT_HANDLE);
HBRUSH blackbrush=CreateSolidBrush(RGB(200,200,200)),whitebrush=CreateSolidBrush(RGB(255,255,255));
int rnd(int x){
return mrand()%x+1;
}
int n;
void Print(){
while(1){
SelectObject(hdc,white);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(M[i][j]){
SelectObject(hdc,whitebrush);
Rectangle(hdc,(j-1)*50,(i-1)*50,j*50,i*50);
}
else{
SelectObject(hdc,blackbrush);
Rectangle(hdc,(j-1)*50,(i-1)*50,j*50,i*50);
}
Sleep(100);
}
}
int main(){
ShowWindow(hWnd,SW_MAXIMIZE);
DWORD mode;
GetConsoleMode(In,&mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(In,mode);
CONSOLE_CURSOR_INFO A;
GetConsoleCursorInfo(Out,&A);
A.bVisible=false;
SetConsoleCursorInfo(Out,&A);
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
M[i][j]=rnd(2)-1;
}
thread t(Print);
// cout<<'1'<<endl;
while(1){
if(KeyDown(VK_LBUTTON)){
POINT p;
GetCursorPos(&p);
int x=p.y/50+1,y=p.x/50+1;
if(x>n||x<0||y>n||y<0) continue;
M[x][y]^=1,M[x][y-1]^=1,M[x][y+1]^=1,M[x-1][y]^=1,M[x+1][y]^=1;
Sleep(100);
}
}
return 0;
}