-6

我对代码有疑问(它是一个 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;
    }
}
4

1 回答 1

0

Simply parameterize your method gano:

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

Then overload the gano method (such that here is one without parameters) checking for the two configurations:

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

You will have to rename the constant win positions:

//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 <---

The modified code is thus something like:

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;
    }
}

On a side note, the presented code is very sloppy and shows some signs of bad design.

Other remarks:

  1. decrease code coupeling.
  2. use @Override
  3. you didn't provide iconopieza, so we cannot run the code
  4. code formatting
于 2014-07-08T06:41:16.413 回答