VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 725|回复: 3

[已解决]这是一段不知道报错原因的问题的代码。。

[复制链接]
56_avatar_middle
最佳答案
0 
在线会员 发表于 2020-4-18 22:08:33 | 显示全部楼层 |阅读模式
@JLDawson
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<windows.h>
  4. #include<stdio.h>
  5. #include<ctime>
  6. #include <stdlib.h>
  7. #include<fstream>
  8. #include<istream>
  9. #include<ostream>
  10. #include<time.h>
  11. #include<string>
  12. #include<conio.h>
  13. #define High 18//高
  14. #define Wide 18//宽
  15. #define Mine 40//雷的数量

  16. HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量,
  17.                                                                                         //并且这个只能在每个头文件中单独定义句柄和函数,否则无效
  18. using namespace std;
  19. class LM          //地雷类      //Landmine 地雷
  20. {
  21. public:
  22.         LM();//构造函数
  23.         void Judge();//判断是否死亡函数
  24.         void Select();//接受键盘指令表示现在选择的位置
  25.         static int x;//现在处于的横坐标
  26.         static int y;//现在处于的纵坐标
  27.         static int Leftover; //表示未被标记的雷的数量
  28. private:
  29.         bool flag;//标志位,表示此位置是否有雷,有雷则为ture,无雷则为false
  30.         int Nome;//Numofmine 表示以此位置为中心的九宫格范围内有多少个雷
  31.         bool Openor;//标志位,标志此位置是否开了
  32.         bool showit;//标志位,表示是否需要输出
  33.         bool Inflag;//判断某位置是否插旗


  34. };
  35. class User //玩家类
  36. {
  37.         friend class LM;
  38. public:
  39.         void Showgame();//展示函数 (地图)
  40.         void Init();//数据初始化
  41.         void RAnd();//生成地雷位置
  42.         void Search();//查找函数
  43.         void Bsearch(int x, int y);//判断哪些数字需要输出
  44.         void Inflagor();//插旗函数
  45. private:


  46. };

  47. //定义变量
  48. bool win = false; //判断是否胜利
  49. bool die = false;//判断是否死亡
  50. time_t second;//定义时间变量
  51. int LM::x = 0;//横坐标
  52. int LM::y = 0;  //纵坐标
  53. int LM::Leftover = Mine;//将可插旗数等于雷数
  54. User u1;
  55. LM G[High][Wide];//游戏面板大小为max*max,每一位数组代表一个格子
  56. int Numoffg = 0;//插旗的个数
  57. long long time1;//开始游戏的时间
  58. long long time2;//通关时间
  59. int t;
  60. string yourname;
  61. string s[100];
  62. //申明类外函数
  63. void coutfile();
  64. void Welcome();
  65. void file();
  66. LM::LM()
  67. {
  68.         //作用是生成对象数组
  69. }
  70. void User::Init()//初始化
  71. {
  72.         for (int i = 0; i < High; i++)
  73.         {
  74.                 for (int j = 0; j < Wide; j++)
  75.                 {
  76.                         G[i][j].flag = false;
  77.                         //G[i][j].flag=false;
  78.                         G[i][j].Nome = 0;
  79.                         G[i][j].Openor = false;
  80.                         G[i][j].showit = false;
  81.                         G[i][j].Inflag = false;
  82.                 }
  83.         }
  84. }
  85. void User::Showgame()//展示函数,在一开始展示出游戏面板,在每一次用户进行一次翻牌后进行一次新的输出
  86. {

  87.         for (int i = 0; i < High; i++)
  88.         {
  89.                 for (int j = 0; j < Wide; j++)
  90.                 {
  91.                         if (i == LM::x && j == LM::y)
  92.                                 cout << "ME";
  93.                         else if (G[i][j].Openor == true)//当点击到地雷数不为0的位置时
  94.                         {
  95.                                 if (G[i][j].Nome == 1) cout << "①";
  96.                                 if (G[i][j].Nome == 2) cout << "②";
  97.                                 if (G[i][j].Nome == 3) cout << "③";
  98.                                 if (G[i][j].Nome == 4) cout << "④";
  99.                                 if (G[i][j].Nome == 5) cout << "⑤";
  100.                                 if (G[i][j].Nome == 6) cout << "⑥";
  101.                                 if (G[i][j].Nome == 7) cout << " 7";
  102.                                 if (G[i][j].Nome == 8) cout << "⑧";

  103.                         }
  104.                         else if (G[i][j].Inflag == true)//当在某位置插旗
  105.                         {
  106.                                 cout << "旗";

  107.                         }
  108.                         else if (G[i][j].showit == true) //当点击到地雷数为0的位置时
  109.                         {
  110.                                 if (G[i][j].showit == true && G[i][j].Nome == 0)
  111.                                         cout << "  ";
  112.                                 if (G[i][j].showit == true && G[i][j].Nome != 0)
  113.                                 {
  114.                                         if (G[i][j].flag == true && G[i][j].Nome == 1) cout << "█";
  115.                                         else if (G[i][j].Nome == 1) cout << "①";
  116.                                         else if (G[i][j].Nome == 2) cout << "②";
  117.                                         else if (G[i][j].Nome == 3) cout << "③";
  118.                                         else if (G[i][j].Nome == 4) cout << "④";
  119.                                         else if (G[i][j].Nome == 5) cout << "⑤";
  120.                                         else if (G[i][j].Nome == 6) cout << "⑥";
  121.                                         else if (G[i][j].Nome == 7) cout << " 7";
  122.                                         else if (G[i][j].Nome == 8) cout << "⑧";
  123.                                 }
  124.                         }

  125.                         else
  126.                                 cout << "█";

  127.                 }
  128.                 cout << endl;

  129.         }


  130. }
  131. void  User::Search()//通过此函数查找计算出此位置的九宫格中有多少个雷
  132. {
  133.         for (int i = 0; i < High; i++)
  134.         {
  135.                 for (int j = 0; j < Wide; j++)
  136.                 {
  137.                         if ((i - 1 >= 0) && (j - 1 >= 0) && (G[i - 1][j - 1].flag == true))  G[i][j].Nome++;//有些地方的判断是有点多余的,但是为了代码好看点嘛
  138.                         if ((i - 1 >= 0) && (j + 0 >= 0) && (G[i - 1][j].flag == true))    G[i][j].Nome++;
  139.                         if ((i - 1 >= 0) && (j + 1 < Wide) && (G[i - 1][j + 1].flag == true)) G[i][j].Nome++;
  140.                         if ((i + 0 >= 0) && (j - 1 >= 0) && (G[i][j - 1].flag == true))           G[i][j].Nome++;
  141.                         if ((i + 0 >= 0) && (j + 0 >= 0) && (G[i][j].flag == true))      G[i][j].Nome++;
  142.                         if ((i + 0 >= 0) && (j + 1 < Wide) && (G[i][j + 1].flag == true))  G[i][j].Nome++;
  143.                         if ((i + 1 < High) && (j - 1 >= 0) && (G[i + 1][j - 1].flag == true)) G[i][j].Nome++;
  144.                         if ((i + 1 < High) && (j + 0 >= 0) && (G[i + 1][j].flag == true))   G[i][j].Nome++;
  145.                         if ((i + 1 < High) && (j + 1 < Wide) && (G[i + 1][j + 1].flag == true))G[i][j].Nome++;
  146.                 }
  147.         }
  148. }

  149. void LM::Select()//键盘操作表示选择的位置 ,在玩家选择完成后调用判断函数并且输出新的游戏界面
  150. {
  151.         char ch = _getch();
  152.         if (ch == 'w' && LM::x - 1 >= 0)        LM::x--;
  153.         if (ch == 's' && LM::x + 1 < High)        LM::x++;
  154.         if (ch == 'a' && LM::y - 1 >= 0)        LM::y--;
  155.         if (ch == 'd' && LM::y + 1 < Wide) LM::y++;
  156.         if (ch == 13) Judge();
  157.         if (ch == 32) u1.Inflagor();

  158. }
  159. void User::RAnd()//生成地雷的位置
  160. {
  161.         srand(time(NULL));
  162.         for (int i = 0; i < Mine; i++)
  163.         {

  164.                 int rand2 = rand() % High;
  165.                 int rand1 = rand() % Wide;
  166.                 G[rand1][rand2].flag = true;
  167.         }
  168. }
  169. void User::Bsearch(int i, int j)//判断需要输出多少个数
  170. {
  171.         //int i,j;
  172.         //i=x;j=y;

  173.         G[i][j].showit = true;
  174.         for (int a = i - 1; a <= i + 1; a++)
  175.         {
  176.                 for (int b = j - 1; b <= j + 1; b++)
  177.                 {

  178.                         if ((a >= 0 && a <= High && b >= 0 && b <= Wide) && G[a][b].Nome == 0)
  179.                         {

  180.                                 if (a == i && b == j)
  181.                                         G[a][b].showit = true;
  182.                                 else if (G[a][b].showit == true)
  183.                                 {

  184.                                 }
  185.                                 else if (G[a][b].showit != true)
  186.                                 {
  187.                                         G[a][b].showit = true;
  188.                                         Bsearch(a, b);
  189.                                 }

  190.                         }
  191.                         else if ((a >= 0 && a <= High && b >= 0 && b <= Wide) && G[a][b].Nome != 0)
  192.                                 G[a][b].showit = true;
  193.                 }

  194.         }
  195. }
  196. //光标定位函数
  197. void cursor(int x, int y)
  198. {
  199.         COORD c;
  200.         c.X = x;
  201.         c.Y = y;
  202.         SetConsoleCursorPosition(hout, c); //设置控制台光标的位置
  203. }

  204. void hide_cursor()//隐藏光标         这我也看不懂啊,反正拿来用嘛
  205. {
  206.         HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
  207.         CONSOLE_CURSOR_INFO cursor_info;
  208.         GetConsoleCursorInfo(h_GAME, &cursor_info);
  209.         cursor_info.bVisible = false;
  210.         SetConsoleCursorInfo(h_GAME, &cursor_info);
  211. }
  212. void Menu()//主菜单
  213. {
  214.         //                time1=Time();
  215.         second = time(NULL);//获取开始游戏的时间
  216.         time1 = second;
  217.         hide_cursor();

  218.         u1.Init();
  219.         u1.RAnd();
  220.         u1.Search();
  221.         u1.Showgame();
  222.         while (1)
  223.         {
  224.                 hide_cursor();
  225.                 while (_kbhit())
  226.                 {
  227.                         G[LM::x][LM::y].Select();
  228.                         cursor(0, 0);
  229.                         if (die == true)
  230.                         {

  231.                                 system("cls");
  232.                                 char ch = 7;
  233.                                 cout << ch << endl; cout << "\t\t\t***********************************************" << endl;
  234.                                 cout << "\t\t\t***********************************************" << endl;
  235.                                 cout << "\t\t\t                                               " << endl;
  236.                                 cout << "\t\t\t                臭弟弟,你踩到雷了!                   " << endl;
  237.                                 cout << "\t\t\t                5秒后自动返回主菜单                             " << endl;
  238.                                 cout << "\t\t\t                                               " << endl;
  239.                                 cout << "\t\t\t***********************************************" << endl;
  240.                                 cout << "\t\t\t***********************************************" << endl;
  241.                                 die = false;
  242.                                 Sleep(5000);
  243.                                 system("cls");
  244.                                 Welcome();
  245.                                 //Welcome();
  246.                         }
  247.                         else if (win == true)
  248.                         {
  249.                                 system("cls");
  250.                                 win = false;

  251.                                 Welcome();
  252.                         }
  253.                         else
  254.                         {
  255.                                 u1.Showgame();
  256.                         }

  257.                 }
  258.         }
  259. }
  260. void LM::Judge()//通过查看此位置对象的标志位,判断是否翻开了雷
  261. {
  262.         if (G[LM::x][LM::y].flag == true)
  263.         {
  264.                 die = true;

  265.         }
  266.         if ((G[LM::x][LM::y].flag == false) && (G[LM::x][LM::y].Nome != 0))//当没有踩到雷,并且当前位置数字不为0
  267.         {
  268.                 G[LM::x][LM::y].Openor = true;

  269.         }
  270.         if ((G[LM::x][LM::y].flag == false) && (G[LM::x][LM::y].Nome == 0))//当没有踩到雷,并且当前位置数字为0
  271.         {
  272.                 u1.Bsearch(LM::x, LM::y);

  273.         }
  274. }
  275. void User::Inflagor()//插旗函数
  276. {

  277.         if (G[LM::x][LM::y].Inflag == false)
  278.         {
  279.                 G[LM::x][LM::y].Inflag = true;
  280.                 if (G[LM::x][LM::y].flag == true)
  281.                         Numoffg++;//插了旗的雷 +1
  282.         }

  283.         else if (G[LM::x][LM::y].Inflag == true)
  284.         {
  285.                 G[LM::x][LM::y].Inflag = false;
  286.                 if (G[LM::x][LM::y].flag == true)
  287.                         Numoffg--;//插了旗的雷 -1
  288.         }
  289.         else
  290.                 ;

  291.         if (G[LM::x][LM::y].flag == true && G[LM::x][LM::y].Inflag == true)
  292.                 LM::Leftover--;
  293.         else if (G[LM::x][LM::y].flag == true && G[LM::x][LM::y].Inflag == false)
  294.                 LM::Leftover++;
  295.         else if (G[LM::x][LM::y].flag == false && G[LM::x][LM::y].Inflag == true)
  296.                 LM::Leftover--;
  297.         else if (G[LM::x][LM::y].flag == false && G[LM::x][LM::y].Inflag == false)
  298.                 LM::Leftover++;
  299.         else
  300.                 ;


  301.         if (LM::Leftover == 0 && Mine == Numoffg)
  302.         {


  303.                 system("cls");
  304.                 win = true;
  305.                 second = time(NULL);
  306.                 time2 = second;
  307.                 t = time2 - time1;
  308.                 file();
  309.                 cout << "\t\t\t***********************************************" << endl;
  310.                 cout << "\t\t\t***********************************************" << endl;
  311.                 cout << "\t\t\t                                               " << endl;
  312.                 cout << "\t\t\t                恭喜你赢了!                   " << endl;
  313.                 cout << "\t\t\t             消耗时间:" << t / 60 << "分钟" << t % 60 << "秒   " << endl;
  314.                 cout << "\t\t\t               5秒后返回主菜单                        " << endl;
  315.                 cout << "\t\t\t                                               " << endl;
  316.                 cout << "\t\t\t***********************************************" << endl;
  317.                 cout << "\t\t\t***********************************************" << endl;
  318.                 Numoffg = 0;
  319.                 LM::Leftover = Mine;
  320.                 Sleep(5000);
  321.                 char ch[90];
  322.                 itoa(t, ch, 10);
  323.                 fp = fopen("123.txt", "a+");
  324.                 fputs(ch, fp);
  325.                 fprintf(fp, "\n");
  326.                 fclose(fp);

  327.         }
  328.         else;
  329. }

  330. int Time()//计时函数
  331. {

  332.         second = time(NULL);
  333.         return second;
  334. }
  335. void Welcome()
  336. {

  337.         int n = 0;

  338.         while (1)
  339.         {
  340.                 cout << "\n\n\n\n\n\t\t\t\t\t欢迎进入扫雷游戏" << endl;
  341.                 cout << "\t\t\t****************************************************" << endl;
  342.                 cout << "\t\t\t*                                                  *" << endl;
  343.                 cout << "\t\t\t*             请根据提示选择你想要的功能           *" << endl;
  344.                 cout << "\t\t\t*                                                  *" << endl;
  345.                 cout << "\t\t\t*             1.开始游戏                           *" << endl;
  346.                 cout << "\t\t\t*             2.游戏规则                               *" << endl;
  347.                 cout << "\t\t\t*             3.退出游戏                           *" << endl;
  348.                 cout << "\t\t\t*                                                   *" << endl;
  349.                 cout << "\t\t\t*                                                      *" << endl;
  350.                 cout << "\t\t\t*                                                  *" << endl;
  351.                 cout << "\t\t\t*                                                  *" << endl;
  352.                 cout << "\t\t\t*                                                  *" << endl;
  353.                 cout << "\t\t\t****************************************************" << endl;
  354.                 cin >> n;
  355.                 switch (n)
  356.                 {
  357.                 case 1:
  358.                         system("cls");
  359.                         cout << "请输入你的游戏昵称:" << endl;
  360.                         cin >> yourname;
  361.                         system("cls");
  362.                         Menu();
  363.                         break;
  364.                 case 2:
  365.                         system("cls");
  366.                         cout << "\n\n\n\n\n\t\t\t\t\t欢迎进入扫雷游戏" << endl;
  367.                         cout << "\t\t\t****************************************************" << endl;
  368.                         cout << "\t\t\t*                                                  *" << endl;
  369.                         cout << "\t\t\t*                                                  *" << endl;
  370.                         cout << "\t\t\t*                                                  *" << endl;
  371.                         cout << "\t\t\t*   W、A、S、D分别控制上下左右                     *" << endl;
  372.                         cout << "\t\t\t*   Enter进行选择,空格代表插旗                        *" << endl;
  373.                         cout << "\t\t\t*   当插旗数等于总雷数即为胜利,若翻到雷则结束游戏 *" << endl;
  374.                         cout << "\t\t\t*                                                        *" << endl;
  375.                         cout << "\t\t\t*                                                   *" << endl;
  376.                         cout << "\t\t\t*                                                      *" << endl;
  377.                         cout << "\t\t\t*                                                  *" << endl;
  378.                         cout << "\t\t\t*                                                  *" << endl;
  379.                         cout << "\t\t\t*                                                  *" << endl;
  380.                         cout << "\t\t\t****************************************************" << endl;
  381.                         //                                cout<<"                                    W、A、S、D分别控制上下左右,Enter进行选择,空格代表插旗"<<endl;
  382.                         //                                cout<<"                                    当插旗数等于总雷数即为胜利,若翻到雷则结束游戏"<<endl;
  383.                         system("pause");
  384.                         break;
  385.                 case 3:
  386.                         cout << "                                     拜拜您嘞" << endl;
  387.                         return;
  388.                         break;
  389.                 default: cout << "没有这个功能" << endl;
  390.                         Sleep(3000);
  391.                         break;
  392.                         exit(0);

  393.                 }
  394.                 system("cls");
  395.         }
  396. }
  397. void file()
  398. {
  399.         ofstream ofs;
  400.         //打开文件
  401.         ofs.open("E:\\c++语言文件\\大作业\\numofuuser.txt", ios::app);

  402.         string ch = "abc";
  403.         if (ofs)  //判断文件是否打开成功
  404.         {

  405.                 ofs << yourname << " ";
  406.                 ofs << t << endl; //直接调用<<运算符进行写入
  407.                 cout << "write success" << endl;
  408.         }
  409.         else
  410.                 cout << "write failed" << endl;
  411.         ofs.close();
  412. }
复制代码



最佳答案
57_avatar_small
2020-4-19 18:42:20
userabao 发表于 2020-4-19 15:48
LM类的对象不能使用自己类的private成员吗?

类的对象若在其他函数中是不可以访问类的 protect  private类型变量
J7]O$F5WJDFYFHV9VVT8[0O.png
M(}ZHXC]3GGHPG8M5YTJT_T.png




上一篇:c语言循环链表
下一篇:寻找一个数据库驱动类库
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-19 12:15:39 | 显示全部楼层
LM类中的成员变量为private,私有成员。无法直接访问。了解一下控制修饰符

评分

参与人数 1威望 +2 驿站币 +2 热心值 +2 收起 理由
51_avatar_small Syc + 2 + 2 + 2 赞一个!

查看全部评分

56_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-4-19 15:48:39 | 显示全部楼层
JLDawson 发表于 2020-4-19 12:15
LM类中的成员变量为private,私有成员。无法直接访问。了解一下控制修饰符

LM类的对象不能使用自己类的private成员吗?
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-19 18:42:20 | 显示全部楼层    本楼为最佳答案   
bestAnswer
userabao 发表于 2020-4-19 15:48
LM类的对象不能使用自己类的private成员吗?

类的对象若在其他函数中是不可以访问类的 protect  private类型变量
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-5-30 00:07

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表