Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int ROWS = 3;
- const int COLS = 3;
- void print_board(vector<vector<char>> xo_board) {
- for(int i = 0; i < ROWS; i++) {
- for(int j = 0; j < COLS; j++) {
- cout << xo_board[i][j] << " ";
- }
- cout << endl;
- }
- }
- bool valid_move(int pi, int pj, vector<vector<char>> xo_board) {
- pi--;
- pj--;
- if(pi >= 0 and pi < ROWS and pj >= 0 and pj < COLS and xo_board[pi][pj] == '.') {
- return true;
- }
- return false;
- }
- void set_board(int pi, int pj, char current_player, vector<vector<char>> & xo_board) {
- xo_board[pi - 1][pj - 1] = current_player;
- }
- int main() {
- vector<vector<char> > xo_board(ROWS, vector<char>(COLS, '.'));
- char current_player = 'X';
- bool game_over = false;
- while(!game_over) {
- print_board(xo_board);
- cout << current_player << "'s turn!" << endl;
- cout << "Where do you want to place " << current_player <<"?" << endl;
- int pi, pj;
- cin >> pi >> pj;
- while(!valid_move(pi, pj, xo_board)) {
- cout << "Not a valid move! Please play again!" << endl;
- cin >> pi >> pj;
- }
- set_board(pi, pj, current_player, xo_board);
- if(current_player == 'X') {
- current_player = 'O';
- }
- else {
- current_player = 'X';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement