Erste Schritte Spiel: Glückliches Sieben

Lina15

Neues Mitglied
Hallo zusammen!

Könnt ihr mit bitte helfen, das Spiel "Glückliches Sieben" etwas schöner (fertig) zu programmieren?

Blatt1_Auf.4.png

Unten ist das, was ich geschafft habe. Weiter muss man daraus eine Schleife machen.. Das hatte ich auch gemacht, aber der Code sah im Endeffekt so verwirrend und unübersichtlich aus, dass ich jetzt nach besseren Lösungen suche.

Wäre froh für eure Lösungsvorschläge!

BEMERKUNG: Nur einfache Konstrukte sind zugelassen, wie if…else, while, int-Datentyp

Java:
public class LustigesSieben extends MiniJava 
{
    public static void main(String[] args) 
    {
        int dice1, dice2, sum_dice, gain, guess, stake;
        int bank_account = 100;
     
        //spieler gibt einen Tip eine seinen Betrag ein
        guess = read("Geben Sie Ihren Tip ein [2,12]:");
        if ((1 < guess) && (guess < 13))
        {
            stake = read("Geben Sie Ihren Betrag ein, aus [1,"+ bank_account + "]");
            if ((stake > 0) && (stake <= bank_account))
            {
                dice1 = dice();
                dice2 = dice();
                sum_dice = dice1 + dice2;
                
                //Prüfe, ob der Tip und die gewürfelte Zahl gleich sind, d.h. ob der
                // Spieler gewonnen oder verloren hat
                if (guess == sum_dice)
                {
                    if (guess == 7)
                        gain = 3*stake; //Falls 7 richtig geraten wurde, erhält der Spieler einen dreifachen Einsatz
                    else 
                        gain = 2*stake; //Falls einfach richtig geraten wurde, erhält der Spieler einen zweifachen Einsatz
                    bank_account = bank_account + gain - stake; //Berechne erneut den Kontostand
                    write("Bingo! Sie haben " + gain + " gewonnen. Die Zahl war " + sum_dice + ". Ihr Kontostand ist " + bank_account + ".");
                }  
                else
                    if ((sum_dice < 7 && guess < 7) || (sum_dice > 7 && guess > 7))
                    {
                        write("Sie behalten Ihr Geld. Die Zahl war " + sum_dice + ". Ihr Kontostand ist " + bank_account + ".");
                    }
                    else 
                    {
                        bank_account = bank_account - stake;
                        write("Leider haben Sie " + stake + " Euro verloren. Die Zahl war " + sum_dice + ". Ihr Kontostand ist " + bank_account + ".");
                    } 
            }
            else
                write ("Der Betrag darf maximal " + bank_account + " groß sein.");
        }
        else
            write ("Die Zahl muss zwieschen 2 und 12 liegen");
              
    }
}

Der Code für MiniJava.java ist gegeben:

Java:
package info1;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MiniJava {
    public static String readString(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();

        if (s == null)
            System.exit(0);
        return s;
    }

    public static String readString() {
        return readString("Eingabe:");
    }

    public static int readInt(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();

        int x = 0;
        if (s == null)
            System.exit(0);
        try {
            x = Integer.parseInt(s.trim());
        } catch (NumberFormatException e) {
            x = readInt(text);
        }
        return x;
    }

    public static int readInt() {
        return readInt("Geben Sie eine ganze Zahl ein:");
    }

    public static int read(String text) {
        return readInt(text);
    }

    public static int read() {
        return readInt();
    }

    public static double readDouble(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();

        double x = 0;
        if (s == null)
            System.exit(0);
        try {
            x = Double.parseDouble(s.trim());
        } catch (NumberFormatException e) {
            x = readDouble(text);
        }
        return x;
    }

    public static double readDouble() {
        return readDouble("Geben Sie eine Zahl ein:");
    }

    public static void write(String output) {
        JFrame frame = new JFrame();
        JOptionPane.showMessageDialog(frame, output, "Ausgabe", JOptionPane.PLAIN_MESSAGE);
        frame.dispose();
    }

    public static void write(int output) {
        write("" + output);
    }

    public static void write(double output) {
        write("" + output);
    }

    
    public static int generateNumber(int minval, int maxval){
        return minval + (new java.util.Random()).nextInt(maxval-minval+1);
    }
    public static int drawCard(){
        return generateNumber(2,11);
    }
    
    public static int dice(){
        return generateNumber(1,6);
    }

}
 
Zuletzt bearbeitet:
Wenn ich das bis jetzt richtig überschaut habe kannst du einfach ein "for"-Schleife in deiner LustigesSieben-Klasse erstellen, welche vor der ersten "if" Abfrage beginnt und nach dem letzten "else" aufhört. Vor dieser "for"-Schleife kannst du noch mit einem weiteren "reader" den Spieler fragen wie oft er spielen will und dann diesen Wert in der "for"-Schleife einbauen.

Freundliche Grüsse Andreas
 
kannst du noch mit einem weiteren "reader" den Spieler fragen wie oft er spielen will

warum zwingend ein weiterer ?

und an den To , der Sinn der OOP ist es nicht alles als static zu deklarieren 😉

Ich geh eben Trainieren , frühstücke und schau mir das ganze dann mal in ruhe an :smoke:
 
Zuletzt bearbeitet:
Der Code für MiniJava.java ist gegeben:

Ist der Code für die Klasse ernsthaft so angegeben ?

Java:
public class MiniJava {
    public static String readString(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();
 
        if (s == null)
            System.exit(0);
        return s;
    }
 
    public static String readString() {
        return readString("Eingabe:");
    }
 
    public static int readInt(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();
 
        int x = 0;
        if (s == null)
            System.exit(0);
        try {
            x = Integer.parseInt(s.trim());
        } catch (NumberFormatException e) {
            x = readInt(text);
        }
        return x;
    }
 
    public static int readInt() {
        return readInt("Geben Sie eine ganze Zahl ein:");
    }
 
    public static int read(String text) {
        return readInt(text);
    }
 
    public static int read() {
        return readInt();
    }
 
    public static double readDouble(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();
 
        double x = 0;
        if (s == null)
            System.exit(0);
        try {
            x = Double.parseDouble(s.trim());
        } catch (NumberFormatException e) {
            x = readDouble(text);
        }
        return x;
    }
 
    public static double readDouble() {
        return readDouble("Geben Sie eine Zahl ein:");
    }
 
    public static void write(String output) {
        JFrame frame = new JFrame();
        JOptionPane.showMessageDialog(frame, output, "Ausgabe", JOptionPane.PLAIN_MESSAGE);
        frame.dispose();
    }
 
    public static void write(int output) {
        write("" + output);
    }
 
    public static void write(double output) {
        write("" + output);
    }
 
 
    public static int generateNumber(int minval, int maxval){
        return minval + (new java.util.Random()).nextInt(maxval-minval+1);
    }
    public static int drawCard(){
        return generateNumber(2,11);
    }
 
    public static int dice(){
        return generateNumber(1,6);
    }
 
}

Falls ja , woher hast du die Aufgabe bzw. den Source Code ? , rein aus Interesse......
 

Zurück
Oben