Dice Game for unlimited players

时间:2017-04-08 22:34:53

标签: java arrays

I'm working on a dice game which shall have 3 variations. I can set the Number of players in the game, the number of the rounds to play and the type of the game.

The addition of the points is regarding the gametype. The players shall roll the dices one after another and the one who has the most points wins in the end.

So far I've modelled a cube, but I have problems with my Dice game class or my player class.

Here's the code for the Cube:

public class Wuerfel {
    int max;
    int min;
    int augen;

    Wuerfel() {
        min = 1;
        max = 6;    
    }

    Wuerfel(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public int getAugen() {
        return augen;
    }

    int wuerfeln() {
        this.augen = (int) (Math.random() * ((max - min)+1)+min);
        return augen;
    }

    public java.lang.String toString() {
        String text = "Wuerfel" + min + " .. " + max + " gewuerfelt: " + augen;
        return text;
    }

And the Player:

public class Spieler {
    String name;
    int gesamtAugen;
    int gewuerfelteAugen;

    public void setName(String name) {
        name = JOptionPane.showInputDialog("Bitte neuen Spielernamen eingeben: ");
    }

    int getGesamtaugen() {
        return gesamtAugen;
    }

    int wuerfeln() {
        Wuerfel w = new Wuerfel();
        w.wuerfeln();
        this.gesamtAugen = gesamtAugen + w.augen;
        this.gewuerfelteAugen = w.augen;
        return gesamtAugen & w.augen;
    }

    Spieler() {
        name = null;
        gesamtAugen = 0;
    }

}

and the Game:

public class Wuerfelspiel {

    int a; //Spieleranzahl
    int v; //Variante
    int r; //Rundenanzahl

    public static void main(String[] args) {

        Wuerfelspiel wu = new Wuerfelspiel();

        wu.a = Integer.parseInt(JOptionPane.showInputDialog("Bitte geben Sie eine Spieleranzahl ein: "));
        wu.v = Integer.parseInt(JOptionPane.showInputDialog("Bitte geben Sie eine Variante ein:"+"\n" +
                "(1) Alle Augen werden addiert" + "\n" +
                "(2) Nur die geraden Augen werden addiert" + "\n" +
                "(3) Die böse Eins" + "\n"));
        wu.r = Integer.parseInt(JOptionPane.showInputDialog("Bitte geben Sie eine Rundenanzahl ein: "));

        Spieler[] spieler = null;
        spieler = new Spieler[wu.a];
        for (int i = 0; i < wu.a; i++) {
            spieler[i].setName(JOptionPane.showInputDialog("Bitte geben Sie einen Spielernamen ein: "));
        }

        for (int i = 0; i < wu.a; i++) {
            spieler[i].wuerfeln();
            System.out.println( spieler[i].name + "hat gewuerfelt: " + spieler[i].gewuerfelteAugen + " Gesamtpunkte: " + spieler[i].gesamtAugen);
        }

        for (int i = 0; i < wu.a; i++) {
            System.out.println("Der Endstand ist: " + "\n" + spieler[i].gesamtAugen);
        }

    }
}

There is nothing so far in my code regarding the game type. I will probably do it in the end...

I know it must be something really stupid, i can't get here. I really have problems to write a main method. Always get a null-pointer exception when I try to change the name of one of the Objects in the Array. Can I save objects from the type Spieler in an array? Why is it not possible to change their variables?

1 个答案:

答案 0 :(得分:1)

You have created an empty spieler array and trying to invoke the methods on each of the elements (which are null), so is the exception.

So you need to initialize the array with Spieler objects first before actually calling the methods (on the array element) as shown below:

Spieler[] spieler = new Spieler[wu.a];
for(int i = 0; i < wu.a; i++) {
    spieler[i] = new Spieler();//load each array element with Spieler object
}

As a side note, it is not a good practice to keep the variable names like a, v, r which are not easily understandable, so name them properly by conveying their purpose inside the class.