Java - Convertir de C++ a JAVA

 
Vista:

Convertir de C++ a JAVA

Publicado por manu (9 intervenciones) el 19/08/2013 14:30:02
Hola a tod@s, soy nuevo haber si me podeis ayudar.
Necesito pasar un programa traductor(castellano-Braille) en c++ a JAVA.
He añadido ////////// donde no veia lo que hacer.
Aqui os dejo primero el trabajo en c++ y el segundo mensaje en JAVA.
En c++ :
--------------------------------------------------------------------------MAIN :
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
#include<iostream>
#include<string>
#include<fstream>
#include "Codificacio.h"
 
using namespace std;
 
void traduccio()
{
	string entrada;
	string sortida;
 
	cout<<"Escribe el nombre de l fitxero d'entrada con .txt"<<endl;
	cout<<"Si no escribes nada se usara la entrada per defecto"<<endl;
	getline(cin, entrada);
 
	if(entrada.empty())
	{
		entrada="entrada.txt";
	}else{
		cout<<entrada<<endl;
	}
 
	cout<<"Escribe el nombre del fichero de salida con el mismo  procedimiento"<<endl;
	getline(cin, sortida);
 
	if(salida.empty())
	{
		sortida="salida.txt";
	}else{
		cout<<sortida<<endl;
	}
 
	bool codificar;
	cout<<"Para codificar escribir 'codificar' para decodificar escribir 'decodificar'"<<endl;
	string ordre;
	getline(cin, ordre);
	if(ordre=="codificar")
	{
		codificar=true;
	}
	if(ordre=="decodificar")
	{
		codificar=false;
	}
	if(ordre!="codificar" && ordre!="decodificar")
	{
		throw exception("La orden no se ha formulado correctamente");
	}
 
	ifstream fEntrada(entrada.c_str());
	ofstream fSortida(sortida.c_str());
 
	if(fEntrada.is_open() && fSortida.is_open())
	{
		if(codificar)
		{
			string linia;
			Codificacio cod;
 
			while(!fEntrada.eof())
			{
				getline(fEntrada, linia);
				cod.codifica(linia);
				cod.imprimeix();
				fSortida<<cod.getb1()<<endl;
				fSortida<<cod.getb2()<<endl;
				fSortida<<cod.getb3()<<endl;
				fSortida<<endl;
				cod.neteja();
			}
		}
 
		if(!codificar)
		{
			string linia1;
			string linia2;
			string linia3;
			Codificacio decod;
			while(!fEntrada.eof())
			{
				getline(fEntrada, linia1);
				while(linia1.length()==0){
					getline(fEntrada, linia1);
				}
				getline(fEntrada, linia2);
				getline(fEntrada, linia3);
				decod.decodifica(linia1, linia2, linia3);
				decod.imprimeix();
				cout<<endl;
				fSortida<<decod.getb1()<<endl;
				fSortida<<endl;
				decod.neteja();
			}
		}
		fEntrada.close();
		fSortida.close();
	}else{
		throw exception("Los nombres de archivos no son correctos");
	}
 
}
 
 
int main(int argc, char** argv)
{
	try
	{
		traduccio();
	}
	catch(exception error)
	{
		cout<<error.what()<<endl;
	}
}

------------------------------------------------------------------------CODIFICACION:
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "Codificacio.h"
#include<iostream>
#include<string>
using namespace std;
 
