lundi 6 mai 2019

How do I reset my values in a switch statement?

I'm working on a stacking boxes games that uses a random number generator but I can't get my program to reset whenever a number is used twice?

1) Pick an implementation of either tracing the boxes as they are created or going directly to a solution. 2) A while loop controls the next three steps. 3) Pick a box 4) Determine if this box can be placed
5) Place a box
I used 6 boolean variables each one representing a box. A switch statement called one of 6 methods depending on the random number given for a box. These six methods analyzed the current stack of boxes and determined which one of the 13 box combinations to call.

package com.chinus;

i
    private static void trace() {
        int boxesUsedSoFar = 0;
        boolean gameIsDone = false;
        int usedOnce;

        while (gameIsDone != true) {
            int number = (int) ((Math.random() * 6) + 1);
            boxesUsedSoFar++;

            System.out.println("Current Box Number : " + number);
            switch (number) {
                case 1:
                    if (number == 1) {
                        box1 = true;

                    }
                    if (box1 == true && box2 == true && box3 == true) {
                        combo6();
                    } else if (box1 == true && box2 == true) {
                        combo4();
                    } else if (box1 == true && box3 == true) {
                        combo7();
                    } else if (box1 == true) {
                        combo1();
                    }
                    else{
                        restart();
                    }
                    box1=false;
                    break;

                case 2:
                    if (number == 2) {
                        box2 = true;


                    }

                    if (box1 == true && box2 == true && box3 == true) {
                        combo6();
                    } else if (box1 == true && box2 == true) {
                        combo4();
                    } else if (box2 == true && box3 == true) {
                        combo5();
                    } else if (box2 == true) {
                        combo2();
                    }
                    else{
                        restart();
                    }
                    box2=false;

                    break;
                case 3:
                    if (number == 3) {
                        box3 = true;
                        int count3 = 0;
                    }
                    boolean box3used =false;


                    if (box1 == true && box2 == true && box3 == true) {
                        combo6();
                        box3used =true;
                    } else if (box1 == true && box3 == true) {
                        combo7();
                        box3used =true;
                    } else if (box2 == true && box3 == true) {
                        combo5();
                        box3used =true;
                    }else if (box3 == true) {
                        combo3();
                        box3used = true;
                    }
                    else {
                        restart();
                    }

                    break;
                case 4:
                    if (number == 4) {
                        box4 = true;
                    }
                    if (box5 == true && box4 == true && box3 == true && box2 == true && box1 == true) {
                        combo12();
                    } else if (box4 == true && box2 == true & box3 == true && box1 == true) {
                        combo10();
                    } else if (box4 == true && box2 == true & box1 == true) {
                        combo8();
                    } else {
                        restart();
                    }

                    break;

                case 5:
                    if (number == 5) {
                        box5 = true;
                    }
                    if (box5 == true && box4 == true && box3 == true && box2 == true && box1 == true) {
                        combo12();
                    } else if (box5 == true && box2 == true & box3 == true && box1 == true) {
                        combo11();
                    } else if (box5 == true && box2 == true & box3 == true) {
                        combo9();
                    } else {
                        restart();
                    }
                    break;

                case 6:
                    if (number == 6) {
                        box6 = true;
                    }
                    if (box4 == true && box5 == true) {
                        combo13();
                        gameIsDone = true;
                    } else {
                        restart();
                    }
                    break;

                default:

            }
            System.out.println("Boxes used so far : " + boxesUsedSoFar);
        }
    }


    private static void restart() {
        box1 = false;
        box2 = false;
        box3 = false;
        box4 = false;
        box5 = false;
        box6 = false;
    }
}

Actual:
Current Box Number : 2
       ___   ___
      |   | |   |
      | 2 | | 3 |
      |___| |___|
Boxes used so far : 49820
Current Box Number : 2
       ___   ___
      |   | |   |
      | 2 | | 3 |
      |___| |___|



Expect:Current Box Number : 2
       ___   ___
      |   | |   |
      | 2 | | 3 |
      |___| |___|
Boxes used so far : 49820
Current Box Number : 2
       ___  
      |   | 
      | 2 | 
      |___|




Aucun commentaire:

Enregistrer un commentaire