vendredi 30 octobre 2020

How to output different random number in do while loop each loop Java

So i am very new to creating public classes and methods and we had an assignment to create a harry potter game in which 2 players roll random numbers which are assigned to spells which each have their own attack damage and defence capabilities. My main issue is that the random number which is assigned to the spells in the do while loop only outputs 2 random sets of spells (a set has 2 spells, one for attack and one for defence), I have tried testing out printing random numbers in another program and it worked, I feel like it might have

   import java.util.Random;
import java.util.Scanner;


public class Game {
  
  public static void main (String [] args) {
    
  Player p2 = new Player ();  
  Player p1 = new Player ();
  Scanner input = new Scanner (System.in);
  Random rand = new Random();
  int y = 0;
  int ready;
  int p1r = p1.roll();
  int p2r = p2.opproll();
  int p2defdmg = p2.getDefensedmg(p2r);
  int p2attdmg = p2.getAttackdmg(p2r);
  int p1defdmg = p1.getDefensedmg(p1r);
  int p1attdmg = p1.getAttackdmg(p1r);
  int p1attfinal = (p1attdmg - p2defdmg);
  int p2attfinal = (p2attdmg - p1defdmg);
  int p2health = p2.getHealth();
  int p1health = p1.getHealth();
  int p2healthfinal = p2health - p1attfinal;
  int p1healthfinal = p1health - p2attfinal;
  
  System.out.println ("Please enter a name for Player 1.");
  p1.setName(input.next());
  
  System.out.println ("Please enter a name for Player 2.");
  p2.setName(input.next());
  
  System.out.println ("Welcome players " + p1.getName() + " and " + p2.getName() + "!\n\n");
  
  do {
  
  System.out.println ("Player " + p1.getName() + " is now attacking. They have rolled a " + p1r + " which is " + p1.getAttack(p1r) + " with a potential of dealing " + p1.getAttackdmg(p1r) + " damage!\n");
  System.out.println ("Player " + p2.getName() + " is now defending. They have rolled a " + p2r + " which is " + p2.getAttack(p2r) + " with a potential of defending " + p2.getDefensedmg(p2r) + " damage!\n");
  
  if (p2defdmg >= p1attdmg) {
    
    System.out.println (p2.getName() + " has successfully defended " + p1.getName() +"'s attack!\n");
    
  }
  
  else {
    
    System.out.println (p2.getName() + "'s defense failed! " + p1.getName() + "'s attack has dealt " + p1attfinal + " damage! Player 2's health has dropped to " + (p2healthfinal) +"\n");
    
  }
  
  do {
    
  System.out.println ("Ready for the next round? 1 = Yes\n\n");
  ready = input.nextInt();
  
  if (ready == 1) {
  y = 0;  
     
  }
 
  else {
   y = 1;
    System.out.println ("Please enter the requested number.");
    }  
 }while (y == 1);
 
 
   System.out.println ("Player " + p2.getName() + " is now attacking. They have rolled a " + p2r + " which is " + p2.getAttack(p1r) + " with a potential of dealing " + p2.getAttackdmg(p2r) + " damage!\n");
  System.out.println ("Player " + p1.getName() + " is now defending. They have rolled a " + p1r + " which is " + p1.getAttack(p2r) + " with a potential of defending " + p1.getDefensedmg(p1r) + " damage!\n");
  
  if (p1defdmg >= p2attdmg) {
    
    System.out.println (p1.getName() + " has successfully defended " + p2.getName() +"'s attack!\n");
    
  }
  
  else {
    
    System.out.println (p1.getName() + "'s defense failed! " + p2.getName() + "'s attack has dealt " + p2attfinal + " damage! Player 1's health has dropped to " + (p1healthfinal) +"\n");
    System.out.println ("The next round will now begin!\n");
  }
  
  do {
    
  System.out.println ("Ready for the next round? 1 = Yes\n\n");
  ready = input.nextInt();
  
  if (ready == 1) {
  y = 0;  
     
  }
 
  else {
   y = 1;
    System.out.println ("Please enter the requested number.");
    }  
 }while (y == 1);
  
     System.out.println ("-----------------------------------------------------------------------------------------------------------------------------\n");
   
  }while (p1health > 0 && p2health > 0);
 
    if (p1health <= 0) {
    
    System.out.println ("Game Over! " + p2.getName() + " Wins!");
    
  }
  
  else {
    
    System.out.println ("Game Over! " + p1.getName() + " Wins!");
    
  }
    
    
   }
}



Aucun commentaire:

Enregistrer un commentaire