elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


  Mostrar Temas
Páginas: [1]
1  Programación / Java / 2048 en consola JAVA en: 8 Diciembre 2014, 09:41 am
Estoy haciendo un 2048 en consola, esto es lo que llevo:

Código
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. import javax.swing.JOptionPane;
  5. public class Tablero {
  6. private Celda[][] cuadros=new Celda [4][4];
  7.  
  8. private int score;
  9.  
  10. private boolean gano,
  11. perdio;
  12.  
  13. public Tablero(){
  14. this.score = 0;
  15. this.gano = false;
  16. this.perdio = false;
  17. }
  18.  
  19. //Constructor con parametros.
  20. //Se inicializa el tablero.
  21. public Tablero(int[]celda){  
  22. if(celda.length==0){
  23. this.generarNumero();
  24. this.generarNumero();
  25.  
  26. int count = 0;
  27. for (int i=0;i<this.cuadros.length; i++){
  28. for (int j=0;j<this.cuadros[i].length;j++){
  29. if(this.cuadros[i][j] == null) this.cuadros[i][j]= new Celda(0);
  30. if(!(i==this.cuadros.length-1 && j==this.cuadros[i].length-1)){
  31. count++;  
  32. }
  33. }
  34. }
  35. }
  36. else{
  37. int count = 0;
  38. for (int j=0;j<this.cuadros.length; j++){
  39. for (int k=0;k<this.cuadros[j].length;k++){
  40. this.cuadros[j][k]= new Celda(celda[count]);
  41. if(!(j==this.cuadros.length-1 && k==this.cuadros[j].length-1)){
  42. count++;  
  43. }
  44. }
  45. }
  46.  
  47. }
  48.  
  49. this.score = 0;
  50. this.gano = false;
  51. this.perdio = false;
  52.  
  53. }
  54.  
  55. //Genera dos numeros random en el tablero 2 o 4.
  56. public void generarNumero(){
  57. Random r=new Random();
  58. int i = r.nextInt(this.cuadros.length-1);
  59.  
  60. Random r2=new Random();
  61. int j = r2.nextInt(this.cuadros[i].length-1);
  62. this.cuadros[i][j]=new Celda();
  63. }
  64.  
  65. //Imprime el tablero en consola.
  66. public void imprimeTablero(){
  67. for(int i=0;i<this.cuadros.length;i++){
  68. for(int j=0;j<this.cuadros[i].length;j++){
  69. System.out.print("|"+this.cuadros[i][j].getValor()+"|");
  70. }
  71. System.out.println();
  72. }
  73. }
  74.  
  75.  
  76.  
  77. //Detecta el movimiento conforme a la letra, acepta mayusculas y minusculas. w es arriba, s es abajo, a es izquierda, d es derecha.
  78. public void pedirMovimiento(){
  79. Scanner sc = new Scanner(System.in);
  80. String result = sc.next();
  81. if(result.equals("w")||result.equals("W")){
  82. arriba();
  83. }
  84. if(result.equals("s")||result.equals("S")){
  85. this.generarNumero();
  86. abajo();
  87. }
  88. if(result.equals("a")||result.equals("A")){
  89. this.generarNumero();
  90. izquierda();
  91. }
  92. if(result.equals("d")||result.equals("D")){
  93. this.generarNumero();
  94. derecha();
  95. }
  96. }
  97.  
  98. //Metodos de arriba,abajo,izquierda y derecha.
  99. public void arriba(){
  100. int c = 0;
  101. boolean move = false;
  102. for(int z=0;z<3;z++){
  103. int zz = z +1;
  104. for(int t=0;t<3;t++){
  105. Celda celdaActual = this.cuadros[z][t];
  106. Celda celdaSuperior = this.cuadros[zz][t];
  107. if (celdaActual.getValor() == celdaSuperior.getValor()){
  108. if(celdaActual.getValor() != 0 && celdaSuperior.getValor() != 0){
  109. //incrementa
  110. celdaSuperior.incrementa();
  111. c++;
  112. }
  113. move = true;
  114. }
  115. else if(celdaSuperior.getValor() == 0){
  116. this.cuadros[zz][t] = celdaActual;
  117. if(z > 0) this.cuadros[z][t] = this.cuadros[z-1][t];
  118. }
  119. }
  120. }
  121. if(move){
  122. this.generarNumero();
  123. }
  124. System.out.println("Sume "+c);
  125. }
  126.  
  127.  
  128.  
  129.  
  130. public void abajo(){
  131. System.out.println("aba");
  132. }
  133.  
  134. public void izquierda(){
  135. System.out.println("izq");
  136. }
  137. public void derecha(){
  138. System.out.println("der");
  139. }
  140. public boolean pierde(){
  141. return false;
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. public boolean gano(int [][] a){
  149. for(int i = 0; i < a.length; i++) {
  150. for (int j = 0; j < a[i].length; j++) {
  151. if(a[i][j]==2048){
  152. gano=true;
  153. }
  154. else{
  155. gano=false;
  156. }
  157. }
  158. }
  159. return gano;
  160.  
  161. }
  162.  
  163.  
  164.  
  165.  
  166. public static void main(String args[]){
  167. int [] arreglo={};
  168. Tablero juego=new Tablero(arreglo);
  169. juego.imprimeTablero();
  170.  
  171.  
  172. for(int i=0; i<16; i++){
  173. juego.pedirMovimiento();
  174. juego.imprimeTablero();
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. }
  182. }
  183.  
  184.  
  185. --------------------------------------------------------------------------------------
  186.  
  187.  
  188. import java.awt.Point;
  189. import java.util.Random;
  190. import java.util.Random;
  191.  
  192. //Clase Celda.
  193. public class Celda{
  194.  
  195.   private int valor;
  196.  
  197.  
  198. //Constructor por default.
  199.   public Celda(){
  200.      Random valor2o4 = new Random ();
  201.      this.valor = (valor2o4.nextInt(2)+1)*2;
  202.   }
  203.  
  204. //Constructor con parametros.  
  205.   public Celda (int valorPoner){
  206.      this.valor = valorPoner;  
  207.   }
  208.  
  209. //Getter y Setter de Valor.
  210.   public int getValor(){
  211.   return this.valor;
  212.   }
  213.  
  214.   public void setValor(int valor){
  215. this.valor=valor;
  216.   }
  217.  
  218.  
  219.  
  220.  
  221. //Metodo que incrementa el doble de cualquier valor.
  222.   public int incrementa(){
  223.      return this.valor*=2;
  224.   }
  225.  
  226.  
  227.  
  228.  
  229. }


Quisiera saber en que estoy mal y que me falta, muchas gracias.



Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines