mercredi 28 décembre 2022

Java while loop random number

i'm new to Java and I am trying to code a little "fighting game" (more random than fight actually). I want each punch to have a random number of damages between 0 and 30. The problem is, the first number randomly sorted will be used until the end (if it's 12, both fighters will punch at 12 damages every turn until Player 1 wins). How can I generate a different random number at every iteration of the loop until there's a winner ?

package jeu_propre;

public class main {
    public static void main(String[] args){
        System.out.println("Player 1, choose your name : ");
        String nom_pone= new java.util.Scanner(System.in).nextLine();
        System.out.println("Player 2, choose your name : ");
        String nom_ptwo= new java.util.Scanner(System.in).nextLine();
                        
        int r=new java.util.Random().nextInt(30);
        
        weapons fists = new weapons();
        fists.name="fists";
        fists.damage = r;
        
        
        player number_one = new player();
        number_one.name= nom_pone;
        number_one.hp= 100;
        
        player number_two= new player();
        number_two.name=nom_ptwo;
        number_two.hp= 100;
        
        
        boolean running=true;
        while (running == true) {
            System.out.println("Fight !");
            System.out.println(number_one.name +" hit " + number_two.name);
            String frapper= new java.util.Scanner(System.in).nextLine();
            System.out.println(fists.damage);
            number_two.hp= number_two.hp -= fists.damage;
            
            if (number_two.hp <=0) {
                System.out.println(number_one.name +" wins !");
                running=false;
                break;
            } else {
                System.out.println(number_two.name + " still has "+number_two.hp);
                running = true;
            }
            System.out.println("Fight !");
            System.out.println(number_two.name +" hit " + number_one.name);
            String frapper2= new java.util.Scanner(System.in).nextLine();
            System.out.println(fists.damage);
            number_one.hp= number_one.hp -= fists.damage;
            
            if (number_one.hp<=0) {
                System.out.println(number_two.name +" wins !");
                running=false;
                break;
            }else {
                System.out.println(number_one.name + " still has "+number_one.hp);
                running = true;
            }
        }

I tried to change the values between the brackets and associating the random command directly to "fists.damage"




Aucun commentaire:

Enregistrer un commentaire