|
@JLDawson
- #include<iostream>
- #include<cstdlib>
- #include<windows.h>
- #include<stdio.h>
- #include<ctime>
- #include <stdlib.h>
- #include<fstream>
- #include<istream>
- #include<ostream>
- #include<time.h>
- #include<string>
- #include<conio.h>
- #define High 18//高
- #define Wide 18//宽
- #define Mine 40//雷的数量
- HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量,
- //并且这个只能在每个头文件中单独定义句柄和函数,否则无效
- using namespace std;
- class LM //地雷类 //Landmine 地雷
- {
- public:
- LM();//构造函数
- void Judge();//判断是否死亡函数
- void Select();//接受键盘指令表示现在选择的位置
- static int x;//现在处于的横坐标
- static int y;//现在处于的纵坐标
- static int Leftover; //表示未被标记的雷的数量
- private:
- bool flag;//标志位,表示此位置是否有雷,有雷则为ture,无雷则为false
- int Nome;//Numofmine 表示以此位置为中心的九宫格范围内有多少个雷
- bool Openor;//标志位,标志此位置是否开了
- bool showit;//标志位,表示是否需要输出
- bool Inflag;//判断某位置是否插旗
- };
- class User //玩家类
- {
- friend class LM;
- public:
- void Showgame();//展示函数 (地图)
- void Init();//数据初始化
- void RAnd();//生成地雷位置
- void Search();//查找函数
- void Bsearch(int x, int y);//判断哪些数字需要输出
- void Inflagor();//插旗函数
- private:
- };
- //定义变量
- bool win = false; //判断是否胜利
- bool die = false;//判断是否死亡
- time_t second;//定义时间变量
- int LM::x = 0;//横坐标
- int LM::y = 0; //纵坐标
- int LM::Leftover = Mine;//将可插旗数等于雷数
- User u1;
- LM G[High][Wide];//游戏面板大小为max*max,每一位数组代表一个格子
- int Numoffg = 0;//插旗的个数
- long long time1;//开始游戏的时间
- long long time2;//通关时间
- int t;
- string yourname;
- string s[100];
- //申明类外函数
- void coutfile();
- void Welcome();
- void file();
- LM::LM()
- {
- //作用是生成对象数组
- }
- void User::Init()//初始化
- {
- for (int i = 0; i < High; i++)
- {
- for (int j = 0; j < Wide; j++)
- {
- G[i][j].flag = false;
- //G[i][j].flag=false;
- G[i][j].Nome = 0;
- G[i][j].Openor = false;
- G[i][j].showit = false;
- G[i][j].Inflag = false;
- }
- }
- }
- void User::Showgame()//展示函数,在一开始展示出游戏面板,在每一次用户进行一次翻牌后进行一次新的输出
- {
- for (int i = 0; i < High; i++)
- {
- for (int j = 0; j < Wide; j++)
- {
- if (i == LM::x && j == LM::y)
- cout << "ME";
- else if (G[i][j].Openor == true)//当点击到地雷数不为0的位置时
- {
- if (G[i][j].Nome == 1) cout << "①";
- if (G[i][j].Nome == 2) cout << "②";
- if (G[i][j].Nome == 3) cout << "③";
- if (G[i][j].Nome == 4) cout << "④";
- if (G[i][j].Nome == 5) cout << "⑤";
- if (G[i][j].Nome == 6) cout << "⑥";
- if (G[i][j].Nome == 7) cout << " 7";
- if (G[i][j].Nome == 8) cout << "⑧";
- }
- else if (G[i][j].Inflag == true)//当在某位置插旗
- {
- cout << "旗";
- }
- else if (G[i][j].showit == true) //当点击到地雷数为0的位置时
- {
- if (G[i][j].showit == true && G[i][j].Nome == 0)
- cout << " ";
- if (G[i][j].showit == true && G[i][j].Nome != 0)
- {
- if (G[i][j].flag == true && G[i][j].Nome == 1) cout << "█";
- else if (G[i][j].Nome == 1) cout << "①";
- else if (G[i][j].Nome == 2) cout << "②";
- else if (G[i][j].Nome == 3) cout << "③";
- else if (G[i][j].Nome == 4) cout << "④";
- else if (G[i][j].Nome == 5) cout << "⑤";
- else if (G[i][j].Nome == 6) cout << "⑥";
- else if (G[i][j].Nome == 7) cout << " 7";
- else if (G[i][j].Nome == 8) cout << "⑧";
- }
- }
- else
- cout << "█";
- }
- cout << endl;
- }
- }
- void User::Search()//通过此函数查找计算出此位置的九宫格中有多少个雷
- {
- for (int i = 0; i < High; i++)
- {
- for (int j = 0; j < Wide; j++)
- {
- if ((i - 1 >= 0) && (j - 1 >= 0) && (G[i - 1][j - 1].flag == true)) G[i][j].Nome++;//有些地方的判断是有点多余的,但是为了代码好看点嘛
- if ((i - 1 >= 0) && (j + 0 >= 0) && (G[i - 1][j].flag == true)) G[i][j].Nome++;
- if ((i - 1 >= 0) && (j + 1 < Wide) && (G[i - 1][j + 1].flag == true)) G[i][j].Nome++;
- if ((i + 0 >= 0) && (j - 1 >= 0) && (G[i][j - 1].flag == true)) G[i][j].Nome++;
- if ((i + 0 >= 0) && (j + 0 >= 0) && (G[i][j].flag == true)) G[i][j].Nome++;
- if ((i + 0 >= 0) && (j + 1 < Wide) && (G[i][j + 1].flag == true)) G[i][j].Nome++;
- if ((i + 1 < High) && (j - 1 >= 0) && (G[i + 1][j - 1].flag == true)) G[i][j].Nome++;
- if ((i + 1 < High) && (j + 0 >= 0) && (G[i + 1][j].flag == true)) G[i][j].Nome++;
- if ((i + 1 < High) && (j + 1 < Wide) && (G[i + 1][j + 1].flag == true))G[i][j].Nome++;
- }
- }
- }
- void LM::Select()//键盘操作表示选择的位置 ,在玩家选择完成后调用判断函数并且输出新的游戏界面
- {
- char ch = _getch();
- if (ch == 'w' && LM::x - 1 >= 0) LM::x--;
- if (ch == 's' && LM::x + 1 < High) LM::x++;
- if (ch == 'a' && LM::y - 1 >= 0) LM::y--;
- if (ch == 'd' && LM::y + 1 < Wide) LM::y++;
- if (ch == 13) Judge();
- if (ch == 32) u1.Inflagor();
- }
- void User::RAnd()//生成地雷的位置
- {
- srand(time(NULL));
- for (int i = 0; i < Mine; i++)
- {
- int rand2 = rand() % High;
- int rand1 = rand() % Wide;
- G[rand1][rand2].flag = true;
- }
- }
- void User::Bsearch(int i, int j)//判断需要输出多少个数
- {
- //int i,j;
- //i=x;j=y;
- G[i][j].showit = true;
- for (int a = i - 1; a <= i + 1; a++)
- {
- for (int b = j - 1; b <= j + 1; b++)
- {
- if ((a >= 0 && a <= High && b >= 0 && b <= Wide) && G[a][b].Nome == 0)
- {
- if (a == i && b == j)
- G[a][b].showit = true;
- else if (G[a][b].showit == true)
- {
- }
- else if (G[a][b].showit != true)
- {
- G[a][b].showit = true;
- Bsearch(a, b);
- }
- }
- else if ((a >= 0 && a <= High && b >= 0 && b <= Wide) && G[a][b].Nome != 0)
- G[a][b].showit = true;
- }
- }
- }
- //光标定位函数
- void cursor(int x, int y)
- {
- COORD c;
- c.X = x;
- c.Y = y;
- SetConsoleCursorPosition(hout, c); //设置控制台光标的位置
- }
- void hide_cursor()//隐藏光标 这我也看不懂啊,反正拿来用嘛
- {
- HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cursor_info;
- GetConsoleCursorInfo(h_GAME, &cursor_info);
- cursor_info.bVisible = false;
- SetConsoleCursorInfo(h_GAME, &cursor_info);
- }
- void Menu()//主菜单
- {
- // time1=Time();
- second = time(NULL);//获取开始游戏的时间
- time1 = second;
- hide_cursor();
- u1.Init();
- u1.RAnd();
- u1.Search();
- u1.Showgame();
- while (1)
- {
- hide_cursor();
- while (_kbhit())
- {
- G[LM::x][LM::y].Select();
- cursor(0, 0);
- if (die == true)
- {
- system("cls");
- char ch = 7;
- cout << ch << endl; cout << "\t\t\t***********************************************" << endl;
- cout << "\t\t\t***********************************************" << endl;
- cout << "\t\t\t " << endl;
- cout << "\t\t\t 臭弟弟,你踩到雷了! " << endl;
- cout << "\t\t\t 5秒后自动返回主菜单 " << endl;
- cout << "\t\t\t " << endl;
- cout << "\t\t\t***********************************************" << endl;
- cout << "\t\t\t***********************************************" << endl;
- die = false;
- Sleep(5000);
- system("cls");
- Welcome();
- //Welcome();
- }
- else if (win == true)
- {
- system("cls");
- win = false;
- Welcome();
- }
- else
- {
- u1.Showgame();
- }
- }
- }
- }
- void LM::Judge()//通过查看此位置对象的标志位,判断是否翻开了雷
- {
- if (G[LM::x][LM::y].flag == true)
- {
- die = true;
- }
- if ((G[LM::x][LM::y].flag == false) && (G[LM::x][LM::y].Nome != 0))//当没有踩到雷,并且当前位置数字不为0
- {
- G[LM::x][LM::y].Openor = true;
- }
- if ((G[LM::x][LM::y].flag == false) && (G[LM::x][LM::y].Nome == 0))//当没有踩到雷,并且当前位置数字为0
- {
- u1.Bsearch(LM::x, LM::y);
- }
- }
- void User::Inflagor()//插旗函数
- {
- if (G[LM::x][LM::y].Inflag == false)
- {
- G[LM::x][LM::y].Inflag = true;
- if (G[LM::x][LM::y].flag == true)
- Numoffg++;//插了旗的雷 +1
- }
- else if (G[LM::x][LM::y].Inflag == true)
- {
- G[LM::x][LM::y].Inflag = false;
- if (G[LM::x][LM::y].flag == true)
- Numoffg--;//插了旗的雷 -1
- }
- else
- ;
- if (G[LM::x][LM::y].flag == true && G[LM::x][LM::y].Inflag == true)
- LM::Leftover--;
- else if (G[LM::x][LM::y].flag == true && G[LM::x][LM::y].Inflag == false)
- LM::Leftover++;
- else if (G[LM::x][LM::y].flag == false && G[LM::x][LM::y].Inflag == true)
- LM::Leftover--;
- else if (G[LM::x][LM::y].flag == false && G[LM::x][LM::y].Inflag == false)
- LM::Leftover++;
- else
- ;
- if (LM::Leftover == 0 && Mine == Numoffg)
- {
- system("cls");
- win = true;
- second = time(NULL);
- time2 = second;
- t = time2 - time1;
- file();
- cout << "\t\t\t***********************************************" << endl;
- cout << "\t\t\t***********************************************" << endl;
- cout << "\t\t\t " << endl;
- cout << "\t\t\t 恭喜你赢了! " << endl;
- cout << "\t\t\t 消耗时间:" << t / 60 << "分钟" << t % 60 << "秒 " << endl;
- cout << "\t\t\t 5秒后返回主菜单 " << endl;
- cout << "\t\t\t " << endl;
- cout << "\t\t\t***********************************************" << endl;
- cout << "\t\t\t***********************************************" << endl;
- Numoffg = 0;
- LM::Leftover = Mine;
- Sleep(5000);
- char ch[90];
- itoa(t, ch, 10);
- fp = fopen("123.txt", "a+");
- fputs(ch, fp);
- fprintf(fp, "\n");
- fclose(fp);
- }
- else;
- }
- int Time()//计时函数
- {
- second = time(NULL);
- return second;
- }
- void Welcome()
- {
- int n = 0;
- while (1)
- {
- cout << "\n\n\n\n\n\t\t\t\t\t欢迎进入扫雷游戏" << endl;
- cout << "\t\t\t****************************************************" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* 请根据提示选择你想要的功能 *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* 1.开始游戏 *" << endl;
- cout << "\t\t\t* 2.游戏规则 *" << endl;
- cout << "\t\t\t* 3.退出游戏 *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t****************************************************" << endl;
- cin >> n;
- switch (n)
- {
- case 1:
- system("cls");
- cout << "请输入你的游戏昵称:" << endl;
- cin >> yourname;
- system("cls");
- Menu();
- break;
- case 2:
- system("cls");
- cout << "\n\n\n\n\n\t\t\t\t\t欢迎进入扫雷游戏" << endl;
- cout << "\t\t\t****************************************************" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* W、A、S、D分别控制上下左右 *" << endl;
- cout << "\t\t\t* Enter进行选择,空格代表插旗 *" << endl;
- cout << "\t\t\t* 当插旗数等于总雷数即为胜利,若翻到雷则结束游戏 *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t* *" << endl;
- cout << "\t\t\t****************************************************" << endl;
- // cout<<" W、A、S、D分别控制上下左右,Enter进行选择,空格代表插旗"<<endl;
- // cout<<" 当插旗数等于总雷数即为胜利,若翻到雷则结束游戏"<<endl;
- system("pause");
- break;
- case 3:
- cout << " 拜拜您嘞" << endl;
- return;
- break;
- default: cout << "没有这个功能" << endl;
- Sleep(3000);
- break;
- exit(0);
- }
- system("cls");
- }
- }
- void file()
- {
- ofstream ofs;
- //打开文件
- ofs.open("E:\\c++语言文件\\大作业\\numofuuser.txt", ios::app);
- string ch = "abc";
- if (ofs) //判断文件是否打开成功
- {
- ofs << yourname << " ";
- ofs << t << endl; //直接调用<<运算符进行写入
- cout << "write success" << endl;
- }
- else
- cout << "write failed" << endl;
- ofs.close();
- }
复制代码
类的对象若在其他函数中是不可以访问类的 protect private类型变量
|
-
-
上一篇: c语言循环链表下一篇: 寻找一个数据库驱动类库
|