0

I'm making a rubik's cube simulator for my science fair project, and I'm having some trouble. This code is supposed to randomize the cube by setting different color values in the sideValues[] array. It's not that complicated, yet it always freezes my computer. (By the way, method1,method2, and method 3 are JLabels, the counter integers are to make sure that there's no more than 9 pieces per color, drawAndButtons is the JPanel the cube is shown on, and cubeSpace is the JPanel drawAndButtons is on.)

method1.setForeground(Color.red);
            method2.setForeground(Color.black);
            method3.setForeground(Color.black);
            for(int h = 1; h <= 54; h++)
            {
                do
                {
                    transfer = generator.nextInt(6) + 1;
                    switch(transfer)
                    {
                        case 1:
                            numy++;
                        case 2:
                            numb++;
                        case 3:
                            numo++;
                        case 4:
                            numg++;
                        case 5:
                            numr++;
                        case 6:
                            numw++;
                        default:
                    }
                    if(numy > 9 || numb > 9 || numo > 9 || numg > 9 || numr > 9 || numw > 9)
                    {
                        rightAmount = false;
                        switch(transfer)
                        {
                            case 1:
                                numy--;
                            case 2:
                                numb--;
                            case 3:
                                numo--;
                            case 4:
                                numg--;
                            case 5:
                                numr--;
                            case 6:
                                numw--;
                            default:
                        }
                    }
                    else
                    {
                        sideValues[h] = transfer;
                        rightAmount = true;
                    }
                }while(rightAmount == false);
            }
            drawAndButtons.add(new graphics());
            cubeSpace.repaint();
            methodValue = 3;
            numy = 0;
            numb = 0;
            numo = 0;
            numg = 0;
            numr = 0;
            numw = 0;
        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3142972
  • 897
  • 1
  • 8
  • 11
  • 3
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) (But based on a hunch..) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Dec 31 '13 at 14:12
  • 2
    Wait, add a break after all the miracles gets resolved :) – Suresh Atta Dec 31 '13 at 14:13
  • 3
    Why you don't have any `break;` in your `switch`? – Maroun Dec 31 '13 at 14:13
  • 2
    @sᴜʀᴇsʜᴀᴛᴛᴀ My twin..... finally found you! – Maroun Dec 31 '13 at 14:14
  • Try to use `&&`s instead of `||`s in your ` if(numy > 9 || numb`. That maybe causing `rightAmount` to always be `false`, hence a freeze from an infinite loop – Paul Samsotha Dec 31 '13 at 14:16
  • 1
    you must use `break;` in every case – Rajendra arora Dec 31 '13 at 14:26
  • What you're trying to do is randomly "shuffle" an array. Take a look at http://stackoverflow.com/questions/7783008/print-an-array-in-random-order – Taylor Dec 31 '13 at 15:39
  • Btw. shuffling rubik's cube in such a way may lead to unsolvable configurations. – Howard Dec 31 '13 at 15:53

1 Answers1

0

Let's say one of your variables starting with n (numy, numb, numo …) is greater than 9, but its case's index in switch is smaller than current transfer. Therefore, you will not decrease it, and the do-while loop will not end. This may potentialy lead to a very-long looping (if your transfer happen to be greater than case-index of the value which is greater than 9). Note this would never happen if your n-starting-variables would be initialized with 0, for example, as if you incremented any of them to be greater than 9, then your second switch would decrement it back. But how are your n-starting-variables initialized before the method? (by n-starting-variables I mean numy, numb, numo…)

What is more, as you are not using break in your cases, if your transfer = 1, you will execute all the cases 1,2,3,4,5,6 and default, and I am not sure if this is what you want to do here. As I understand you want to increment the "amount" of one-coloured pieces, specified by the pseudo-random number transfer. To do so, you must add break; in every case of your code here, so only the case = transfer will be executed.

3yakuya
  • 2,622
  • 4
  • 25
  • 40