Codificacio::Codificacio(void)
{
	string str="abcdefghijklmnñopqrstuvwxyz ";
	for(int i=0; i<28; i++)
	{
		caracters[i]=str[i];
	}
	braille[0]="o.....";
	braille[1]="o.o...";
	braille[2]="oo....";
	braille[3]="oo.o..";
	braille[4]="o..o..";
	braille[5]="ooo...";
	braille[6]="oooo..";
	braille[7]="o.oo..";
	braille[8]=".oo...";
	braille[9]=".ooo..";	//j
	braille[10]="o...o.";
	braille[11]="o.o.o.";
	braille[12]="oo..o.";
	braille[13]="oo.oo.";
	braille[14]="oooo.o";	//ñ
	braille[15]="o..oo.";
	braille[16]="ooo.o.";
	braille[17]="ooooo.";
	braille[18]="o.ooo.";
	braille[19]=".oo.o.";
	braille[20]=".oooo.";
	braille[21]="o...oo";
	braille[22]="o.o.oo";
	braille[23]=".ooo.o";
	braille[24]="oo..oo";
	braille[25]="oo.ooo";
	braille[26]="o..ooo";
	braille[27]="      ";
}
 
 
void Codificacio::imprimeix()
{
	if(b2.empty())
	{
		cout<<b1<<endl;
	}
	else
	{
		cout<<b1<<endl;
		cout<<b2<<endl;
		cout<<b3<<endl;
	}
 
}
 
 
void Codificacio::neteja()
{
	if(b2.empty())
	{
		b1.clear();
	}
	else
	{
		b1.clear();
		b2.clear();
		b3.clear();
	}
}
 
 
string Codificacio::getb1()
{
	return b1;
}
 
 
string Codificacio::getb2()
{
	return b2;
}
 
 
string Codificacio::getb3()
{
	return b3;
}
 
 
void Codificacio::codifica(string str)
{
	unsigned int i=0;
	while(i<str.length())
	{
		int u=0;
		while(str[i]!=caracters[u] && u<28)
		{
			u++;
		}
		if(u==28)
		{
			if(str[i]!='Ñ' && (str[i]<65 || str[i]>90) && (str[i]<48 || str[i]>57))
			{
				throw exception("Caracter desconocido");
			}
 
			if(str[i]=='Ñ')
			{
				b1+=".o";
				b2+="..";
				b3+=".o";
				b1+=braille[14].substr(0,2);
				b2+=braille[14].substr(2,2);
				b3+=braille[14].substr(4,2);
				i++;
			}
 
			if(str[i]>=65 && str[i]<=90)
			{
				b1+=".o";
				b2+="..";
				b3+=".o";
				char caracter;
				caracter=str[i]+32;
				int u=0;
				while(caracter!=caracters[u])
				{
					u++;
				}
				b1+=braille[u].substr(0,2);
				b2+=braille[u].substr(2,2);
				b3+=braille[u].substr(4,2);
				i++;
			}
 
			if(str[i]>=48 && str[i]<=57)
			{
				b1+=".o";
				b2+=".o";
				b3+="oo";
 
				char caracter;
				while(str[i]!=' ' && i<str.length())
				{
					if(str[i]<48 || str[i]>57)
					{
						throw exception("si hay un caracter diferente despues o nombre despues de un nombre");
					}
					if(str[i]==48)
					{
						caracter=106;
					}else{
						caracter=str[i]+48;
					}
					int u=0;
					while(caracter!=caracters[u])
					{
						u++;
					}
					b1+=braille[u].substr(0,2);
					b2+=braille[u].substr(2,2);
					b3+=braille[u].substr(4,2);
					i++;
				}
			}
 		}
		else
		{
			b1+=braille[u].substr(0,2);
			b2+=braille[u].substr(2,2);
			b3+=braille[u].substr(4,2);
			i++;
		}
	}
}
 
 
void Codificacio::decodifica(string str1, string str2, string str3)
{
	if(str1.length()!=str2.length() || str1.length()!=str3.length() || str2.length()!=str3.length())
	{
		throw exception("si hay una linea de diferente longitud. Cualquier caracter introducido despues del codido Braille se tiene en cuenta!");
	}
	string conjunt;
	unsigned int i=0;
 
	while(i<(str1.length()/2))
	{
		conjunt=str1[2*i];
		conjunt+=str1[2*i+1];
		conjunt+=str2[2*i];
		conjunt+=str2[2*i+1];
		conjunt+=str3[2*i];
		conjunt+=str3[2*i+1];
		int u=0;
		while(conjunt!=braille[u] && u<28)
		{
			u++;
		}
 
		if(u==28)
		{
			if(conjunt==".o...o")
			{
				i++;
				conjunt=str1[2*i];
				conjunt+=str1[2*i+1];
				conjunt+=str2[2*i];
				conjunt+=str2[2*i+1];
				conjunt+=str3[2*i];
				conjunt+=str3[2*i+1];
				if(conjunt==".o.ooo")
				{
					throw exception("Error: caracter de nombre despues del caracter mayuscula");
				}
				int j=0;
				while(conjunt!=braille[j])
				{
						j++;
				}
				if(j==14)
				{
					b1+="Ñ";
				}else
				{
					b1+=(caracters[j]-32);
				}
			}
 
			if(conjunt==".o.ooo")
			{
				i++;
				conjunt=str1[2*i];
				conjunt+=str1[2*i+1];
				conjunt+=str2[2*i];
				conjunt+=str2[2*i+1];
				conjunt+=str3[2*i];
				conjunt+=str3[2*i+1];
				while(conjunt!=braille[27] && i<(str1.length()/2))
				{
					if(conjunt==braille[9])
					{
						b1+='0';
					}
					else
					{
						int j=0;
						while(conjunt!=braille[j] && j<10)
						{
							j++;
						}
						if(j==10)
						{
							throw exception("Error: caracter incorrecto despues del simbolo nombre");
						}
						b1+=(caracters[j]-48);
					}
					i++;
					conjunt=str1[2*i];
					conjunt+=str1[2*i+1];
					conjunt+=str2[2*i];
					conjunt+=str2[2*i+1];
					conjunt+=str3[2*i];
					conjunt+=str3[2*i+1];
				}
				b1+=' ';
			}
		}
		else
		{
			b1+=caracters[u];
 
		}
		i++;
	}
}
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

