Advertisement
Josif_tepe

Untitled

Mar 17th, 2023
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 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. int main() {
  26.     vector<vector<char> > xo_board(ROWS, vector<char>(COLS, '.'));
  27.     char current_player = 'X';
  28.     bool game_over = false;
  29.    
  30.     while(!game_over) {
  31.         print_board(xo_board);
  32.         cout << current_player << "'s turn!" << endl;
  33.         cout << "Where do you want to place " << current_player  <<"?" << endl;
  34.        
  35.         int pi, pj;
  36.         cin >> pi >> pj;
  37.        
  38.         while(!valid_move(pi, pj, xo_board)) {
  39.             cout << "Not a valid move! Please play again!" << endl;
  40.             cin >> pi >> pj;
  41.         }
  42.         set_board(pi, pj, current_player, xo_board);
  43.        
  44.         if(current_player == 'X') {
  45.             current_player = 'O';
  46.         }
  47.         else {
  48.             current_player = 'X';
  49.         }
  50.     }
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement