Hey Leute
ich hab in der Uni ein Projekt und hab da schon einiges hinbekommen aber es scheitert gerade noch an einigen Sachen bezüglicher der GUI und der Kartendarstellung ich hoffe das ihr mir helfen könnt.
Es handelt sich beim Projekt um das Kartenspiel UNO dies soll über ein Netzwerk erfolgen also sollen maximal vier Spieler das Spiel spielen können, die Netzwerkverbindung hab ich schon.
Im Prinzip hab ich eine Klasse Card und die extends JLabel und über eine LinkedList komm ich quasi an die Karten der Spieler ran die befindet sich in der Klasse Game.
Ich bekomme es gerade einfach nicht hin... sobald ich auf die Liste zugreife bekomme ich nur NullPointer XD .. und ich weiß noch nicht wie das mit den jeweiligen Clients und ihren Kartendarstellungen machen soll. Ich hoffe ihr könnt mir etwas helfen eventuell hab ich einen riesigen Denkfehler > ___ <
Die Klasse Card:
Die Klasse Game:
Ich muss aber sagen das ich GUI und so nicht wo wirklich kann ich versteh das nicht >_ < falls ihr noch was braucht könnt ihr das ja kommentieren ich füg das nachträglich nach.
Und das war mein Versuch:
ich hab in der Uni ein Projekt und hab da schon einiges hinbekommen aber es scheitert gerade noch an einigen Sachen bezüglicher der GUI und der Kartendarstellung ich hoffe das ihr mir helfen könnt.
Es handelt sich beim Projekt um das Kartenspiel UNO dies soll über ein Netzwerk erfolgen also sollen maximal vier Spieler das Spiel spielen können, die Netzwerkverbindung hab ich schon.
Im Prinzip hab ich eine Klasse Card und die extends JLabel und über eine LinkedList komm ich quasi an die Karten der Spieler ran die befindet sich in der Klasse Game.
Ich bekomme es gerade einfach nicht hin... sobald ich auf die Liste zugreife bekomme ich nur NullPointer XD .. und ich weiß noch nicht wie das mit den jeweiligen Clients und ihren Kartendarstellungen machen soll. Ich hoffe ihr könnt mir etwas helfen eventuell hab ich einen riesigen Denkfehler > ___ <
Die Klasse Card:
Java:
public class Card extends JLabel implements CardInterface{
public final static Card[] possibleCards = new Card[]{
new Card(MyColor.BLAU, 0, new ImageIcon ("./bin/Blau_0.png")),
new Card(MyColor.BLAU, 1, new ImageIcon ("./bin/Blau_1.png")),
new Card(MyColor.BLAU, 2, new ImageIcon ("./bin/Blau_2.png")),
new Card(MyColor.BLAU, 3, new ImageIcon ("./bin/Blau_3.png")),
new Card(MyColor.BLAU, 4, new ImageIcon ("./bin/Blau_4.png")),
new Card(MyColor.BLAU, 5, new ImageIcon ("./bin/Blau_5.png")),
new Card(MyColor.BLAU, 6, new ImageIcon ("./bin/Blau_6.png")),
new Card(MyColor.ROT, 0, new ImageIcon ("./bin/Rot_0.png")),
new Card(MyColor.ROT, 1, new ImageIcon ("./bin/Rot_1.png")),
new Card(MyColor.ROT, 2, new ImageIcon ("./bin/Rot_2.png")),
new Card(MyColor.ROT, 3, new ImageIcon ("./bin/Rot_3.png")),
new Card(MyColor.ROT, 4, new ImageIcon ("./bin/Rot_4.png")),
new Card(MyColor.ROT, 5, new ImageIcon ("./bin/Rot_5.png")),
new Card(MyColor.ROT, 6, new ImageIcon ("./bin/Rot_6.png")),
new Card(MyColor.GELB, 0, new ImageIcon ("./bin/Gelb_0.png")),
new Card(MyColor.GELB, 1, new ImageIcon ("./bin/Gelb_1.png")),
new Card(MyColor.GELB, 2, new ImageIcon ("./bin/Gelb_2.png")),
new Card(MyColor.GELB, 3, new ImageIcon ("./bin/Gelb_3.png")),
new Card(MyColor.GELB, 4, new ImageIcon ("./bin/Gelb_4.png")),
new Card(MyColor.GELB, 5, new ImageIcon ("./bin/Gelb_5.png")),
new Card(MyColor.GELB, 6, new ImageIcon ("./bin/Gelb_6.png")),
new Card(MyColor.GRUEN, 0, new ImageIcon ("./bin/Grün_0.png")),
new Card(MyColor.GRUEN, 1, new ImageIcon ("./bin/Grün_1.png")),
new Card(MyColor.GRUEN, 2, new ImageIcon ("./bin/Grün_2.png")),
new Card(MyColor.GRUEN, 3, new ImageIcon ("./bin/Grün_3.png")),
new Card(MyColor.GRUEN, 4, new ImageIcon ("./bin/Grün_4.png")),
new Card(MyColor.GRUEN, 5, new ImageIcon ("./bin/Grün_5.png")),
new Card(MyColor.GRUEN, 6, new ImageIcon ("./bin/Grün_6.png")),
new Card(MyColor.GRUEN, EffectCard.ZiehZwei, new ImageIcon ("./bin/Grün_+2.png")),
new Card(MyColor.ROT, EffectCard.ZiehZwei, new ImageIcon ("./bin/Rot_+2.png")),
new Card(MyColor.BLAU, EffectCard.ZiehZwei, new ImageIcon ("./bin/blau_+2.png")),
new Card(MyColor.GELB, EffectCard.ZiehZwei, new ImageIcon ("./bin/Gelb_+2.png")),
new Card(MyColor.GRUEN, EffectCard.Richtungswechsel, new ImageIcon ("./bin/Grün_Richtungswechsel.png")),
new Card(MyColor.ROT, EffectCard.Richtungswechsel, new ImageIcon ("./bin/Rot_Richtungswechsel.png")),
new Card(MyColor.BLAU, EffectCard.Richtungswechsel, new ImageIcon ("./bin/Blau_Richtungswechsel.png")),
new Card(MyColor.GELB, EffectCard.Richtungswechsel, new ImageIcon ("./bin/Gelb_Richtungswechsel.png")),
new Card(MyColor.GRUEN, EffectCard.Aussetzen, new ImageIcon ("./bin/Grün_Aussetzen.png")),
new Card(MyColor.ROT, EffectCard.Aussetzen, new ImageIcon ("./bin/Rot_Aussetzen.png")),
new Card(MyColor.BLAU, EffectCard.Aussetzen, new ImageIcon ("./bin/Blau_Aussetzen.png")),
new Card(MyColor.GELB, EffectCard.Aussetzen, new ImageIcon ("./bin/Gelb_Aussetzen.png")),
new Card(MyColor.SCHWARZ, EffectCard.ZiehVierFarbwahl, new ImageIcon ("./bin/Schwarz_+4.png")),
new Card(MyColor.SCHWARZ, EffectCard.Farbwahl, new ImageIcon ("./bin/Schwarz_Farbauswahl.png")),
};
private final MyColor color;
private final EffectCard effectCard;
//actionCard?
private final int value;
/**
* Constructor
* @param c the color of this card
* @param v its value
* @throws {@link IllegalArgumentException} if c is null or v is not in range of 0-9
*/
public Card(MyColor c, int v, ImageIcon bild){
if(c == null)
throw new IllegalArgumentException("Karte muss eine Farbe haben!");
if(v < 0 || v > 9) throw new IllegalArgumentException("Karte muss einen wert zwischen 0-9 haben!");
color = c;
value = v;
this.setIcon(bild);
effectCard = null;
}
public Card(MyColor c, EffectCard ec, ImageIcon bild){
if(c == null)
throw new IllegalArgumentException("Karte muss eine Farbe haben!");
color = c;
value = -1;
this.setIcon(bild);
effectCard = ec;
}
public Card(String color, String value, String effect){
System.out.println(color);
System.out.println(value);
System.out.println(effect);
if(effect.equals("null") ||effect.equals(-1)){
effectCard = null;
} else if(effect.equals("ZiehZwei")){
effectCard = EffectCard.ZiehZwei;
} else if(effect.equals("ZiehVierFarbwahl")){
effectCard = EffectCard.ZiehVierFarbwahl;
} else if(effect.equals("Farbwahl")){
effectCard = EffectCard.Farbwahl;
} else if(effect.equals("Richtungswechsel")){
effectCard = EffectCard.Richtungswechsel;
} else if(effect.equals("Aussetzen")){
effectCard = EffectCard.Aussetzen;
} else {
System.out.println(effect);
throw new IllegalArgumentException("ERROR");
}
this.value = Integer.valueOf(value);
if(color.equals("SCHWARZ")){
this.color = MyColor.SCHWARZ;
}
else if(color.equals("BLAU")){
this.color = MyColor.BLAU;
}
else if(color.equals("GELB")){
this.color = MyColor.GELB;
}
else if(color.equals("ROT")){
this.color = MyColor.ROT;
}
else if(color.equals("GRUEN")){
this.color = MyColor.GRUEN;
} else {
throw new IllegalArgumentException("ERROR");
}
}
/**
*
* @return color, effectCard and value
*/
public MyColor getColor() {
return color;
}
public EffectCard getEffectCard() {
return effectCard;
}
public int getValue() {
return value;
}
public String toString(){
return "Karte: " + getColor() + " " + getValue() + " " + getEffectCard();
}
@Override
public boolean equals(Object o){
if(!(o instanceof Card)){
return super.equals(o);
}
Card oth = (Card) o;
System.out.println(oth);
return oth.color == color && oth.value == value && oth.effectCard == effectCard;
}
}
Die Klasse Game:
Java:
public class Game {
private LinkedList<Player> player;
private boolean gameOver;
private Stack<Card> cardDeck;
private int yourTurn = 0;
private boolean rotation = true;
public Player getPlayerYourTurn() {
return player.get(yourTurn % player.size());
}
public Game() {
player = new LinkedList<>();
cardDeck = new Stack<>();
}
public void addPlayer(Player p) {
player.add(p);
}
public boolean playCard(Card c, String s) {
Optional<Player> game = player.stream().filter(sp -> sp.getUser().equals(s)).findAny();
if (!game.isPresent() || c == null)
return false;
Player sp = game.get();
Optional<Card> ownCard = sp.getCard().stream().filter(kar -> kar.equals(c)).findAny();
if(!ownCard.isPresent()){
return false;
}
Card k = ownCard.get();
Card oben = cardDeck.peek();
boolean nextRound = false;
if (oben == null) {
nextRound = true;
} else if (k.getColor().equals(MyColor.SCHWARZ)) {
nextRound = true;
} else if (k.getColor().equals(oben.getColor())) {
nextRound = true;
}
if(nextRound){
sp.getCard().remove(k);
cardDeck.push(k);
try {
sp.sendHand();
} catch (IOException e) {
e.printStackTrace();
}
if(k.getEffectCard() != null){
if(k.getEffectCard() == EffectCard.Aussetzen){
yourTurn = (rotation)? yourTurn+1:yourTurn-1;
} else if (k.getEffectCard() == EffectCard.Farbwahl){
//TODO hängt von der GUI ab
} else if (k.getEffectCard() == EffectCard.Richtungswechsel){
rotation = !rotation;
} else if (k.getEffectCard() == EffectCard.ZiehVierFarbwahl){
int next = (rotation)? yourTurn+1:yourTurn-1;
player.get(next % player.size()).getCard().add(Card.possibleCards[(int) Math.round((Math.random() * Card.possibleCards.length))]);
player.get(next % player.size()).getCard().add(Card.possibleCards[(int) Math.round((Math.random() * Card.possibleCards.length))]);
player.get(next % player.size()).getCard().add(Card.possibleCards[(int) Math.round((Math.random() * Card.possibleCards.length))]);
player.get(next % player.size()).getCard().add(Card.possibleCards[(int) Math.round((Math.random() * Card.possibleCards.length))]);
//TODO Farbwechsel
} else if (k.getEffectCard() == EffectCard.ZiehZwei){
int next = (rotation)? yourTurn+1:yourTurn-1;
player.get(next % player.size()).getCard().add(Card.possibleCards[(int) Math.round((Math.random() * Card.possibleCards.length))]);
player.get(next % player.size()).getCard().add(Card.possibleCards[(int) Math.round((Math.random() * Card.possibleCards.length))]);
}
}
yourTurn = (rotation)? yourTurn+1:yourTurn-1;
for(Player PlayerNew : player){
try {
PlayerNew.getClient().writeUTF("Kartenstapel: " + k.toString());
PlayerNew.getClient().writeUTF("Zug: " + getPlayerYourTurn().getUser());
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
return false;
}
public void start() {
// Anzahl der Karten am Anfang
int nrOfCards = 7;
for (Player p : player) {
for (int i = 0; i < nrOfCards; i++) {
p.getCard().add(Card.possibleCards[(int) Math.round((Math.random() * Card.possibleCards.length))]);
}
}
for (Player p : player) {
try {
p.sendHand();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean gewonnen() {
return player.stream().filter(p -> p.getCard().isEmpty()).count() > 0;
}
}
Ich muss aber sagen das ich GUI und so nicht wo wirklich kann ich versteh das nicht >_ < falls ihr noch was braucht könnt ihr das ja kommentieren ich füg das nachträglich nach.
Und das war mein Versuch:
Java:
private JLabel getLbKarte() {
if (lbKarte == null) {
lbKarte = new Card (MyColor.ROT, 2 , new ImageIcon("./bin/Rot_0.png"));
lbKarte.setBounds(321, 468, 355, 162);
int i = 0;
for (Card card : game.getPlayerYourTurn().getCard()) {
lbKarte.add(card, i++);
}
}
return lbKarte;
}