Advertisement
Josif_tepe

Untitled

Mar 24th, 2023
845
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 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. void print_board(vector<vector<char>> xo_board) {
  7.     for(int i = 0; i < ROWS; i++) {
  8.         for(int j = 0; j < COLS; j++) {
  9.             cout << xo_board[i][j] << " ";
  10.         }
  11.         cout << endl;
  12.     }
  13. }
  14. bool valid_move(int pi, int pj, vector<vector<char>> xo_board) {
  15.     pi--;
  16.     pj--;
  17.     if(pi >= 0 and pi < ROWS and pj >= 0 and pj < COLS and xo_board[pi][pj] == '.') {
  18.         return true;
  19.     }
  20.     return false;
  21. }
  22. void set_board(int pi, int pj, char current_player, vector<vector<char>> & xo_board) {
  23.     xo_board[pi - 1][pj - 1] = current_player;
  24. }
  25. bool has_anyone_won(vector<vector<char>> xo_board, char current_player) {
  26.     if(xo_board[0][0] == current_player and xo_board[1][1] == current_player and xo_board[2][2] == current_player) {
  27.         return true;
  28.     }
  29.     if(xo_board[0][2] == current_player and xo_board[1][1] == current_player and xo_board[2][0] == current_player) {
  30.         return true;
  31.     }
  32.     if(xo_board[0][0] == current_player and xo_board[0][1] == current_player and xo_board[0][2] == current_player) {
  33.         return true;
  34.     }
  35.     if(xo_board[1][0] == current_player and xo_board[1][1] == current_player and xo_board[1][2] == current_player) {
  36.         return true;
  37.     }
  38.     if(xo_board[2][0] == current_player and xo_board[2][1] == current_player and xo_board[2][2] == current_player) {
  39.         return true;
  40.     }
  41.     if(xo_board[0][0] == current_player and xo_board[1][0] == current_player and xo_board[2][0] == current_player) {
  42.         return true;
  43.     }
  44.     if(xo_board[0][1] == current_player and xo_board[1][1] == current_player and xo_board[2][1] == current_player) {
  45.         return true;
  46.     }
  47.     return false;
  48. }
  49. bool is_draw(vector<vector<char>> xo_board) {
  50.     for(int i = 0; i < ROWS; i++) {
  51.         for(int j = 0; j < COLS; j++) {
  52.             if(xo_board[i][j] == '.') {
  53.                 return false;
  54.             }
  55.         }
  56.     }
  57.     return true;
  58. }
  59. int main() {
  60.     vector<vector<char> > xo_board(ROWS, vector<char>(COLS, '.'));
  61.     char current_player = 'X';
  62.     bool game_over = false;
  63.    
  64.     while(!game_over) {
  65.         print_board(xo_board);
  66.         cout << current_player << "'s turn!" << endl;
  67.         cout << "Where do you want to place " << current_player  <<"?" << endl;
  68.        
  69.         int pi, pj;
  70.         cin >> pi >> pj;
  71.        
  72.         while(!valid_move(pi, pj, xo_board)) {
  73.             cout << "Not a valid move! Please play again!" << endl;
  74.             cin >> pi >> pj;
  75.         }
  76.         set_board(pi, pj, current_player, xo_board);
  77.        
  78.         if(has_anyone_won(xo_board, current_player)) {
  79.             print_board(xo_board);
  80.             cout << current_player << " has won!" << endl << "CONGRATULATIONS!!!" << endl;
  81.             break;
  82.         }
  83.         if(is_draw(xo_board)) {
  84.             cout << "It's a draw! Try again!" << endl;
  85.             break;
  86.         }
  87.         if(current_player == 'X') {
  88.             current_player = 'O';
  89.         }
  90.         else {
  91.             current_player = 'X';
  92.         }
  93.     }
  94.     return 0;
  95. }
  96.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement