Dev - C++ - Alguien que sepa porque este codigo no funciona

 
Vista:

Alguien que sepa porque este codigo no funciona

Publicado por Si soy (1 intervención) el 05/04/2022 02:49:28
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
 
// Variables globales
string jugador1, jugador2;
int jugador;
string aGato[10]={"",};
string mensaje;
 
// Declaración de funciones
void ImprimirGato();
bool Tirar(int posic);
int buscarGanador();
bool buscarEmpate();
 
int main() {
    bool salir = false;
    int tiro=0;
    int resultado;
 
    system("cls");
    cout << "REGISTRO DE COMPETIDORES.  --El Gato--" << endl;
    cout << "--------------------------------------" << endl << endl;
    cout << "Escribe el nombre del JUGADOR 1:";
    cin >> jugador1;
    cout << "Escribe el nombre del JUGADOR 2:";
    cin >> jugador2;
    mensaje = "BIENVENIDOS";
 
    while (!salir) {
        if (jugador ==1) {
            jugador =2;
        }
        else {
            jugador =1;
        }
 
        ImprimirGato();
        cin>>tiro;
        if (Tirar(tiro)) {
            // Si el tiro fue válido volver a imprimir el gato
            resultado = buscarGanador();
            switch(resultado) {
                case 0: // Todavia no hay ganador
                    if (buscarEmpate()) {
                        mensaje = "EMPATE. Lo sentimos";
                        salir = true;
                    }
                    else {
                        mensaje = "Es TURNO de: " + (jugador==1?jugador2:jugador1);
                    }
                    break;
                case 1: // Ganador jugador 1
                    mensaje = "GANASTE!!! " + jugador1;
                    salir = true;
                    break;
                case 2: // Ganador jugador 2
                    mensaje = "GANASTE!!! " + jugador2;
                    salir = true;
                    break;
            }
        }
        else
        {
            // Mostrar mensaje de error en tiro
            mensaje = "Posicion ocupada!";
            if (jugador ==1) {
                jugador =2;
            }
            else {
                jugador =1;
            }
        }
    }
    ImprimirGato();
    cin.get();
    cin.get();
 
    return 0;
}
 
 
void ImprimirGato() {
    system("cls");
 
    conio a;
    a.mensajeXY("JUEGO DEL",26,3);
    a.mensajeXY("####  ####  ####  ####",26,4);
    a.mensajeXY("#     #  #   ##   #  #",26,5);
    a.mensajeXY("# ##  ####   ##   #  #",26,6);
    a.mensajeXY("#  #  #  #   ##   #  #",26,7);
    a.mensajeXY("####  #  #   ##   ####",26,8);
 
    a.mensajeXY("|   |",37,10);
    a.mensajeXY("|   |",37,11);
    a.mensajeXY("----+---+----",33,12);
    a.mensajeXY("|   |",37,13);
    a.mensajeXY("|   |",37,14);
    a.mensajeXY("|   |",37,15);
    a.mensajeXY("----+---+----",33,16);
    a.mensajeXY("|   |",37,17);
    a.mensajeXY("|   |",37,18);
 
    a.mensajeXY((aGato[1]==""?"1":aGato[1]),35,11);
    a.mensajeXY((aGato[2]==""?"2":aGato[2]),39,11);
    a.mensajeXY((aGato[3]==""?"3":aGato[3]),43,11);
    a.mensajeXY((aGato[4]==""?"4":aGato[4]),35,14);
    a.mensajeXY((aGato[5]==""?"5":aGato[5]),39,14);
    a.mensajeXY((aGato[6]==""?"6":aGato[6]),43,14);
    a.mensajeXY((aGato[7]==""?"7":aGato[7]),35,17);
    a.mensajeXY((aGato[8]==""?"8":aGato[8]),39,17);
    a.mensajeXY((aGato[9]==""?"9":aGato[9]),43,17);
 
    a.mensajeXY("JUGADORES",3,19);
    a.mensajeXY("X -" + jugador1,7,20);
    a.mensajeXY("O -" + jugador2,7,21);
    if (jugador==1) {
        a.mensajeXY("==>",3,20);
    }
    else {
        a.mensajeXY("==>",3,21);
    }
 
    a.mensajeXY(" ------------------------  ",52,21);
    a.mensajeXY("|                        | ",52,22);
    a.mensajeXY(" ------------------------ |",52,23);
    a.mensajeXY("   -----------------------",52,24);
 
    a.mensajeXY(mensaje,54,22);
    a.imprimir();
}
 
 
bool Tirar(int posic) {
    bool retorno = false;
    if (aGato[posic]=="") {
        // Tirar
        if (jugador == 1) {
            aGato[posic] = "X";
        }
        else {
            aGato[posic] = "O";
        }
        retorno = true;
    }
 
    return retorno;
}
 
int buscarGanador() {
    int retorno =0;
    bool HayGanador = false;
    string cadenaGanadora;
    if (jugador == 1) {
        cadenaGanadora = "XXX";
    }
    else {
        cadenaGanadora = "OOO";
    }
 
    if (aGato[1] + aGato[2] + aGato[3] == cadenaGanadora) HayGanador = true;
    if (aGato[4] + aGato[5] + aGato[6] == cadenaGanadora) HayGanador = true;
    if (aGato[7] + aGato[8] + aGato[9] == cadenaGanadora) HayGanador = true;
    if (aGato[1] + aGato[4] + aGato[7] == cadenaGanadora) HayGanador = true;
    if (aGato[2] + aGato[5] + aGato[8] == cadenaGanadora) HayGanador = true;
    if (aGato[3] + aGato[6] + aGato[9] == cadenaGanadora) HayGanador = true;
    if (aGato[1] + aGato[5] + aGato[9] == cadenaGanadora) HayGanador = true;
    if (aGato[3] + aGato[5] + aGato[7] == cadenaGanadora) HayGanador = true;
 
    if (HayGanador) {
        retorno = jugador;
    }
 
    return retorno;
}
 
bool buscarEmpate () {
    bool retorno = false;
    string cadena ="";
    for (int i=1; i<=9; i++) {
        cadena = cadena + aGato[i];
    }
    if (cadena.length()==9){
        retorno = true;
    }
 
    return retorno;
}
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
sin imagen de perfil
Val: 1
Ha aumentado su posición en 3 puestos en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Alguien que sepa porque este codigo no funciona

Publicado por antonio (85 intervenciones) el 05/04/2022 11:58:18
Pues no lo se porque lo he probado y funciona pero puede ser porque tienes que hacer la clase conio la he encontrado en:
http://upavprogramacionii.blogspot.com/2012/03/
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