fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. char mark[] = {'X', 'O'};
  6. int x_win = 0, o_win = 0, draw = 0;
  7.  
  8. bool win(char board[3][3])
  9. {
  10. for (int i = 0; i < 3; i++) {
  11. if (board[i][0] != '\0' && board[i][0] == board[i][1] && board[i][2] == board[i][1]) {
  12. return true;
  13. }
  14. if (board[0][i] != '\0' && board[0][i] == board[1][i] && board[2][i] == board[1][i]) {
  15. return true;
  16. }
  17. }
  18.  
  19. if (board[0][0] != '\0' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
  20. return true;
  21. }
  22.  
  23. if (board[0][2] != '\0' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
  24. return true;
  25. }
  26.  
  27. return false;
  28. }
  29.  
  30. int move(char board[3][3], int turn, int chances)
  31. {
  32. if (win(board)) {
  33. if(turn == 0) {
  34. o_win++;
  35. } else {
  36. x_win++;
  37. }
  38. return 1;
  39. }
  40.  
  41. if (chances == 9) {
  42. draw++;
  43. return 1;
  44. }
  45.  
  46. int ans = 0;
  47. for (int i = 0; i < 3; i++) {
  48. for (int j = 0; j < 3; j++) {
  49. if (board[i][j] == '\0') {
  50. board[i][j] = mark[turn];
  51. ans += move(board, 1 - turn, chances + 1);
  52. board[i][j] = '\0';
  53. }
  54. }
  55. }
  56.  
  57. return ans;
  58. }
  59.  
  60. int main() {
  61. char board[3][3] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
  62.  
  63. int ans = move(board, 0, 0);
  64.  
  65. cout << "Total Games: " << ans << endl;
  66. cout << "X Wins: " << x_win << endl;
  67. cout << "Y wins: " << o_win << endl;
  68. cout << "Draw: " << draw << endl;
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0.03s 3096KB
stdin
Standard input is empty
stdout
Total Games: 255168
X Wins: 131184
Y wins: 77904
Draw: 46080