Java - URGENTE 2048 en consola

 
Vista:
sin imagen de perfil

URGENTE 2048 en consola

Publicado por Daniel (1 intervención) el 08/12/2014 08:59:42
Asi con esos metodos que ya tiene sin agregarle mas ocupo terminar este programa ayuda porfavor.
Agradeceria mucho la ayuda.

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
import java.util.Random;
import java.util.Scanner;
 
import javax.swing.JOptionPane;
public class Tablero {
	private Celda[][] cuadros=new Celda [4][4];
	private static final int GRID_WIDTH = 4;
	private int	score;
	private int currentScore;
	private int currentCell;
	private boolean gano,
	perdio;
 
	public Tablero(){
		this.score = 0;
		this.gano =	false;
		this.perdio	= false;
	}
 
	//Constructor con parametros. 
	//Se inicializa el tablero.
	public Tablero(int[]celda){
		if(celda.length==0){
			this.generarNumero();
			this.generarNumero();
 
			int count =	0;
			for (int i=0;i<this.cuadros.length; i++){
				for (int j=0;j<this.cuadros[i].length;j++){
					if(this.cuadros[i][j] == null) this.cuadros[i][j]= new Celda(0);
					if(!(i==this.cuadros.length-1 && j==this.cuadros[i].length-1)){
						count++;
					}
				}
			}
		}
		else{
			int count =	0;
			for (int j=0;j<this.cuadros.length; j++){
				for (int k=0;k<this.cuadros[j].length;k++){
					this.cuadros[j][k]= new	Celda(celda[count]);
					if(!(j==this.cuadros.length-1 && k==this.cuadros[j].length-1)){
						count++;
					}
				}
			}
 
		}
 
		this.score = 0;
		this.gano =	false;
		this.perdio	= false;
 
	}
 
	//Genera dos numeros random en el tablero 2 o 4.
	public void generarNumero(){
		Random r=new Random();
		int i = r.nextInt(this.cuadros.length-1);
 
		Random r2=new Random();
		int j = r2.nextInt(this.cuadros[i].length-1);
		this.cuadros[i][j]=new Celda();
	}
 
	//Imprime el tablero en consola.
	public void imprimeTablero(){
		for(int i=0;i<this.cuadros.length;i++){
			for(int j=0;j<this.cuadros[i].length;j++){
				System.out.print("|"+this.cuadros[i][j].getValor()+"|");
			}
			System.out.println();
		}
	}
 
 
 
	//Detecta el movimiento conforme a la letra, acepta mayusculas y minusculas. w es arriba, s es abajo, a es izquierda, d es derecha.
	public void pedirMovimiento(){
		Scanner sc = new Scanner(System.in);
		String result = sc.next();
		if(result.equals("w")||result.equals("W")){
			arriba();
		}
		if(result.equals("s")||result.equals("S")){
			this.generarNumero();
			abajo();
		}
		if(result.equals("a")||result.equals("A")){
			this.generarNumero();
			izquierda();
		}
		if(result.equals("d")||result.equals("D")){
			this.generarNumero();
			derecha();
		}
	}
 
	//Metodos de arriba,abajo,izquierda y derecha.
	public void arriba(){
		int c = 0;
		boolean move = false;
		for(int z=0;z<3;z++){
			int zz = z +1;
			for(int t=0;t<3;t++){
				Celda celdaActual = this.cuadros[z][t];
				Celda celdaSuperior = this.cuadros[zz][t];
				if (celdaActual.getValor() == celdaSuperior.getValor()){
					if(celdaActual.getValor() != 0 && celdaSuperior.getValor() != 0){
						//incrementa
						celdaSuperior.incrementa();
						c++;
					}
					move = true;
				}
				else if(celdaSuperior.getValor() == 0){
					this.cuadros[zz][t] = celdaActual;
					if(z > 0) this.cuadros[z][t] = this.cuadros[z-1][t];
				}
			}
		}
		if(move){
			this.generarNumero();
		}
		System.out.println("Sume "+c);
	}
 
 
 
 
	public void abajo(){
		System.out.println("aba");
	}
 
	public void izquierda(){
		System.out.println("izq");
	}
	public void derecha(){
		System.out.println("der");
	}
	public boolean pierde(){
		return false;
	}
 
 
 
 
 
	public boolean gano(int [][] a){
		for(int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				if(a[i][j]==2048){
					gano=true;
				}
				else{
					gano=false;
				}
			}
		}
		return gano;
 
	}
 
 
 
 
	public static void main(String args[]){
		int [] arreglo={};
		Tablero juego=new Tablero(arreglo);
		juego.imprimeTablero();
 
 
		for(int i=0; i<16; i++){
			juego.pedirMovimiento();
			juego.imprimeTablero();
		}
 
 
 
 
 
	}
}
 
 
--------------------------------------------------
import java.awt.Point;
import java.util.Random;
import java.util.Random;
 
//Clase Celda.
public class Celda{
 
   private int valor;
 
 
//Constructor por default.
   public Celda(){
      Random valor2o4 = new Random ();
      this.valor = (valor2o4.nextInt(2)+1)*2;
   }
 
//Constructor con parametros.   
   public Celda (int valorPoner){
      this.valor = valorPoner;
   }
 
//Getter y Setter de Valor.
   public int getValor(){
	   return this.valor;
   }
 
   public void setValor(int valor){
	this.valor=valor;
   }
 
 
 
 
//Metodo que incrementa el doble de cualquier valor.
   public int incrementa(){
      return this.valor*=2;
   }
 
 
 
 
}
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