Advertisement
Josif_tepe

Untitled

Mar 24th, 2023
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. const int ROWS = 3;
  5. const int COLS = 3;
  6. /*
  7.  X | O | X
  8. ___|___|___
  9.  O | X | O
  10. ---|---|--
  11.  O | X | O
  12.  
  13.  
  14.  **/
  15.  
  16. void print_board(vector<vector<char>> xo_board) {
  17.     for(int i = 0; i < ROWS; i++) {
  18.         for(int j = 0; j < COLS; j++) {
  19.             if(j == COLS / 2) {
  20.                 cout << "|";
  21.             }
  22.             if(xo_board[i][j] == '.') {
  23.                 cout << "   ";
  24.             }
  25.             else {
  26.                 cout << " " << xo_board[i][j] << " ";
  27.             }
  28.             if(j == COLS / 2) {
  29.                 cout << "|";
  30.             }
  31.            
  32.         }
  33.         cout << endl;
  34.         if(i == 0) {
  35.             cout << "___|___|___" << endl;
  36.         }
  37.         else if(i == 1) {
  38.             cout << "---|---|---" << endl;
  39.         }
  40.     }
  41. }
  42. bool valid_move(int pi, int pj, vector<vector<char>> xo_board) {
  43.     pi--;
  44.     pj--;
  45.     if(pi >= 0 and pi < ROWS and pj >= 0 and pj < COLS and xo_board[pi][pj] == '.') {
  46.         return true;
  47.     }
  48.     return false;
  49. }
  50. void set_board(int pi, int pj, char current_player, vector<vector<char>> & xo_board) {
  51.     xo_board[pi - 1][pj - 1] = current_player;
  52. }
  53. bool has_anyone_won(vector<vector<char>> xo_board, char current_player) {
  54.     bool main_diagonal_win = true;
  55.     bool anti_diagonal_win = true;
  56.  
  57.     for(int i = 0; i < ROWS; i++) {
  58.         if(xo_board[i][i] != current_player) {
  59.             main_diagonal_win = false;
  60.         }
  61.         if(xo_board[i][ROWS - i - 1] != current_player) {
  62.             anti_diagonal_win = false;
  63.         }
  64.     }
  65.     if(main_diagonal_win or anti_diagonal_win) {
  66.         return true;
  67.     }
  68.     for(int i = 0; i < ROWS; i++) {
  69.         bool column_win = true;
  70.         for(int j = 0; j < COLS; j++) {
  71.             if(xo_board[i][j] != current_player) {
  72.                 column_win = false;
  73.             }
  74.         }
  75.         if(column_win) {
  76.             return true;
  77.         }
  78.     }
  79.     for(int j = 0; j < COLS; j++) {
  80.         bool row_win = true;
  81.         for(int i = 0; i < ROWS; i++) {
  82.             if(xo_board[i][j] != current_player) {
  83.                 row_win = false;
  84.             }
  85.         }
  86.         if(row_win) {
  87.             return true;
  88.         }
  89.     }
  90.     return false;
  91. }
  92. bool is_draw(vector<vector<char>> xo_board) {
  93.     for(int i = 0; i < ROWS; i++) {
  94.         for(int j = 0; j < COLS; j++) {
  95.             if(xo_board[i][j] == '.') {
  96.                 return false;
  97.             }
  98.         }
  99.     }
  100.     return true;
  101. }
  102. int main() {
  103.     vector<vector<char> > xo_board(ROWS, vector<char>(COLS, '.'));
  104.     char current_player = 'X';
  105.     bool game_over = false;
  106.    
  107.     while(!game_over) {
  108.         print_board(xo_board);
  109.         cout << current_player << "'s turn!" << endl;
  110.         cout << "Where do you want to place " << current_player  <<"?" << endl;
  111.        
  112.         int pi, pj;
  113.         cin >> pi >> pj;
  114.        
  115.         while(!valid_move(pi, pj, xo_board)) {
  116.             cout << "Not a valid move! Please play again!" << endl;
  117.             cin >> pi >> pj;
  118.         }
  119.         set_board(pi, pj, current_player, xo_board);
  120.        
  121.         if(has_anyone_won(xo_board, current_player)) {
  122.             print_board(xo_board);
  123.             cout << current_player << " has won!" << endl << "CONGRATULATIONS!!!" << endl;
  124.             break;
  125.         }
  126.         if(is_draw(xo_board)) {
  127.             cout << "It's a draw! Try again!" << endl;
  128.             break;
  129.         }
  130.         if(current_player == 'X') {
  131.             current_player = 'O';
  132.         }
  133.         else {
  134.             current_player = 'X';
  135.         }
  136.     }
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement