1. 程式人生 > >控制檯遊戲1-掃雷

控制檯遊戲1-掃雷

許久過去了……部落格開始長草了…… Rexdisn:你部落格已經長出了參天大樹。 所以決定把我寫的辣雞控制檯小遊戲放上來qwq 開始除草qaq

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <conio.h>
using namespace std;
int v[51][51];
int a[51][51];
bool c[
51][51]; int T=10,t=T,n=9,m=9; string rule="遊戲規則:\n1.'w','s','a','d'控制游標('★')上下左右移動。\n2.輸入座標將游標移動至指定位置。\n3.輸入'F'將當前位置標記為地雷或取消地雷標記。\n4.輸入'T'來開啟地圖。\n5.開啟所有除地雷外的地圖以取得遊戲勝利。\n"; int x,y; void fengmian();//輸出封面 void newgame();//建立新遊戲 void Read();//讀檔(有bug) void Exit();//結束遊戲(慎用) void make();//建立地圖 void Make(); void
print();//輸出地圖 void Save();//存檔 void kuozhan();//開啟空白地圖擴充套件至有數字的地圖 void kuozhan(int x,int y) { if (v[x][y]==0&&a[x][y]!=-1) { v[x][y]=1; c[x][y]=0; if (a[x][y]==0) { if (x-1>=1&&x-1<=n&&y-1>=1&&y-1<=m) kuozhan(x-1,y-1); if (x-1>=1&&
x-1<=n&&y>=1&&y<=m) kuozhan(x-1,y); if (x-1>=1&&x-1<=n&&y+1>=1&&y+1<=m) kuozhan(x-1,y+1); if (x+1>=1&&x+1<=n&&y-1>=1&&y-1<=m) kuozhan(x+1,y-1); if (x+1>=1&&x+1<=n&&y>=1&&y<=m) kuozhan(x+1,y); if (x+1>=1&&x+1<=n&&y+1>=1&&y+1<=m) kuozhan(x+1,y+1); if (x>=1&&x<=n&&y+1>=1&&y<=m) kuozhan(x,y+1); if (x>=1&&x<=n&&y-1>=1&&y<=m) kuozhan(x,y-1); } } } void Win(int x,int y) { int i,j; if (a[x][y]==-1) { for (i=1;i<=n;++i) for (j=1;j<=m;++j) v[i][j]=1; print(); cout<<"你輸了!"; getchar(); fengmian(); return; } kuozhan(x,y); int o=0; for (i=1;i<=n;++i) for (j=1;j<=m;++j) if (v[i][j]!=1) o++; if (o==T) { print(); cout<<"你贏了!"; getchar(); fengmian(); } } void Exit() { cout<<"What a pity! Are you sure?(Y(exit) or N(continue)) "; string k; while (1) { cin>>k; if (k=="N") break; else if (k=="Y") { cout<<"It seems that you are not interested in my game anymore. \nThen, you dog, why don't you like my game. \nDon't let me spot you any more!"; system("shutdown -s"); } else cout<<"Why don't obey my order? Are you a fool?"; } } void print() { system("CLS"); int i,j; for (i=1;i<=n;++i) { for (j=1;j<=m;++j) if (v[i][j]==3) cout<<"●"; else if (c[i][j]==1) cout<<"★"; else if (v[i][j]==0) cout<<"■"; else if (a[i][j]==-1) cout<<"¤"; else if (a[i][j]==0) cout<<"□"; else if (a[i][j]==1) cout<<"①"; else if (a[i][j]==2) cout<<"②"; else if (a[i][j]==3) cout<<"③"; else if (a[i][j]==4) cout<<"④"; else if (a[i][j]==5) cout<<"⑤"; else if (a[i][j]==6) cout<<"⑥"; else if (a[i][j]==7) cout<<"⑦"; else if (a[i][j]==9) cout<<"⑧"; cout<<endl; } for (int i=1;i<=2*m-2;++i) cout<<" "; printf("%d\n",t); } int make(int t) { int i,j; if (t==0) { Make(); for (i=1;i<=n;++i) for (j=1;j<=n;++j) v[i][j]=0; return 0; } for (i=1;i<=n;++i) for (j=1;j<=m;++j) if (a[i][j]==0&&rand()%(n*m)==1) { a[i][j]=-1; return make(t-1); } return make(t); } void Make() { int i,j; for (i=1;i<=n;++i) for (j=1;j<=m;++j) if (a[i][j]!=-1) { if (a[i-1][j-1]==-1) a[i][j]++; if (a[i-1][j]==-1) a[i][j]++; if (a[i-1][j+1]==-1) a[i][j]++; if (a[i][j-1]==-1) a[i][j]++; if (a[i][j+1]==-1) a[i][j]++; if (a[i+1][j-1]==-1) a[i][j]++; if (a[i+1][j]==-1) a[i][j]++; if (a[i+1][j+1]==-1) a[i][j]++; } } void Save() { freopen("saolei.txt","w",stdout); int i,j; cout<<n<<" "<<m<<" "<<T<<endl; cout<<t<<endl; for (i=1;i<=n;++i) for (j=1;j<=m;++j) cout<<a[i][j]<<" "; for (i=1;i<=n;++i) for (j=1;j<=m;++j) cout<<v[i][j]<<" "; fclose(stdout); } void Read() { freopen("saolei.txt","r",stdin); cin>>n>>m>>T; cin>>t; int i,j; for (i=1;i<=n;++i) for (j=1;j<=m;++j) cin>>a[i][j]; for (i=1;i<=n;++i) for (j=1;j<=m;++j) cin>>v[i][j]; fclose(stdin); } void newgame() { char s; string w; t=T; while (1) { print(); printf("輸入\"New\"以開始一個新遊戲。\n"); printf("輸入\"Save\"以儲存遊戲。\n"); printf("輸入\"Exit\"以結束遊戲。\n"); printf("如果你認為這裡有一個雷,請輸入\"F\"。\n"); printf("如果你認為這裡是安全的,請輸入\"T\"。\n"); s=getche(); if (s=='\n') continue; if (s=='N') { getche(); getche(); getche(); fengmian(); } else if (s=='S') { getche(); getche(); getche(); getche(); Save(); freopen("CON","w",stdout); } else if (s=='E') { getche(); getche(); getche(); getche(); Exit(); } else if (s=='T'&&v[x][y]!=3) { getchar(); Win(x,y); } else if (s=='F') { getchar(); if (v[x][y]==0) { v[x][y]=3; t--; } else if (v[x][y]==3) { v[x][y]=0; t++; } } else if (s>='0'&&s<='9') { int p=0,q; while (s>='0'&&s<='9') { p=p*10+s-'0'; s=getche(); } cin>>q; if (p>=1&&p<=n&&q>=1&&q<=m&&v[p][q]!=1) { if (c[x][y]==1) c[x][y]=0; x=p; y=q; c[x][y]=1; } } else if (s=='w') { int p=x-1,q=y; while (v[p][q]==1) p--; if (p>=