Java - ¿Como correr el siguiente codigo?

 
Vista:

¿Como correr el siguiente codigo?

Publicado por Jessy (3 intervenciones) el 25/10/2012 19:49:16
El codigo esta en java como lo ejecuto?? me dice que falta el main
como se inicializa dentro del main?

lo hice asi pero me da error: Tablero obj=new Tablero(5,4);
-----------------------------------------------------------------------------------------------------------

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
public class Tablero extends Component {
 
static final int HORIZONTAL = 0;
static final int VERTICAL = 1;
static final int DRAGAMINAS = 2;
static final int DESTRUCTOR = 3;
static final int ACORAZADO = 4;
static final int PORTAAVIONES = 5;
static final int TIRO_ERROR = 0;
static final int TIRO_ACIERTO = 1;
static final int TIRO_NULO = 2;
 
static final char VACIO = 'V';
static final char NAVE = 'N';
static final char ACIERTO = 'A';
static final char ERROR = 'E';
 
private static String[] nombre_nave = {"Dragaminas",
"Destructor",
"Acorazado",
"Portaaviones"};
 
private char[][] tablero;
private int[][] posicion_flota;
private ArrayList flota;
private int fils,cols;
private int ind_nave;
private int restan_naves;
class Nave {
String id;
int longitud;
 
Nave() {
id = "";
longitud = 0;
}
 
Nave(int i,int l) {
id = new String(nombre_nave[i-2]);
longitud = l;
}
}
 
 
Tablero(int m,int n) {
fils = m;
cols = n;
ind_nave = 0;
tablero = new char[m][n];
posicion_flota = new int[m][n];
flota = new ArrayList();
inicia_tablero();
}
 
 
 
public void inicia_tablero() {
for(int i=0; i<fils; i++) {
for(int j=0; j<cols; j++)
tablero[i][j] = 'V';
for(int k=0; k<cols; k++)
posicion_flota[i][k] = -1;
}
}
 
public boolean despliega_nave(int tipo,int xp,int yp,int h) {
if(xp < 1 || yp < 1) return false;
if(h == HORIZONTAL) {
if(yp<=fils && xp+tipo-1 <= cols) {
for(int i=0; i<tipo; i++)
if(tablero[yp-1][xp+i-1] != 'V')
return false;
for(int j=0; j<tipo; j++) {
tablero[yp-1][xp+j-1] = 'N';
posicion_flota[yp-1][xp+j-1] = ind_nave;
}
flota.add(new Nave(tipo,tipo));
ind_nave++;
restan_naves++;
}
else
return false;
}
else {
if(xp <= cols && yp+tipo-1 <= fils) {
for(int i=0; i<tipo; i++)
if(tablero[yp+i-1][xp-1] != 'V')
return false;
for(int j=0; j<tipo; j++) {
tablero[yp+j-1][xp-1] = 'N';
posicion_flota[yp+j-1][xp-1] = ind_nave;
}
flota.add(new Nave(tipo,tipo));
ind_nave++;
restan_naves++;
}
else
return false;
}
return true;
}
 
private boolean disparo_acertado(int x,int y) {
if(tablero[y-1][x-1] == 'N')
return true;
else
return false;
}
 
private boolean disparo_al_agua(int x,int y) {
if(tablero[y-1][x-1] == 'V')
return true;
else
return false;
}
 
public int disparo(int a,int b) {
if((a>=1 && a<=cols) && (b>=1 && b<=fils)) {
if(disparo_acertado(a,b)) {
tablero[b-1][a-1] = 'A';
((Nave)flota.get(posicion_flota[b-1][a…
if(((Nave)flota.get(posicion_flota[b-1… == 0)
restan_naves--;
return TIRO_ACIERTO;
}
else if(disparo_al_agua(a,b)) {
tablero[b-1][a-1] = 'E';
return TIRO_ERROR;
}
else
return TIRO_NULO;
}
return TIRO_NULO;
}
 
public static String tipoNave(int n) {
if(n > 1 && n < nombre_nave.length+2)
return new String(nombre_nave[n-2]);
else
return new String("");
}
 
public String naveAtacada(int a,int b) {
if(tablero[b-1][a-1] == 'A' || tablero[b-1][a-1] == 'N')
return ((Nave)flota.get(posicion_flota[b-1][a-1…
else
return new String("");
}
 
public boolean naveHundida(int a,int b) {
if(tablero[b-1][a-1] == 'A' || tablero[b-1][a-1] == 'N')
if(((Nave)flota.get(posicion_flota[b-1… == 0)
return true;
return false;
}
 
public char status_casilla(int a,int b) {
return tablero[b-1][a-1];
}
 
public int num_filas() {
return fils;
}
 
public int num_columnas() {
return cols;
}
 
public boolean flota_hundida() {
return(restan_naves <= 0);
}
 
public int obtener_resto() {
return restan_naves;
}
 
public void display() {
for(int i=0; i<fils; i++) {
for(int j=0; j<cols; j++)
System.out.print(tablero[i][j]+" ");
System.out.println();
}
System.out.println();
}
}
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

¿Como correr el siguiente codigo?

Publicado por jhonnathan Cardona (3 intervenciones) el 26/10/2012 17:39:25
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
package main;
 
import java.awt.Component;
import java.util.ArrayList;
 
public class Tablero extends Component {
 
	static final int HORIZONTAL = 0;
	static final int VERTICAL = 1;
	static final int DRAGAMINAS = 2;
	static final int DESTRUCTOR = 3;
	static final int ACORAZADO = 4;
	static final int PORTAAVIONES = 5;
	static final int TIRO_ERROR = 0;
	static final int TIRO_ACIERTO = 1;
	static final int TIRO_NULO = 2;
 
	static final char VACIO = 'V';
	static final char NAVE = 'N';
	static final char ACIERTO = 'A';
	static final char ERROR = 'E';
 
	private static String[] nombre_nave = { "Dragaminas", "Destructor",
			"Acorazado", "Portaaviones" };
 
	private char[][] tablero;
	private int[][] posicion_flota;
	private ArrayList flota;
	private int fils, cols;
	private int ind_nave;
	private int restan_naves;
 
	class Nave {
		String id;
		int longitud;
 
		Nave() {
			id = "";
			longitud = 0;
		}
 
		Nave(int i, int l) {
			id = new String(nombre_nave[i - 2]);
			longitud = l;
		}
	}
public static void main(String a[]){
	Tablero t= new Tablero(4, 5);
	t.display();
}
	Tablero(int m, int n) {
		fils = m;
		cols = n;
		ind_nave = 0;
		tablero = new char[m][n];
		posicion_flota = new int[m][n];
		flota = new ArrayList();
		inicia_tablero();
	}
 
	public void inicia_tablero() {
		for (int i = 0; i < fils; i++) {
			for (int j = 0; j < cols; j++)
				tablero[i][j] = 'V';
			for (int k = 0; k < cols; k++)
				posicion_flota[i][k] = -1;
		}
	}
 
	public boolean despliega_nave(int tipo, int xp, int yp, int h) {
		if (xp < 1 || yp < 1)
			return false;
		if (h == HORIZONTAL) {
			if (yp <= fils && xp + tipo - 1 <= cols) {
				for (int i = 0; i < tipo; i++)
					if (tablero[yp - 1][xp + i - 1] != 'V')
						return false;
				for (int j = 0; j < tipo; j++) {
					tablero[yp - 1][xp + j - 1] = 'N';
					posicion_flota[yp - 1][xp + j - 1] = ind_nave;
				}
				flota.add(new Nave(tipo, tipo));
				ind_nave++;
				restan_naves++;
			} else
				return false;
		} else {
			if (xp <= cols && yp + tipo - 1 <= fils) {
				for (int i = 0; i < tipo; i++)
					if (tablero[yp + i - 1][xp - 1] != 'V')
						return false;
				for (int j = 0; j < tipo; j++) {
					tablero[yp + j - 1][xp - 1] = 'N';
					posicion_flota[yp + j - 1][xp - 1] = ind_nave;
				}
				flota.add(new Nave(tipo, tipo));
				ind_nave++;
				restan_naves++;
			} else
				return false;
		}
		return true;
	}
 
	private boolean disparo_acertado(int x, int y) {
		if (tablero[y - 1][x - 1] == 'N')
			return true;
		else
			return false;
	}
 
	private boolean disparo_al_agua(int x, int y) {
		if (tablero[y - 1][x - 1] == 'V')
			return true;
		else
			return false;
	}
 
	public int disparo(int a,int b) {
	if((a>=1 && a<=cols) && (b>=1 && b<=fils)) {
	if(disparo_acertado(a,b)) {
	tablero[b-1][a-1] = 'A';
//	((Nave)flota.get(posicion_flota[b-1][a…if(((Nave)flota.get(posicion_flota[b-1…])) == 0)
	restan_naves--;
	return TIRO_ACIERTO;
	}
	else if(disparo_al_agua(a,b)) {
	tablero[b-1][a-1] = 'E';
	return TIRO_ERROR;
	}
	else
	return TIRO_NULO;
	}
	return TIRO_NULO;
}
 
	public static String tipoNave(int n) {
		if (n > 1 && n < nombre_nave.length + 2)
			return new String(nombre_nave[n - 2]);
		else
			return new String("");
	}
 
	public String naveAtacada(int a,int b) {
	if(tablero[b-1][a-1] == 'A' || tablero[b-1][a-1] == 'N')
	return (String)flota.get(posicion_flota[b-1][a-1]);
	else
	return new String("");
}
 
	public boolean naveHundida(int a,int b) {
if(tablero[b-1][a-1] == 'A' || tablero[b-1][a-1] == 'N')
if(b == 0)
return true;
return false;
}
 
	public char status_casilla(int a, int b) {
		return tablero[b - 1][a - 1];
	}
 
	public int num_filas() {
		return fils;
	}
 
	public int num_columnas() {
		return cols;
	}
 
	public boolean flota_hundida() {
		return (restan_naves <= 0);
	}
 
	public int obtener_resto() {
		return restan_naves;
	}
 
	public void display() {
		for (int i = 0; i < fils; i++) {
			for (int j = 0; j < cols; j++)
				System.out.print(tablero[i][j] + " ");
			System.out.println();
		}
		System.out.println();
	}
}
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