CSP模拟1

Enter 开点

Bakcspace 插旗

设置说明:

main number,雷比,即为单个格子是雷的概率
open degree,开格参数,防止一次性开太多格子,一次性开格上限为 \(n*n*opendegree\)

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#define endl '\n'
using namespace std;
int white=0x0F,black=0xF0,flags=0x0F;
double max_opencnt=0.1;
double mine_cr=0.2;
bool mp[1001][1001];
int pl[1001][1001];
void scbc(char put,int color){
	if(color==-1) return;
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle,color==0?white:color);
	cout<<put<<" ";
	SetConsoleTextAttribute(handle,white);
}
void scbc(string put,int color){
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle,color==0?white:color);
	cout<<put;
	SetConsoleTextAttribute(handle,white);
}
int cor=1,siz=10;
unsigned long long seed;
void print_main(){
    system("cls");
    cout<<"[MAIN PAGE]"<<endl<<endl;
    scbc("Reset Seed",cor==1?black:white);cout<<endl;
    scbc("Resize",cor==2?black:white);cout<<"  Now size: "<<siz<<endl<<endl;
    scbc("Mine Number",cor==3?black:white);cout<<"  Now: "<<mine_cr<<endl;
    scbc("Open Degree",cor==4?black:white);cout<<"  Now: "<<max_opencnt<<endl<<endl;
    cout<<"Color Setting"<<endl;
	scbc("Background   ",cor==5?black:white);scbc('B',white);cout<<endl;
	scbc("Choose       ",cor==6?black:white);scbc('C',black);cout<<endl;
	scbc("Flag         ",cor==7?black:white);scbc('F',flags);cout<<endl<<endl;
    scbc("START",cor==8?black:white);cout<<endl<<endl;
    cout<<"SEED "<<seed<<endl;
}
int maincnt;
void create(){
	maincnt=0;
    for(int i=1;i<=siz;++i){
        for(int j=1;j<=siz;++j){
            if(rand()<=mine_cr*RAND_MAX){
            	maincnt++;
                mp[i][j]=1;
            }
            else mp[i][j]=0;
        }
    }
}
int nx=1,ny=1;
char tochar(int x){
	if(x==-1) return '!';
	if(x==0) return ' ';
	if(x==-2) return 'x';
	if(x==-3) return '0';
	return x+'0';
}
bool flag_sta=true;
void print(){
	system("cls");
	if(!flag_sta) cout<<"Total Mains: "<<maincnt<<endl;
	for(int i=1;i<=siz;++i){
		for(int j=1;j<=siz;++j){
			if(i==nx and j==ny){
				scbc(tochar(pl[i][j]),black);
			}
			else cout<<tochar(pl[i][j])<<' ';
		}
		cout<<endl;
	}
	cout<<endl;
	cout<<"SEED "<<seed<<endl;
}
struct node{
	int x,y,op;
};
node seleted(){
	print();
	while(1){
		if(kbhit()){
			char ch=getch();
			if(ch==72 and nx!=1){
				nx--;
				print();
			}
			if(ch==80 and nx!=siz){
				nx++;
				print();
			}
			if(ch==75 and ny!=1){
				ny--;
				print();
			}
			if(ch==77 and ny!=siz){
				ny++;
				print();
			}
			if(ch==13){
				return {nx,ny,1};
			}
			if(ch==8){
				return {nx,ny,2};
			}
		}
	}
}
/*
-1 flag
-2 mine
-3 space
*/
int dx[10]={0,0,0,1,-1,1,-1,1,-1};
int dy[10]={0,1,-1,0,0,-1,1,1,-1};
int cnt=0;
void open(int x,int y){
	cnt++;
	if(cnt>=siz*siz*max_opencnt) return;
	int cnt=0;
	for(int i=1;i<=8;++i){
		if(x+dx[i]>=1 and x+dx[i]<=siz and y+dy[i]>=1 and y+dy[i]<=siz){
			if(mp[x+dx[i]][y+dy[i]]==1) cnt++;
		}
	}
	pl[x][y]=cnt;
	if(cnt==0) pl[x][y]=-3;
	for(int i=1;i<=4;++i){
		if(x+dx[i]>=1 and x+dx[i]<=siz and y+dy[i]>=1 and y+dy[i]<=siz){
			if(mp[x+dx[i]][y+dy[i]]==0 and (pl[x+dx[i]][y+dy[i]]==0 or pl[x+dx[i]][y+dy[i]]==-1)){
				open(x+dx[i],y+dy[i]);
			}
		}
	}
}
bool check(){
	for(int i=1;i<=siz;++i){
		for(int j=1;j<=siz;++j){
			if(mp[i][j]==0 and (pl[i][j]==0 or pl[i][j]==-1)) return false;
		}
	}
	return true;
}
void start(){
	flag_sta=true;
	memset(pl,0,sizeof pl);
	bool first=true;bool win=false;
    while(1){
    	if(check()){
    		win=true;
    		break;
		}
    	node u=seleted();
    	if(pl[u.x][u.y]!=0 and pl[u.x][u.y]!=-1) continue;
    	if(u.op==2){
    		if(pl[u.x][u.y]==-1){
    			maincnt++;
				pl[u.x][u.y]=0;
			}
    		else{
    			maincnt--;
				pl[u.x][u.y]=-1;
			}
		}
		else{
			if(first){
				while(mp[u.x][u.y]==1){
					srand(rand());
					create();
				}
				flag_sta=false;
				cnt=0;
				open(u.x,u.y);
				first=false;
			}
			else{
				if(mp[u.x][u.y]==1){
					pl[u.x][u.y]=-2;
					break;
				}
				cnt=0;
				open(u.x,u.y);
			}
		}
		print();
	}
	for(int i=1;i<=siz;++i){
		for(int j=1;j<=siz;++j){
			if(mp[i][j]==1){
				pl[i][j]=-2;
			}
		}
	}
	print();
	cout<<(win?"You Win":"You Lost")<<endl;
	system("pause");
}
int main(){
//	while(1){
//        if(kbhit()){
//        	char ch=getch();
//        	cout<<(int)ch<<endl;
//        }
//    }
	srand(time(0));seed=rand()*rand();
	srand(seed);
	print_main();
    while(1){
        if(kbhit()){
            char ch=getch();
            if(ch==72 and cor!=1){
                cor--;
                print_main();
            }
            if(ch==80 and cor!=8){
                cor++;
                print_main();
            }
            if(ch==13){
                if(cor==1){
                    cout<<"Seed: ";
                    cin>>seed;
                    srand(time(0));
                }
                if(cor==2){
                    cout<<"Size: ";
                    cin>>siz;
                }
                if(cor==8){
                    create();
                    start();
                   	srand(time(0));seed=rand()*rand();
					srand(seed);
                }
                if(cor==3){
                	cout<<"MINE NUMBER: ";
                	cin>>mine_cr;
                	mine_cr=min(mine_cr,0.9);
				}
				if(cor==4){
                	cout<<"OPEN DEGREE: ";
                	cin>>max_opencnt;
				}
				if(cor==5){
                	cout<<"Background: ";
                	cin>>white;
				}
				if(cor==6){
                	cout<<"Choose: ";
                	cin>>black;
				}
				if(cor==7){
                	cout<<"Flag: ";
                	cin>>flags;
				}
                print_main();
            }
        }
    }
}


posted @ 2024-09-06 20:42  HaneDaniko  阅读(10)  评论(0编辑  收藏  举报