Kartenhand Array

Jambolo

Mitglied
Moin,

Ich brauche bei der Prozedur add() hilfe. Die Prozedur muss beliebig viele Karten hinzufügen können. Entweder selbst als Karte oder in form einer neuen Kartenhand.
Das heißt, wenn ich zuvor die Kartenhand {SK} habe, und dann mit der prozedur add() die Karten {S2,S4,S9,SQ} als Hand hinzufügen will, muss auf der Konsole dann die Kartenhand {SK,S2,S4,S9,SQ} ausgegeben werde.
Ich weiß leider nicht wie ich hinbekomme. Ich denke ich brauch ein neues Array, welches einmal die alten Werte (in dem Fall SK) aufnimmt, und es in die neue Hand (S2,S4,S9,SQ) einsetzt, so dass am ende (SK,S2,S4,S9,SQ) meine neue Kartenhand ist.

Java:
public class Hand {
    Card [] hand;
    Card [] newHand;
    Deck d = new Deck();
    int nextCard = 0;
    
    public Hand(Card [] hand) {
            this.hand = hand;
    }
    
    public void add(Card[] newHand) {
        
        this.hand = newHand;
    }
    
    public void add(Hand anotherHand) {

        
    }
    
    public boolean isSuited() {   

        return true;
    }
    
    public Card[] getHandCards() {
        return hand;
    }
    
    public void setHandCards(Card[] hand) {
        this.hand = hand;
    }
    
}
 
Versuche es doch besser mit z.B.: einer List, das ist weniger umständlich.
Java:
public class Hand {

    List<Card> cards;

    public Hand(final Card[] hand) {
        this.cards = new ArrayList<>(Arrays.asList(hand));
    }

    public Hand(final Collection<Card> cards) {
        this.cards = new ArrayList<>(cards);
    }

    public void add(final Card[] newHand) {
        this.cards.addAll(Arrays.asList(newHand));
    }

    public void add(final Hand anotherHand) {
        this.cards.addAll(Arrays.asList(anotherHand.getHandCards()));

    }

    public boolean isSuited() {
        return true;
    }

    public Card[] getHandCards() {
        return this.cards.toArray(new Card[this.cards.size()]);
    }

    public void setHandCards(final Card[] hand) {
        this.cards = new ArrayList<>(Arrays.asList(hand));
    }

    /* simple delegate methods */

    public int size() {
        return this.cards.size();
    }

    public boolean isEmpty() {
        return this.cards.isEmpty();
    }

    public void clear() {
        this.cards.clear();
    }

    public boolean contains(final Card card) {
        return this.cards.contains(card);
    }

    public boolean add(final Card card) {
        return this.cards.add(card);
    }

    public void add(final int index, final Card card) {
        this.cards.add(index, card);
    }

    public boolean addAll(final Collection<Card> cards) {
        return cards.addAll(cards);
    }

    public boolean remove(final Card card) {
        return this.cards.remove(card);
    }

    public Card remove(final int index) {
        return this.cards.remove(index);
    }

    public boolean containsAll(final Collection<Card> cards) {
        return cards.containsAll(cards);
    }

    public Card get(final int index) {
        return this.cards.get(index);
    }


    public int indexOf(final Object o) {
        return this.cards.indexOf(o);
    }

}
 
Oder anders gefragt, ich will, dass mein Array die neuen Werte mit der prozedur add() aufnehmen kann
Java:
System.out.printf( "Expected ->   CJ,  C7, CT, C2, CQ, C5    (s)\n" );
        someHand = new Hand( new Card[]{ CJ } );
        someHand.add( new Card[]{ C7, CT, C2, CQ, C5 } );
        System.out.printf(
            "%s  %s\n",
            Arrays.toString( someHand.getHandCards() ),
            someHand.isSuited()  ?  "SUITED"  :  "NON suited"
        );
        System.out.printf( "\n\n" );
        
        
        System.out.printf( "Expected ->   SK   (s)\n" );
        Card[] temp = { SK };
        anotherHand = new Hand( temp );
        System.out.printf(
            "%s  %s\n",
            Arrays.toString( anotherHand.getHandCards() ),
            someHand.isSuited()  ?  "SUITED"  :  "NON suited"
        );
        System.out.printf( "\n\n" );
        
        
        System.out.printf( "Expected ->   CJ, C7, CT, C2, CQ, C5,  SK    (n)\n" );
        anotherHand = new Hand( new Card[]{ SK } );
        someHand.add( anotherHand );
        System.out.printf(
            "%s  %s\n",
            Arrays.toString( someHand.getHandCards() ),
            someHand.isSuited()  ?  "SUITED"  :  "NON suited"
        );
        System.out.printf( "\n\n" );

Der muss halt diese Tests bestehen
 
der soll aber ins index 0 und die anderen karten sollen einfach dann um 1 index weiter rutschen
Wenn schon was im Array drin ist, dann musst du ein neues leeres Array anlegen, welches das ursprüngliche plus das zusätzliche aufnehmen kann. Dann kopierst du zuerst das ursprüngliche ab Index 0 und danach das zusätzliche ab dem ursprünglichem Ende.
 
Java:
public void add(Card[] newHand) {
        this.newHand = newHand;
        this.completeHand = completeHand;
        System.arraycopy(hand, 0, completeHand, 0, 1);
        System.arraycopy(newHand, 0, completeHand, 1, 5);
        this.hand = completeHand;
    }
1652213056390.png
Funktioniert nicht wie es sein soll
 
Java:
void add(Card... newcards)
card[] newhand = new card[ newcards.length + oldhand.length]
...
int index =0
....
wie wäre es mit sowas
 
Funktioniert nicht wie es sein soll
Du erzeugst ja auch an keiner Stelle eine neues, größeres Array!

Außerdem arbeitest du beim Aufruf von arraycopy() z, B. mit festen Längenangaben. Was machst du denn, wenn dein, als Parameter übergebenes, Array länger oder kürzer ist als fünf?

Nochmal außerdem, kommen mir deine Instanzvariablen seltsam vor.
Java:
public class Hand {
    Card [] hand; // OK
    Card [] newHand; // Wozu?
    Deck d = new Deck(); // Wozu?
    int nextCard = 0; // Wozu?

Du sollst eine Anzahl von Karten speichern, dazu dient "hand". Du sollst per add() weitere Karten hinzufügen, dazu dient ebenfalls "hand", denn es sollten nach dem Hinzufügen ja mehr Karten darin enthalten sein. Alle weiteren nötigen Variablen sollten lokale Variablen der Methode sein.

Du kennst die Länge von "hand" (per length) und auch die Länge der zusätzlichen Karten. Damit musst du natürlich was machen.
 
Zuletzt bearbeitet:

Zurück
Oben