#NPuzzle

56 messages · Page 1 of 1 (latest)

pastel pagoda
#
public static void shufflePuzzle(int puzzle[][], int N, int k) {
            
            int posi0 = 0;
            int posj0 = 0;
            
            for(int i=0; i<N; i++) {
                
                for(int j=0; j<N; j++) {
                    
                    if(puzzle[i][j] == 0) {
                        
                        posi0 = i;
                        posj0 = j;
                        break;
                        
                    }
                    
                }
                
            }
                
            for (int x = 0; x < k; x++) {
                
                int validMoves = 0;
                
                if(posi0 + 1 < N) {
                    validMoves++;
                }
                if(posi0 - 1 >= 0) {
                    validMoves++;
                }
                if(posj0 + 1 < N) {
                    validMoves++;
                }
                if(posj0 - 1 >= 0) {
                    validMoves++;
                }

wooden meadowBOT
#

This post has been reserved for your question.

Hey @pastel pagoda! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

pastel pagoda
#
double value = Math.random();
                boolean shuffled = false;
                double segment = 1.0 / validMoves;
                
                if (posj0 > 0 && value < segment) { 
                    shuffleLeft(puzzle, posi0, posj0);
                    posj0--;
                    shuffled = true;
                } else if (posi0 > 0 && value < 2*segment) {
                    shuffleUp(puzzle, posi0, posj0);
                    posi0--;
                    shuffled = true;
                } else if (posj0 < N - 1 && value < 3*segment) { 
                    shuffleRight(puzzle, posi0, posj0);
                    posj0++;
                    shuffled = true;
                } else if (posi0 < N - 1) { // Shuffle down
                    shuffleDown(puzzle, posi0, posj0);
                    posi0++;
                    shuffled = true;
                }

                if (shuffled == false) {
                    x--;
                }
            }
            
        }
#

this is the code i currently have for my shufflePuzzle function

#

• Mode 1: The user, at the beginning, has to choose from a menu. With the first selection of the menu, the user chooses the degree of difficulty k. The number k represents how many times (in number of possible moves) the program should randomly (Math.random()) shuffle the puzzle.
When you shuffle the puzzle, the priority of possible moves should be: left, up, right, down, according to the values ​​given by random.
E.g. if the possible movements are 3, then for random values ​​[0 , 0.3] the movement will be left, for values ​​[0.3 , 0.6] up, and for values ​​[0.6 , 1] right.
And these are the instructions for shuffling the puzzle

#

Is my logic correct or am i doing something wrong??

#

N is the size of the array puzzle and k is the number of times it has to shuffle the array

pearl loom
#

can u show the full question or any constraint?

pastel pagoda
#

There is no other thing mentionted for shuffling the puzzle

#

But the only thing is that we have to use math random

pearl loom
#

is there any test cases? or smt , how did u get that puzzle is a 2d array with N size?

#

there is no clue what the puzzle is

pulsar tide
#

array.length is better than N.

#

easy to read

pastel pagoda
#

The array will look like this:
1 2 3
4 5 6
7 8 0

#

An i have to shuffle the array based on the 0 tile

#

For example in this formation the only valid moves 0 can do are up and right.
So if math.random generates a number if it is from 0-0.5 it will go left (simce left comes before up in the move priority list) and if it is from 0.5-0.99 it will go up

#

And i have to repeat this for k amount of times while only doing valid swaps

pearl loom
#

i see, so u only have 4 probabilities

#

what the output ask for?

#

or u just rotating the puzzle?

pastel pagoda
#

I am not sure if what i wrote works correctly tho

#

Or if i am missing some cases

pearl loom
#

i think if that is just need to rotate the puzzle u just need to modulus k with 4 (k=k%4) because is the same so the probability will be 0-0,25 0,25-0.5 0.5-0.75 0.75-100

#

u can use integer to avoid the double inprecision

pearl loom
#

no matter what is the puzzle value, u just want to rotate it randomly right?

pastel pagoda
#

Wdym?

#

I can only rotate number 0

#

Not any other numbers

pearl loom
pastel pagoda
#

So if it moves right or down it exits the array

pastel pagoda
pearl loom
#

okay i get it

#

so the thing is u want to rotate the 0 around the 2d array right

#

it is guarantee the array is always have 0 number?

opaque nicheBOT
#

u don't this code
int validMoves = 0;
if(posi0 + 1 < N) { validMoves++; } if(posi0 - 1 >= 0) { validMoves++; } if(posj0 + 1 < N) { validMoves++; } if(posj0 - 1 >= 0) { validMoves++; }

This message has been formatted automatically. You can disable this using /preferences.

#

because the probability to move is left,up,right,down u can handle from if else at this code f (posj0 > 0 && value < segment) { shuffleLeft(puzzle, posi0, posj0); posj0--; shuffled = true; } else if (posi0 > 0 && value < 2*segment) { shuffleUp(puzzle, posi0, posj0); posi0--; shuffled = true; } else if (posj0 < N - 1 && value < 3*segment) { shuffleRight(puzzle, posi0, posj0); posj0++; shuffled = true; } else if (posi0 < N - 1) { // Shuffle down
shuffleDown(puzzle, posi0, posj0);
posi0++;
shuffled = true;

This message has been formatted automatically. You can disable this using /preferences.

pearl loom
#

u can do like u did before, but it's brute force and not precision if u always divide the probability

pastel pagoda
#

Isnt your code and mine the same?

pearl loom
#

yea i mean u should change that code, ofc that's your code 🤣

opaque nicheBOT
#

for this section shuffled cannot be false, because minimum of possible move is 2 if (shuffled == false) { x--; }

This message has been formatted automatically. You can disable this using /preferences.

pearl loom
#

ur steps should be work, but would return false move if the k value is big because of floating number precision

novel sky
#

where is the main method?

#

i cant run it

pastel pagoda
wooden meadowBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.