交易卡甲板生成器

时间:2018-05-21 14:36:38

标签: java arrays save load

我试图为HearthStone制作随机牌组生成器,而card []数组出现问题。它给了我空指针异常,并且第一次出现在readFile()方法中,调试器说问题在于这一行(好吧它始终在那里开始)card[a].setName(name);。我知道它不是字符串变量" name"因为我可以在System.out.print()中引用它。

P.S。我的编译器是netbeans IDE 8.0.2

HearthStoneDeckMaker

package hearthstone.deck.maker;

import java.io.*;
import java.lang.*;
import java.util.*;

public class HearthStoneDeckMaker {

    static createfile save = new createfile();

    public static double manaAVG = 5;
    public static int maxTaunt = 30;
    public static int maxSpell = 30;
    public static int maxMinions = 30;
    public static int minTaunt = 0;
    public static int minSpell = 0;
    public static int minMinions = 0;
    static Scanner input = new Scanner(System.in);
    public static int numberOfCards = 0;
    static Card[] card = new Card[9999];
    static int i;
    static int usedMinions = 0;
    static int usedSpells = 0;
    static int usedTaunts = 0;
    static String cls = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

    private static Scanner x;       

    public static void addcards(){
        for (i = numberOfCards; i <= numberOfCards; i++) {
          boolean DoneWithCard = false;
          while(DoneWithCard == false){
            card[i] = new Card();
            System.out.print("Name of Card >> ");
            card[i].setName(input.nextLine());

            System.out.print(card[i].getName()+"'s Class >> ");
            card[i].setCharacter(input.nextLine());
            boolean isValid = false;
            String message = "";
            String cardtype = "";
            while(isValid == false){
                System.out.println(message +"Is " + card[i].getName() + " a minion or a spell >>");
                if(input.nextLine().equalsIgnoreCase("minion")){
                    card[i].setIsMinion(true);
                    isValid = true;
                    cardtype = "minion";
                }
                else if(input.nextLine().equalsIgnoreCase("spell")){
                    card[i].setIsSpell(true);
                    isValid = true;
                    cardtype = "spell";
                }
                else{
                    message = "<INVALID ENTRY TRY AGAIN> ";
                }
            }
            String hastaunt = "";
            System.out.print("Does "+ card[i].getName() + " have Taunt (Y/N) >>");
            if(input.nextLine().equalsIgnoreCase("Y")) {
                card[i].setHasTaunt(true);
                hastaunt = "Does";
            }
            else{
                card[i].setHasTaunt(false);
                hastaunt = "Does not";
            }
            System.out.print("What is " +card[i].getName() +"'s mana cost >>");
            card[i].setMana(input.nextInt());

            if(card[i].getIsSpell() == false){
                System.out.print("What is " +card[i].getName() +"'s health >>");
                card[i].setHealth(input.nextInt());
            }
            else{
                card[i].setHealth(0);
            }


            System.out.print("What is " +card[i].getName() +"'s damage >>");
            card[i].setDamage(input.nextInt());

            System.out.println("would you like to add a desription (Y/N)");
            String ADesc = input.nextLine();
            if(input.nextLine().equalsIgnoreCase("y")){
                System.out.println("What is "+ card[i].getName() + "'s description >>");
                card[i].setDescription(input.nextLine());
            }
            else{
                System.out.println("Alright next");
            }

            System.out.println(card[i].getName() + " is a(n) " + cardtype + " with " +card[i].getDamage() + ""
                    + "/" + card[i].getHealth() + "\nand " + hastaunt + " have taunt");

            System.out.print("is this correct? (Y/N)");
            if(input.nextLine().equalsIgnoreCase("Y")){
                DoneWithCard = true;
            }
            else{
                DoneWithCard = false;
            }

          }
          System.out.print("Do you want to enter another card? (Y/N)");
          if(input.nextLine().equalsIgnoreCase("n")){
              numberOfCards = i+1;
              save.OpenFile();
              save.addRecords();
              save.closeFile();
              numberOfCards++;
              parameters();
          }
          else{
              numberOfCards++;

          }

        } 
    }

    public static void setup(){
        int counter  = 0;
        boolean add = false;

        System.out.println("Current Cards >>");

        openFile();


    }

    public static void openFile(){
        try{
            x = new Scanner(new File("HearthCards.txt"));
        }
        catch(Exception e){
            System.out.println("Load Failure");
        }

        readFile();
    }

    public static void readFile(){
        Card[] cardie = new Card[numberOfCards];
        while(x.hasNext()){
            numberOfCards++;
            int a = 0;
            String name = x.next();
            String Character= x.next();
            int Health = Integer.parseInt(x.next());
            int Damage = Integer.parseInt(x.next());
            int Mana = Integer.parseInt(x.next());
            int TimesUsed = Integer.parseInt(x.next());
            boolean hasTaunt = Boolean.parseBoolean(x.next());
            boolean isMinion = Boolean.parseBoolean(x.next());
            boolean isSpell = Boolean.parseBoolean(x.next());

                card[a].setName(name);
                card[a].setCharacter(Character);
                card[a].setHealth(Health);
                card[a].setDamage(Damage);
                card[a].setMana(Mana); 
                card[a].setTimesUsed(0);
                card[a].setHasTaunt(hasTaunt);
                card[a].setIsMinion(isMinion);
                card[a].setIsSpell(isSpell);
                a++;


        } 
        closeFile();
    }

    public static void closeFile(){
        x.close();
        setupcont();
    }

    public static void setupcont(){

        for (int j = 0; j <= 30; j++) {
            String type = "oof";
            String taunts = "oofer";
            if(card[j].getIsMinion() == true){
                type = "minion";
            }
            else if(card[j].getIsSpell() == true){
                type = "spell";
            }

            if(card[j].getHasTaunt() == true){
                taunts = "has";
            }
            else{
                taunts = "doesnt have";
            }

            System.out.println(cls);

                System.out.println(card[j].getName() + " >> " + card[j].getName() + " is a " + type + " that "+ taunts + " taunt");

        }


        System.out.print("would you like to add any new Cards (Y/N)");
        if(input.nextLine().equalsIgnoreCase("y")){
            addcards();
        }
        else{
            makedeck();
        }


    }

    public static void parameters(){
        System.out.print("Would you like to use default max and mins, or custom >> ");
        if(input.nextLine().equalsIgnoreCase("default")){
            makedeck();
        }
        else{
            System.out.print("What should the mana average be?");
            manaAVG = input.nextInt();
            System.out.println("What is the max taunt minions ?");
            maxTaunt = input.nextInt();
            System.out.print("What is max minions (1-30) >> ");
            maxMinions = input.nextInt();
            System.out.print("What is the max spells (1-30) >> ");
            maxSpell = input.nextInt();
            if(maxMinions + maxSpell < 30){
                System.out.println("<INVALID ENTRY TRY AGAIN> What is the max spells (1-30 >> ");
                maxSpell = input.nextInt();
            }

        }
    }

