#include #include #include using namespace std; char matriz[3][3]; void tabuleiro(); void iniciarMatriz(); void iniciarJogo(); void solicitarJogada(char simbolo); bool sequenciaEncontrada(); bool empate(); void iniciarJogo() { string vencedor; string nome; bool finalizado; char simbolo; vector jogadores; vector simbolos; int indexAtual = 0; system("cls"); cout << "=======================" << endl; cout << "==| JOGO DA VELHA |==" << endl; cout << "=======================" << endl; cout << endl; cout << "DIGITE O NOME DO JOGADOR 1: "; getline(cin, nome); jogadores.insert(jogadores.end(), nome); simbolos.insert(simbolos.end(), 'X'); cout << "DIGITE O NOME DO JOGADOR 2: "; getline(cin, nome); jogadores.insert(jogadores.end(), nome); simbolos.insert(simbolos.end(), 'O'); finalizado = false; vencedor = " "; iniciarMatriz(); while (!finalizado) { system("cls"); tabuleiro(); cout << endl; cout << "\nE a vez do Jogador " << jogadores.at(indexAtual) << endl; solicitarJogada(simbolos.at(indexAtual)); if (sequenciaEncontrada()) { vencedor = jogadores.at(indexAtual); finalizado = true; } else if (empate()) { finalizado = true; } indexAtual = (indexAtual + 1) % jogadores.size(); } if (vencedor == " ") { system("cls"); tabuleiro(); cout << endl; cout << "\n O jogo terminou empatado!\n" << endl; } else { system("cls"); tabuleiro(); cout << endl; cout << "\n\tO Jogador " << vencedor << " ganhou!\n" << endl; } cout << endl; system("pause"); jogadores.clear(); simbolos.clear(); } void iniciarMatriz() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) matriz[i][j] = ' '; } void tabuleiro() { cout << "\n\n"; for (int i = 0; i < 3; i++) { cout << "\t " << matriz[i][0] << " | " << matriz[i][1] << " | " << matriz[i][2] << endl; if (i < 2) { cout << "\t -----|-----|-----" << endl; } } cout << endl; } int main() { string opcao; do { iniciarJogo(); system("cls"); cout << endl; cout << "\tDeseja jogar novamente? (s/n): "; getline(cin, opcao); } while (!opcao.compare("s")); return 0; } void solicitarJogada(char simbolo) { int i, j; int jogada = 0; do { cout << endl; cout << "Linha: "; cin >> i; cin.get(); cout << "Coluna: "; cin >> j; cin.get(); if (i > 3 || i < 1 || j > 3 || j < 1) { cout << endl; cout << "Erro: Jogada invalida!" << endl; jogada = 0; continue; } else { jogada = 1; } if (matriz[i - 1][j - 1] != ' ') { cout << endl; cout << "Erro: Essa jogada ja foi realizada!" << endl; jogada = 0; continue; } else { jogada = 1; } } while (!jogada); matriz[i - 1][j - 1] = simbolo; } bool empate() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (matriz[i][j] == ' ') return false; return true; } bool sequenciaEncontrada() { for (int i = 0; i < 3; i++) { // Linhas if (matriz[i][0] == matriz[i][1] && matriz[i][1] == matriz[i][2] && matriz[i][0] != ' ') { return true; } // Colunas if (matriz[0][i] == matriz[1][i] && matriz[1][i] == matriz[2][i] && matriz[0][i] != ' ') { return true; } } // Diagonais if (matriz[0][0] == matriz[1][1] && matriz[1][1] == matriz[2][2] && matriz[0][0] != ' ') { return true; } if (matriz[2][0] == matriz[1][1] && matriz[1][1] == matriz[0][2] && matriz[2][0] != ' ') { return true; } return false; }