Convertir de C++ a JAVA

Publicado por manu (9 intervenciones) el 21/08/2013 10:51:10
Sorry no he puesto el programa traducido en java. Por cierto no me compila nada.

Aqui lo teneis:
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
public class Codificacio
{
 
	private String caracters = new String(new char[28]);
	private String[] braille = new String[28];
	private String b1;
	private String b2;
	private String b3;
 
 
	public Codificacio()
	{
		String str = "abcdefghijklmnñopqrstuvwxyz ";
		for (int i = 0; i < 28; i++)
		{
			caracters.charAt(i) = str.charAt(i);
		}
		braille[0] = "o.....";
		braille[1] = "o.o...";
		braille[2] = "oo....";
		braille[3] = "oo.o..";
		braille[4] = "o..o..";
		braille[5] = "ooo...";
		braille[6] = "oooo..";
		braille[7] = "o.oo..";
		braille[8] = ".oo...";
		braille[9] = ".ooo.."; //j
		braille[10] = "o...o.";
		braille[11] = "o.o.o.";
		braille[12] = "oo..o.";
		braille[13] = "oo.oo.";
		braille[14] = "oooo.o"; //ñ
		braille[15] = "o..oo.";
		braille[16] = "ooo.o.";
		braille[17] = "ooooo.";
		braille[18] = "o.ooo.";
		braille[19] = ".oo.o.";
		braille[20] = ".oooo.";
		braille[21] = "o...oo";
		braille[22] = "o.o.oo";
		braille[23] = ".ooo.o";
		braille[24] = "oo..oo";
		braille[25] = "oo.ooo";
		braille[26] = "o..ooo";
		braille[27] = "      ";
	}
 
