Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Leonardo Leal
- */
- #include <stdio.h>
- int mat[3][3];
- int turno;
- void limparMatriz() {
- int i, j;
- for(i = 0; i < 3; i++){
- for(j = 0; j < 3; j++) {
- mat[i][j] = -1;
- }
- }
- }
- void desenharJogo() {
- int i, j;
- system("cls");
- printf("\n 1 2 3 X\n +---+---+---+\n");
- for(i = 0; i < 3; i++){
- printf(" %d |", i + 1);
- for(j = 0; j < 3; j++) {
- switch(mat[i][j]) {
- case -1:
- printf(" |");
- break;
- case 0:
- printf(" o |");
- break;
- case 1:
- printf(" x |");
- break;
- }
- }
- printf("\n +---+---+---+\n");
- }
- printf(" Y\n");
- }
- int verificarGanhador() {
- int i;
- for(i = 0; i < 3; i++) {
- //Verificar Horizontal
- if(mat[i][0] != -1 && mat[i][0] == mat[i][1] && mat[i][1] == mat[i][2])
- return mat[i][0];
- //Verificar Vertical
- if(mat[0][i] != -1 && mat[0][i] == mat[1][i] && mat[1][i] == mat[2][i])
- return mat[0][i];
- }
- //Verificar Diagonais
- if(mat[1][1] != -1) {
- if(mat[0][0] == mat[1][1] && mat[1][1] == mat[2][2])
- return mat[1][1];
- if(mat[0][2] == mat[1][1] && mat[1][1] == mat[2][0])
- return mat[1][1];
- }
- return -1;
- }
- void lerPosicao(int* x, int* y) {
- printf(" - Jogador %d: Insira a posicao (x y) que voce deseja marcar: ", (turno % 2) + 1);
- scanf("%d %d", y, x);
- if(*x < 1 || *x > 3) {
- printf(" Posicao X invalida.\n");
- return lerPosicao(x, y);
- }
- else if(*y < 1 || *y > 3) {
- printf(" Posicao Y invalida.\n");
- return lerPosicao(x, y);
- }
- *x = *x - 1;
- *y = *y - 1;
- if(mat[*x][*y] != -1) {
- printf(" Esta posicao ja esta ocupada.\n");
- return lerPosicao(x, y);
- }
- }
- void iniciarJogo() {
- int x, y;
- int ganhador;
- limparMatriz();
- desenharJogo();
- for(turno = 0; turno < 9; turno++) {
- lerPosicao(&x, &y);
- mat[x][y] = turno % 2;
- ganhador = verificarGanhador();
- desenharJogo();
- if(ganhador != -1) {
- printf("\n O jogador %d ganhou!\n", (turno % 2) + 1);
- break;
- }
- }
- if(ganhador == -1) {
- printf("\n Ninguem ganhou o jogo.\n");
- }
- printf("\n Insira 1 para jogar novamente.\n ");
- scanf("%d", &x);
- if(x == 1)
- iniciarJogo();
- }
- int main() {
- iniciarJogo();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement