Wie würde das mit der Instanzvariable dann aussehen?
CardDeck deck1 = new CardDeck();
und nicht dass Herz DREI oder so
@Override
public String toString() {
return "CardDeck";
}
@Override
public String toString()
{
return "CardDeck{" +
"cards=" + cards +
'}';
}
Du musst schon selbst dafür sorgen, dass das ausgegeben wird, was Du willst.und immer noch nicht Herz DREI.
Ehrlich, man nimmt einfach Strings dafür und keine Objekte - das meinte ich damit sich nicht jeden Kokolores andrehen zu lassen - und nebenbei sei angemerkt ist das auch effizienter. => Weniger Overhead und weniger Boilerplate, schneller zu schreiben, besser zu lesen.Kokolores?
Mit Strings wäre das allerdings kein nennenswertes Problem.Du musst schon selbst dafür sorgen, dass das ausgegeben wird, was Du willst.
public enum CardColor
{
Karo, Herz, Pik, Kreuz
}
public enum CardValue
{
ZWEI, DREI, VIER, FUENF, SECHS, SIEBEN, ACHT, NEUN, ZEHN, BUBE, DAME, KOENIG, Ass
}
public class Card
{
private CardColor color;
private CardValue value;
public Card(CardColor color, CardValue value)
{
}
@Override
public String toString()
{
return "Card{" +
"color=" + color +
", value=" + value +
'}';
}
}
import java.util.ArrayList;
import java.util.List;
public class CardDeck
{
public ArrayList<Card> cards;
public CardDeck()
{
cards = new ArrayList<>();
for (CardColor color: CardColor.values()) {
for (CardValue value: CardValue.values()) {
cards.add(new Card(color, value));
}
}
cards.add(new Card(CardColor.Kreuz, CardValue.ZWEI));
}
@Override
public String toString()
{
return "CardDeck{" +
"cards=" + cards +
'}';
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class MainGui
{
private JPanel panel1;
private JLabel balance;
private JButton geldEinzahlenButton;
private JTextField geldEinzahlenfield;
private JButton geldWettenButton;
private JTextField geldWettenEinzahlenfield;
private JLabel wettgeld;
private JLabel kCroupier;
private JButton hitButton;
private JButton standButton;
private JButton neuesSpielbutton;
private JButton doublebutton;
private JButton splitButton;
public static void main(String[] args)
{
JFrame frame = new JFrame("Black Jack");
frame.setContentPane(new MainGui().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public MainGui()
{
geldEinzahlenButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Main.balance = Main.balance + Integer.parseInt(geldEinzahlenfield.getText());
balance.setText("" + Main.balance);
geldEinzahlenfield.setText("0");
}
});
geldWettenButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
int temp = Integer.parseInt(geldWettenEinzahlenfield.getText());
if (Main.balance < temp)
{
JOptionPane.showMessageDialog(null,"Sie müssen zuerst Geld einzahlen");
}
else
{
Main.wbalance = Main.wbalance + Integer.parseInt(geldWettenEinzahlenfield.getText());
Main.balance = Main.balance - Integer.parseInt(geldWettenEinzahlenfield.getText());
wettgeld.setText("" + Main.wbalance);
balance.setText("" + Main.balance);
geldWettenEinzahlenfield.setText("0");
}
}
});
CardDeck deck1 = new CardDeck();
neuesSpielbutton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
neuesSpielbutton.setVisible(false);
System.out.println(deck1.toString());
}
});
}
}
public class Main
{
public static int balance;
public static int wbalance;
public Main()
{
balance = 0;
wbalance = 0;
}
public static void main(String[] args)
{
MainGui.main(args);
}
}
Beim Konstruktor hatte ich doch den Inhalt weggelassen um es kurz zu halten. Wie muss denn den Konstruktor aussehen, damit die Parameter in den Instanzvariablen gespeichert werden?Code:public class Card { private CardColor color; private CardValue value; public Card(CardColor color, CardValue value) { } @Override public String toString() { return "Card{" + "color=" + color + ", value=" + value + '}'; } }
public void RandomCard()
{
int temp =(int) (Math.random() * cards.size() - 1);
System.out.println(cards.get(temp));
cards.remove(temp); // Karte soll pro spiel nur einmal vorkommen
}
Das kann man über einen getter aus der Klasse card machen oder ?
Genau hier sollte man beginnen: ein Plan. Um das Vorhaben übersichtlicher zu machen würde ich fürs Erste auf GUI verzichten.Die Idee ist doch, dass man eine saubere Objektstruktur bekommt
public CardDeck()
{
cards = new ArrayList<>();
for (CardColor color: CardColor.values()) {
for (CardValue value: CardValue.values()) {
cards.add(new Card(color, value));
}
}
}
public CardDeck()
{
createCardDeck();
}
private void createCardDeck()
{
//Dein Code
}
public Card getNextCard()
{
if(cards.isEmpty())
{
createCardDeck();
}
Card card ...
}
public String createName(boolean gender)
{
if(gender)
{
//Men
} else
{
//Women
}
}
String name = NameCreator.createName(false);
Player p = new ComputerPlayer(name);
public class ComputerPlayer
{
public ComputerPlayer(String name)
{
super(name);
}
}
autoButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
One_Sec_Clock.ThreadStarten();
}
});
}
public void Auto ()
{
System.out.println("a");
if (Main.balance == 0)
{
JOptionPane.showMessageDialog(null,"Sie haben nicht genügend Geld");
One_Sec_Clock.ThreadStoppen();
return;
}
Main.balance = Main.balance - 1;
balance.setText("" + Main.balance);
Automat automat1 = new Automat();
automat1.getRandomNr();
w1.setText("" + Automat.w1);
w2.setText("" + Automat.w2);
w3.setText("" + Automat.w3);
w4.setText("" + Automat.w4);
if (Automat.w1 == Automat.w2 && Automat.w2 == Automat.w3 && Automat.w3 == Automat.w4 && Automat.w4 == 1)
{
Main.balance = Main.balance + 100;
balance.setText("" + Main.balance);
}
System.out.println("b");
public class One_Sec_Clock
{
public static class Thread1 extends Thread
{
public void run()
{
MainGui o = new MainGui();
while(true)
{
System.out.println("test1");
try {Thread.sleep(1000); }
catch (Exception e1) {
System.out.println("return");
return;
}
System.out.println("test2");
o.Auto();
System.out.println("test3");
}
}
}
public static void ThreadStarten()
{
Thread thread = new Thread1();
thread.start();
}
public static void ThreadStoppen()
{
Thread.currentThread().stop();
}
}
public class OneSecClock implements Runnable // Statt von Thread ableiten einfach Runnable implementieren.
{
private Thread thread = null; // Wir speichern den erzeugten Thread für später.
private MainGui mainGui; // Wir speichern die MainGui - gefällt mir vom Design noch nicht, aber erst mal egal!
public OneSecClock(final MainGui mainGui) { // Konstruktor setzt die MainGui!
this.mainGui = mainGui;
}
// Run Methode von Runnable.
public void run()
{
MainGui o = new MainGui();
while(true) {
System.out.println("test1");
try {Thread.sleep(1000); }
catch (Exception e1) {
System.out.println("return");
return;
}
System.out.println("test2");
mainGui.Auto(); // Auto ist eine Methode und muss klein geschrieben werden!
System.out.println("test3");
}
}
public void start() {
// Check if thread already exists.
if (thread != null && thread.isAlive()) return;
Thread thread = new Thread(this);
thread.start();
}
public void stop() {
if (thread == null || !thread.isAlive()) return;
thread.stop();
}
}
Zum Beispiel:Welche vorhandene Timer gibt es denn?
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainGui
{
private JPanel Jpanel;
private JLabel w1;
private JLabel w2;
private JLabel w3;
private JButton PlayButton;
private JLabel balance;
private JButton InsertMoneyButton;
private JTextField InsertMoneyValue;
private JButton autoButton;
private JLabel w4;
public static void main(String[] args)
{
JFrame frame = new JFrame("Einarmiger Bandit");
frame.setContentPane(new MainGui().Jpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public MainGui()
{
InsertMoneyButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Main.balance = Main.balance + Integer.parseInt(InsertMoneyValue.getText());
balance.setText("" + Main.balance);
InsertMoneyValue.setText("0");
}
});
PlayButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (Main.balance == 0)
{
JOptionPane.showMessageDialog(null,"Sie haben nicht genügend Geld");
return;
}
Main.balance = Main.balance - 1;
balance.setText("" + Main.balance);
Automat automat1 = new Automat();
automat1.getRandomNr();
w1.setText("" + Automat.w1);
w2.setText("" + Automat.w2);
w3.setText("" + Automat.w3);
w4.setText("" + Automat.w4);
if (Automat.w1 == Automat.w2 && Automat.w2 == Automat.w3 && Automat.w3 == Automat.w4 && Automat.w4 == 1)
{
JOptionPane.showMessageDialog(null,"Sie haben Gewonnen!");
Main.balance = Main.balance + 100;
balance.setText("" + Main.balance);
}
}
});
autoButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
}
public void auto ()
{
System.out.println("a");
if (Main.balance == 0)
{
JOptionPane.showMessageDialog(null,"Sie haben nicht genügend Geld");
//TODO Thread stoppen muss geschrieben und hier implementiert werden
return;
}
Main.balance = Main.balance - 1;
balance.setText("" + Main.balance);
Automat automat1 = new Automat();
automat1.getRandomNr();
w1.setText("" + Automat.w1);
w2.setText("" + Automat.w2);
w3.setText("" + Automat.w3);
w4.setText("" + Automat.w4);
if (Automat.w1 == Automat.w2 && Automat.w2 == Automat.w3 && Automat.w3 == Automat.w4 && Automat.w4 == 1)
{
Main.balance = Main.balance + 100;
balance.setText("" + Main.balance);
}
System.out.println("b");
}
}
public class One_Sec_Clock implements Runnable
{
private Thread thread = null;
private MainGui mainGui;
public One_Sec_Clock(MainGui mainGui)
{
this.mainGui = mainGui;
}
public void run()
{
while(true)
{
System.out.println("test1");
try {Thread.sleep(1000); }
catch (Exception e1) {
System.out.println("return");
return;
}
System.out.println("test2");
mainGui.auto();
System.out.println("test3");
}
}
public void threadstarten()
{
thread.start();
}
public void threadstoppen()
{
}
}
public class Walze
{
/*Eine Walze hat 9 Möglichkeiten zu stehen von 1 bis 9*/
public static int number;
public int RandomNumber()
{
int temp = (int) (Math.random() * 9 + 1);
return temp;
}
}
public class Automat
{
public static int w1;
public static int w2;
public static int w3;
public static int w4;
public void getRandomNr()
{
Walze w = new Walze();
w1 = w.RandomNumber();
w2 = w.RandomNumber();
w3 = w.RandomNumber();
w4 = w.RandomNumber();
}
}
One_Sec_Clock o = new One_Sec_Clock();
.One_Sec_Clock o = new One_Sec_Clock(this);