• C++ Among Us 太空狼人杀(上半部分)


    出于字数原因,暂时无法在一个文章里写完,所以分了上下两半部分

    目录

    基础设施

    主函数

    void Load()

    void ScreenUpdate()

    函数主体

    试运行

    运行结果

    void MobUpdate()

    打酱油?!

    只是你打酱油而已,我才不打

    void TryMove

    初级回顾


    基础设施

    首先,是最普通的头文件,分别是:

    1. #include
    2. //万能头文件
    3. //注意:剩下的两个头文件不是在万能头文件里的!
    4. #include
    5. //检测键盘输入
    6. #include
    7. //主要为了Sleep(int dilliseconds)函数

    接下来,我们需要一个叫world的二维数组,来储存地图(建议开到world[20][40],出于字符长是宽的两倍的原因,所以第二个下标最好是第一个下标的2倍)

    然后为了方便需要一个叫PlayerCnt的常量,来寄存一共有多少个人(因为后续可能会做调整)

    定义完PlayerCnt之后,就可以分别定义:

    1. int PlayerCnt=10;//推荐10个人
    2. bool Alive[PlayerCnt];//
    3. bool Imposter[Playercnt];//
    4. char Source[PlayerCnt];//分别记录每个玩家的形象,后面会说
    5. int MobX[PlayerCnt];//X坐标
    6. int MobY[PlayerCnt];//Y坐标
    7. bool Killing;//后面有用
    8. int CrewLeft,ImposterLeft;//分别代表剩余船员、剩余内鬼
    9. bool Restart=0,Reported;//在后面有用
    10. string str;//在后面有用

    注意:没提到const的数据千万别作死在前面加个const

    主函数

    主函数也是很简单的,先上代码:

    1. int main()
    2. {
    3. //{'M','%','@','V','H','A','S','D','W','O'} 这个是废弃的样子
    4. Restart=0;
    5. Reported=0;
    6. Source[0]='1';Source[1]='2';Source[2]='3';Source[3]='4';Source[4]='5';Source[5]='6';Source[6]='7';Source[7]='8';Source[8]='9';Source[9]='O';
    7. //重置样子(不能忽略)
    8. //别把它“优化”成了for(int i=0;i
    9. //然后也千万别把char改成int
    10. system("cls");
    11. played=1;//没啥用,但是如果要做测试的话可以用
    12. load();//
    13. while(1)
    14. {
    15. ScreenUpdate();//最重要的函数
    16. if(Restart==1)//判断有没有重置游戏
    17. {
    18. system("cls");
    19. cout<<"Creating new game..."<"Press any key to continue"<
    20. load();
    21. getch();
    22. main();//主函数调用
    23. }
    24. }
    25. }

    void Load()

    这是一个无返回值的函数,所以别手欠在后面补上return 0;

    然后这个函数的目的是初始化游戏,因为后面有个递归调用main()

    代码就不写了,因为真的太太太太太太太太简单了,只是需要提醒几点:

    1. X坐标及Y坐标的重置(别超出20*40)

    2. Imposter数组的重置(将三个下标设为1)

    3.Alive数组的重置(将所有都设为1)

    4.地图要加边框

    void ScreenUpdate()

    终于,我们到了最重要的部分了!

    函数主体

    这个函数我就直接公开代码吧,因为它就是一堆别的函数的集合

    1. void ScreenUpdate()
    2. {
    3. MobUpdate();//另外一个套了很多函数的函数
    4. system("cls");//清屏
    5. bool x=0;
    6. //嵌套循环输出整个世界
    7. for(int i=0;i<20;i++)//world[ 20 ][40]
    8. {
    9. for(int j=0;j<40;j++)//world[20][ 40 ]
    10. {
    11. //cout<
    12. x=0;
    13. //检测这个地方有没有玩家,如果有,则输出形象,没有的话就正常输出
    14. for(int k=0;k
    15. {
    16. if(MobX[k]==j&&19-MobY[k]==i)
    17. {
    18. x=1;
    19. cout<
    20. break;
    21. }
    22. }
    23. if(x==0)cout<
    24. }
    25. cout<
    26. }
    27. CheckAlive();//检查状况
    28. //这里是来提示玩家现在状况的,自己看它们代表什么 注:\t 代表 tab
    29. cout<<"Crews left: "<"\tImposters left: "<
    30. if(Evil[9]==1)cout<<"You're the imposter No.10"<
    31. else cout<<"You're the crew No.10"<
    32. if(Alive[9]==0)
    33. {
    34. char key;
    35. cout<<"You're Dead"<"Press space bar to restart";
    36. }else if(Reported==0)cout<<"You can Press R to report once"<
    37. Sleep(200);
    38. }

    试运行

    好了,现在就差不多可以试运行了,这里提供了一个简介的代码(也就是删掉了没定义的函数避免报错)来运行:

    1. #include
    2. using namespace std;
    3. char world[20][40];
    4. const int PlayerCnt=10;
    5. char Source[PlayerCnt]={'M','%','@','V','H','A','S','D','W','O'};
    6. int MobX[PlayerCnt];
    7. int MobY[PlayerCnt];
    8. bool Alive[PlayerCnt];
    9. bool Evil[PlayerCnt];
    10. bool played=0,Killing;
    11. int CrewLeft,ImposterLeft;
    12. bool Restart=0,Reported;
    13. string str;
    14. void load()
    15. {
    16. srand(time(0));
    17. for(int i=0;i<20;i++)
    18. {
    19. for(int j=0;j<40;j++)
    20. {
    21. if(i==0||i==19||j==0||j==39)
    22. {
    23. world[i][j]='#';
    24. }else world[i][j]=' ';
    25. }
    26. }
    27. for(int i=0;i
    28. {
    29. Evil[i]=0;
    30. Alive[i]=1;
    31. MobX[i]=rand()%36+1;
    32. MobY[i]=rand()%16+1;
    33. }
    34. for(int i=0;i<3;i++)Evil[rand()%PlayerCnt]=1;
    35. }
    36. void ScreenUpdate()
    37. {
    38. system("cls");
    39. bool x=0;
    40. for(int i=0;i<20;i++)
    41. {
    42. for(int j=0;j<40;j++)
    43. {
    44. //cout<
    45. x=0;
    46. for(int k=0;k
    47. {
    48. if(MobX[k]==j&&19-MobY[k]==i)
    49. {
    50. x=1;
    51. cout<
    52. break;
    53. }
    54. }
    55. if(x==0)cout<
    56. }
    57. cout<
    58. }
    59. cout<<"Crews left: "<"\tImposters left: "<
    60. //这里的两个变量都会是0因为还没定义CheckAlive();
    61. if(Evil[9]==1)cout<<"You're the imposter No.10"<
    62. else cout<<"You're the crew No.10"<
    63. if(Alive[9]==0)
    64. {
    65. char key;
    66. cout<<"You're Dead"<"Press space bar to restart";
    67. }else if(Reported==0)cout<<"You can Press R to report once"<
    68. Sleep(200);
    69. }
    70. int main()
    71. {
    72. //{'M','%','@','V','H','A','S','D','W','O'}
    73. Restart=0;
    74. Reported=0;
    75. Source[0]='1';Source[1]='2';Source[2]='3';Source[3]='4';Source[4]='5';Source[5]='6';Source[6]='7';Source[7]='8';Source[8]='9';Source[9]='O';
    76. system("cls");
    77. played=1;
    78. load();
    79. while(1)
    80. {
    81. ScreenUpdate();
    82. if(Restart==1)
    83. {
    84. system("cls");
    85. cout<<"Creating new game..."<"Press any key to continue"<
    86. load();
    87. getch();
    88. main();
    89. }
    90. }
    91. }

    运行结果

    很好!

    我们可以看见已经会有一个基础的场景了,后续会对这个场景进行优化来添加很多装饰!

    void MobUpdate()

    注意看!这个函数也很重要,因为它的编写决定了玩家的移动

    上——代——码——

    1. void MobUpdate()
    2. {
    3. TryMove();//控制玩家的移动
    4. srand(time(0));
    5. //船员报告
    6. if(rand()%30==0)
    7. {
    8. Report(rand()%10+1);
    9. }
    10. //拿循环来控制每个人的移动(后续会优化)
    11. for(int i=0;i-1;i++)
    12. {
    13. if(Alive[i]==1)
    14. {
    15. //我爱随机数~
    16. if(rand()%3==0)MobX[i]+=rand()%3-1;
    17. if(rand()%3==0)MobY[i]+=rand()%3-1;
    18. }
    19. //防出界
    20. if(MobX[i]<1)MobX[i]=1;
    21. if(MobX[i]>38)MobX[i]=38;
    22. if(MobY[i]<1)MobY[i]=1;
    23. if(MobY[i]>18)MobY[i]=18;
    24. }
    25. //两个void
    26. Kill();
    27. BodyReport();
    28. }

    打酱油?!

    然后如果不介意的话顺便介绍一个基本上啥用都没void CheckAlive()

    这个代码我就不说了,看就能明白

    1. void CheckAlive()
    2. {
    3. CrewLeft=0;
    4. for(int i=0;i<10;i++)
    5. {
    6. if(Source[i]!='X')CrewLeft++;
    7. }
    8. ImposterLeft=0;
    9. for(int i=0;i<10;i++)
    10. {
    11. if(Evil[i]==1&&Source[i]!='X')ImposterLeft++;
    12. }
    13. }

    只是你打酱油而已,我才不打

    介绍完“没用”的东西之后,是时候介绍“有用”得了

    我们就来分解一下MobUpdate()这个东西

    void TryMove

    Wow!我们终于可以移动了!

    还不赶紧上代码?

    1. void TryMove()
    2. {
    3. char key;//这就是为啥要
    4. key=0;//重置按键
    5. Killing=0;
    6. if(_kbhit()&&Alive[9]==1)//检测输入
    7. {
    8. key=getch();
    9. if(key=='w'&&MobY[PlayerCnt-1]<18)MobY[PlayerCnt-1]++;
    10. if(key=='s'&&MobY[PlayerCnt-1]>1)MobY[PlayerCnt-1]--;
    11. if(key=='a'&&MobX[PlayerCnt-1]>1)MobX[PlayerCnt-1]--;
    12. if(key=='d'&&MobX[PlayerCnt-1]<38)MobX[PlayerCnt-1]++;
    13. if(key=='r'&&Reported==0)Report(10);
    14. if(key==' '&&Alive[9]==0)Restart=1;
    15. }
    16. }

    R和空格键我们后面再说

    初级回顾

    我们来回顾一下之前的代码来重新明确每个函数的用途:

    首先,我们定义了变量

    其次,我们定义了ScreenUpdate来控制屏幕输出

    然后我们写了MobUpdate来使角色可以移动

    然后我们整了一个打酱油的函数拿来统计生还人数和内鬼剩余数量

    接下来又是快乐的试运行!

    1. #include
    2. using namespace std;
    3. char world[20][40];
    4. const int PlayerCnt=10;
    5. char Source[PlayerCnt]={'M','%','@','V','H','A','S','D','W','O'};
    6. int MobX[PlayerCnt];
    7. int MobY[PlayerCnt];
    8. bool Alive[PlayerCnt];
    9. bool Evil[PlayerCnt];
    10. bool played=0,Killing;
    11. int CrewLeft,ImposterLeft;
    12. bool Restart=0,Reported;
    13. string str;
    14. void load()
    15. {
    16. srand(time(0));
    17. for(int i=0;i<20;i++)
    18. {
    19. for(int j=0;j<40;j++)
    20. {
    21. if(i==0||i==19||j==0||j==39)
    22. {
    23. world[i][j]='#';
    24. }else world[i][j]=' ';
    25. }
    26. }
    27. for(int i=0;i
    28. {
    29. Evil[i]=0;
    30. Alive[i]=1;
    31. MobX[i]=rand()%36+1;
    32. MobY[i]=rand()%16+1;
    33. }
    34. for(int i=0;i<3;i++)Evil[rand()%PlayerCnt]=1;
    35. }
    36. void TryMove()
    37. {
    38. char key;
    39. key=0;
    40. Killing=0;
    41. if(_kbhit()&&Alive[9]==1)
    42. {
    43. key=getch();
    44. if(key=='w'&&MobY[PlayerCnt-1]<18)MobY[PlayerCnt-1]++;
    45. if(key=='s'&&MobY[PlayerCnt-1]>1)MobY[PlayerCnt-1]--;
    46. if(key=='a'&&MobX[PlayerCnt-1]>1)MobX[PlayerCnt-1]--;
    47. if(key=='d'&&MobX[PlayerCnt-1]<38)MobX[PlayerCnt-1]++;
    48. if(key=='r'&&Reported==0)//Report(10);
    49. if(key==' '&&Alive[9]==0)Restart=1;
    50. }
    51. }
    52. void MobUpdate()
    53. {
    54. TryMove();
    55. srand(time(0));
    56. for(int i=0;i-1;i++)
    57. {
    58. if(Alive[i]==1)
    59. {
    60. if(rand()%3==0)MobX[i]+=rand()%3-1;
    61. if(rand()%3==0)MobY[i]+=rand()%3-1;
    62. }
    63. if(MobX[i]<1)MobX[i]=1;
    64. if(MobX[i]>38)MobX[i]=38;
    65. if(MobY[i]<1)MobY[i]=1;
    66. if(MobY[i]>18)MobY[i]=18;
    67. }
    68. }
    69. void CheckAlive()
    70. {
    71. CrewLeft=0;
    72. for(int i=0;i<10;i++)
    73. {
    74. if(Source[i]!='X')CrewLeft++;
    75. }
    76. ImposterLeft=0;
    77. for(int i=0;i<10;i++)
    78. {
    79. if(Evil[i]==1&&Source[i]!='X')ImposterLeft++;
    80. }
    81. }
    82. void ScreenUpdate()
    83. {
    84. MobUpdate();
    85. system("cls");
    86. bool x=0;
    87. for(int i=0;i<20;i++)
    88. {
    89. for(int j=0;j<40;j++)
    90. {
    91. //cout<
    92. x=0;
    93. for(int k=0;k
    94. {
    95. if(MobX[k]==j&&19-MobY[k]==i)
    96. {
    97. x=1;
    98. cout<
    99. break;
    100. }
    101. }
    102. if(x==0)cout<
    103. }
    104. cout<
    105. }
    106. CheckAlive();
    107. cout<<"Crews left: "<"\tImposters left: "<
    108. if(Evil[9]==1)cout<<"You're the imposter No.10"<
    109. else cout<<"You're the crew No.10"<
    110. if(Alive[9]==0)
    111. {
    112. char key;
    113. cout<<"You're Dead"<"Press space bar to restart";
    114. }else if(Reported==0)cout<<"You can Press R to report once"<
    115. Sleep(200);
    116. }
    117. int main()
    118. {
    119. //{'M','%','@','V','H','A','S','D','W','O'}
    120. Restart=0;
    121. Reported=0;
    122. Source[0]='1';Source[1]='2';Source[2]='3';Source[3]='4';Source[4]='5';Source[5]='6';Source[6]='7';Source[7]='8';Source[8]='9';Source[9]='O';
    123. system("cls");
    124. played=1;
    125. load();
    126. //CrewLeft>3&&Evil[9]==1||ImposterLeft>0&&Evil[9]==0
    127. while(1)
    128. {
    129. ScreenUpdate();
    130. if(Restart==1)
    131. {
    132. system("cls");
    133. cout<<"Creating new game..."<"Press any key to continue"<
    134. load();
    135. getch();
    136. main();
    137. }
    138. }
    139. if(Evil[9]==1)cout<<"Imposter won!";
    140. else cout<<"crew won!";
    141. cout<"Press any key to restart"<
    142. getch();
    143. main();
    144. }

    代码已经长起来了!

    中级逻辑

    这是整个游戏最难的部分,一旦搞定这个,我们剩下的就很轻松了

    首先,当然是把之前MobUpdate的函数全部弄完

    这里我就推荐你们自己写,别抄样例代码了,要不然后面会很难看懂的

    我就告诉你们要写什么:

    1.void Kill()击杀

    2. void BodyReport()尸体报告

    3.void Report()紧急会议


    如果你真的写出来了,那么这里有一个样例代码:

    1. #include
    2. using namespace std;
    3. char world[20][40];
    4. const int PlayerCnt=10;
    5. char Source[PlayerCnt]={'M','%','@','V','H','A','S','D','W','O'};
    6. int MobX[PlayerCnt];
    7. int MobY[PlayerCnt];
    8. bool Alive[PlayerCnt];
    9. bool Evil[PlayerCnt];
    10. bool played=0,Killing;
    11. int CrewLeft,ImposterLeft;
    12. bool Restart=0,Reported;
    13. string str;
    14. void load()
    15. {
    16. srand(time(0));
    17. for(int i=0;i<20;i++)
    18. {
    19. for(int j=0;j<40;j++)
    20. {
    21. if(i==0||i==19||j==0||j==39)
    22. {
    23. world[i][j]='#';
    24. }else world[i][j]=' ';
    25. }
    26. }
    27. for(int i=0;i
    28. {
    29. Evil[i]=0;
    30. Alive[i]=1;
    31. MobX[i]=rand()%36+1;
    32. MobY[i]=rand()%16+1;
    33. }
    34. for(int i=0;i<3;i++)Evil[rand()%PlayerCnt]=1;
    35. }
    36. /************************
    37. Define functions here |
    38. ************************/
    39. /*void Mission()
    40. {
    41. for(int i=0;i<10;i++)
    42. {
    43. if(MobX[i]==)
    44. }
    45. }*/
    46. void Report(int num)
    47. {
    48. system("cls");
    49. cout<<"No."<" made a emergency meeting"<
    50. cout<<"Conversation is going to start"<
    51. cout<<"_______________________________"<
    52. srand(time(0));
    53. int cnt=rand()%10+3,r=rand()%20*100;
    54. for(int i=0;i
    55. {
    56. cout<<"No."<<rand()%9+1<<" : ";
    57. if(rand()%2==0)
    58. {
    59. if(rand()%2==0)
    60. {
    61. cout<<"Why someone called a report again?!"<
    62. }else
    63. {
    64. cout<<"Darn!"<
    65. }
    66. }
    67. else
    68. {
    69. if(rand()%2==0)
    70. {
    71. cout<<"He's lying!"<
    72. }
    73. else
    74. {
    75. cout<<"I think I know who's the imposter"<
    76. }
    77. }
    78. Sleep(r);
    79. r=rand()%20*100;
    80. }
    81. system("cls");
    82. cout<<"Voting"<
    83. cout<<"_________________________"<
    84. for(int i=0;i<9;i++)
    85. {
    86. cout<<"No."<1<<" has voted"<
    87. r=rand()%20*100;
    88. Sleep(r);
    89. }
    90. cin>>cnt;
    91. if(rand()%5==0)
    92. {
    93. cout<<"No."<" was thrown out of the ship"<
    94. Alive[cnt-1]=0;
    95. }else
    96. {
    97. cnt=rand()%10+1;
    98. cout<<"No."<" was thrown out of the ship"<
    99. Alive[cnt-1]=0;
    100. }
    101. Sleep(3000);
    102. }
    103. void TryMove()
    104. {
    105. char key;
    106. key=0;
    107. Killing=0;
    108. if(_kbhit()&&Alive[9]==1)
    109. {
    110. key=getch();
    111. if(key=='w'&&MobY[PlayerCnt-1]<18)MobY[PlayerCnt-1]++;
    112. if(key=='s'&&MobY[PlayerCnt-1]>1)MobY[PlayerCnt-1]--;
    113. if(key=='a'&&MobX[PlayerCnt-1]>1)MobX[PlayerCnt-1]--;
    114. if(key=='d'&&MobX[PlayerCnt-1]<38)MobX[PlayerCnt-1]++;
    115. if(key=='r'&&Reported==0)Report(10);
    116. if(key==' '&&Alive[9]==0)Restart=1;
    117. }
    118. }
    119. void Kill()
    120. {
    121. srand(time(0));
    122. for(int i=0;i<9;i++)
    123. {
    124. for(int j=i+1;j<10;j++)
    125. {
    126. if(MobX[i]==MobX[j]&&MobY[i]==MobY[j]&&rand()%2==0)
    127. {
    128. if(Evil[i]==1&&rand()%2==0&&Alive[i]==1)
    129. {
    130. Alive[j]=0;
    131. Source[j]='X';
    132. }
    133. if(Evil[j]==1&&Alive[j]==1)
    134. {
    135. if(j==10&&Killing==1)
    136. {
    137. Alive[i]=0;
    138. Source[i]='X';
    139. }else if(rand()%2==0)
    140. {
    141. Alive[i]=0;
    142. Source[i]='X';
    143. }
    144. }
    145. }
    146. }
    147. }
    148. if(Alive[9]==0)Source[9]='X';
    149. }
    150. void BodyReport()
    151. {
    152. srand(time(0));
    153. int cnt=rand()%10+3,r=rand()%20*100;
    154. for(int i=0;i<9;i++)
    155. {
    156. for(int j=i+1;j<10;j++)
    157. {
    158. if(MobX[j]==MobX[i]&&MobY[j]==MobY[i])
    159. {
    160. if(Source[i]=='X'&&Evil[j]==0)
    161. {
    162. Source[i]='+';
    163. system("cls");
    164. cout<<"No."<" found the dead body of No."<"!"<
    165. cout<<"Conversation is going to start"<
    166. int r=100;
    167. cout<<"____________________________________"<
    168. Sleep(1000);
    169. for(int i=0;i
    170. {
    171. cout<<"No."<<rand()%9+1<<" : ";
    172. if(rand()%2==0)
    173. {
    174. if(rand()%2==0)
    175. {
    176. cout<<"I'm not doing anything!";
    177. }
    178. else
    179. {
    180. if(rand()%2==0)
    181. {
    182. cout<<"What's our disicion?";
    183. }
    184. else
    185. {
    186. cout<<"I have no idea...";
    187. }
    188. }
    189. }
    190. else
    191. {
    192. if(rand()%2==0)
    193. {
    194. cout<<"I guess No."<<rand()%10+1<<" is the imposter!";
    195. }
    196. else
    197. {
    198. if(rand()%2==0)
    199. {
    200. cout<<"Vote No."<<rand()%10+1<<"! I saw him killing No."<<rand()%10+1;
    201. }
    202. else
    203. {
    204. cout<<"So which crew should we pick?";
    205. }
    206. }
    207. }
    208. // cout<<"";
    209. cout<
    210. Sleep(r);
    211. r=rand()%20*100;
    212. }
    213. cout<<"Your turn:"<
    214. str="1";
    215. getline(cin,str);
    216. if(rand()%2==0)
    217. {
    218. cout<<"No."<<rand()%10+1<<" : "<<"Hmmmm, I agree with you";
    219. }
    220. system("cls");
    221. cout<"Press any key to vote"<
    222. for(int i=0;i<9;i++)
    223. {
    224. cout<<"No."<1<<" has voted"<
    225. r=rand()%20*100;
    226. Sleep(r);
    227. }
    228. cin>>cnt;
    229. if(rand()%5==0)
    230. {
    231. cout<<"No."<" was thrown out of the ship"<
    232. Alive[cnt-1]=0;
    233. Source[i]='+';
    234. }else
    235. {
    236. cnt=rand()%10+1;
    237. cout<<"No."<" was thrown out of the ship"<
    238. Alive[cnt-1]=0;
    239. Source[i]='+';
    240. }
    241. //MobX[9]++;\]
    242. Sleep(3000);
    243. }
    244. if(Source[j]=='X'&&Evil[i]==0)
    245. {
    246. Source[j]='+';
    247. system("cls");
    248. cout<<"No."<" found the dead body of No."<"!"<
    249. cout<<"Conversation is going to start"<
    250. int r=100;
    251. cout<<"____________________________________"<
    252. Sleep(1000);
    253. for(int i=0;i
    254. {
    255. cout<<"No."<<rand()%9+1<<" : ";
    256. if(rand()%2==0)
    257. {
    258. if(rand()%2==0)
    259. {
    260. cout<<"I'm not doing anything!";
    261. }
    262. else
    263. {
    264. if(rand()%2==0)
    265. {
    266. cout<<"What's our disicion?";
    267. }
    268. else
    269. {
    270. cout<<"I have no idea...";
    271. }
    272. }
    273. }
    274. else
    275. {
    276. if(rand()%2==0)
    277. {
    278. cout<<"I guess No."<<rand()%10+1<<" is the imposter!";
    279. }
    280. else
    281. {
    282. if(rand()%2==0)
    283. {
    284. cout<<"Vote No."<<rand()%10+1<<"! I saw him killing No."<<rand()%10+1;
    285. }
    286. else
    287. {
    288. cout<<"So which crew should we pick?";
    289. }
    290. }
    291. }
    292. // cout<<"";
    293. cout<
    294. Sleep(r);
    295. r=rand()%20*100;
    296. }
    297. cout<<"Your turn:"<
    298. str="1";
    299. getline(cin,str);
    300. if(rand()%2==0)
    301. {
    302. cout<<"No."<<rand()%10+1<<" : "<<"Hmmmm, I agree with you";
    303. }
    304. system("cls");
    305. cout<"Press any key to vote"<
    306. for(int i=0;i<9;i++)
    307. {
    308. cout<<"No."<1<<" has voted"<
    309. r=rand()%20*100;
    310. Sleep(r);
    311. }
    312. cin>>cnt;
    313. if(rand()%5==0)
    314. {
    315. cout<<"No."<" was thrown out of the ship"<
    316. Alive[cnt-1]=0;
    317. Source[j]='+';
    318. }else
    319. {
    320. cnt=rand()%10+1;
    321. cout<<"No."<" was thrown out of the ship"<
    322. Alive[cnt-1]=0;
    323. Source[j]='+';
    324. }
    325. //MobX[9]++;\]
    326. Sleep(3000);
    327. }
    328. }
    329. }
    330. }
    331. }
    332. void MobUpdate()
    333. {
    334. TryMove();
    335. srand(time(0));
    336. if(rand()%30==0)
    337. {
    338. Report(rand()%10+1);
    339. }
    340. for(int i=0;i-1;i++)
    341. {
    342. if(Alive[i]==1)
    343. {
    344. if(rand()%3==0)MobX[i]+=rand()%3-1;
    345. if(rand()%3==0)MobY[i]+=rand()%3-1;
    346. }
    347. if(MobX[i]<1)MobX[i]=1;
    348. if(MobX[i]>38)MobX[i]=38;
    349. if(MobY[i]<1)MobY[i]=1;
    350. if(MobY[i]>18)MobY[i]=18;
    351. }
    352. Kill();
    353. BodyReport();
    354. }
    355. void CheckAlive()
    356. {
    357. CrewLeft=0;
    358. for(int i=0;i<10;i++)
    359. {
    360. if(Source[i]!='X')CrewLeft++;
    361. }
    362. ImposterLeft=0;
    363. for(int i=0;i<10;i++)
    364. {
    365. if(Evil[i]==1&&Source[i]!='X')ImposterLeft++;
    366. }
    367. }
    368. void ScreenUpdate()
    369. {
    370. MobUpdate();
    371. system("cls");
    372. bool x=0;
    373. for(int i=0;i<20;i++)
    374. {
    375. for(int j=0;j<40;j++)
    376. {
    377. //cout<
    378. x=0;
    379. for(int k=0;k
    380. {
    381. if(MobX[k]==j&&19-MobY[k]==i)
    382. {
    383. x=1;
    384. cout<
    385. break;
    386. }
    387. }
    388. if(x==0)cout<
    389. }
    390. cout<
    391. }
    392. CheckAlive();
    393. cout<<"Crews left: "<"\tImposters left: "<
    394. if(Evil[9]==1)cout<<"You're the imposter No.10"<
    395. else cout<<"You're the crew No.10"<
    396. if(Alive[9]==0)
    397. {
    398. char key;
    399. cout<<"You're Dead"<"Press space bar to restart";
    400. }else if(Reported==0)cout<<"You can Press R to report once"<
    401. Sleep(200);
    402. }
    403. int main()
    404. {
    405. //{'M','%','@','V','H','A','S','D','W','O'}
    406. Restart=0;
    407. Reported=0;
    408. Source[0]='1';Source[1]='2';Source[2]='3';Source[3]='4';Source[4]='5';Source[5]='6';Source[6]='7';Source[7]='8';Source[8]='9';Source[9]='O';
    409. system("cls");
    410. played=1;
    411. load();
    412. //CrewLeft>3&&Evil[9]==1||ImposterLeft>0&&Evil[9]==0
    413. while(1)
    414. {
    415. ScreenUpdate();
    416. if(Restart==1)
    417. {
    418. system("cls");
    419. cout<<"Creating new game..."<"Press any key to continue"<
    420. load();
    421. getch();
    422. main();
    423. }
    424. }
    425. if(Evil[9]==1)cout<<"Imposter won!";
    426. else cout<<"crew won!";
    427. cout<"Press any key to restart"<
    428. getch();
    429. main();
    430. }

    恭喜我们写出了434行的代码!

    但是出于本文章字数太多,现在已经打不了字了

    所以欲知后事如何,且听下回分解~

    注:现在我的电脑已经被这个14108个字的文档卡爆了!

  • 相关阅读:
    部署和使用dinky问题总结
    952. 按公因数计算最大组件大小 : 枚举质因数 + 并查集运用题
    走进Redis:哨兵集群
    Java 类和对象
    [Linux]进程概念
    docker报错问题解决:Error Invalid or corrupt jarfile app.jar
    【云原生】玩转docker实战(二):单机环境的容器编排工具docker-compose
    java毕业生设计原创网络文学管理系统计算机源码+系统+mysql+调试部署+lw
    ROSCAR试运行
    【QEMU系统分析之启动篇(十一)】
  • 原文地址:https://blog.csdn.net/BurningJames/article/details/132653171