Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- bool check_win(const vector<char>& board, char player) {
- int win_lines[8][3] = {
- {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
- {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
- {0, 4, 8}, {2, 4, 6}
- };
- for (int i = 0; i < 8; ++i) {
- if (board[win_lines[i][0]] == player &&
- board[win_lines[i][1]] == player &&
- board[win_lines[i][2]] == player) {
- return true;
- }
- }
- return false;
- }
- int count_games(vector<char>& board, bool is_x_turn, int moves) {
- char prev_player = is_x_turn ? 'O' : 'X';
- if (moves > 0 && check_win(board, prev_player)) {
- return 1;
- }
- if (moves == 9) {
- return 1;
- }
- int total_games = 0;
- char current_player = is_x_turn ? 'X' : 'O';
- for (int i = 0; i < 9; ++i) {
- if (board[i] == '-') {
- board[i] = current_player;
- total_games += count_games(board, !is_x_turn, moves + 1);
- board[i] = '-';
- }
- }
- return total_games;
- }
- int main() {
- vector<char> board(9, '-');
- cout << count_games(board, true, 0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment