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;
- /*
- X | O | X
- ___|___|___
- O | X | O
- ---|---|--
- O | X | O
- **/
- void print_board(vector<vector<char>> xo_board) {
- for(int i = 0; i < ROWS; i++) {
- for(int j = 0; j < COLS; j++) {
- if(j == COLS / 2) {
- cout << "|";
- }
- if(xo_board[i][j] == '.') {
- cout << " ";
- }
- else {
- cout << " " << xo_board[i][j] << " ";
- }
- if(j == COLS / 2) {
- cout << "|";
- }
- }
- cout << endl;
- if(i == 0) {
- cout << "___|___|___" << endl;
- }
- else if(i == 1) {
- 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;
- }
- bool has_anyone_won(vector<vector<char>> xo_board, char current_player) {
- bool main_diagonal_win = true;
- bool anti_diagonal_win = true;
- for(int i = 0; i < ROWS; i++) {
- if(xo_board[i][i] != current_player) {
- main_diagonal_win = false;
- }
- if(xo_board[i][ROWS - i - 1] != current_player) {
- anti_diagonal_win = false;
- }
- }
- if(main_diagonal_win or anti_diagonal_win) {
- return true;
- }
- for(int i = 0; i < ROWS; i++) {
- bool column_win = true;
- for(int j = 0; j < COLS; j++) {
- if(xo_board[i][j] != current_player) {
- column_win = false;
- }
- }
- if(column_win) {
- return true;
- }
- }
- for(int j = 0; j < COLS; j++) {
- bool row_win = true;
- for(int i = 0; i < ROWS; i++) {
- if(xo_board[i][j] != current_player) {
- row_win = false;
- }
- }
- if(row_win) {
- return true;
- }
- }
- return false;
- }
- bool is_draw(vector<vector<char>> xo_board) {
- for(int i = 0; i < ROWS; i++) {
- for(int j = 0; j < COLS; j++) {
- if(xo_board[i][j] == '.') {
- return false;
- }
- }
- }
- return true;
- }
- 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(has_anyone_won(xo_board, current_player)) {
- print_board(xo_board);
- cout << current_player << " has won!" << endl << "CONGRATULATIONS!!!" << endl;
- break;
- }
- if(is_draw(xo_board)) {
- cout << "It's a draw! Try again!" << endl;
- break;
- }
- if(current_player == 'X') {
- current_player = 'O';
- }
- else {
- current_player = 'X';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement