vendredi 7 octobre 2016

Android Random make a negative result, how to fix it?

I'm trying to make a random between 1 and 12. But the result is already negative and it make me the app stop.

This is my codes :

public class PlacementPerso extends AireDeJeu{
private boolean trou1, trou2, trou3, trou4, trou5, trou6, trou7, trou8, trou9, trou10, trou11, trou12;

private Random r = new Random();
private int random, baseMax, baseMin;

public void PlacementPerso(){
    trou1 = true; trou2 = true; trou3 = true; trou4 = true; trou5 = true; trou6 = true; trou7 = true; trou8 = true; trou9 = true; trou10 = true; trou11 = true; trou12 = true;
    random = 1; baseMax = 1; baseMin = 12;

}

public void aPlacer(Perso pPerso){
    final int ressource = pPerso.getRessource();


//This is the crash line
    random = r.nextInt((baseMax - baseMin) + 1) + baseMin;
/**
*
* AND MORE ....
*
*/

And the caller :

public class GenerateurPartie extends GenerateurPerso{

private int mNiveau;
public static int mAvancement = 0;
private int mDelai;
private Thread mThread;
private Perso mPerso;

private final HandlerDelai handlerDelai = new HandlerDelai();

protected static PlacementPerso mPlacementPerso = new PlacementPerso();

public GenerateurPartie(int pNiveau) {
    this.mNiveau = pNiveau;
    this.mDelai = 2500;

    mPlacementPerso.PlacementPerso();
   mThread = new Thread(new Runnable() {
       @Override
       public void run() {
           try {
           int i = 0;
               while (mAvancement < 100) {

                   i += 1;

                   if (mAvancement >= 80)
                       mDelai = mDelai / 2;

                       Thread.sleep(mDelai);

                   handlerDelai.postDelayed(new Runnable() {
                       @Override
                       public void run() {
                           mPerso = GenerateurPerso(mNiveau, mAvancement);

                           mPlacementPerso.aPlacer(mPerso);
                       }
                   }, mDelai);
               }
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
   }});
    mThread.start();


    }
}

I've already read some threads on stackOverFlow and tryed what they suggest to do but nothing resolve my problem. I tryed to leave "random = 1;" in my class "PlacementPerso", but the way is already the same.

This is my log:

10-07 10:42:17.885 490-490/com.dunomade.ecraseurdetaupe E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.dunomade.ecraseurdetaupe, PID: 490
                                                                      java.lang.IllegalArgumentException: n <= 0: -10
                                                                          at java.util.Random.nextInt(Random.java:182)
                                                                          at com.dunomade.ecraseurdetaupe.PlacementPerso.aPlacer(PlacementPerso.java:27)
                                                                          at com.dunomade.ecraseurdetaupe.GenerateurPartie$1$1.run(GenerateurPartie.java:56)
                                                                          at android.os.Handler.handleCallback(Handler.java:815)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                          at android.os.Looper.loop(Looper.java:207)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Maybe someone know how to resolve it. I don't understand why my result is already <=0. Thank's advance =)




Aucun commentaire:

Enregistrer un commentaire