    public static void makedeck(){
        Card deck[] = new Card[30];

        for(int a = 0; a <= 30; a++){

        Random ran = new Random(i);
        int rand = ran.nextInt();

        if(card[rand].getTimesUsed() <= 2){
            deck[a] = card[rand];

            String hastaunt = "does not"; 
            if(card[rand].getHasTaunt() == true){
                hastaunt = "does";
                usedTaunts++; 
            }
            String cardtype = "";
            if(card[rand].getIsMinion() == true && usedMinions < maxMinions){
                usedMinions++;
                cardtype = "minion";
            }


            System.out.println(deck[rand].getName() + " is a(n) " + cardtype + " with " +deck[rand].getDamage() + ""
                        + "/" + deck[rand].getHealth() + "\nand " + hastaunt + " have taunt");
            }
        }
        boolean isDone = false;
        Random ran = new Random(i);
        int rand = ran.nextInt();

        boolean minMinDone = false;
        boolean maxMinDone = false;
        boolean minSpellDone = false;
        boolean maxSpellDone = false;
        boolean minTauntDone = false;
        boolean maxTauntDone = false;
        boolean correctClass = false;

        System.out.println("What Class would you like to use");
        String usedClass = input.nextLine();

        while(isDone == false){
            Random R = new Random(i);
            int Ran = R.nextInt();


            if(usedMinions < minMinions){
                Ran = R.nextInt();
                if(deck[Ran].getIsMinion() == false){
                    int random = R.nextInt();
                    if(card[random].getTimesUsed() <= 2){
                        deck[Ran] = card[random];
                    }
                }
            }
            else{
                minMinDone = true;
            }

            if(usedMinions > maxMinions){
                Ran = R.nextInt();
                if(deck[Ran].getIsMinion() == true){
                    int random = R.nextInt();
                    deck[Ran] = card[random];
                    if(card[random].getTimesUsed() <= 2){
                        deck[Ran] = card[random];
                    }
                }
            }
            else{
                maxMinDone = true;
            }

            if(usedSpells < minSpell){
                Ran = R.nextInt();
                if(deck[Ran].getIsSpell() == false){
                    int random = R.nextInt();
                    if(card[random].getTimesUsed() <= 2){
                        deck[Ran] = card[random];
                    }
                }
            }
            else{
                minSpellDone = true;
            }

            if(usedSpells > maxSpell){
                Ran = R.nextInt();
                if(deck[Ran].getIsMinion() == true){
                    int random = R.nextInt();
                    if(card[random].getTimesUsed() <= 2){
                        deck[Ran] = card[random];
                    }
                }
            }
            else{
                maxSpellDone = true;
            }

            if(usedTaunts < minTaunt){
                Ran = R.nextInt();
                if(deck[Ran].getHasTaunt() == false){
                    int random = R.nextInt();
                    if(card[random].getTimesUsed() <= 2){
                        deck[Ran] = card[random];
                        card[Ran].setTimesUsed(card[Ran].getTimesUsed() + 1);
                    }
                }
            }
            else{
                minTauntDone = true;
            }

            if(usedTaunts > maxTaunt){
                Ran = R.nextInt();
                if(deck[Ran].getHasTaunt() == true){
                    int random = R.nextInt();
                    if(card[random].getTimesUsed() <= 2){
                        deck[Ran] = card[random];

                    }
                }
            }
            else{
                maxTauntDone = true;
            }

            correctClass = false;
            for (int j = 0; j <= 30; j++) {
                while(!deck[j].getCharacter().equalsIgnoreCase(usedClass)){
                    int random = R.nextInt();
                    if(card[random].getTimesUsed() <= 2 && card[random].getCharacter().equalsIgnoreCase(usedClass)){
                        deck[j] = card[random];
                    }
                }

            }
            correctClass = true;

            if(minMinDone == true && maxMinDone == true && minSpellDone == true && maxSpellDone == true && minTauntDone == true && maxTauntDone == true && correctClass == true){
                isDone = true;
            }
            else{
                isDone = false;
            }

        }

        for (int j = 0; j <= 30; j++) {
            String type = "oof";
            String taunts = "oofer";
            if(deck[j].getIsMinion() == true){
                type = "minion";
            }
            else if(deck[j].getIsSpell() == true){
                type = "spell";
            }

            if(deck[j].getHasTaunt() == true){
                taunts = "has";
            }
            else{
                taunts = "doesnt have";
            }

            System.out.println(cls);

            if(!deck[j].getDescription().equals("")){
                System.out.println(deck[j].getName() + " >> " + deck[j].getName() + " is a " + type + " that "+ taunts + " taunt >>> \n\t" + deck[j].getDescription());
            }
            else{
                System.out.println(deck[j].getName() + " >> " + deck[j].getName() + " is a " + type + " that "+ taunts + " taunt");
            }
        }

    }

    public static void main(String[] args) {
        setup();
    }

}

package hearthstone.deck.maker;

public class Card {

    private String Name;
    private String Class;
    private boolean HasTaunt;
    private int Mana;
    private int Damage;
    private int Health;
    private boolean isMinion = false;
    private boolean isSpell = false;
    private boolean isElemental = false;
    private int TimesUsed = 0;
    private String Description = " no description ";
    //Omitted getters and setters        
    public Card(){

    }                              
}

CREATEFILE

package hearthstone.deck.maker;

import java.io.*;
import java.lang.*;
import java.util.*;

public class createfile {
    int numCards = HearthStoneDeckMaker.numberOfCards; 
    private Formatter x;
    public void OpenFile(){
        try{
            x = new Formatter("HearthCards.txt");

        }
        catch(Exception e){
            System.out.println("Save Failed");
        }
    }

    public void addRecords(){

        for (int j = 0; j < HearthStoneDeckMaker.numberOfCards; j++) {
            x.format("%s %s %x %x %x %x %b %b %b %n", HearthStoneDeckMaker.card[j].getName(), HearthStoneDeckMaker.card[j].getCharacter(), HearthStoneDeckMaker.card[j].getHealth(), HearthStoneDeckMaker.card[j].getDamage(), HearthStoneDeckMaker.card[j].getMana(), HearthStoneDeckMaker.card[j].getTimesUsed(), HearthStoneDeckMaker.card[j].getHasTaunt(), HearthStoneDeckMaker.card[j].getIsMinion(), HearthStoneDeckMaker.card[j].getIsSpell());
        }
    }

    public void closeFile(){
        x.close();
    }


}

0 个答案:

没有答案