Iam_Sandeep

Untitled

Mar 27th, 2026
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. bool check_win(const vector<char>& board, char player) {
  7.     int win_lines[8][3] = {
  8.         {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
  9.         {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
  10.         {0, 4, 8}, {2, 4, 6}
  11.     };
  12.     for (int i = 0; i < 8; ++i) {
  13.         if (board[win_lines[i][0]] == player &&
  14.             board[win_lines[i][1]] == player &&
  15.             board[win_lines[i][2]] == player) {
  16.             return true;
  17.         }
  18.     }
  19.     return false;
  20. }
  21.  
  22. int count_games(vector<char>& board, bool is_x_turn, int moves) {
  23.     char prev_player = is_x_turn ? 'O' : 'X';
  24.    
  25.     if (moves > 0 && check_win(board, prev_player)) {
  26.         return 1;
  27.     }
  28.    
  29.     if (moves == 9) {
  30.         return 1;
  31.     }
  32.  
  33.     int total_games = 0;
  34.     char current_player = is_x_turn ? 'X' : 'O';
  35.  
  36.     for (int i = 0; i < 9; ++i) {
  37.         if (board[i] == '-') {
  38.             board[i] = current_player;
  39.             total_games += count_games(board, !is_x_turn, moves + 1);
  40.             board[i] = '-';
  41.         }
  42.     }
  43.  
  44.     return total_games;
  45. }
  46.  
  47. int main() {
  48.     vector<char> board(9, '-');
  49.     cout << count_games(board, true, 0) << endl;
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment