fork(1) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int minEval(int board[9]);
  6. int maxEval(int board[9]);
  7.  
  8.  
  9. //This function displays the board.
  10. void displayBoard(int board[9]) {
  11. for (int i = 0; i<9; i++) {
  12. if (board[i] == 0) {
  13. cout << i << "\t";
  14. }
  15. else if (board[i] == 1) {
  16. cout << "X\t";
  17. }
  18. else if (board[i] == -1) {
  19. cout << "O\t";
  20. }
  21.  
  22. if (i == 2 || i == 5 || i == 8) {
  23. cout << "\n" << endl;
  24. }
  25. }
  26. }
  27.  
  28. //This functions checks if the game is won
  29. bool checkWin(int board[9]) {
  30. if ((board[0] == board[1] && board[1] == board[2] && board[0] != 0) ||
  31. (board[3] == board[4] && board[4] == board[5] && board[4] != 0) ||
  32. (board[6] == board[7] && board[7] == board[8] && board[6] != 0)) {
  33. return true;
  34. }
  35. else if ((board[0] == board[3] && board[3] == board[6] && board[0] != 0) ||
  36. (board[1] == board[4] && board[4] == board[7] && board[4] != 0) ||
  37. (board[2] == board[5] && board[5] == board[8] && board[2] != 0)) {
  38. return true;
  39. }
  40. else if ((board[0] == board[4] && board[4] == board[8] && board[4] != 0) ||
  41. (board[2] == board[4] && board[4] == board[6] && board[4] != 0)) {
  42. return true;
  43. }
  44. else {
  45. return false;
  46. }
  47. }
  48.  
  49. //This function checks for a draw.
  50. bool checkDraw(int board[9]) {
  51. int counter = 0;
  52. for (int i = 0; i<9; i++) {
  53. if (board[i] != 0) {
  54. counter++;
  55. }
  56. }
  57. if (counter == 9) {
  58. return true;
  59. }
  60. else {
  61. return false;
  62. }
  63. }
  64.  
  65. int minEval(int board[9]) {
  66. //Checks if the board state is a win and returns 1000, which means max wins.
  67. if (checkWin(board)) {
  68. return 1000;
  69. }
  70. //If the game is drawn, it returns 0.
  71. else if (checkDraw(board)) {
  72. return 0;
  73. }
  74. //Defines the finalScore, which is the score such that max wins.
  75. int finalScore = 1000;
  76. //int position;
  77. //Loops through the board and if that spot is empty, it places O at the slot and runs maxEval and reverts the board after getting the score.
  78. for (int i = 0; i < 9; i++) {
  79. if (board[i] == 0) {
  80. board[i] = -1;
  81. int score = maxEval(board);
  82. if (score < finalScore) {
  83. finalScore = score;
  84. //position = i;
  85. }
  86. board[i] = 0;
  87. }
  88. }
  89. //cout << position << endl;
  90. return finalScore;
  91. }
  92.  
  93. //Does exactly what minEval does with a couple of minor differences.
  94. int maxEval(int board[9]) {
  95. if (checkWin(board)) {
  96. return -1000;
  97. }
  98. else if (checkDraw(board)) {
  99. return 0;
  100. }
  101.  
  102. int finalScore = -1000;
  103. //int position;
  104. for (int i = 0; i < 9; i++) {
  105. if (board[i] == 0) {
  106. board[i] = 1;
  107. int score = minEval(board);
  108. if (score > finalScore) {
  109. finalScore = score;
  110. //position = i;
  111. }
  112. board[i] = 0;
  113. }
  114. }
  115. //cout << position << endl;
  116. return finalScore;
  117. }
  118.  
  119. //This function is supposed to determine which position corresponds to the best score and place either X or 0 on that position.
  120. void playMove(int board[9], int player) {
  121. //finalScore is either 1000 or -1000.
  122. int finalScore = player * -1000;
  123. int position;
  124. //Like min and max, it loops through the board and places either an X or an O depending on the input, player, and calls either min or max.
  125. for (int i = 0; i < 9; i++) {
  126. if (board[i] == 0) {
  127. board[i] = player;
  128. int score;
  129. if (player == 1) {
  130. score = maxEval(board);
  131. }
  132. else {
  133. score = minEval(board);
  134. }
  135. //The added position should correspond to the one with the best score, but on execution, something completely different happens.
  136. //The AI makes terrible moves and doesn't play to win.
  137. if (player == 1 && score >= finalScore) {
  138. finalScore = score;
  139. //cout << finalScore << endl;
  140. position = i;
  141. }
  142. else if (player == -1 && score <= finalScore) {
  143. finalScore = score;
  144. //cout << finalScore << endl;
  145. position = i;
  146. }
  147. board[i] = 0;
  148. }
  149. }
  150.  
  151. board[position] = player;
  152. }
  153.  
  154. int main() {
  155. //The starting board with all 0s, indicating the board is empty. 1 is for X and -1 is for O.
  156. int matrix[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  157.  
  158. //int matrix[9] = { 1, -1, 1, 0, -1, 0, -1, 0, 0 };
  159. //int test = maxEval(matrix);
  160. //cout << test << endl;
  161. //Loops through and plays by itself.
  162. for (int i = 0; i < 9; i++) {
  163. int player;
  164. if (i % 2 == 0) {
  165. player = -1;
  166. }
  167. else {
  168. player = 1;
  169. }
  170.  
  171. playMove(matrix, player);
  172.  
  173. displayBoard(matrix);
  174. cout << "\n\n";
  175. }
  176. cin.get();
  177. return 0;
  178. }
Success #stdin #stdout 0.04s 3468KB
stdin
Standard input is empty
stdout
0	1	2	

3	4	5	

6	7	O	



0	1	2	

3	4	5	

X	7	O	



0	1	2	

3	4	5	

X	O	O	



0	1	2	

3	4	X	

X	O	O	



0	1	2	

3	O	X	

X	O	O	



0	1	2	

X	O	X	

X	O	O	



0	1	O	

X	O	X	

X	O	O	



0	X	O	

X	O	X	

X	O	O	



O	X	O	

X	O	X	

X	O	O