C++#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
using namespace std;
void color(int a) {
if(a==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);//red
else if(a==2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);//green
else if(a==3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//white
else if(a==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE);//full white
else if(a==5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED);//full red
else if(a==6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE);//blue
else if(a==7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_BLUE);//full blue
else if(a==8) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN);//full yellow
else if(a==9) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_GREEN);//full green
else if(a==10) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);//yellow
}
int n, sum, __han, __lie;
int s[8][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}, t[105][105], xian[105][105];
char x[205][205];
bool zan[205][205], flag = true, flag_out, bfir;
int mxh=GetSystemMetrics(SM_CYSCREEN)/180*11,mxl=(GetSystemMetrics(SM_CXSCREEN)/40-1)*5/2;
struct node {
int x,y,s,bl;
} fl[1010];
struct nod {
int x,y;
} pz[1010],y_zd;
int fnum,cl,lstl=-1,blod,blg,nwbc;
deque<nod>zd;
bool cbl[210][210],lcbl[210][210];
void put(int a) {
while (a--) putchar('\n');
}
void setpos(int x,int y) {
COORD pos;
pos.X=y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void big() {
HWND hwnd = GetForegroundWindow();
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);
SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void init() {
big();
color(3);
srand(time(0));
}
void green_spot() {
setpos(__han+6,6+3*__lie);
color(2);
cout<<"■";
color(3);
}
void change_sao(int a,int b) {
int x=xian[a][b];
setpos(a+6,6+3*b);
if(x==0)
cout<<"□";
else if(x==1)
cout<<" "<<t[a][b];
else {
color(1);
cout<<"×";
}
color(3);
}
void change_sum() {
setpos(3,11);
cout<<sum;
if(sum==9||sum==99) cout<<"颗雷 ";
}
int cinn(string t,int maxx) {
aa:
system("cls");
cout << t;
int sum = 0, len = 0;
char xu = getch();
bb:
while ((xu >= '0' && xu <= '9') || xu == 8) {
if (xu == 8) {
sum /= 10;
len--;
if (len < 0) len = 0;
cout<<"\b \b";
xu = getch();
goto bb;
}
cout << xu;
len++;
sum = sum * 10 + (xu ^ 48);
if (sum > maxx) goto aa;
xu = getch();
}
if (sum) return sum;
else goto aa;
}
void clear() {
sum = 0;
flag = true;
memset(t, 0, sizeof(t));
memset(xian, 0, sizeof(xian));
memset(zan, false, sizeof(zan));
}
void shibai() {
system("cls");
put(1);
puts(" 失败!");
put(1);
puts(" 正确答案:");
put(3);
for (int i = 1; i <= n; i++) {
cout<<" ";
for (int j = 1; j <= n; j++)
if (x[i][j] == '?') printf("%3d", t[i][j]);
else {
color(1);
cout << " ×";
color(3);
}
put(1);
}
setpos(7+n,0);
cout << " 按任意键继续";
getch();
}
void shengli() {
system("cls");
put(3);
puts(" 成功!");
put(3);
for (int i = 1; i <= n; i++) {
cout<<" ";
for (int j = 1; j <= n; j++)
if (xian[i][j] == 0) cout << " □";
else if (xian[i][j] == 1) printf("%3d", t[i][j]);
else {
color(1);
cout << " ×";
color(3);
}
put(1);
}
setpos(7+n,0);
cout << " 按任意键继续";
getch();
}
void guize() {
system("cls");
puts(" 你的任务是标记出每一个没有地雷的格子所在的位置");
puts(" 每个格子上的数字代表上、下、左、右、左上、右上、左下、右下八个方向共有几颗雷");
color(2);
cout << " ■";
color(3);
cout << "是你所在的位置,按↑↓←→控制";
color(2);
cout << "■";
color(3);
puts("的位置");
puts(" 确认位置后可以进行三种操作");
puts(" (1) 按f键,表示翻开这个位置");
puts(" (2) 按c键,表示标记这个位置有雷");
puts(" (3) 按s键,表示删除这个位置的标记");
put(2);
cout << " 按任意键继续";
getch();
}
void xuanze() {
n=cinn(" 请输入边长(1~37):",37);
__han = (n + 1) / 2;
__lie = (n + 1) / 2;
}
void kaishi() {
bb:
system("cls");
puts(" 是否开始游戏?");
puts(" 1.开始");
puts(" 2.查看规则");
put(2);
cout << " 请输入序号:";
aa:
char xu = getch();
cout << xu;
if (xu == '1') {
xuanze();
return;
}
if (xu == '2') {
guize();
goto bb;
}
setpos(5,19);
goto aa;
}
void chuxian(int han, int lie) {
zan[han][lie] = true;
int k[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
for (int i = 0; i < 4; i++) {
int a = han + k[i][0], b = lie + k[i][1];
if (a > 0 && a <= n && b > 0 && b <= n && t[a][b] == 0 && zan[a][b] == false && xian[a][b] == 0) {
zan[a][b] = true;
chuxian(a, b);
}
}
}
bool pan() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (x[i][j] == '?' && xian[i][j] != 1)
return true;
return false;
}
void caozuo() {
aa:
char k = getch();
while (k == 72 || k == 77 || k == 80 || k == 75) {
if (k == 72) {
if (__han > 1) {
__han--;
green_spot();
change_sao(__han+1,__lie);
if(flag_out) {
setpos(7+n,0);
cout << " ";
flag_out=false;
}
} else {
setpos(7+n,0);
cout << "出界了";
flag_out=true;
}
} else if (k == 77) {
if (__lie < n) {
__lie++;
green_spot();
change_sao(__han,__lie-1);
if(flag_out) {
setpos(7+n,0);
cout << " ";
flag_out=false;
}
} else {
setpos(7+n,0);
cout << "出界了";
flag_out=true;
}
} else if (k == 80) {
if (__han < n) {
__han++;
green_spot();
change_sao(__han-1,__lie);
if(flag_out) {
setpos(7+n,0);
cout << " ";
flag_out=false;
}
} else {
setpos(7+n,0);
cout << "出界了";
flag_out=true;
}
} else if (k == 75) {
if (__lie > 1) {
__lie--;
green_spot();
change_sao(__han,__lie+1);
if(flag_out) {
setpos(7+n,0);
cout << " ";
flag_out=false;
}
} else {
setpos(7+n,0);
cout << "出界了";
flag_out=true;
}
}
k = getch();
}
if (k != 's' && (__han < 1 || __han > n || __lie < 1 || __lie > n || xian[__han][__lie] != 0)) goto aa;
if (k == 'f') {
if (t[__han][__lie] == 9) {
flag = false;
return;
}
xian[__han][__lie] = 1;
if (t[__han][__lie] == 0) {
memset(zan, false, sizeof(zan));
chuxian(__han, __lie);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (zan[i][j]) {
xian[i][j] = 1;
change_sao(i,j);
for (int p = 0; p < 8; p++) {
int a = i + s[p][0], b = j + s[p][1];
if (a > 0 && a <= n && b > 0 && b <= n && xian[a][b] == 0 && t[a][b] != 0) {
xian[a][b] = 1;
change_sao(a,b);
}
}
}
green_spot();
}
} else if (k == 'c') {
if (sum <= 0) goto aa;
xian[__han][__lie] = 2;
sum--;
change_sum();
} else {
if (xian[__han][__lie] == 2) {
xian[__han][__lie] = 0;
sum++;
} else goto aa;
}
}
char st() {
system("cls");
put(3);
puts(" 1.重新开始");
puts(" 2.更换难度");
puts(" 3.回到主页");
puts(" 4.关闭游戏");
cout << " 请输入序号:";
aa:
char xu = getch();
cout << char(xu);
if (xu == '4') exit(0);
__han = (n + 1) / 2;
__lie = (n + 1) / 2;
if (xu == '1' || xu == '2' || xu == '3') return xu;
setpos(7,19);
goto aa;
}
void sao_lei() {
kaishi();
sc:
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int s = rand() % 9;
if (s == 0) {
x[i][j] = '*';
sum++;
} else x[i][j] = '?';
}
if (sum < n * n / 9 - 1 || sum > n * n / 9 + 1) {
sum = 0;
goto sc;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (x[i][j] == '*') {
t[i][j] = 9;
continue;
}
int p = 0;
for (int k = 0; k < 8; k++) {
int han = i + s[k][0], lie = j + s[k][1];
if (han > 0 && han <= n && lie > 0 && lie <= n && x[han][lie] == '*') p++;
}
t[i][j] = p;
}
system("cls");
put(3);
cout<<" 剩余"<<sum<<"颗雷";
put(4);
for (int i = 1; i <= n; i++) {
cout<<" ";
for (int j = 1; j <= n; j++)
if (i == __han && j == __lie) {
color(2);
cout << " ■";
color(3);
} else cout << " □";
put(1);
}
while (pan() && flag) caozuo();
if (flag) shengli();
else shibai();
char k = st();
clear();
if (k == '1') goto sc;
if (k == '2') {
xuanze();
goto sc;
}
if (k == '3') return ;
}
void r_bl(int now) {
int s=fl[now].s,x=fl[now].x,y=fl[now].y;
for(int i=0; i<s; i++)
for(int j=0; j<=i*2; j++)
cbl[x-s+1+i][y-i+j]=true;
for(int i=s-2; i>=0; i--)
for(int j=0; j<=i*2; j++)
cbl[x+s-1-i][y-i+j]=true;
}
void flan(int s,int x,int y,int py) {
fl[++fnum]= {x,y,s,0};
pz[fnum]= {1,py};
color(4);
for(int i=0; i<s; i++) {
setpos(x-s+1+i,2*(y-i));
for(int j=0; j<=i*2; j++){
lcbl[x-s+1+i][y-i+j]=true;
cout<<" ";
}
}
for(int i=s-2; i>=0; i--) {
setpos(x+s-i-1,2*(y-i));
for(int j=0; j<=i*2; j++){
lcbl[x+s-1-i][y-i+j]=true;
cout<<" ";
}
}
r_bl(fnum);
}
void add_b() {
int s,x,y,py;
if(cl<mxh) {
y=mxh-cl;
py=-1;
} else {
y=mxl+mxl-mxh-cl;
py=1;
}
s=min(rand()%4+2,y+1);
if(s<2) return ;
x=s-1;
flan(s,x,y,py);
}
void mov_b() {
memset(cbl,false,sizeof(cbl));
for(int i=1; i<=fnum; i++) {
fl[i].x+=pz[i].x;
fl[i].y+=pz[i].y;
int x=fl[i].x,y=fl[i].y,s=fl[i].s;
if(x-s+1==0||x+s-1==mxh) pz[i].x*=-1;
if(y-s+1==0||y+s-1==mxl) pz[i].y*=-1;
r_bl(i);
}
for(int i=0; i<=mxh; i++)
for(int j=0; j<=mxl; j++)
if(lcbl[i][j]!=cbl[i][j]) {
if(j==mxl&&i<=31&&i>=32-blg)
continue;
if(lcbl[i][j]) {
if(((j==cl-1||j==cl+1)&&i>=43)||(j==cl&&i>=40)) color(5);
else if(((j==cl-1||j==cl+1)&&i<=1)||(j==cl&&i<=4)) color(9);
else color(3);
}else color(4);
setpos(i,j*2);
cout<<" ";
}
memcpy(lcbl,cbl,sizeof(cbl));
}
void getcar(int col) {
color(col);
for(int i=0; i<3; i++) {
setpos(40+i,2*cl);
cout<<" ";
}
for(int i=0; i<2; i++) {
setpos(43+i,2*(cl-1));
cout<<" ";
}
}
void getcar2(int col) {
color(col);
for(int i=0; i<3; i++) {
setpos(i+2,2*cl);
cout<<" ";
}
for(int i=0; i<2; i++) {
setpos(i,2*(cl-1));
cout<<" ";
}
}
void muvcr(int pos) {
getcar(3);
getcar2(3);
cl+=pos*2;
cl=max(cl,1);
cl=min(cl,76);
getcar(5);
getcar2(9);
}
void fir() {
int tzd=zd.size();
zd.push_back({39,cl});
for(int i=0; i<tzd; i++)
if(zd[i].x==0) {
color(3);
setpos(0,zd[i].y*2);
cout<<" ";
zd.pop_front();
} else break;
tzd=zd.size();
for(int i=0; i<tzd; i++) {
nod ps=zd[i];
color(3);
setpos(ps.x,ps.y*2);
cout<<" ";
zd[i].x--;
color(6);
setpos(ps.x-1,ps.y*2);
cout<<"●";
}
}
void lfir() {
if(cl!=lstl) {
for(int i=5; i<40; i++) {
color(3);
setpos(i,lstl*2);
cout<<" ";
color(7);
setpos(i,cl*2);
cout<<" ";
}
lstl=cl;
}
}
int diy() {
system("color 4F");
setpos(22,73);
color(3);
cout<<"game over";
Sleep(1500);
system("color 0F");
system("cls");
put(3);
puts(" 1.重新开始");
puts(" 2.回到主页");
puts(" 3.关闭游戏");
cout << " 请输入序号:";
aa:
char t=getch();
if(t=='1') return 0;
else if(t=='2') return 1;
else if(t=='3') exit(0);
setpos(6,19);
goto aa;
}
bool cg_bl() {
int thg=blod/5+(blod%5>0);
if(thg!=blg) {
int tcl;
if(blod>=50) tcl=9;
else if(blod>=25) tcl=8;
else if(blod>0) tcl=5;
else return false;
color(3);
for(int i=31; i>=32-blg; i--) {
setpos(i,154);
cout<<" ";
}
color(tcl);
for(int i=31; i>=32-thg; i--) {
setpos(i,154);
cout<<" ";
}
blg=thg;
nwbc=tcl;
}
return true;
}
void add_zd() {
y_zd.x=5;
y_zd.y=cl;
setpos(5,cl*2);
color(10);
cout<<"●";
}
bool mov_zd() {
int x=++y_zd.x,y=y_zd.y;
if((y==cl&&x>=40)||((y+1==cl||y-1==cl)&&x>=43)) {
blod-=10;
if(!cg_bl())
return false;
add_zd();
return true;
}
color(3);
setpos(x-1,y*2);
cout<<" ";
if(x>mxh) add_zd();
else {
setpos(x,y*2);
color(10);
cout<<"●";
}
return true;
}
void duck() {
aa:
system("cls");
cl=38;
blod=100;
zd.clear();
getcar(5);
getcar2(9);
cg_bl();
add_b();
add_zd();
int vll,nw_t=0;
while(1) {
while(_kbhit()==0) {
int t=int(clock()/1000)+1;
if(t!=nw_t&&t%5==0) {
add_b();
if(!cg_bl()) {
int t=diy();
if(t==0) goto aa;
if(t==1) return ;
}
}
nw_t=t;
if(!bfir) {
if(lstl!=-1) {
color(3);
for(int i=5; i<40; i++) {
setpos(i,lstl*2);
cout<<" ";
}
lstl=-1;
}
fir();
} else {
if(!zd.empty()) {
color(3);
for(int i=0; i<zd.size(); i++) {
nod ps=zd[i];
setpos(ps.x,ps.y*2);
cout<<" ";
}
zd.clear();
}
lfir();
}
if(!mov_zd()) {
int t=diy();
if(t==0) goto aa;
if(t==1) return ;
}
mov_b();
Sleep(10);
}
vll=getch();
if(vll==75) muvcr(-1);
else if(vll==77) muvcr(1);
else if(vll==72) bfir=!bfir;
}
}
int main() {
bb:
init();
system("cls");
puts(" 主页");
puts(" 1.扫雷游戏");
puts(" 2.更新中...");
cout << " 请输入序号:";
aa:
char t = getch();
cout << char(t);
if (t == '1') {
sao_lei();
goto bb;
}
if (t == '2') {
duck();
goto bb;
}
setpos(3,19);
goto aa;
return 0;
}
C++#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
void setpos(int x,int y) {
COORD pos;
pos.X=y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void big() {
HWND hwnd = GetForegroundWindow();
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);
SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void color(int a) {
if(a==0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE);
}
int t[8][2]= {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
int a[100][200],c[100][200];
int main() {
big();
a[20][76]=a[20][77]=a[21][76]=a[21][78]=a[22][76]=1;
setpos(20,76);
cout<<"x";
setpos(20,77);
cout<<"x";
setpos(21,76);
cout<<"x";
setpos(21,78);
cout<<"x";
setpos(22,76);
cout<<"x";
while(1) {
for(int i=0; i<=44; i++){
for(int j=0; j<=155; j++) {
int cnt=0;
for(int k=0; k<8; k++) {
int h=i+t[k][0];
int l=j+t[k][1];
if(h>=0&&l>=0&&h<=44&&l<=154) cnt+=a[h][l];
}
if(!a[i][j]) {
if(cnt==3) c[i][j]=1;
else c[i][j]=0;
} else {
if(cnt==3||cnt==2) c[i][j]=1;
else c[i][j]=0;
}
}
}
for(int i=0;i<=44;i++)
for(int j=0;j<=155;j++)
if(c[i][j]&&rand()%10==0){
if(c[i+1][j]&&c[i][j+1]&&c[i+1][j+1]
&&!c[i-1][j-1]&&!c[i-1][j]&&!c[i-1][j+1]&&!c[i-1][j+2]
&&!c[i][j-1]&&!c[i][j+2]&&!c[i+1][j-1]&&!c[i+1][j+2]
&&!c[i+2][j-1]&&!c[i+2][j]&&!c[i+2][j+1]&&!c[i+2][j+2]
){
if(i-1>=0&&j-1>=0) c[i+1][j+1]=0,c[i-1][j]=c[i][j-1]=1;
else c[i][j]=0,c[i+1][j+2]=c[i+2][j+1]=1;
}
if(c[i][j+1]&&c[i][j+2]
&&!c[i-1][j-1]&&!c[i-1][j]&&!c[i-1][j+1]&&!c[i-1][j+2]&&!c[i-1][j+3]
&&!c[i][j-1]&&!c[i][j+3]
&&!c[i+1][j-1]&&!c[i+1][j]&&!c[i+1][j+1]&&!c[i+1][j+2]&&!c[i+1][j+3]
)c[i-1][j+1]=c[i+1][j+1]=1;
}
for(int i=0;i<=44;i++)
for(int j=0;j<=155;j++)
if(c[i][j]!=a[i][j]){
setpos(i,j);
if(c[i][j]) cout<<"x";
else cout<<" ";
a[i][j]=c[i][j];
}
}
return 0;
}