vendredi 15 janvier 2016

JAVA- Public Static Void: Preventing a Returning Random (Dependent on User Input)

I am new to the site and to Java, but decided to try my hand at coding under the influence of a friend.

This is a snippet from a Pirate RPG I am making (yes I know RPG Game is redundant), and includes only the essential parts to the problem, which I fear there may be many factors. My friend had recommended I work in the static void, in which I had begun everything seamlessly until now, when I tried to reference a non-static object from outside the void. (Ahhh!)

The problem is- I would like that for each time I mention random values, like playerdamage, that they be different each time. All of these are dependent from user input, found in the static void, and therefore cannot be moved before the user input (aka outside of the static void).

A random number, like player damage, is simply decided once and then repeated. I am wondering if I need to dramatically change my classes, or if there is a simple fix? I had already searched up some solutions, and the second snippet of code is one of my efforts.

Here is what I have so far, that is relevant to the problem:

package rpg.game;

import java.util.*;

public class RPGGAME {

    Random randnum;

    public RPGGAME() { 
                              //from here to PotentialSolutionEnd, a way I found on the site to return random numbers, however is NOT recognized by objects in the static void
    randnum = new Random(); //specifically, randnum
    randnum.setSeed(123456789);}  

      public int random(int i){

      return randnum.nextInt(i);

    } //PotentialSolutionEnd

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        System.out.println("CONSOLE: Let's see... What are you like? \nPress ENTER to continue..."); 
kb.nextLine();

        System.out.println("You have 10 available character points to use. You can spend them on Obstinacy, Resolve, or Dexterity abilities. \nPress ENTER to continue...");
kb.nextLine(); //Explanation of points not shown


int charPoints = 10;
int obstinacy = 0;
int resolve = 0;
int dexterity = 0;

//Below is a loop to ensure that the values for each stat are correctly recorded. Included because these user-inputted values are essential to modify all other variables.

while(charPoints!=0){
System.out.println("How many points would you like to assign to obstinacy? Enter a positive integer value.");
int oAssign = kb.nextInt();
if ((charPoints - oAssign)>=0){
charPoints -= oAssign;
obstinacy += oAssign;
}

else{
System.out.println("You don't have that many points. You only have "+charPoints+" to use.");
}

System.out.println("How many points would you like to assign to resolve? Enter a positive integer value.");
int rAssign = kb.nextInt();
if ((charPoints - rAssign)>=0){
charPoints -= rAssign;
resolve += rAssign;
}

else{
System.out.println("You don't have that many points. You only have "+charPoints+" to use.");
}

System.out.println("How many points would you like to assign to dexterity? Enter a positive integer value.");
int dAssign = kb.nextInt();
if ((charPoints - dAssign)>=0){
charPoints -= dAssign;
dexterity += dAssign;
}

else{
System.out.println("You don't have that many points. You only have "+charPoints+" to use.");
}
}

double health = (resolve * 5 + 25);
double obstinacypulled = (obstinacy);
double resolvepulled = (resolve);
double dexteritypulled = (dexterity); //Only works if I redefine doubles as 'pulled sources' for some reason

//Read-outs of value chosen, proof that can be referenced, ex. 'health'
System.out.println("Your character has the following skills:");
System.out.println("Obstinacy- "+obstinacy+" points");
System.out.println("Resolve- "+resolve+" points");
System.out.println("Dexterity- "+dexterity+" points");
System.out.println("You have "+health+" health. \nPress ENTER to continue...");
kb.nextLine();
kb.nextLine();

//MAIN PROBLEM: Current random system, generates a static random that cannot be changed, as well as randnum cannot be mentioned in this area.

Random damage1 = new Random(); 

double randomdamage = damage1.nextInt(25 - 5 + 1) + 5; //range of random, without added player bonuses

double potentialdamage = (randomdamage);

double addeddamagemultiplier = (obstinacypulled * 5/100);

double damagemultiplier = (addeddamagemultiplier + 1);

double playerdamage = (potentialdamage * damagemultiplier);

double subtractedresistancemultiplier = (obstinacypulled * 25/1000);

double playerresistance = (1 - subtractedresistancemultiplier);

double initialpiratedamage = damage1.nextInt (5 - 1 + 1) + 1; //Randoms with different ranges do produce different numbers, even with same mentioned random of 'damage1'.

double piratedamage = (initialpiratedamage * playerresistance);

double resistancemultiplierpercentage = (subtractedresistancemultiplier * 100);

double specialhealamount = (health * 1/8);

double specialcriticalmultiplier= 1.5;

double specialcriticalamount = (playerdamage * specialcriticalmultiplier);

//Function tests- Read Below
System.out.println("Character points function test:");

System.out.println("If there was an enemy here, and you had 0 obstinacy points, you would deal "+randomdamage+" damage. \nPress ENTER to continue...");
kb.nextLine(); //Example of static random each playthrough (1-25)

System.out.println("However, because of your "+damagemultiplier+" damage multiplier, you would instead deal "+playerdamage+" damage. \nPress ENTER to continue..."); 
kb.nextLine();

System.out.println("If there was an enemy here, and you had 0 obstinacy points, you would have taken "+initialpiratedamage+" damage. \nPress ENTER to continue...");
kb.nextLine();

System.out.println("However, because of your -"+resistancemultiplierpercentage+"% resistance reduction, you would instead receive "+piratedamage+" damage. \nPress ENTER to continue...");
kb.nextLine();

System.out.println("If you did not critical special here, you would have dealt "+playerdamage+" damage. \nPress ENTER to continue..."); //I would like this value to be different, as playerdamage needs to be random reach time it is mentioned. It is currently the same.
kb.nextLine();

System.out.println("However, because of your 150% *Critical* Special multiplier, you would instead deal "+specialcriticalamount+" damage. \nPress ENTER to continue...");
kb.nextLine();

//end of function tests
}
}

Even if I move random doubles out of the static void, but under a static context, they cannot be referenced because they are non-static...

public class RPGGAME {
    Random randnum;
    static double randomdamage = randnum.nextInt(25 - 5 + 1) + 5; //Referencing error.
    public RPGGAME() {
    randnum = new Random();
    randnum.setSeed(123456789);
    }  
      public int random(int i){
      return randnum.nextInt(i);
    }

I would greatly appreciate any help, as to advance progress on my game! Thank you!




Aucun commentaire:

Enregistrer un commentaire