#include #include //#include // windows only #define N 3 #define MAX 100 using namespace std; void imprime_matriz(char mat[N][N]) { int i, j; system("clear"); // ou system("cls"); i = 0; cout << " 0 1 2" << endl; while (i < N) { cout << i << " "; j = 0; while (j < N) { cout << mat[i][j] << " "; j = j + 1; } cout << endl; i = i + 1; } } void inicializa(char mat[N][N]) { int i,j; i = 0; while (i < N) { j = 0; while (j < N) { mat[i][j] = '.'; j = j +1; } i = i +1; } } void muda(char mat[N][N], int linha, int coluna, int k) { if (k==1) { mat[linha][coluna] = 'X'; } else { mat[linha][coluna] = 'O'; } } // retorna um vencedor se tiver alguma linha cheia (=XXX ou =OOO), 0 cc int alguma_linha(char mat[N][N]) { int i, j, somatorio; i=0; while (i < N) { j = 1; somatorio = 0; while (j < N) { if (mat[i][0] == mat[i][j] && mat[i][j] != '.') { somatorio = somatorio + 1; } j = j+1; } if (somatorio == N-1) { if (mat[i][0] == 'X') return 1; else return 2; } i = i+1; } return 0; } // retorna vencedor se tiver alguma coluna cheia (=XXX ou =OOO), 0 cc int alguma_coluna(char mat[N][N]) { int i, j, somatorio; j=0; while (j < N) { i = 1; somatorio = 0; while (i < N) { if (mat[0][j] == mat[i][j] && mat[i][j] != '.') { somatorio = somatorio + 1; } i = i+1; } if (somatorio == N-1) { if (mat[0][j] == 'X') return 1; else return 2; } j = j+1; } return 0; } // retorna vencedor se tiver alguma diagonal cheia (=XXX ou =OOO), 0 cc int alguma_diagonal(char mat[N][N]) { int i, somatorio; // diagonal principal = 0,0 1,1, 2,2 i = 1; somatorio = 0; while (i < N) { if (mat[0][0] == mat[i][i] && mat[i][i] != '.') { somatorio = somatorio + 1; } i = i+1; } if (somatorio == N-1) { if (mat[0][0] == 'X') return 1; else return 2; } // diagonal secundaria = 0,2 1,1 2,0 i = 1; somatorio = 0; while (i < N) { if (mat[0][N-1] == mat[i][N-1-i] && mat[i][N-1-i] != '.') { somatorio = somatorio + 1; } i = i+1; } if (somatorio == N-1) { if (mat[0][N-1] == 'X') return 1; else return 2; } return 0; } int empatou(char mat[N][N]) { int i,j; i = 0; while (i < N) { j = 0; while (j < N) { if (mat[i][j] == '.') { return 0; } j = j +1; } i = i +1; } return 3; } // retorna 0 se nao terminou ainda // retorna 1 se o jogador 1 ganhou // retorna 2 se o jogador 2 ganhou // retorna 3 se o jogo empatou int terminou(char mat[N][N]) { if (alguma_linha(mat) != 0) { return alguma_linha(mat); } else if (alguma_coluna(mat) != 0) { return alguma_coluna(mat); } else if (alguma_diagonal(mat) != 0) { return alguma_diagonal(mat); } else if (empatou(mat) != 0) { return empatou(mat); } else { return 0; } } int main() { // X = jogador 1, O = jogador 2, . = vazio char matriz[N][N]; char nome1[MAX], nome2[MAX]; int i, j, jogador; cout << "Nome do jogador 1: "; cin >> nome1; cout << "Nome do jogador 2: "; cin >> nome2; inicializa(matriz); imprime_matriz(matriz); jogador = 1; while (terminou(matriz) == 0) { if (jogador == 1) { cout << "Digite uma jogada do jogador " << nome1 << ": "; } else { cout << "Digite uma jogada do jogador " << nome2 << ": "; } cin >> i >> j; while (i < 0 || i >= N || j < 0 || j >= N || matriz[i][j] != '.') { cout << "Jogada invalida. Repita: "; cin >> i >> j; } muda(matriz, i, j, jogador); imprime_matriz(matriz); if (jogador == 1) { jogador = 2; } else { jogador = 1; } } if (terminou(matriz) == 1) { cout << "Vencedor eh jogador " << nome1 << endl; } else if (terminou(matriz) == 2) { cout << "Vencedor eh jogador " << nome2 << endl; } else { cout << "Deu velha!" << endl; } return 0; }