Advertisement
JonathanA007

235150301111015-Tugas8-Bonus XOXO

Jun 1st, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | Source Code | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int N = 3;
  5. const int MAX_WINS = 5;
  6.  
  7.  
  8. struct GameState {
  9.     char board[N][N];
  10.     char currentPlayer;
  11. };
  12.  
  13.  
  14. void printBoard(char board[N][N]) {
  15.     for (int i = 0; i < N; ++i) {
  16.         for (int j = 0; j < N; ++j) {
  17.             cout << board[i][j] << ' ';
  18.         }
  19.         cout << endl;
  20.     }
  21.     cout << endl;
  22. }
  23.  
  24.  
  25. char checkWin(char board[N][N]) {
  26.     for (int i = 0; i < N; ++i) {
  27.         if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
  28.             return board[i][0];
  29.         }
  30.         if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
  31.             return board[0][i];
  32.         }
  33.     }
  34.  
  35.     if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
  36.         return board[0][0];
  37.     }
  38.     if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
  39.         return board[0][2];
  40.     }
  41.     return ' ';
  42. }
  43.  
  44.  
  45. bool dfs(GameState state, int depth, int& winCount, char wins[MAX_WINS][N][N]) {
  46.     char winner = checkWin(state.board);
  47.     if (winner != ' ') {
  48.         cout << "Pemenang: " << winner << endl;
  49.         printBoard(state.board);
  50.        
  51.         if (winCount < MAX_WINS) {
  52.             for (int i = 0; i < N; ++i)
  53.                 for (int j = 0; j < N; ++j)
  54.                     wins[winCount][i][j] = state.board[i][j];
  55.             ++winCount;
  56.         }
  57.        
  58.         return true;
  59.     }
  60.     if (depth == N * N) {
  61.         return false;
  62.     }
  63.     bool foundWin = false;
  64.     for (int i = 0; i < N; ++i) {
  65.         for (int j = 0; j < N; ++j) {
  66.             if (state.board[i][j] == ' ') {
  67.                 state.board[i][j] = state.currentPlayer;
  68.                 state.currentPlayer = (state.currentPlayer == 'X') ? 'O' : 'X';
  69.                 foundWin = dfs(state, depth + 1, winCount, wins) || foundWin;
  70.                 state.board[i][j] = ' ';
  71.                 state.currentPlayer = (state.currentPlayer == 'X') ? 'O' : 'X';
  72.                 if (winCount >= MAX_WINS) {
  73.                     return true;
  74.                 }
  75.             }
  76.         }
  77.     }
  78.     return foundWin;
  79. }
  80.  
  81. int main() {
  82.     GameState initialState = {{{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}, 'X'};
  83.     char wins[MAX_WINS][N][N];
  84.     int winCount = 0;
  85.    
  86.     dfs(initialState, 0, winCount, wins);
  87.    
  88.     if (winCount < MAX_WINS) {
  89.         cout << "Kurang dari " << MAX_WINS << " kemanangan yang ditemukan." << endl;
  90.     } else {
  91.         cout << "Menemukan " << MAX_WINS << " cara kemenangan:" << endl;
  92.         for (int w = 0; w < winCount; ++w) {
  93.             cout << "Metode Menang " << w + 1 << ":\n";
  94.             printBoard(wins[w]);
  95.         }
  96.     }
  97.    
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement