Skip to main content
edited tags; edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Rollback to Revision 4
Source Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309
try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(insertTableSQL);
            byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
    
           static StateSet<State> sSET = new State(0, initialState);
            QUEUE.addHashSet<State>(s);
            State current = null;{
         private static final intlong numInsertedserialVersionUID = 0;1L;

            while (!QUEUE.isEmpty()) {
                numInserted++;@Override
               public ifboolean contains(numInserted % 20000 ==Object 0obj) {
                    System.out.println(numInserted + "State "v += current.getMoves()State);
                }obj;
                current =for QUEUE.remove();
             State mad : SET.add(currentthis); {
                preparedStatementif (Arrays.setBytesequals(1, currentmad.getState());
                preparedStatement.setInt(2, currentv.getMovesgetState());) {

                preparedStatement.executeUpdate();
                for (State n : findNeighbours(current)) {
                    if (!SET.contains(n)) {
                        SET.add(n);
                        if (!QUEUE.contains(n)) {
                            QUEUE.add(n);
                        }
                   return }true;
                }
            }
            System.out.println("Recorded inserted :D");

        } catch (SQLException e) {
            System.out.println(e);
        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }
            return false;
        }

public static ArrayList<State> findNeighbours(State currentState) {

    byte[] state = currentState.getState();
    ArrayList<State> neighbours = new ArrayList<>();
    for (int i = 0; i < state.length; i++) {
        if (state[i] == 0) {
            if (i % 4 != 0) {
                byte[] left = new byte[16];
                System.arraycopy(state, 0, left, 0, left.length);
                byte temp = left[i];
                left[i] = left[i - 1];
                left[i - 1] = temp;
                State s = new State(currentState.getMoves() + 1, left);
                neighbours.add(s);

            }
            if (i % 4 != 3) {
                byte[] right = new byte[16];
                System.arraycopy(state, 0, right, 0, right.length);

                byte temp = right[i];
                right[i] = right[i + 1];
                right[i + 1] = temp;
                State s = new State(currentState.getMoves() + 1, right);
                neighbours.add(s);

            }
            if (i > 3) {
;
               static byte[]Queue<State> upQUEUE = new byte[16];
                System.arraycopyLinkedList<>(state, 0, up, 0, up.length);
                byte temp = up[i];
                up[i] = up[i - 4];
                up[i - 4] = temp;

                State spublic =static newvoid State(currentState.getMovesmain() + 1,String[] upargs);
 throws SQLException {
        // TODO code application logic neighbours.add(s);here

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;
        String insertTableSQL = "INSERT INTO six_six_three.six"
                + "(STATE, VALUE) VALUES"
                + "(?,?)";
    try {
                dbConnection = getDBConnection();
                preparedStatement = dbConnection.prepareStatement(insertTableSQL);
                byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
        
                State s = new State(0, initialState);
                QUEUE.add(s);
                State current = null;
                int numInserted = 0;
    
                while (!QUEUE.isEmpty()) {
                    numInserted++;
                    if (numInserted % 20000 == 0) {
                        System.out.println(numInserted + " " + current.getMoves());
                    }
                    current = QUEUE.remove();
                    SET.add(current);
                    preparedStatement.setBytes(1, current.getState());
                    preparedStatement.setInt(2, current.getMoves());
    
                    preparedStatement.executeUpdate();
                    for (State n : findNeighbours(current)) {
                        if (!SET.contains(n)) {
                            SET.add(n);
                            if (!QUEUE.contains(n)) {
                                QUEUE.add(n);
                            }
                        }
                    }
                }
                System.out.println("Recorded inserted :D");
    
            } catch (SQLException e) {
                System.out.println(e);
            } finally {
    
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
    
                if (dbConnection != null) {
                    dbConnection.close();
                }
    
            }
            if (i < 12) {
                byte[] down = new byte[16];
                System.arraycopy(state, 0, down, 0, down.length);
                byte temp = down[i];
                down[i] = down[i + 4];
                down[i + 4] = temp;
                State s = new State(currentState.getMoves() + 1, down);
                neighbours.add(s);

    public static ArrayList<State> findNeighbours(State currentState) {
    
        byte[] state = currentState.getState();
        ArrayList<State> neighbours = new ArrayList<>();
        for (int i = 0; i < state.length; i++) {
            if (state[i] == 0) {
                if (i % 4 != 0) {
                    byte[] left = new byte[16];
                    System.arraycopy(state, 0, left, 0, left.length);
                    byte temp = left[i];
                    left[i] = left[i - 1];
                    left[i - 1] = temp;
                    State s = new State(currentState.getMoves() + 1, left);
                    neighbours.add(s);
    
                }
                if (i % 4 != 3) {
                    byte[] right = new byte[16];
                    System.arraycopy(state, 0, right, 0, right.length);
    
                    byte temp = right[i];
                    right[i] = right[i + 1];
                    right[i + 1] = temp;
                    State s = new State(currentState.getMoves() + 1, right);
                    neighbours.add(s);
    
                }
                if (i > 3) {
    
                    byte[] up = new byte[16];
                    System.arraycopy(state, 0, up, 0, up.length);
                    byte temp = up[i];
                    up[i] = up[i - 4];
                    up[i - 4] = temp;
    
                    State s = new State(currentState.getMoves() + 1, up);
                    neighbours.add(s);
    
                }
                if (i < 12) {
                    byte[] down = new byte[16];
                    System.arraycopy(state, 0, down, 0, down.length);
                    byte temp = down[i];
                    down[i] = down[i + 4];
                    down[i + 4] = temp;
                    State s = new State(currentState.getMoves() + 1, down);
                    neighbours.add(s);
    
                }
    
            }

         }
    }
    return neighbours;
}

public static class State {

    int moves;
    byte[] composition;

    public State(int moves, byte[] composition) {
        this.moves = moves;
        this.composition = composition;
    }

    public int getMoves() {
        return moves;
    }

    public void setMoves(int moves) {
        this.moves = moves;
    }
    
    public static class State {
    
        int moves;
        byte[] composition;
    
        public State(int moves, byte[] composition) {
            this.moves = moves;
            this.composition = composition;
        }
    
        public int getMoves() {
            return moves;
        }
    
        public void setMoves(int moves) {
            this.moves = moves;
        }
    
        public byte[] getState() {
            return composition;
        }
    }
}
try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(insertTableSQL);
            byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
    
            State s = new State(0, initialState);
            QUEUE.add(s);
            State current = null;
            int numInserted = 0;

            while (!QUEUE.isEmpty()) {
                numInserted++;
                if (numInserted % 20000 == 0) {
                    System.out.println(numInserted + " " + current.getMoves());
                }
                current = QUEUE.remove();
                SET.add(current);
                preparedStatement.setBytes(1, current.getState());
                preparedStatement.setInt(2, current.getMoves());

                preparedStatement.executeUpdate();
                for (State n : findNeighbours(current)) {
                    if (!SET.contains(n)) {
                        SET.add(n);
                        if (!QUEUE.contains(n)) {
                            QUEUE.add(n);
                        }
                    }
                }
            }
            System.out.println("Recorded inserted :D");

        } catch (SQLException e) {
            System.out.println(e);
        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

public static ArrayList<State> findNeighbours(State currentState) {

    byte[] state = currentState.getState();
    ArrayList<State> neighbours = new ArrayList<>();
    for (int i = 0; i < state.length; i++) {
        if (state[i] == 0) {
            if (i % 4 != 0) {
                byte[] left = new byte[16];
                System.arraycopy(state, 0, left, 0, left.length);
                byte temp = left[i];
                left[i] = left[i - 1];
                left[i - 1] = temp;
                State s = new State(currentState.getMoves() + 1, left);
                neighbours.add(s);

            }
            if (i % 4 != 3) {
                byte[] right = new byte[16];
                System.arraycopy(state, 0, right, 0, right.length);

                byte temp = right[i];
                right[i] = right[i + 1];
                right[i + 1] = temp;
                State s = new State(currentState.getMoves() + 1, right);
                neighbours.add(s);

            }
            if (i > 3) {

                byte[] up = new byte[16];
                System.arraycopy(state, 0, up, 0, up.length);
                byte temp = up[i];
                up[i] = up[i - 4];
                up[i - 4] = temp;

                State s = new State(currentState.getMoves() + 1, up);
                neighbours.add(s);

            }
            if (i < 12) {
                byte[] down = new byte[16];
                System.arraycopy(state, 0, down, 0, down.length);
                byte temp = down[i];
                down[i] = down[i + 4];
                down[i + 4] = temp;
                State s = new State(currentState.getMoves() + 1, down);
                neighbours.add(s);

            }

         }
    }
    return neighbours;
}

public static class State {

    int moves;
    byte[] composition;

    public State(int moves, byte[] composition) {
        this.moves = moves;
        this.composition = composition;
    }

    public int getMoves() {
        return moves;
    }

    public void setMoves(int moves) {
        this.moves = moves;
    }

    public byte[] getState() {
        return composition;
    }
}
 static Set<State> SET = new HashSet<State>() {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean contains(Object obj) {
            State v = (State) obj;
            for (State mad : this) {
                if (Arrays.equals(mad.getState(), v.getState())) {

                    return true;
                }
            }
            return false;
        }

    };
    static Queue<State> QUEUE = new LinkedList<>();

    public static void main(String[] args) throws SQLException {
        // TODO code application logic here

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;
        String insertTableSQL = "INSERT INTO six_six_three.six"
                + "(STATE, VALUE) VALUES"
                + "(?,?)";
    try {
                dbConnection = getDBConnection();
                preparedStatement = dbConnection.prepareStatement(insertTableSQL);
                byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
        
                State s = new State(0, initialState);
                QUEUE.add(s);
                State current = null;
                int numInserted = 0;
    
                while (!QUEUE.isEmpty()) {
                    numInserted++;
                    if (numInserted % 20000 == 0) {
                        System.out.println(numInserted + " " + current.getMoves());
                    }
                    current = QUEUE.remove();
                    SET.add(current);
                    preparedStatement.setBytes(1, current.getState());
                    preparedStatement.setInt(2, current.getMoves());
    
                    preparedStatement.executeUpdate();
                    for (State n : findNeighbours(current)) {
                        if (!SET.contains(n)) {
                            SET.add(n);
                            if (!QUEUE.contains(n)) {
                                QUEUE.add(n);
                            }
                        }
                    }
                }
                System.out.println("Recorded inserted :D");
    
            } catch (SQLException e) {
                System.out.println(e);
            } finally {
    
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
    
                if (dbConnection != null) {
                    dbConnection.close();
                }
    
            }

    public static ArrayList<State> findNeighbours(State currentState) {
    
        byte[] state = currentState.getState();
        ArrayList<State> neighbours = new ArrayList<>();
        for (int i = 0; i < state.length; i++) {
            if (state[i] == 0) {
                if (i % 4 != 0) {
                    byte[] left = new byte[16];
                    System.arraycopy(state, 0, left, 0, left.length);
                    byte temp = left[i];
                    left[i] = left[i - 1];
                    left[i - 1] = temp;
                    State s = new State(currentState.getMoves() + 1, left);
                    neighbours.add(s);
    
                }
                if (i % 4 != 3) {
                    byte[] right = new byte[16];
                    System.arraycopy(state, 0, right, 0, right.length);
    
                    byte temp = right[i];
                    right[i] = right[i + 1];
                    right[i + 1] = temp;
                    State s = new State(currentState.getMoves() + 1, right);
                    neighbours.add(s);
    
                }
                if (i > 3) {
    
                    byte[] up = new byte[16];
                    System.arraycopy(state, 0, up, 0, up.length);
                    byte temp = up[i];
                    up[i] = up[i - 4];
                    up[i - 4] = temp;
    
                    State s = new State(currentState.getMoves() + 1, up);
                    neighbours.add(s);
    
                }
                if (i < 12) {
                    byte[] down = new byte[16];
                    System.arraycopy(state, 0, down, 0, down.length);
                    byte temp = down[i];
                    down[i] = down[i + 4];
                    down[i + 4] = temp;
                    State s = new State(currentState.getMoves() + 1, down);
                    neighbours.add(s);
    
                }
    
            }
        }
        return neighbours;
    }
    
    public static class State {
    
        int moves;
        byte[] composition;
    
        public State(int moves, byte[] composition) {
            this.moves = moves;
            this.composition = composition;
        }
    
        public int getMoves() {
            return moves;
        }
    
        public void setMoves(int moves) {
            this.moves = moves;
        }
    
        public byte[] getState() {
            return composition;
        }
    }
Rollback to Revision 3
Source Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309
try static{
 Set<State> SET          dbConnection = newgetDBConnection();
 HashSet<State>           preparedStatement = dbConnection.prepareStatement(insertTableSQL);
            byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
     
    private static final long serialVersionUID    State s = 1L;new State(0, initialState);
            QUEUE.add(s);
            State current = null;
            int numInserted = 0;

        @Override    while (!QUEUE.isEmpty()) {
        public boolean contains(Object obj) {    numInserted++;
            State v =  if (StatenumInserted % 20000 == 0) obj;{
            for        System.out.println(StatenumInserted mad+ :" this" + current.getMoves());
 {               }
                ifcurrent (Arrays= QUEUE.equalsremove(mad);
                SET.getStateadd(current);
                preparedStatement.setBytes(1, vcurrent.getState()));
 {               preparedStatement.setInt(2, current.getMoves());

                preparedStatement.executeUpdate();
    return true;           for (State n : findNeighbours(current)) {
                    if (!SET.contains(n)) {
                        SET.add(n);
                        if (!QUEUE.contains(n)) {
                            QUEUE.add(n);
                        }
                    }
                }
            }
            returnSystem.out.println("Recorded false;inserted :D");

        } catch (SQLException e) {
            System.out.println(e);
        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

    };
   public static Queue<State> QUEUE = newArrayList<State> LinkedList<>findNeighbours(State currentState); {

    publicbyte[] staticstate void= maincurrentState.getState(String[]);
 args   ArrayList<State> neighbours = new ArrayList<>();
 throws SQLException  for (int i = 0; i < state.length; i++) {
        //if TODO(state[i] code== application0) logic{
 here           if (i % 4 != 0) {
                byte[] left = new byte[16];
                System.arraycopy(state, 0, left, 0, left.length);
                byte temp = left[i];
                left[i] = left[i - 1];
                left[i - 1] = temp;
                State s = new State(currentState.getMoves() + 1, left);
                neighbours.add(s);

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;
        String insertTableSQL = "INSERT INTO six_six_three.six"
                + "(STATE, VALUE) VALUES"
                + "(?,?)";
    try {
                dbConnection = getDBConnection();
                preparedStatement = dbConnection.prepareStatement(insertTableSQL);
                byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
        
                State s = new State(0, initialState);
                QUEUE.add(s);
                State current = null;
                int numInserted = 0;
    
                while (!QUEUE.isEmpty()) {
                    numInserted++;
                    if (numInserted % 20000 == 0) {
                        System.out.println(numInserted + " " + current.getMoves());
                    }
                    current = QUEUE.remove();
                    SET.add(current);
                    preparedStatement.setBytes(1, current.getState());
                    preparedStatement.setInt(2, current.getMoves());
    
                    preparedStatement.executeUpdate();
                    for (State n : findNeighbours(current)) {
                        if (!SET.contains(n)) {
                            SET.add(n);
                            if (!QUEUE.contains(n)) {
                                QUEUE.add(n);
                            }
                        }
                    }
                }
                System.out.println("Recorded inserted :D");
    
            } catch (SQLException e) {
                System.out.println(e);
            } finally {
    
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
    
                if (dbConnection != null) {
                    dbConnection.close();
                }
    
            }
            if (i % 4 != 3) {
                byte[] right = new byte[16];
                System.arraycopy(state, 0, right, 0, right.length);

    public static ArrayList<State> findNeighbours(State currentState) {
    
        byte[] state = currentState.getState();
        ArrayList<State> neighbours = new ArrayList<>();
        for (int i = 0; i < state.length; i++) {
            if (state[i] == 0) {
                if (i % 4 != 0) {
                    byte[] left = new byte[16];
                    System.arraycopy(state, 0, left, 0, left.length);
                    byte temp = left[i];
                    left[i] = left[i - 1];
                    left[i - 1] = temp;
                    State s = new State(currentState.getMoves() + 1, left);
                    neighbours.add(s);
    
                }
                if (i % 4 != 3) {
                    byte[] right = new byte[16];
                    System.arraycopy(state, 0, right, 0, right.length);
    
                    byte temp = right[i];
                    right[i] = right[i + 1];
                    right[i + 1] = temp;
                    State s = new State(currentState.getMoves() + 1, right);
                    neighbours.add(s);
    
                }
                if (i > 3) {
    
                    byte[] up = new byte[16];
                    System.arraycopy(state, 0, up, 0, up.length);
                    byte temp = up[i];
                    up[i] = up[i - 4];
                    up[i - 4] = temp;
    
                    State s = new State(currentState.getMoves() + 1, up);
                    neighbours.add(s);
    
                }
                if (i < 12) {
                    byte[] down = new byte[16];
                    System.arraycopy(state, 0, down, 0, down.length);
                    byte temp = down[i];
                    down[i] = down[i + 4];
                    down[i + 4] = temp;
                    State s = new State(currentState.getMoves() + 1, down);
                    neighbours.add(s);
    
                }
    
            }
 
        }
        return neighbours;
    }
    
   return neighbours;
}

public static class State {
    
        int moves;
        byte[] composition;
    
        public State(int moves, byte[] composition) {
            this.moves = moves;
            this.composition = composition;
        }
    
        public int getMoves() {
            return moves;
        }
    
        public void setMoves(int moves) {
            this.moves = moves;
        }
    
        public byte[] getState() {
            return composition;
        }
    }

    public void setMoves(int moves) {
        this.moves = moves;
    }

    public byte[] getState() {
        return composition;
    }
}
 static Set<State> SET = new HashSet<State>() {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean contains(Object obj) {
            State v = (State) obj;
            for (State mad : this) {
                if (Arrays.equals(mad.getState(), v.getState())) {

                    return true;
                }
            }
            return false;
        }

    };
    static Queue<State> QUEUE = new LinkedList<>();

    public static void main(String[] args) throws SQLException {
        // TODO code application logic here

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;
        String insertTableSQL = "INSERT INTO six_six_three.six"
                + "(STATE, VALUE) VALUES"
                + "(?,?)";
    try {
                dbConnection = getDBConnection();
                preparedStatement = dbConnection.prepareStatement(insertTableSQL);
                byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
        
                State s = new State(0, initialState);
                QUEUE.add(s);
                State current = null;
                int numInserted = 0;
    
                while (!QUEUE.isEmpty()) {
                    numInserted++;
                    if (numInserted % 20000 == 0) {
                        System.out.println(numInserted + " " + current.getMoves());
                    }
                    current = QUEUE.remove();
                    SET.add(current);
                    preparedStatement.setBytes(1, current.getState());
                    preparedStatement.setInt(2, current.getMoves());
    
                    preparedStatement.executeUpdate();
                    for (State n : findNeighbours(current)) {
                        if (!SET.contains(n)) {
                            SET.add(n);
                            if (!QUEUE.contains(n)) {
                                QUEUE.add(n);
                            }
                        }
                    }
                }
                System.out.println("Recorded inserted :D");
    
            } catch (SQLException e) {
                System.out.println(e);
            } finally {
    
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
    
                if (dbConnection != null) {
                    dbConnection.close();
                }
    
            }

    public static ArrayList<State> findNeighbours(State currentState) {
    
        byte[] state = currentState.getState();
        ArrayList<State> neighbours = new ArrayList<>();
        for (int i = 0; i < state.length; i++) {
            if (state[i] == 0) {
                if (i % 4 != 0) {
                    byte[] left = new byte[16];
                    System.arraycopy(state, 0, left, 0, left.length);
                    byte temp = left[i];
                    left[i] = left[i - 1];
                    left[i - 1] = temp;
                    State s = new State(currentState.getMoves() + 1, left);
                    neighbours.add(s);
    
                }
                if (i % 4 != 3) {
                    byte[] right = new byte[16];
                    System.arraycopy(state, 0, right, 0, right.length);
    
                    byte temp = right[i];
                    right[i] = right[i + 1];
                    right[i + 1] = temp;
                    State s = new State(currentState.getMoves() + 1, right);
                    neighbours.add(s);
    
                }
                if (i > 3) {
    
                    byte[] up = new byte[16];
                    System.arraycopy(state, 0, up, 0, up.length);
                    byte temp = up[i];
                    up[i] = up[i - 4];
                    up[i - 4] = temp;
    
                    State s = new State(currentState.getMoves() + 1, up);
                    neighbours.add(s);
    
                }
                if (i < 12) {
                    byte[] down = new byte[16];
                    System.arraycopy(state, 0, down, 0, down.length);
                    byte temp = down[i];
                    down[i] = down[i + 4];
                    down[i + 4] = temp;
                    State s = new State(currentState.getMoves() + 1, down);
                    neighbours.add(s);
    
                }
    
            }
        }
        return neighbours;
    }
    
    public static class State {
    
        int moves;
        byte[] composition;
    
        public State(int moves, byte[] composition) {
            this.moves = moves;
            this.composition = composition;
        }
    
        public int getMoves() {
            return moves;
        }
    
        public void setMoves(int moves) {
            this.moves = moves;
        }
    
        public byte[] getState() {
            return composition;
        }
    }
try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(insertTableSQL);
            byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
     
            State s = new State(0, initialState);
            QUEUE.add(s);
            State current = null;
            int numInserted = 0;

            while (!QUEUE.isEmpty()) {
                numInserted++;
                if (numInserted % 20000 == 0) {
                    System.out.println(numInserted + " " + current.getMoves());
                }
                current = QUEUE.remove();
                SET.add(current);
                preparedStatement.setBytes(1, current.getState());
                preparedStatement.setInt(2, current.getMoves());

                preparedStatement.executeUpdate();
                for (State n : findNeighbours(current)) {
                    if (!SET.contains(n)) {
                        SET.add(n);
                        if (!QUEUE.contains(n)) {
                            QUEUE.add(n);
                        }
                    }
                }
            }
            System.out.println("Recorded inserted :D");

        } catch (SQLException e) {
            System.out.println(e);
        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

public static ArrayList<State> findNeighbours(State currentState) {

    byte[] state = currentState.getState();
    ArrayList<State> neighbours = new ArrayList<>();
    for (int i = 0; i < state.length; i++) {
        if (state[i] == 0) {
            if (i % 4 != 0) {
                byte[] left = new byte[16];
                System.arraycopy(state, 0, left, 0, left.length);
                byte temp = left[i];
                left[i] = left[i - 1];
                left[i - 1] = temp;
                State s = new State(currentState.getMoves() + 1, left);
                neighbours.add(s);

            }
            if (i % 4 != 3) {
                byte[] right = new byte[16];
                System.arraycopy(state, 0, right, 0, right.length);

                byte temp = right[i];
                right[i] = right[i + 1];
                right[i + 1] = temp;
                State s = new State(currentState.getMoves() + 1, right);
                neighbours.add(s);

            }
            if (i > 3) {

                byte[] up = new byte[16];
                System.arraycopy(state, 0, up, 0, up.length);
                byte temp = up[i];
                up[i] = up[i - 4];
                up[i - 4] = temp;

                State s = new State(currentState.getMoves() + 1, up);
                neighbours.add(s);

            }
            if (i < 12) {
                byte[] down = new byte[16];
                System.arraycopy(state, 0, down, 0, down.length);
                byte temp = down[i];
                down[i] = down[i + 4];
                down[i + 4] = temp;
                State s = new State(currentState.getMoves() + 1, down);
                neighbours.add(s);

            }
 
        }
    }
    return neighbours;
}

public static class State {

    int moves;
    byte[] composition;

    public State(int moves, byte[] composition) {
        this.moves = moves;
        this.composition = composition;
    }

    public int getMoves() {
        return moves;
    }

    public void setMoves(int moves) {
        this.moves = moves;
    }

    public byte[] getState() {
        return composition;
    }
}
added 1436 characters in body
Source Link
Loading
added 1744 characters in body
Source Link
Loading
deleted 40 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
Loading