2016-11-26 2 views
-4

Also für noch ein anderes Projekt mache ich einen RPG-Code. Wie Dungeons und Drachen. Mein besonderes Problem sind die Attribute. Grundsätzlich gibt die Anwendung dem Spieler Statistiken, und wenn der Spieler die erhaltenen Statistiken nicht mag, gibt ihm die Anwendung die Möglichkeit, sie erneut zu spielen. Der erste Wurf ist in Ordnung, aber wenn der Benutzer sich neu wälzt, addieren sich die Werte (sowohl der erste als auch der nächste) zueinander. Hier ist mein Code:Es fügt einfach auf

Die Haupt Methode:

package bagOfHolding; 

    public class Advanced { 
    public static void main(String [] args){ 
    GameMaster.game(); 

    } 
    } 

The Dice Class (this rolls the stats for the statistics): 

package bagOfHolding; 

import java.util.Random; 

public class DiceBag { 
    private static int sum; 

    public static int rollD6() { 
     int[] Dice = new int[3]; 
     Random num = new Random(); 

     for (int i = 0; i < Dice.length; i++) { 
      Dice[i] = num.nextInt((6)) + 1; 
     } 

     for (int i : Dice) { 
      sum += i; 
     } 
     return sum; 
    } 
    // public static int getSum() { 
    // return sum; 
    // } 
    // public static void setSum(int sum) { 
    // DiceBag.sum = sum; 
    // } 
} 

The Game Master (Dies macht das ganze Spiel):

package bagOfHolding; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class GameMaster { 
    public static void game() { 
     Hero.attributes(); 
    } 

    public static void ReRoll() { 
     BufferedReader delta = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.println("Would you like to reroll your hero? 1) Yes or 2) No"); 
     System.out.println("Please enter a number"); 
     System.out.println("Any number other than 1 or 2 will exit the application"); 
     try { 
      String userInput = delta.readLine(); 
      int input = Integer.parseInt(userInput); 
      if (input == 1) { 
       Hero.setStrength(DiceBag.rollD6()); 
       Hero.setDexterity(DiceBag.rollD6()); 
       Hero.setIntelligence(DiceBag.rollD6()); 
       Hero.attributes(); 
      } else if (input == 2) { 
       System.exit(0); 
      } else { 
       System.exit(0); 
      } 
     } catch (NumberFormatException NFE) { 
      System.out.println("Invalid"); 
     } catch (IOException IOE) { 
      System.out.println("Invalid"); 
     } 
    } 
} 

Und die Heldenklasse (dies hat alle Statistiken):

package bagOfHolding; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class Hero { 
    /* 
    * Attributes - Randomly determined by 3d6 
    * 
    */ 
    /* 
    * -3 attributes - Strength - Damage bonus - If over 15 every pt = +1 (_++;) 
    * - Negative Damage Bonus - If under 10 every pt = -1 (_--;) - Dexterity 
    * -Strike bonus - every 2 pts over 14 = (_%2 + 1) - Negative Strike bonus - 
    * every 2 pts below 10 = (_%2 -1) - Dodge bonus - every 2 pts over 15 = 
    * (_%2 + 1) - Negative dodge bonus - every 2 pts below 11 = (_%2 -1) - 
    * Intelligence -Spell Strength Bonus - every pt over 15 = (++2) - Negative 
    * Spell Strength Bonus - every pt below 11 = (--2) 
    * 
    * Base Attributes - Health -Strength * 10 - MP - Intelligence *5 
    */ 
    private static int strength = DiceBag.rollD6(); 
    private static int intelligence = DiceBag.rollD6(); 
    private static int dexterity = DiceBag.rollD6(); 

    public static int getIntelligence() { 
     return intelligence; 
    } 

    public static void setIntelligence(int intelligence) { 
     Hero.intelligence = intelligence; 
    } 

    public static int getDexterity() { 
     return dexterity; 
    } 

    public static void setDexterity(int dexterity) { 
     Hero.dexterity = dexterity; 
    } 

    public static int getStrength() { 
     return strength; 
    } 

    public static void setStrength(int strength) { 
     Hero.strength = strength; 
    } 

    public static void attributes() { 
     strength = getStrength(); 
     System.out.println("Here is your hero: "); 
     // DiceBag.rollD6(); 
     System.out.println("Strength = " + strength); 
     if (strength > 15) { 
      System.out.println("Damage Bonus = " + "+" + (strength - 15)); 
     } else if (strength < 10) { 
      System.out.println("Negative Damage Bonus = " + "-" + (10 - strength)); 
     } else { 
      System.out.println("You do not have damage bonus"); 
     } 

     intelligence = getIntelligence(); 
     System.out.println("Intelligence = " + intelligence); 
     if (intelligence > 15) { 
      System.out.println("Spell Strength Bonus = " + "+" + ((intelligence - 15) * 2)); 
     } else if (strength < 11) { 
      System.out.println("Negative Spell Strength Bonus = " + "-" + ((11 - intelligence) * 2)); 
     } else { 
      System.out.println("You do not have a spell strength bonus"); 
     } 

     dexterity = getDexterity(); 
     System.out.println("Dexterity = " + dexterity); 
     if (dexterity > 15 && dexterity % 2 == 0) { 
      System.out.println("Dodge Bonus = " + "+" + (dexterity - 15)); 
     } else if (dexterity < 11 && dexterity % 2 == 0) { 
      System.out.println("Negative Dodge Bonus = " + "-" + (11 - dexterity)); 
     } else { 
      System.out.println("You do not have a dodge bonus"); 
     } 
     if (dexterity > 14 && dexterity % 2 == 0) { 
      System.out.println("Strike Bonus = " + "+" + (dexterity - 14)); 
     } else if (dexterity < 10 && dexterity % 2 == 0) { 
      System.out.println("Negative Strike bonus = " + "-" + (10 - dexterity)); 
     } else { 
      System.out.println("You do not have a strike bonus"); 
     } 

     int health = strength * 10; 
     System.out.println("Health = " + health); 

     int MP = intelligence * 5; 
     System.out.println("MP = " + MP); 

     GameMaster.ReRoll(); 
    } 
} 
+1

Das ist auf schrecklichen Titel und einen großen Klecks Code, vielleicht gehen Sie [MCVE] und kommen mit einem besseren Titel. – rene

+1

Die Tatsache, dass es hinzugefügt wird, sollte Ihnen helfen, in die richtige Richtung zu zeigen. Das ist eine Menge Code, den man einfach ablegen kann. Überprüfen Sie Ihren Prozess zum Setzen von Statistiken mit einem Debugger, so dass Sie Variablenwerte sehen können. Irgendwo behalten Sie den vorherigen Wert bei. Persönlich würde ich entweder xyz.reset() hinzufügen, um die Statistiken zurückzusetzen und sie dann wie Sie neu zu rollen. (Ich bin jetzt auf dem Handy und kann einige Tage nicht zu meinem Dev-Gerät kommen. Sobald ich das tue, werde ich versuchen, dir eine genaue Lösung zu finden, falls sie noch benötigt wird.) – SirBagels

Antwort

0

Die DiceBag-Summe sollte lokal zu rollD6 statt statisch sein.

Verwandte Themen