c++ ASCII强度游戏不会计算“Bot”单词值

c0vxltue  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(113)

我在做一个游戏来测试用户和机器人的ASCII强度。(也有一个双人模式,但运行良好。)完整的描述在我的.cpp文件的顶部。作为一个基本的分解,机器人打开一个包含500个常见的四个字母单词的txt文件,并将它们插入一个大小为500的数组。然后随机生成一个数字,以选择一个随机的。然后经过tovalue()的过程来接收它的ASCII值,其中在tovalue()中运行chartoint()四次,一次对应单词的每个字符。我的问题是,程序计算用户生成的单词的ASCII值非常精确,但总是为botword返回0(0000),无论是什么单词。
我已经尝试了几次generateword的迭代()函数,包括使用向量,但总是得到相同的结果。我做了大量的挖掘,并没有找到任何解决方案,虽然我怀疑图表()函数可以更好地优化,只是不确定如何为这个特定的情况实现任何更好的解决方案。此外,我认为问题不在海图上(),因为它对用户输入工作正常,但我非常肯定问题出在generatewword上()、海图制作建议()会很有帮助,但这不是我现在的主要优先事项,因为我只需要程序首先100%工作。另外,我已经确认我的.txt文件中的所有单词都是大写字母,每行只有四个字符。

  1. // Write the code for a game called “ASCII Strength” of a four-letter word selected by Player 1
  2. // followed by a four-letter word selected by Player 2. The result would be the sum
  3. //of the ASCII value of each of the letters of the selected words and whoever has higher sum (called ASCII strength) wins.
  4. #include <iostream>
  5. #include <vector>
  6. #include <fstream>
  7. #include <string>
  8. #include <cstdlib>
  9. #include <ctime>
  10. using namespace std;;
  11. int chartoint(char a) {
  12. switch (a) {
  13. case 'A':
  14. return 1;
  15. break;
  16. case 'B':
  17. return 2;
  18. break;
  19. case 'C':
  20. return 3;
  21. break;
  22. case 'D':
  23. return 4;
  24. break;
  25. case 'E':
  26. return 5;
  27. break;
  28. case 'F':
  29. return 6;
  30. break;
  31. case 'G':
  32. return 7;
  33. break;
  34. case 'H':
  35. return 8;
  36. break;
  37. case 'I':
  38. return 9;
  39. break;
  40. case 'J':
  41. return 10;
  42. break;
  43. case 'K':
  44. return 11;
  45. break;
  46. case 'L':
  47. return 12;
  48. break;
  49. case 'M':
  50. return 13;
  51. break;
  52. case 'N':
  53. return 14;
  54. break;
  55. case 'O':
  56. return 15;
  57. break;
  58. case 'P':
  59. return 16;
  60. break;
  61. case 'Q':
  62. return 17;
  63. break;
  64. case 'R':
  65. return 18;
  66. break;
  67. case 'S':
  68. return 19;
  69. break;
  70. case 'T':
  71. return 20;
  72. break;
  73. case 'U':
  74. return 21;
  75. break;
  76. case 'V':
  77. return 22;
  78. break;
  79. case 'W':
  80. return 23;
  81. break;
  82. case 'X':
  83. return 24;
  84. break;
  85. case 'Y':
  86. return 25;
  87. break;
  88. case 'Z':
  89. return 26;
  90. break;
  91. }
  92. return 0;
  93. }
  94. int tovalue(string input) {
  95. int first = chartoint(input[0]);
  96. int second = chartoint(input[1]);
  97. int third = chartoint(input[2]);
  98. int fourth = chartoint(input[3]);
  99. cout << first << second << third << fourth; // EXISTS TO TEST CALCULATION
  100. int value = first + second + third + fourth;
  101. return value;
  102. }
  103. string generateword() {
  104. string arr[500];
  105. ifstream file("words.txt");
  106. if (file.is_open())
  107. {
  108. for (int i = 0; i < 500; i++) {
  109. string temp;
  110. getline(file, temp);
  111. arr[i] = temp;
  112. }
  113. file.close();
  114. }
  115. else
  116. {
  117. cout << "Error: Unable to open file.";
  118. exit(0);
  119. }
  120. srand(time(0));
  121. int random_index = rand() % 500;
  122. string random_word = arr[random_index];
  123. return random_word;
  124. }
  125. int main()
  126. {
  127. cout << "Welcome to ASCII strength, a game where the strongest word wins!";
  128. cout << "\nTo play, you must enter a four letter word. The program will calculate the 'ASCII strength' of your word and compare it to your opponent.";
  129. cout << "\nWhoever has the higher sum will win!";
  130. char another;
  131. another = 'y';
  132. while (another == 'y' || another == 'Y') {
  133. cout << "\nWould you like to play against a friend, or against a bot? (F/B)";
  134. char mode;
  135. cin >> mode;
  136. if (mode == 'F' || mode == 'f') {
  137. cout << "\nPlayer 1, please input your four letter word in all caps: ";
  138. string answer1;
  139. cin >> answer1;
  140. int value1;
  141. value1 = tovalue(answer1);
  142. cout << "\nPlayer 2, please input your four letter word in all caps: ";
  143. string answer2;
  144. cin >> answer2;
  145. int value2;
  146. value2 = tovalue(answer2);
  147. if (value1 > value2) {
  148. cout << "\nPlayer 1 wins!";
  149. }
  150. else if (value2 > value1) {
  151. cout << "\nPlayer 2 wins!";
  152. }
  153. else if (value1 == value2) {
  154. cout << "\nTie!";
  155. }
  156. }
  157. else if (mode == 'B' || mode == 'b') {
  158. cout << "\nPlease input your four letter word in all caps: ";
  159. string answer;
  160. cin >> answer;
  161. int valueanswer;
  162. valueanswer = tovalue(answer);
  163. string botword;
  164. botword = generateword();
  165. cout << "\nThe bot generates a random word based on a list of popular four letter words.";
  166. cout << "\nThe bot has generated this word: " << botword;
  167. int valuebot;
  168. valuebot = tovalue("botword");
  169. cout << valueanswer << " " << valuebot; // THIS EXISTS PURELY TO TEST WHETHER THE VALUES ARE PROPERLY CALCULATING
  170. if (valueanswer > valuebot) {
  171. cout << "\nYou win!";
  172. }
  173. else if (valuebot > valueanswer) {
  174. cout << "\nThe bot wins!";
  175. }
  176. else if (valueanswer == valuebot) {
  177. cout << "\nTie!";
  178. }
  179. }
  180. cout << "\nWould you like to start a new game? (y/n)";
  181. cin >> another;
  182. }
  183. }
hxzsmxv2

hxzsmxv21#

您的问题是这一行:

  1. valuebot = tovalue("botword");

因为“botword”中的所有字符都是小写,所以你得0分。你可能是想写

  1. valuebot = tovalue(botword);

相关问题