Dev - C++ - Juego del gato para cualquier dimensión del tablero

 
Vista:
sin imagen de perfil
Val: 31
Ha disminuido su posición en 4 puestos en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Juego del gato para cualquier dimensión del tablero

Publicado por fernanda (10 intervenciones) el 29/01/2021 18:52:40
como hago para que funcione para cualquier dimensión del tablero


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
int main(){
 
    char matriz[3][ 3], opc;
    int i, j;
 
    for(i=0; i<3; i++){
        for(j=0; j<3; j++){
            matriz[i][j]=' ';
            printf(" %c", matriz[i][j]);
         }
        printf("\n");
    }
 
    int fila, col, ganador=0, turno=1;
 
        //para jugador 1
        do{
            if(turno%2==1){
                do{
                    scanf("%d", &fila);
                    scanf("%d", &col);
                    //ciclo para cuando el usuario ingrese coordenadas invalidas
                    if(matriz[fila][col] == 'x' || matriz[fila][col] == 'o' || fila > 2 || col > 2){
                    }
                }while(matriz[fila][col] == 'x' || matriz[fila][col] == 'o' || fila > 2 || col > 2);
 
                matriz[fila][col]='x';
 
 
                for(i=0; i<3; i++){
                    for(j=0; j<3; j++){
                        printf(" %c", matriz[i][j]);
                    }
                    printf("\n");
                }
                turno++;
 
            //para jugador dos
            } else if(turno%2==0){
                do{
                    scanf("%d", &fila);
                    scanf("%d", &col);
 
                    //ciclo para cuando el usuario ingrese coordenadas invalidas
                    if(matriz[fila][col] == 'x' || matriz[fila][col] == 'o' || fila > 2 || col > 2){
                    }
                } while(matriz[fila][col] == 'x' || matriz[fila][col] == 'o' || fila > 2 || col > 2);
 
                matriz[fila][col]='o';
 
 
                for(i=0; i<3; i++){
                    for(j=0; j<3; j++){
                        printf(" %c", matriz[i][j]);
                    }
                    printf("\n");
                }
                turno++;
            }
 
            if(matriz[0][0] == 'x' && matriz[0][0] == matriz[0][1] && matriz[0][0] == matriz[0][2]
                || matriz[1][0] == 'x' && matriz[1][0] == matriz[1][1] && matriz[1][0] == matriz[1][2]
                    || matriz[2][0] == 'x' && matriz[2][0] == matriz[2][1] && matriz[2][0] == matriz[2][2]
 
                        || matriz[0][0] == 'x' && matriz[0][0] == matriz[1][0] && matriz[0][0] == matriz[2][0]
                            || matriz[0][1] == 'x' && matriz[0][1] == matriz[1][1] && matriz[0][1] == matriz[2][1]
                                || matriz[0][2] == 'x' && matriz[0][2] == matriz[1][2] && matriz[0][2] == matriz[2][2]
 
                                    || matriz[0][0] == 'x' && matriz[0][0] == matriz[1][1] && matriz[0][0] == matriz[2][2]
                                        || matriz[0][2] == 'x' && matriz[0][2] == matriz[1][1] && matriz[0][2] == matriz[2][0]){
                ganador=1;
                printf("1\n");
            }
 
                                        if(matriz[0][0] == 'o' && matriz[0][0] == matriz[0][1] && matriz[0][0] == matriz[0][2]
                                            || matriz[1][0] == 'o' && matriz[1][0] == matriz[1][1] && matriz[1][0] == matriz[1][2]
                                                || matriz[2][0] == 'o' && matriz[2][0] == matriz[2][1] && matriz[2][0] == matriz[2][2]
 
                                                    || matriz[0][0] == 'o' && matriz[0][0] == matriz[1][0] && matriz[0][0] == matriz[2][0]
                                                        || matriz[0][1] == 'o' && matriz[0][1] == matriz[1][1] && matriz[0][1] == matriz[2][1]
                                                            || matriz[0][2] == 'o' && matriz[0][2] == matriz[1][2] && matriz[0][2] == matriz[2][2]
 
                                                                || matriz[0][0] == 'o' && matriz[0][0] == matriz[1][1] && matriz[0][0] == matriz[2][2]
                                                                    || matriz[0][2] == 'o' && matriz[0][2] == matriz[1][1] && matriz[0][2] == matriz[2][0]){
                                            ganador=1;
                                            printf(" 2\n");
                                        }
 
        } while(ganador != 1);
 
 
    return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Rodrigo
Val: 1.755
Plata
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Juego del gato para cualquier dimensión del tablero

Publicado por Rodrigo (539 intervenciones) el 29/01/2021 21:47:16
Te sugiero reemplazar lo que se guarda en la matriz por un numero. 1 indicaria un jugador, -1 el otro jugador y en vez de comparar cada casilla, sumas por filas y sumas por columnas, y si la suma da el numero total de filas el ganador es un jugador pero si da -(el numero de filas), el ganador es el otro.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar