#include <iostream>
using namespace std;
// Função para imprimir o tabuleiro
void printBoard(char board[3][3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << board[i][j];
if (j < 2) {
cout << " | ";
}
}
cout << endl;
if (i < 2) {
cout << "---------" << endl;
}
}
}
// Função para verificar se há um vencedor
char checkWinner(char board[3][3]) {
// Verificação das linhas, colunas e diagonais
for (int i = 0; i < 3; ++i) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
return board[i][0];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
return board[0][2];
}
return ' ';
}
int main() {
char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
char currentPlayer = 'X';
int row, col;
cout << "Jogo da Velha" << endl;
for (int turn = 0; turn < 9; ++turn) {
cout << "Jogador " << currentPlayer << ", é sua vez." << endl;
printBoard(board);
cout << "Digite a linha (0-2) e coluna (0-2): ";
cin >> row >> col;
if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') {
cout << "Jogada inválida. Tente novamente." << endl;
--turn;
continue;
}
board[row][col] = currentPlayer;
char winner = checkWinner(board);
if (winner != ' ') {
printBoard(board);
cout << "O jogador " << winner << " venceu!" << endl;
return 0;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
printBoard(board);
cout << "O jogo terminou em empate." << endl;
return 0;
}
