Alper Göktepe
Alper Göktepe

Reputation: 45

Dice rolling fight system

I am doing something like this: When you clicked the button it will roll a dice in 1-6 and it will extract it from your opponet's hp.I write a code but it extratcs the number from the first hp of opponent.HP must be change in every roll, it must be less than starting hp. Here is my code:

public static final Random RANDOM1 = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dosto = 20;
    düsmann = 20;
    dostt = (TextView) findViewById(R.id.dost);
    düsman = (TextView) findViewById(R.id.rakip);
    saldir = (Button) findViewById(R.id.a);
    saldirr = (TextView) findViewById(R.id.textView);

    dostt.setText(dosto + "");
    düsman.setText(düsmann + "");

    saldir.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            randomD();
        }
    });
}

public void randomD() {

    int sayi = RANDOM1.nextInt(6) + 1;

    switch (sayi) {
        case 1:
            saldirr.setText(1 + " hasar");
            düsman.setText(düsmann-1 + "");
            break;
        case 2:
            saldirr.setText(2 + " hasar");
            düsman.setText(düsmann-2 + "");
            break;

        case 3:
            saldirr.setText(3 + " hasar");
                düsman.setText(düsmann-3 + "");
            break;

        case 4:
            saldirr.setText(4 + " hasar");
            düsman.setText(düsmann-4 + "");
            break;

        case 5:
            saldirr.setText(5 + " hasar");
                düsman.setText(düsmann-5 + "");
            break;

        case 6:
            saldirr.setText(6 + " hasar");
                düsman.setText(düsmann-6 + "");
            break;
    }
}

}

Upvotes: 1

Views: 63

Answers (1)

tung
tung

Reputation: 769

you forgot to update your düsmann variable so do something like this for all your cases in the switch example for case 1:

case 1:
    düsmann = düsmann - 1;
    düsman.setText(düsmann);
    break;

Also to limit some code duplication you could use a method to do this

private void subtractFromDüsman(int sub){
   düsmann = düsmann - sub;
   düsman.setText(düsmann);
}

And call it from your cases

case 2 :
    subtractFromDüsman(2);
    break;

Upvotes: 1

Related Questions