	public final void imprimeix()
	{
		if (b2.length() == 0)
		{
			System.out.print(b1);
			System.out.print("\n");
		}
		else
		{
			System.out.print(b1);
			System.out.print("\n");
			System.out.print(b2);
			System.out.print("\n");
			System.out.print(b3);
			System.out.print("\n");
		}
 
	}
	public final void codifica(String str)
	{	int i = 0;
		while (i < str.length())
		{
			int u = 0;
			while (str.charAt(i) != caracters.charAt(u) && u < 28)
			{
				u++;
			}
			if (u == 28)
			{
				if (str.charAt(i) != 'Ñ' && (str.charAt(i) < 65 || str.charAt(i)>90) && (str.charAt(i) < 48 || str.charAt(i)>57))
				{
					throw exception("Caracter desconocido");
				}
 
				if (str.charAt(i) == 'Ñ')
				{
					b1 += ".o";
					b2 += "..";
					b3 += ".o";
					b1 += braille[14].substring(0,2);
					b2 += braille[14].substring(2, 4);
					b3 += braille[14].substring(4, 6);
					i++;
				}
 
				if (str.charAt(i) >= 65 && str.charAt(i) <= 90)
				{
					b1 += ".o";
					b2 += "..";
					b3 += ".o";
					byte caracter;
					caracter = str.charAt(i) + 32;
					int u = 0;
					while (caracter != caracters.charAt(u))
					{
						u++;
					}
					b1 += braille[u].substring(0,2);
					b2 += braille[u].substring(2, 4);
					b3 += braille[u].substring(4, 6);
					i++;
				}
 
				if (str.charAt(i) >= 48 && str.charAt(i) <= 57)
				{
					b1 += ".o";
					b2 += ".o";
					b3 += "oo";
 
					byte caracter;
					while (str.charAt(i) != ' ' && i < str.length())
					{
						if (str.charAt(i) < 48 || str.charAt(i)>57)
						{
							throw exception("Si hay un caracter diferente d'espacio o nombre despues de un nombre");
						}
						if (str.charAt(i) == 48)
						{
							caracter = 106;
						}
						else
						{
							caracter = str.charAt(i) + 48;
						}
						int u = 0;
						while (caracter != caracters.charAt(u))
						{
							u++;
						}
						b1 += braille[u].substring(0,2);
						b2 += braille[u].substring(2, 4);
						b3 += braille[u].substring(4, 6);
						i++;
					}
				}
			}
			else
			{
				b1 += braille[u].substring(0,2);
				b2 += braille[u].substring(2, 4);
				b3 += braille[u].substring(4, 6);
				i++;
			}
		}
	}
	public final void decodifica(String str1, String str2, String str3)
	{
		if (str1.length() != str2.length() || str1.length() != str3.length() || str2.length() != str3.length())
		{
			throw exception("Si hay una linea de diferente longitud. Qualquier caracter introducido despues del codigo Braille se tiene en cuenta!");
		}
		String conjunt;
		int i = 0;
 
		while (i < (str1.length() / 2))
		{
			conjunt = str1.charAt(2 * i);
			conjunt += str1.charAt(2 * i + 1);
			conjunt += str2.charAt(2 * i);
			conjunt += str2.charAt(2 * i + 1);
			conjunt += str3.charAt(2 * i);
			conjunt += str3.charAt(2 * i + 1);
			int u = 0;
			while (!braille[u].equals(conjunt) && u < 28)
			{
				u++;
			}
 
			if (u == 28)
			{
				if (conjunt.equals(".o...o"))
				{
					i++;
					conjunt = str1.charAt(2 * i);
					conjunt += str1.charAt(2 * i + 1);
					conjunt += str2.charAt(2 * i);
					conjunt += str2.charAt(2 * i + 1);
					conjunt += str3.charAt(2 * i);
					conjunt += str3.charAt(2 * i + 1);
					if (conjunt.equals(".o.ooo"))
					{
						throw exception("Error: caracter de nombre despues del caracter mayuscula");
					}
					int j = 0;
					while (!braille[j].equals(conjunt))
					{
							j++;
					}
					if (j == 14)
					{
						b1 += "Ñ";
					}
					else
					{
						b1 += (caracters.charAt(j) - 32);
					}
				}
 
				if (conjunt.equals(".o.ooo"))
				{
					i++;
					conjunt = str1.charAt(2 * i);
					conjunt += str1.charAt(2 * i + 1);
					conjunt += str2.charAt(2 * i);
					conjunt += str2.charAt(2 * i + 1);
					conjunt += str3.charAt(2 * i);
					conjunt += str3.charAt(2 * i + 1);
					while (!braille[27].equals(conjunt) && i < (str1.length() / 2))
					{
						if (braille[9].equals(conjunt))
						{
							b1 += '0';
						}
						else
						{
							int j = 0;
							while (!braille[j].equals(conjunt) && j < 10)
							{
								j++;
							}
							if (j == 10)
							{
								throw exception("Error: caracter incorrecto despues del simbolo nombre");
							}
							b1 += (caracters.charAt(j) - 48);
						}
						i++;
						conjunt = str1.charAt(2 * i);
						conjunt += str1.charAt(2 * i + 1);
						conjunt += str2.charAt(2 * i);
						conjunt += str2.charAt(2 * i + 1);
						conjunt += str3.charAt(2 * i);
						conjunt += str3.charAt(2 * i + 1);
					}
					b1 += ' ';
				}
			}
			else
			{
				b1 += caracters.charAt(u);
 
			}
			i++;
		}
	}
	public final void neteja()
	{
		if (b2.length() == 0)
		{
			b1.clear();
		}
		else
		{
			b1.clear();
			b2.clear();
			b3.clear();
		}
	}
	public final String getb1()
	{
		return b1;
	}
	public final String getb2()
	{
		return b2;
	}
	public final String getb3()
	{
		return b3;
	}
}
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

Convertir de C++ a JAVA

Publicado por manu (9 intervenciones) el 22/08/2013 16:32:28
Echadme una mano pleaseee
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
sin imagen de perfil

Convertir de C++ a JAVA

Publicado por manu (9 intervenciones) el 03/09/2013 00:18:16
Hola a todos, bueno ya he avanzado bastante. Me podriais decir como hacer un metodo LeerBraille() que me devuelva un objeto braille del fichero de entrada, y como haria para leer solo 3 lineas para decodificar de braille a castellano?
Gracias
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

Convertir de C++ a JAVA

