El problema es el siguiente, yo estuve haciendo un programa en java que se intente parecer a la aplicación 2048(juego para smartphone) donde aparece un "tablero" y algunas "fichas", la meta es ir sumando los números que sean iguales hasta llegar a 2048.
El error es que al apretar para algún lado ('a' 's' 'd' o 'w') se mueve en mal, no asía la dirección que debería.
Les dejo el código:
Código PHP:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/* Positions[][];
______ ______ ______ _______
| | | | | | | |
| 0|0 | | 0|1 | | 0|2 | | 0|3 |
|_____| |_____| |_____| |______|
______ ______ ______ _______
| | | | | | | |
| 1|0 | | 1|1 | | 1|2 | | 1|3 |
|_____| |_____| |_____| |______|
______ ______ ______ _______
| | | | | | | |
| 2|0 | | 2|1 | | 2|2 | | 2|3 |
|_____| |_____| |_____| |______|
______ ______ ______ _______
| | | | | | | |
| 3|0 | | 3|1 | | 3|2 | | 3|3 |
|_____| |_____| |_____| |______|
*/
public class Panel extends JFrame {
private int[][] Positions;
private int PosX = 50;
private int PosY = 80;
public void paint(Graphics g) {
super.paint(g);
int RandOK = 0;
do
{
Random randomGenerator = new Random();
int rand1 = randomGenerator.nextInt(3);
int rand2 = randomGenerator.nextInt(3);
if ( Positions[rand1][rand1] == 0 )
{
Positions[rand1][rand2] = 2;
RandOK = 1;
}
} while (RandOK == 0);
for (int i = 0;i < 4;i++)
{
for (int x = 0;x < 4;x++)
{
Toolkit t = Toolkit.getDefaultToolkit ();
Image imagen = t.getImage ("C:/Users/Facu/Desktop/carpeta especial para las pelotudeces que me pasa karma/2048/"+Positions[i][x]+".jpg");
g.drawImage (imagen, PosX, PosY, this);
PosX += 65;
}
PosX = 50;
PosY += 65;
}
PosX = 50;
PosY = 80;
}
public Panel()
{
KeyListener listener = new MyKeyListener();
addKeyListener(listener);
setFocusable(true);
}
public class MyKeyListener implements KeyListener {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
switch(KeyEvent.getKeyText(e.getKeyCode()))
{
case "A": moveKeys(1);
case "S": moveKeys(2);
case "D": moveKeys(3);
case "W": moveKeys(4);
}
}
public void keyReleased(KeyEvent e) {
}
}
public void moveKeys(int arg)
{
switch(arg)
{
case 1: { //Izquierda
for (int i = 0;i < 4;i++)
{
for (int x = 0;x < 4;x++)
{
if (Positions[i][x] == 0 || x == 0)
continue;
else if ( Positions[i][x] == Positions[i][x-1] )
{
Positions[i][x-1] *= 2;
Positions[i][x] = 0;
}
else if ( Positions[i][x-1] == 0 )
{
Positions[i][x-1] = Positions[i][x];
Positions[i][x] = 0;
}
}
}
}
case 2: { //Abajo
for (int i = 0;i < 4;i++)
{
for (int x = 0;x < 4;x++)
{
if (Positions[i][x] == 0 || i == 3)
continue;
else if ( Positions[i][x] == Positions[i+1][x] )
{
Positions[i+1][x] *= 2;
Positions[i][x] = 0;
}
else if ( Positions[i+1][x] == 0 )
{
Positions[i+1][x] = Positions[i][x];
Positions[i][x] = 0;
}
}
}
}
case 3: { //Derecha
for (int i = 0;i < 4;i++)
{
for (int x = 0;x < 4;x++)
{
if (Positions[i][x] == 0 || x == 3)
continue;
else if ( Positions[i][x] == Positions[i][x+1] )
{
Positions[i][x+1] *= 2;
Positions[i][x] = 0;
}
else if ( Positions[i][x+1] == 0 )
{
Positions[i][x+1] = Positions[i][x];
Positions[i][x] = 0;
}
}
}
}
case 4: { //Arriba
for (int i = 0;i < 4;i++)
{
for (int x = 0;x < 4;x++)
{
if (Positions[i][x] == 0 || i == 0)
continue;
else if ( Positions[i][x] == Positions[i-1][x] )
{
Positions[i-1][x] *= 2;
Positions[i][x] = 0;
}
else if ( Positions[i-1][x] == 0 )
{
Positions[i-1][x] = Positions[i][x];
Positions[i][x] = 0;
}
}
}
}
}
repaint();
}
public void CargarPos()
{
Positions = new int[4][4];
}
public static void main(String[] arg)
{
Panel Tablero = new Panel();
Tablero.CargarPos();
Tablero.setBounds(40, 40, 400, 400);
Tablero.setTitle("2048 - Prototype");
Tablero.setVisible(true);
}
}