game.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. const int L = 4, S = 10, maxt = 9, Size = 5040;
  6. //数字个数,数字范围[0,S),最多猜测次数,不同的答案个数
  7. const pair<int, int> error = make_pair(-1, -1);
  8. //错误猜测时范围的信息
  9. const pair<int, int> over = make_pair(-2, -2);
  10. //猜测次数已达最多猜测次数且未猜出答案,当局游戏自动结束
  11. string AIname = "Noname";
  12. class game {
  13. int P, Ps, T[maxt + 1];
  14. //已猜局数,总局数,T[i]表示i次猜出的局数,T[0]表示未猜出的局数
  15. int a[L], t;
  16. //当前局的数字,当前局已猜次数
  17. int time;
  18. //记录时间
  19. public:
  20. game()
  21. //初始化
  22. {
  23. P = t = 0;
  24. Ps = 1;
  25. for (int i = 0; i < L; ++i)
  26. Ps *= S - i;
  27. memset(T, 0, sizeof(T));
  28. for (int i = 0; i < L; ++i)
  29. a[i] = i;
  30. }
  31. pair<int, int> guess(int* b)
  32. //猜数字,b数组为所猜的L个数字,以pair<A,B>返回猜测结果
  33. {
  34. if (!P && !t)
  35. freopen((AIname + "_log.txt").c_str(), "w", stdout);
  36. if (!t) {
  37. cout << "Game " << P + 1 << " : ";
  38. for (int i = 0; i < L; ++i)
  39. cout << a[i] << " ";
  40. cout << endl;
  41. }
  42. ++t;
  43. int A = 0, B = 0;
  44. for (int i = 0; i < L; ++i)
  45. if (b[i] < 0 || b[i] > S) return error;
  46. for (int i = 0; i < L; ++i)
  47. for (int j = i + 1; j < L; ++j)
  48. if (b[i] == b[j]) return error;
  49. for (int i = 0; i < L; ++i)
  50. if (a[i] == b[i]) ++A;
  51. for (int i = 0; i < L; ++i)
  52. for (int j = 0; j < L; ++j)
  53. if (a[i] == b[j]) ++B;
  54. cout << "guess " << t << " : ";
  55. for (int i = 0; i < L; ++i)
  56. cout << b[i] << " ";
  57. cout << " " << A << "A" << B - A << "B" << endl;
  58. if (A == L || t == maxt) {
  59. if (A == L) {
  60. ++T[t];
  61. cout << "Seccessful game in " << t << " guesses" << endl << endl;
  62. }else {
  63. ++T[0];
  64. cout << "Failed game" << endl << endl;
  65. }
  66. if (++P == Ps) {
  67. freopen((AIname + "_summary.txt").c_str(), "w", stdout);
  68. //cout << P << " Games: " << P - T[0] << " Y / " << T[0] << " N" << endl;
  69. int max = 0, sum = 0;
  70. for (int i = 1; i <= maxt; ++i) {
  71. if (T[i]) max = i;
  72. sum += i * T[i];
  73. }
  74. cout << fixed;
  75. cout.precision(3);
  76. cout << "平均猜测次数 : " << double(sum) / (P - T[0]) << endl;
  77. cout << "最多猜测次数 : " << max << endl;
  78. cout << "总耗时 :" << clock() / 1000.0 << " seconds" << endl;
  79. cout << "每种猜测次数完成游戏的局数如下:" << endl;
  80. cout << "次数" << "\t";
  81. for (int i = 1; i <= maxt; ++i)
  82. cout << i << "\t";
  83. cout << endl;
  84. cout << "局数" << "\t";
  85. for (int i = 1; i <= maxt; ++i)
  86. cout << T[i] << "\t";
  87. cout << endl;
  88. exit(0);
  89. }else {
  90. t = 0;
  91. bool u[S];
  92. memset(u, 0, sizeof(u));
  93. int x = P;
  94. for (int i = 0; i < L; ++i) {
  95. int y = x;
  96. for (int j = i + 1; j < L; ++j)
  97. y /= S - j;
  98. for (int j = 0; j < S; ++j)
  99. if (!u[j]) {
  100. --y;
  101. if (y < 0) {
  102. u[j] = true;
  103. a[i] = j;
  104. break;
  105. }
  106. }
  107. y = 1;
  108. for (int j = i + 1; j < L; ++j)
  109. y *= S - j;
  110. x %= y;
  111. }
  112. }
  113. }
  114. return make_pair(A, B - A);
  115. }
  116. } G;
  117. pair<int, int> guess(int* b)
  118. {
  119. return G.guess(b);
  120. }
  121. pair<int, int> guess(vector<int> vb)
  122. {
  123. int b[L];
  124. for (int i = 0; i < L; ++i)
  125. b[i] = vb[i];
  126. return G.guess(b);
  127. }