diff --git a/README.md b/README.md index 442056a..4e7e48f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,2 @@ -# Aufgabe ArrayList Lottozahlen: - -a) Lassen Sie die zufälligen Lottozahlen 6 aus 49 ziehen, setzen Sie diese in eine ArrayList lottozahlen. - -b) Geben Sie Ihre getippten eigenen 6 Zahlen ein und ermitteln Sie die Anzahl der Richtigen! \ No newline at end of file +# Lotto +![Vorschaubild](img/Lotto_Vorschaubild.png) \ No newline at end of file diff --git a/img/Lotto_Vorschaubild.png b/img/Lotto_Vorschaubild.png new file mode 100644 index 0000000..e07ab5b Binary files /dev/null and b/img/Lotto_Vorschaubild.png differ diff --git a/src/gfn/marc/Feld.java b/src/gfn/marc/Feld.java new file mode 100644 index 0000000..bc454e5 --- /dev/null +++ b/src/gfn/marc/Feld.java @@ -0,0 +1,85 @@ +package gfn.marc; + +import javax.swing.*; + +public class Feld { + private static int feldAnzahl = 0; + private int feldId; + private Rechteck rechteck; + private JLabel label = new JLabel(); + private boolean gesetzt; + private Kreuz kreuz; + private Kreis kreis; + + static { + + } + + // Konstruktor + public Feld(Rechteck rechteck) { + if (feldAnzahl == 49) { + Feld.feldAnzahl = 0; + } + this.rechteck = rechteck; + Feld.feldAnzahl++; + this.feldId = Feld.feldAnzahl; + + // Label Position und Größe des Rechtecks zuweisen + this.label.setBounds((this.rechteck.getPara1() - Spielfeld.KORREKTUR_X), (this.rechteck.getPara2() - + Spielfeld.KORREKTUR_Y), this.rechteck.getPara3(), this.rechteck.getPara4()); + this.label.setVisible(true); + + kreuz = new Kreuz((int) (rechteck.getPara1() + (rechteck.getPara3() * 0.25)), + ((int) (rechteck.getPara2() + (rechteck.getPara4() * 0.25))), + ((int) (rechteck.getPara1() + (rechteck.getPara3() * 0.75))), + ((int) (rechteck.getPara2() + (rechteck.getPara4() * 0.75)))); + + kreis = new Kreis((int) (rechteck.getPara1() + (0.1 * rechteck.getPara3())), + (int) (rechteck.getPara2() + (0.1 * rechteck.getPara4())), + ((int) (rechteck.getPara3() * 0.8)), + ((int) (rechteck.getPara4() * 0.8))); + + } + + public void setZeichen(Form form) { + if (form.getClass() == kreuz.getClass()) { + kreuz.setSichtbar(true); + } else { + kreis.setSichtbar(true); + } + gesetzt = true; + + } + + public JLabel getLabel() { + return label; + } + + public Rechteck getRechteck() { + return rechteck; + } + + public Kreuz getKreuz() { + return kreuz; + } + + public void setKreuz(Kreuz kreuz) { + this.kreuz = kreuz; + } + + public Kreis getKreis() { + return kreis; + } + + public void setKreis(Kreis kreis) { + this.kreis = kreis; + } + + public int getFeldId() { + return feldId; + } + + public boolean isGesetzt() { + return gesetzt; + } +} diff --git a/src/gfn/marc/Form.java b/src/gfn/marc/Form.java new file mode 100644 index 0000000..20833b3 --- /dev/null +++ b/src/gfn/marc/Form.java @@ -0,0 +1,64 @@ +package gfn.marc; + +import java.awt.*; + +public abstract class Form { + private int para1; + private int para2; + private int para3; + private int para4; + private boolean sichtbar; + + public Form() { + } + + public Form(int para1, int para2, int para3, int para4) { + this.para1 = para1; + this.para2 = para2; + this.para3 = para3; + this.para4 = para4; + } + + public abstract void paintMe(Graphics2D g); + + public int getPara1() { + return para1; + } + + public int getPara2() { + return para2; + } + + public int getPara3() { + return para3; + } + + public int getPara4() { + return para4; + } + + public boolean isSichtbar() { + return sichtbar; + } + + public void setPara1(int para1) { + this.para1 = para1; + } + + public void setPara2(int para2) { + this.para2 = para2; + } + + public void setPara3(int para3) { + this.para3 = para3; + } + + public void setPara4(int para4) { + this.para4 = para4; + } + + public void setSichtbar(boolean sichtbar) { + this.sichtbar = sichtbar; + } + +} diff --git a/src/gfn/marc/Kreis.java b/src/gfn/marc/Kreis.java new file mode 100644 index 0000000..30e131b --- /dev/null +++ b/src/gfn/marc/Kreis.java @@ -0,0 +1,22 @@ +package gfn.marc; + +import java.awt.*; + +public class Kreis extends Form { + + public Kreis() { + } + + public Kreis(int para1, int para2, int para3, int para4) { + super(para1, para2, para3, para4); + } + + @Override + public void paintMe(Graphics2D g) { + if (this.isSichtbar()) { + g.setColor(Color.red); + g.setStroke(new BasicStroke(6)); + g.drawOval(this.getPara1(), this.getPara2(), this.getPara3(), this.getPara4()); + } + } +} diff --git a/src/gfn/marc/Kreuz.java b/src/gfn/marc/Kreuz.java new file mode 100644 index 0000000..19227f5 --- /dev/null +++ b/src/gfn/marc/Kreuz.java @@ -0,0 +1,24 @@ +package gfn.marc; + +import java.awt.*; + +public class Kreuz extends Form { + + public Kreuz() { + } + + public Kreuz(int para1, int para2, int para3, int para4) { + super(para1, para2, para3, para4); + } + + @Override + public void paintMe(Graphics2D g) { + if (this.isSichtbar()) { + g.setColor(Color.blue); + g.setStroke(new BasicStroke(8)); + g.drawLine(this.getPara1(), this.getPara2(), this.getPara3(), this.getPara4()); + g.drawLine(this.getPara3(), this.getPara2(), this.getPara1(), this.getPara4()); + } + } + +} diff --git a/src/gfn/marc/Lotto.java b/src/gfn/marc/Lotto.java new file mode 100644 index 0000000..5ebafdf --- /dev/null +++ b/src/gfn/marc/Lotto.java @@ -0,0 +1,92 @@ +package gfn.marc; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.util.*; +import static gfn.marc.Spielfeld.KORREKTUR_X; +import static gfn.marc.Spielfeld.KORREKTUR_Y; + +public class Lotto extends JFrame { + + static Frame f; + static int fensterGroesse = 600; + static Spielfeld spielfeld; + static Spiel spiel; + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + boolean eingabeKorrekt = false; + + try { + boolean nochEinSpiel = true; + while (nochEinSpiel) { + + f = new Lotto(); + f.setSize((fensterGroesse + KORREKTUR_X), (fensterGroesse + KORREKTUR_Y)); // Fenster Breite und Höhe + f.setLocationRelativeTo(null); + f.setLayout(null); + f.setResizable(false); + f.setTitle("Lotto"); // Fenster Titeltext + f.setVisible(true); // Fenster anzeigen + f.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); // sofortiges Programmende + } + }); + + spielfeld = new Spielfeld(f); + spiel = new Spiel(f, spielfeld); + nochEinSpiel = spiel.spielen(); + + // aufräumen + spielfeld.aufraeumen(); + spiel = null; + spielfeld = null; + Zug.setZugNummer(0); + f.setVisible(false); + f = null; + } + } catch (InterruptedException ie) { + System.err.println("Da ist was schief gelaufen!"); + } + System.exit(0); // sofortiges Programmende + + } + + public static Spielfeld getSpielfeld() { + return spielfeld; + } + + public static Frame getF() { + return f; + } + + public void paint(Graphics g) { + + Graphics2D g2 = (Graphics2D) g; + + if (spielfeld != null) { + spielfeld.zeichneSpielfeld(g2); + spielfeld.setSpielfeldExistiert(true); + } + + if (spielfeld != null) { + for (Feld feld : spielfeld.getFelder()) { + feld.getKreuz().paintMe(g2); + feld.getKreis().paintMe(g2); + } + } + + if (spielfeld != null) { + for (Feld feld : spielfeld.getFelder()) { + feld.getRechteck().faerbeGruen(g2); + feld.getKreuz().paintMe(g2); + feld.getKreis().paintMe(g2); + } + } + } +} diff --git a/src/gfn/marc/Lottozahlen.java b/src/gfn/marc/Lottozahlen.java deleted file mode 100644 index 6294e49..0000000 --- a/src/gfn/marc/Lottozahlen.java +++ /dev/null @@ -1,83 +0,0 @@ -package gfn.marc; - -import java.util.*; - -public class Lottozahlen { - - public static void main(String[] args) { - - Scanner scanner = new Scanner(System.in); - boolean eingabeKorrekt = false; - - // Lottoschein ausfüllen - HashSet lottoschein = new HashSet<>(6); - while (!eingabeKorrekt) { - System.out.println("Bitte geben Sie sechs, durch Kommata getrennte, Lottozahlen (1 - 49) ein. " + - "(Beispiel: \"6,12,29,34,40,46\")"); - String eingabe = scanner.nextLine(); - - // Eingabe überprüfen - if (eingabe.matches("(\\s*(([1-4][0-9])|[1-9])\\s*,?){6}")) { - - // Eingabe in ArrayList übertragen - String[] zahlen = eingabe.split("[,]"); - for (String zahl : zahlen) { - lottoschein.add(Integer.parseInt(zahl.trim())); - } - - // Prüfung: doppelte Zahlen? - if (lottoschein.size() == 6) { - eingabeKorrekt = true; - } else { - System.err.println("Doppelte Zahlen!"); - } - - // Prüfung: Zahlen ungültig? - } else { - System.err.println("Ungültige Zahlen!"); - } - } - - // Lottozahlen ziehen - HashSet lottozahlenSet = new HashSet<>(6); - while (lottozahlenSet.size() < 6) { - lottozahlenSet.add((int) (Math.random() * 49) + 1); - } - ArrayList lottozahlen = new ArrayList<>(lottozahlenSet); - - // Lottozahlen ausgeben - System.out.println(); - System.out.print("Lottozahlen: "); - Collections.sort(lottozahlen); - for (int i = 0; i < lottozahlen.size(); i++) { - if (i < lottozahlen.size() - 1) { - System.out.print(lottozahlen.get(i) + ", "); - } else { - System.out.print(lottozahlen.get(i)); - } - } - System.out.println("\n"); - - // Lottoschein auswerten - ArrayList richtige = new ArrayList<>(); - for (int zahl : lottoschein) { - if (lottozahlen.contains(zahl)) { - richtige.add(zahl); - } - } - - // Ergebnis ausgeben - Collections.sort(richtige); - System.out.println(richtige.isEmpty() ? "Sie haben leider keine richtige Zahl!" : - "Sie haben " + richtige.size() + " Richtige: "); - if (!richtige.isEmpty()) { - for (int i = 0; i < richtige.size(); i++) { - if (i < richtige.size() - 1) { - System.out.print(richtige.get(i) + ", "); - } else { - System.out.print(richtige.get(i)); - } - } - } - } -} diff --git a/src/gfn/marc/Rechteck.java b/src/gfn/marc/Rechteck.java new file mode 100644 index 0000000..0601ccc --- /dev/null +++ b/src/gfn/marc/Rechteck.java @@ -0,0 +1,44 @@ +package gfn.marc; + +import java.awt.*; + +public class Rechteck extends Form { + private boolean gewinnerFeld = false; + + public Rechteck(int para1, int para2, int para3, int para4) { + super(para1, para2, para3, para4); + } + + @Override + public void paintMe(Graphics2D g) { + if (this.isSichtbar()) { + g.setColor(Color.black); + g.setStroke(new BasicStroke(5)); + g.drawRect(this.getPara1(), this.getPara2(), this.getPara3(), this.getPara4()); + } + } + + public void faerbeGruen(Graphics2D g) { + if (this.isGewinnerFeld()) { + g.setColor(Color.green); + g.fillRect(this.getPara1(), this.getPara2(), this.getPara3(), this.getPara4()); + g.setColor(Color.black); + g.setStroke(new BasicStroke(5)); + g.drawRect(this.getPara1(), this.getPara2(), this.getPara3(), this.getPara4()); + } + } + + public void paintId(Graphics g, int i) { + String s = Integer.toString(i); + g.drawString(s, ((int) (this.getPara1() + (0.5 * this.getPara3()))), + ((int) (this.getPara2() + (0.5 * this.getPara4())))); + } + + public boolean isGewinnerFeld() { + return gewinnerFeld; + } + + public void setGewinnerFeld(boolean gewinnerFeld) { + this.gewinnerFeld = gewinnerFeld; + } +} diff --git a/src/gfn/marc/Spiel.java b/src/gfn/marc/Spiel.java new file mode 100644 index 0000000..26d9c0c --- /dev/null +++ b/src/gfn/marc/Spiel.java @@ -0,0 +1,95 @@ +package gfn.marc; + +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.HashSet; + +public class Spiel { + + private Frame f; + private Spielfeld spielfeld; + private HashSet lottoschein = new HashSet<>(6); + + Spiel(Frame f, Spielfeld spielfeld) throws InterruptedException { + + this.f = f; + + // Neues Spielfeld anlegen + this.spielfeld = spielfeld; + + } + + public boolean spielen() { + + // Zahlen tippen + try { + Zug zug = new Zug(this); + while (Zug.getZugNummer() < 6) { + Zug.macheZug(); + f.setTitle(Zug.getZugNummer() + "/6 Feldern getippt"); + } + f.repaint(); + Thread.sleep(500); + + // Lottozahlen ziehen + HashSet lottozahlenSet = new HashSet<>(6); + while (lottozahlenSet.size() < 6) { + lottozahlenSet.add((int) (Math.random() * 49) + 1); + } + ArrayList lottozahlen = new ArrayList<>(lottozahlenSet); + + // gezogene Zahlen anzeigen + Feld[] felder = spielfeld.getFelder(); + for (int zahl : lottozahlen) { + for (int i = 0; i < felder.length; i++) { + if (zahl == felder[i].getFeldId()) { + felder[i].getRechteck().setGewinnerFeld(true); + f.repaint(); + break; + } + } + } + + // Lottoschein auswerten + ArrayList richtige = new ArrayList<>(); + for (int zahl : lottoschein) { + if (lottozahlen.contains(zahl)) { + richtige.add(zahl); + } + } + + // richtige Zahlen einkreisen + for (int zahl : richtige) { + for (int i = 0; i < felder.length; i++) { + if (zahl == felder[i].getFeldId()) { + felder[i].setZeichen(new Kreis()); + } + } + } + + // Noch ein Spiel? + if (richtige.size() != 0) { + int auswahl = JOptionPane.showOptionDialog(f, "Glückwunsch! Sie haben " + richtige.size() + + " Richtige!\nNoch ein Spiel?", "Lotto", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, null, null); + return auswahl != JOptionPane.NO_OPTION; + } else { + int auswahl = JOptionPane.showOptionDialog(f, "Sie haben leider keine Zahl richtig getippt." + + "\nNoch ein Spiel?", "Lotto", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, null, null); + return auswahl != JOptionPane.NO_OPTION; + } + + + } catch ( + InterruptedException ie) { + System.err.println("Da ist was schief gelaufen!"); + } + return false; + } + + public void setNumberToLottoschein(int nummer) { + this.lottoschein.add(nummer); + } +} diff --git a/src/gfn/marc/Spielfeld.java b/src/gfn/marc/Spielfeld.java new file mode 100644 index 0000000..9c64e28 --- /dev/null +++ b/src/gfn/marc/Spielfeld.java @@ -0,0 +1,70 @@ +package gfn.marc; + +import java.awt.*; + +public class Spielfeld { + + final static int KORREKTUR_X = 3; // Korrektur der Spielfeldposition auf X Achse + final static int KORREKTUR_Y = 30; // Korrektur der Spielfeldposition auf Y Achse + + private static int fensterGroesse = 600; + private int quadrant = fensterGroesse / 7; + private Frame f; + private Feld[] felder; + private boolean spielfeldExistiert; + + // Unterteilung des Spielfelds in rechteckige Felder + public Spielfeld(Frame f) { + Feld[] felder = new Feld[49]; + this.f = f; + + int k = 0; + for (int i = 0; i < 7; i++) { + for (int j = 0; j < 7; j++) { + felder[k] = new Feld(new Rechteck(((this.quadrant * j) + KORREKTUR_X), (KORREKTUR_Y + (this.quadrant * i)), + this.quadrant, this.quadrant)); + k++; + } + } + + this.felder = felder; + } + + // zeichne Spielfeld + public void zeichneSpielfeld(Graphics2D g) { + + for (Feld feld : this.felder) { + feld.getRechteck().setSichtbar(true); + feld.getRechteck().paintMe(g); + // Zahlen auf Felder einblenden, wenn Debug-Mode aktiviert ist + this.f.add(feld.getLabel()); + feld.getRechteck().paintId(g, feld.getFeldId()); + + } + } + + // Spielfeld äufräumen + public void aufraeumen() { + + felder = null; + this.f = null; + + } + + public Feld[] getFelder() { + return felder; + } + + public Frame getF() { + return f; + } + + public boolean isSpielfeldExistiert() { + return spielfeldExistiert; + } + + public void setSpielfeldExistiert(boolean spielfeldExistiert) { + this.spielfeldExistiert = spielfeldExistiert; + } +} + diff --git a/src/gfn/marc/Zug.java b/src/gfn/marc/Zug.java new file mode 100644 index 0000000..4543fa6 --- /dev/null +++ b/src/gfn/marc/Zug.java @@ -0,0 +1,132 @@ +package gfn.marc; + +import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +public class Zug { + + private static Frame f; + private static Spielfeld spielfeld; + static Spiel spiel; + private static int zugNummer = 0; + private static boolean zugLaeuft; + private static CustomMouseListener[] cmls = new CustomMouseListener[49]; + + public Zug(Spiel spiel) { + Zug.spiel = spiel; + Zug.spielfeld = Lotto.getSpielfeld(); + Zug.f = Lotto.getF(); + createMouseListeners(); + } + + // Zuganzahl hochzählen und Titelleiste setzen + public static void macheZug() { + if (zugNummer == 9) { + Zug.zugNummer = 0; + } + Zug.zugNummer++; + Zug.setZugLaeuft(true); + + + // Mouselistener auf Felder setzen, die noch nicht gesetzt wurden + for (int i = 0; i < spielfeld.getFelder().length; i++) { + if (!spielfeld.getFelder()[i].isGesetzt()) { + spielfeld.getFelder()[i].getLabel().addMouseListener(cmls[i]); + } + } + + // Auf setzen eines Feldes warten + while (Zug.isZugLaeuft()) { + try { + Thread.sleep(200); + } catch (InterruptedException ie) { + break; + } + } + + // MouseListener von allen Feldern entfernen + for (int i = 0; i < spielfeld.getFelder().length; i++) { + spielfeld.getFelder()[i].getLabel().removeMouseListener(cmls[i]); + } + + } + + public static void createMouseListeners() { + for (int i = 0; i < spielfeld.getFelder().length; i++) { + cmls[i] = new CustomMouseListener(f, spielfeld.getFelder()[i]); + } + } + + public static int getZugNummer() { + return zugNummer; + } + + public static boolean isZugLaeuft() { + return zugLaeuft; + } + + public static void setZugLaeuft(boolean zugLaeuft) { + Zug.zugLaeuft = zugLaeuft; + } + + public static void setF(Frame f) { + Zug.f = f; + } + + public static void setSpielfeld(Spielfeld spielfeld) { + Zug.spielfeld = spielfeld; + } + + public static void setZugNummer(int zugNummer) { + Zug.zugNummer = zugNummer; + } + + public static void setCmls(CustomMouseListener[] cmls) { + Zug.cmls = cmls; + } +} + + +class CustomMouseListener implements MouseListener { + + private Frame f; + private Feld feld; + + public CustomMouseListener(Frame f, Feld feld) { + this.f = f; + this.feld = feld; + } + + // Auf Setzen des Feldes prüfen und ggf. Form zeichnen lassen + @Override + public void mouseClicked(MouseEvent mouseEvent) { + this.feld.setZeichen(new Kreuz()); + f.repaint(); + Zug.spiel.setNumberToLottoschein(this.feld.getFeldId()); + Zug.setZugLaeuft(false); + } + + // notwendige Implementierungen ohne weitere Funktion + @Override + public void mousePressed(MouseEvent mouseEvent) { + + } + + @Override + public void mouseReleased(MouseEvent mouseEvent) { + + } + + @Override + public void mouseEntered(MouseEvent mouseEvent) { + + } + + @Override + public void mouseExited(MouseEvent mouseEvent) { + + } +} + +