修改8拼图java

时间:2014-07-08 06:28:47

标签: java 8-puzzle

我对代码有疑问(它是3x3矩阵),我必须能够以2种方式获胜(水平或圆形)

1 | 2 | 3 
4 | 5 | 6 
7 | 8 | 0 

1 | 2 | 3 
8 | 0 | 4 
7 | 6 | 5 

“0”是空白

现在我正在评论一条线(而且我一次只能赢一条路),我的问题是:我可以通过什么方式来赢​​得所描述的两种方式? 非常感谢你。

public class Juego implements ActionListener {

    private String game = "game";
    private int fila = 3;
    private int columna = 3;
//posicion ganador del juego
//Posicion ganadora "game1"
    private int[] win = {1, 2, 3, 4, 5, 6, 7, 8, 0}; // 0 = casilla vacia <----
//Posicion ganadora "game2"
//private int[] win = {1,2,3,4,0 ,6,7,8,9} ; // 0= casilla vacia <---

    private int[] pos_juego = new int[win.length];
    private iconopieza matriz[] = new iconopieza[fila * columna];

    public Juego() {
        System.out.println("Equipo:");
    }

    public void NewGame(iconopieza m[]) {
        this.matriz = m;
        llenar_tablero(win, true);
    }

    public void Comenzar() {

        for (int i = 0; i < win.length; i++) {
            matriz[i].setEnabled(true);
        }

        int[] tmp = win;
        int count = 0;
        int numRandom;
        for (int i = 0; i < pos_juego.length; i++) {
            pos_juego[i] = 0;
        }
        do {

            numRandom = (int) (Math.random() * win.length);

            if (pos_juego[numRandom] == 0) {
                pos_juego[numRandom] = tmp[count];
                count++;
            }
        } while (count < pos_juego.length);
        llenar_tablero(pos_juego, false);
    }

    public void Terminar() {
        for (int i = 0; i < win.length; i++) {
            matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/Piezas/Ext/logo.jpg")));
            matriz[i].setEnabled(false);
        }
    }

    private void llenar_tablero(int[] m, boolean band) {
        for (int i = 0; i < win.length; i++) {
            if (m[i] > -1) {
                matriz[i].setIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + m[i] + ".jpg")));
                matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + m[i] + ".jpg")));
            } else if (band) {
                matriz[i].setIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + (i + 1) + ".jpg")));
                matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + (i + 1) + ".jpg")));
            } else {
                matriz[i].setIcon(new ImageIcon(getClass().getResource("/Piezas/Ext/vacio.jpg")));
                matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/Piezas/Ext/vacio.jpg")));
            }
        }
    }

    public int getFila() {
        return this.fila;
    }

    public int getColumna() {
        return this.columna;
    }

    public void actionPerformed(ActionEvent ev) {

        String comando = ev.getActionCommand();

        int[] pos = new int[8];
        if (Integer.valueOf(comando) == columna - 1) {
            pos[0] = -1;
            pos[1] = -1;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = -1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        } else if (Integer.valueOf(comando) == (fila * columna - columna))//esquina inferior izquierda
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = -1;
            pos[4] = Integer.valueOf(comando) + 1;
            pos[5] = -1;
            pos[6] = -1;
            pos[7] = -1;
        } else if (Integer.valueOf(comando) == (fila * columna - 1))//esquina inferior derecha
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = -1;
            pos[5] = -1;
            pos[6] = -1;
            pos[7] = -1;
        } else if (Integer.valueOf(comando) % columna == 0)//primera columna
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = -1;
            pos[4] = Integer.valueOf(comando) + 1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        } else if ((Integer.valueOf(comando) + 1) % columna == 0)//ultima columna
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = -1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        } else //cualquier otra casilla
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = Integer.valueOf(comando) + 1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        }

        for (int i = 0; i < pos.length; i++) {
            if (mover(pos[i], Integer.valueOf(comando))) {
                break;
            }
        }

        llenar_tablero(pos_juego, false);

        if (gano()) {
            llenar_tablero(win, true);
            JOptionPane.showMessageDialog(null, "Has Ganado!");
            Terminar();
        }
    }

    private boolean mover(int value, int index) {
        int tmp;

        if (value >= 0 && value < fila * columna) {

            if (pos_juego[value] == -1) {
                tmp = pos_juego[value];
                pos_juego[value] = pos_juego[index];
                pos_juego[index] = tmp;
                return true;
            }
        }
        return false;
    }

    private boolean gano() {
        for (int i = 0; i < win.length; i++) {
            if (win[i] != pos_juego[i]) {
                return false;
            }
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

只需参数化您的方法gano

private boolean gano(int[] expected) {
    for ( int i=0; i < expected.length ; i++ ) {
        if(expected[i] != pos_juego[i]) {
            return false;
        }
        return true;
    }
}

然后重载gano方法(这里是一个没有参数的方法)检查​​两个配置:

private boolean gano () {
    return gano(win0) || gano(win1);
}

您必须重命名恒定胜利位置:

//Posicion ganadora "game1"
private int[] win0 = {1,2,3,4,5,6,7,8,0}; // 0 = casilla vacia <----
//Posicion ganadora "game2"
private int[] win1 = {1,2,3,4,0 ,6,7,8,9} ; // 0= casilla vacia <---

修改后的代码就像:

public class Juego implements ActionListener {

    private String game = "game";
    private int fila = 3;
    private int columna = 3;
//posicion ganador del juego
//Posicion ganadora "game1"
    private int[] win0 = {1, 2, 3, 4, 5, 6, 7, 8, 0}; // 0 = casilla vacia <----
//Posicion ganadora "game2"
    private int[] win1 = {1, 2, 3, 4, 0, 6, 7, 8, 9}; // 0= casilla vacia <---

    private int[] pos_juego = new int[win0.length];
    private iconopieza matriz[] = new iconopieza[fila * columna];

    public Juego() {
        System.out.println("Equipo:");
    }

    public void NewGame(iconopieza m[]) {
        this.matriz = m;
        llenar_tablero(win0, true);
    }

    public void Comenzar() {

        for (int i = 0; i < win0.length; i++) {
            matriz[i].setEnabled(true);
        }

        int[] tmp = win0;
        int count = 0;
        int numRandom;
        for (int i = 0; i < pos_juego.length; i++) {
            pos_juego[i] = 0;
        }
        do {

            numRandom = (int) (Math.random() * win0.length);

            if (pos_juego[numRandom] == 0) {
                pos_juego[numRandom] = tmp[count];
                count++;
            }
        } while (count < pos_juego.length);
        llenar_tablero(pos_juego, false);
    }

    public void Terminar() {
        for (int i = 0; i < win0.length; i++) {
            matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/Piezas/Ext/logo.jpg")));
            matriz[i].setEnabled(false);
        }
    }

    private void llenar_tablero(int[] m, boolean band) {
        for (int i = 0; i < win0.length; i++) {
            if (m[i] > -1) {
                matriz[i].setIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + m[i] + ".jpg")));
                matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + m[i] + ".jpg")));
            } else if (band) {
                matriz[i].setIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + (i + 1) + ".jpg")));
                matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/puzzle/" + game + "/" + (i + 1) + ".jpg")));
            } else {
                matriz[i].setIcon(new ImageIcon(getClass().getResource("/Piezas/Ext/vacio.jpg")));
                matriz[i].setDisabledIcon(new ImageIcon(getClass().getResource("/Piezas/Ext/vacio.jpg")));
            }
        }
    }

    public int getFila() {
        return this.fila;
    }

    public int getColumna() {
        return this.columna;
    }

    public void actionPerformed(ActionEvent ev) {

        String comando = ev.getActionCommand();

        int[] pos = new int[8];
        if (Integer.valueOf(comando) == columna - 1) {
            pos[0] = -1;
            pos[1] = -1;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = -1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        } else if (Integer.valueOf(comando) == (fila * columna - columna))//esquina inferior izquierda
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = -1;
            pos[4] = Integer.valueOf(comando) + 1;
            pos[5] = -1;
            pos[6] = -1;
            pos[7] = -1;
        } else if (Integer.valueOf(comando) == (fila * columna - 1))//esquina inferior derecha
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = -1;
            pos[5] = -1;
            pos[6] = -1;
            pos[7] = -1;
        } else if (Integer.valueOf(comando) % columna == 0)//primera columna
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = -1;
            pos[4] = Integer.valueOf(comando) + 1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        } else if ((Integer.valueOf(comando) + 1) % columna == 0)//ultima columna
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = -1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        } else //cualquier otra casilla
        {
            pos[0] = -1;
            pos[1] = Integer.valueOf(comando) - columna;
            pos[2] = -1;
            pos[3] = Integer.valueOf(comando) - 1;
            pos[4] = Integer.valueOf(comando) + 1;
            pos[5] = -1;
            pos[6] = Integer.valueOf(comando) + columna;
            pos[7] = -1;
        }

        for (int i = 0; i < pos.length; i++) {
            if (mover(pos[i], Integer.valueOf(comando))) {
                break;
            }
        }

        llenar_tablero(pos_juego, false);

        if (gano()) {
            llenar_tablero(win0, true);
            JOptionPane.showMessageDialog(null, "Has Ganado!");
            Terminar();
        }
    }

    private boolean mover(int value, int index) {
        int tmp;

        if (value >= 0 && value < fila * columna) {

            if (pos_juego[value] == -1) {
                tmp = pos_juego[value];
                pos_juego[value] = pos_juego[index];
                pos_juego[index] = tmp;
                return true;
            }
        }
        return false;
    }

    private boolean gano() {
        return gano(win0) || gano(win1);
    }

    private boolean gano(int[] expected) {
        for (int i = 0; i < expected.length; i++) {
            if (expected[i] != pos_juego[i]) {
                return false;
            }
        }
        return true;
    }
}

另一方面,所提出的代码非常草率,并显示出一些设计不良的迹象。

其他评论:

  1. 减少代码coupeling。
  2. 使用@Override
  3. 您没有提供iconopieza,因此我们无法运行代码
  4. 代码格式