0% found this document useful (0 votes)
10 views1 page

C++ Complexicity Codes

The document contains a C++ implementation of a TicTacToe game class. It includes methods for initializing the game board, drawing the board, making a move, and checking for a win condition. The game alternates between two players, represented by 'X' and 'O'.

Uploaded by

fukreedits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

C++ Complexicity Codes

The document contains a C++ implementation of a TicTacToe game class. It includes methods for initializing the game board, drawing the board, making a move, and checking for a win condition. The game alternates between two players, represented by 'X' and 'O'.

Uploaded by

fukreedits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

#include <vector>

class TicTacToe {
private:
char board[3][3];
char current;

public:
TicTacToe() {
current = 'X';
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
board[i][j] = ' ';
}

void draw() {
std::cout << "---------\n";
for (int i = 0; i < 3; ++i) {
std::cout << "| ";
for (int j = 0; j < 3; ++j)
std::cout << board[i][j] << " ";
std::cout << "|\n";
}
std::cout << "---------\n";
}

bool play(int row, int col) {


if (board[row][col] == ' ') {
board[row][col] = current;
return true;
}
return false;
}

bool checkWin() {
for (int i = 0; i < 3; ++i)
if (board[i][0] == current && board[i][1] == current && board[i][2] ==
current)
return true;

for (int i = 0; i < 3; ++i)


if (board[0][i] == current && board[1][i] == current && board[2][i] ==
current)
return true;

if (board[0][0] == current && board[1][1] == current && board[2][2] ==


current)
return true;

if (board[0][2] == current && board[1][1] == current && board[2][0] ==


current)
return true;

You might also like