Publicado por Raul Melo Ramirez (1 intervención) el 10/12/2013 20:43:07
Eit amigos quiero que me ayuden a convertir un programa de c++ a java xfa ya que me es mui complicado programa si alguien me puede ayudar se lo agradeceria este es el programa en c++: "es un conector"
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
#include <conio.h>
#include <stdio.h>
 
float f (float x, float y)
{
 return x-y;
}
float rk4 (float x, float y, float h)
{
float k1,k2,k3,k4;
k1= f(x,y);
 
k2= f(x+h/2, y+(h*k1)/2);
 
k3= f(x+h/2, y+(h*k2)/2);
 
k4= f(x+h, y+(h*k3));
 
return y+(h/6)*(k1+2*k2+2*k3+k4);
}
main(){
       float x,x0,x1,h,yp;
       float y[10];
       int i;
       //leer x0, y[0], h, x1;
       scanf ("%f",&x0);
       scanf ("%f",&y[0]);
       scanf ("%f",&x1);
       scanf ("%f",&h);
       for (x=x0, i=1; i<3; i++, x=x+h)
       {y[i]= rk4 (x, y[i-1],h);
 
       }
       yp=y[2];
 
       for(x=x0+2*h, i=2; x<=x1; x=x+h, i++)
       {
                     printf ("\n valor de yp es %f",yp);
                     y[i]=y[i-2]+(h/3)*(f(x,yp)+4*f(x-h,y[i-1])+f(x-2*h,y[i-2]));
                     yp=rk4(x,y[i],h);
                     }
       printf ("\n resultado %f", y[i-1]);
       getch();
 
}
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

Convertir de C++ a JAVA

Publicado por Silvana (1 intervención) el 21/11/2017 04:27:04
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
 
using namespace std;
 
const int n=6;
const int m=8;
 
bool sobranEspaciosLibres(int tablero[n][m])
{
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            if(tablero[i][j] == 0)
            {
                //Sobran espacios donde colocar fechas
                //cout<<"Hay espacios"<<endl;
                return true;
            }
        }
    }
 
    //No sobran mas espacios donde colocar fichas, juego empatado
    cout<<"No hay mas espacios: empate"<<endl;
    return false;
}
 
void ImprimirTablero(int tablero[n][m])
{
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            if(tablero[i][j] == 1)
            {
                cout<<" O ";
            }
            else if(tablero[i][j] == 2)
            {
                cout<<" X ";
            }
            else
            {
                cout<<" . ";
            }
 
        }
        cout<<endl;
    }
}
 
int ColocarFichaEn(int tablero[n][m], int columna)
{
    for(int i = 0; i < 6; i++)
    {
        if(!(i+1 > 5))
        {
            if(tablero[i][columna] == 0 && tablero[i+1][columna] != 0 )
            {
                return i;
            }
        }
        else
        {
            //Alcanzo la base del tablero
            if(tablero[i][columna] == 0)
            {
                return i;
            }
        }
    }
 
    return -1;
}
 
bool columnaATope(int tablero[n][m], int columna)
{
    if(tablero[0][columna] != 0)
    {
        cout<<"No hay espacio para colocar la ficha ahi"<<endl;
        return true;
    }
    return false;
}
 
bool ganador(int tablero[n][m], int fila, int columna, int jugador)
{
    //Vertical
    bool encontrado = false;
    int total = 0;
 
    for(int i = 0; i < n; i++)
    {
        //cout<<"i: "<<i<<endl;
        if(encontrado)
        {
            if(tablero[i][columna] == jugador)
            {
                total++;
            }
            else
            {
                encontrado = false;
                total = 0;
            }
        }
        if(tablero[i][columna] == jugador && !encontrado)
        {
            encontrado = true;
            total++;
        }
 
        //cout<<"total: "<<total<<endl;
        if(total == 4)
        {
            cout<<"El jugador "<<jugador<<" gana!"<<endl;
            return true;
        }
    }
 
    //Horizontal
    encontrado = false;
    total = 0;
 
    for(int i = 0; i < m; i++)
    {
        //cout<<"i: "<<i<<endl;
        if(encontrado)
        {
            if(tablero[fila][i] == jugador)
            {
                total++;
            }
            else
            {
                encontrado = false;
                total = 0;
            }
        }
        if(tablero[fila][i] == jugador && !encontrado)
        {
            encontrado = true;
            total++;
        }
 
        //cout<<"total: "<<total<<endl;
        if(total == 4)
        {
            cout<<"El jugador "<<jugador<<" gana!"<<endl;
            return true;
        }
    }
 
    //Diagonal \
    //Obtener Coordenadas donde inicia la diagonal en base a fila - columna
    int nuevaFila = fila;
    int nuevaColumna = columna;
    encontrado = false;
    total = 0;
 
    //cout<<"Fila: "<<fila<<" Columna: "<<columna<<endl;
 
    while((nuevaFila != 0 || nuevaColumna != 0))
    {
        nuevaFila--;
        nuevaColumna--;
 
        //cout<<"nueva Fila: "<<nuevaFila<<" nueva Columna: "<<nuevaColumna<<endl;
        if(nuevaFila == 0 || nuevaColumna == 0)
        break;
    }
 
    //cout<<"nueva Fila: "<<nuevaFila<<" nueva Columna: "<<nuevaColumna<<endl;
 
    do
    {
        if(nuevaFila >= n)
        break;
 
        //cout<<"nueva Fila: "<<nuevaFila<<" nueva Columna: "<<nuevaColumna<<endl;
        if(encontrado)
        {
            if(tablero[nuevaFila][nuevaColumna] == jugador)
            {
                total++;
            }
            else
            {
                encontrado = false;
                total = 0;
            }
        }
        if(tablero[nuevaFila][nuevaColumna] == jugador && !encontrado)
        {
            encontrado = true;
            total++;
        }
 
        //cout<<"total: "<<total<<endl;
        if(total == 4)
        {
            cout<<"El jugador "<<jugador<<" gana!"<<endl;
            return true;
        }
 
        nuevaFila++;
        nuevaColumna++;
 
    }while(nuevaFila < n);
 
 
 
    //Diagonal /
    nuevaFila = fila;
    nuevaColumna = columna;
    encontrado = false;
    total = 0;
 
    //cout<<"Fila: "<<fila<<" Columna: "<<columna<<endl;
 
    while((nuevaFila != 0 || nuevaColumna != m))
    {
        nuevaFila--;
        nuevaColumna++;
 
        //cout<<"nueva Fila: "<<nuevaFila<<" nueva Columna: "<<nuevaColumna<<endl;
        if(nuevaFila == 0 || nuevaColumna == m)
        break;
    }
 
    //cout<<"nueva Fila: "<<nuevaFila<<" nueva Columna: "<<nuevaColumna<<endl;
 
    do
    {
        //cout<<"nueva Fila: "<<nuevaFila<<" nueva Columna: "<<nuevaColumna<<endl;
        if(nuevaFila >= n)
        break;
 
        //cout<<"nueva Fila: "<<nuevaFila<<" nueva Columna: "<<nuevaColumna<<endl;
        if(encontrado)
        {
            if(tablero[nuevaFila][nuevaColumna] == jugador)
            {
                total++;
            }
            else
            {
                encontrado = false;
                total = 0;
            }
        }
        if(tablero[nuevaFila][nuevaColumna] == jugador && !encontrado)
        {
            encontrado = true;
            total++;
        }
 
        //cout<<"total: "<<total<<endl;
        if(total == 4)
        {
            cout<<"El jugador "<<jugador<<" gana!"<<endl;
            return true;
        }
 
        nuevaFila++;
        nuevaColumna--;
 
    }while(nuevaFila < n);
 
    return false;
}
 
int main()
{
    int matriz[n][m];
 
    //Preparamos el tablero del juego rellenandolo con 0's
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            matriz[i][j] = 0;
            //cout<<matriz[i][j]<<" ";
        }
        //cout<<" "<<endl;
    }
 
    //Logica
    int jugador = 0;
    int ultimo = 0;
    do
    {
        int colocarColumna = -1;
        bool columnaTope = true;
 
        if(ultimo == 1)
        {
            jugador = 2;
        }
        else
        {
            jugador = 1;
        }
 
        do
        {
            cout<<"Turno del jugador "<<jugador<<" - Elije un numero del 1 al 8: "; cin>>colocarColumna; cout<<endl;
            colocarColumna--;
            columnaTope = columnaATope(matriz,colocarColumna);
        }
        while((colocarColumna < 0 || colocarColumna > 7) || columnaTope);
 
        int colocarFila = ColocarFichaEn(matriz,colocarColumna);
        matriz[colocarFila][colocarColumna] = jugador;
        ImprimirTablero(matriz);
 
        //Revisar si hay un 4 en raya
        if(ganador(matriz,colocarFila,colocarColumna,jugador))
        {
            break;
        }
 
        ultimo = jugador;
        jugador++;
    }
    while(sobranEspaciosLibres(matriz));
 
    return 0;
}
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