Bei den meisten Kartenspielen werden zu Beginn Karten an die Spieler verteilt. In der Klassenbibliothek CardGame wird für das Austeilen der Karten die Methode dealingOut(int nbPlayers, int nbCards) verwendet. Der erste Parameter gibt die Zahl Spieler an, der zweite Anzahl Karten, welche jeder erhält. Wenn der zweite Parameter 0 ist, werden alle Karten verteilt. Die Parameterliste kann mit einem dritten Parameter ergänzt werden dealingOut(int nbPlayers, int nbCards, false). False bewirkt, dass die Karten nicht zufällig gemischt, sondern nach Suitpriority geordnet sind. Die Verteilung von Karten kann animiert dargestellt werden.
Beispiel 1: Alle Karten aus dem Deck werden zuerst auf einem Stappel (talon) zufällig gemischt bereitgestellt. Danach werden die Karten vom Stappel zu den Spielern transferiert. Es müssen fünf Hands erzeugt werden: hands[0], hands[1], hands[2], hands[3] für die Spieler, hand[4] für den talon. Auf dem Stappel sind die Karten zugedeckt, in den Spielerhänds sind sie aufgedeckt und werden beim Hinzufügen von neuen Karten nach der Suitpriorität angeordnet und neu dargestellt.
|
![]() |
Programmcode:
// CardGameEx10.java import ch.aplu.jcardgame.*; import ch.aplu.jgamegrid.*; public class CardGameEx10 extends CardGame { public enum Suit { KREUZ, HERZ, KARO, PIK } public enum Rank { ASS, KOENIG, DAME, BAUER, ZEHN, NEUN, ACHT, SIEBEN, SECHS } private final Deck deck = new Deck(Suit.values(), Rank.values(), "cover"); private final int nbPlayers = 4; private final Location[] handLocations = { new Location(300, 525), new Location(75, 300), new Location(300, 75), new Location(525, 300), new Location(300, 300) }; private Hand[] hands; private Hand talon; public CardGameEx10() { super(600, 600, 30); hands = deck.dealingOut(nbPlayers, 0); // All cards in talon talon = hands[nbPlayers]; // hand[4] is talon for (int i = 0; i < nbPlayers; i++) { hands[i].setView(this, new RowLayout(handLocations[i], 300, 90 * i)); hands[i].draw(); hands[i].setTouchEnabled(true); } talon.setView(this, new StackLayout(handLocations[nbPlayers])); talon.setVerso(true); talon.draw(); while (!talon.isEmpty()) { for (int i = 0; i < nbPlayers; i++) { Card top = talon.getLast(); talon.transfer(top, hands[i], true); hands[i].setVerso(false); hands[i].sort(Hand.SortType.SUITPRIORITY, true); } } } public static void main(String[] args) { new CardGameEx10(); } } |
hands = deck.dealingOut(nbPlayers, 0) |
Alle Karten werden in den talon geschoben |
talon = hands[nbPlayers] | talon wird als eine zusätzliche 5. Hand deklariert |
Card top = talon.getLast() | Die letzte Karte aus dem talon wird als top bezeichnet |
talon.transfer(top, hands[i], true) | Die Karte top wird animiert tranferiert |
hands[i].sort(Hand.SortType.SUITPRIORITY, true) | Die Zielhand wird nach Suitpriorität geordnet |
Beispiel 2: Auch hier werden alle Karten zuerst zufällig geordnet in den Talon aufgenommen. Für drei Spieler werden je 5 Karten verdeckt verteilt, in der Hand[0] werden Karten normal aggezeigt.
|
![]() |
Programmcode:
// CardGameEx11.java import ch.aplu.jcardgame.*; import ch.aplu.jgamegrid.*; public class CardGameEx11 extends CardGame { public enum Suit { KREUZ, HERZ, KARO, PIK } public enum Rank { ASS, KOENIG, DAME, BAUER, ZEHN, NEUN, ACHT, SIEBEN, SECHS } // private final Deck deck = new Deck(Suit.values(), Rank.values(), "cover"); private final int nbPlayers = 4; private final Location[] handLocations = { new Location(300, 525), new Location(75, 300), new Location(300, 75), new Location(525, 300), new Location(300, 300) }; private Hand[] hands; private int nbCard = 5; private Hand talon; public CardGameEx10() { super(600, 600, 30); dealingOut(); setStatusText("Dealing Out...done."); } private void dealingOut() { setStatusText("Dealing Out..."); hands = deck.dealingOut(nbPlayers, 0); // All cards in talon talon = hands[nbPlayers]; for (int i = 0; i < nbPlayers; i++) { hands[i].setView(this, new RowLayout(handLocations[i], 300, 90 * i)); hands[i].draw(); hands[i].setTouchEnabled(true); } talon.setView(this, new StackLayout(handLocations[nbPlayers])); talon.setVerso(true); talon.draw(); for (int k = 0; k < nbCard; k++) { for (int i = 0; i < nbPlayers; i++) { Card top = talon.getLast(); talon.transfer(top, hands[i], true); hands[i].sort(Hand.SortType.SUITPRIORITY, true); hands[0].setVerso(false); } } } public static void main(String[] args) { new CardGameEx11(); } } |
for (int k = 0; k < nbCard; k++) { } | Mit einer for-Schleife wird nur eine bestimmte Anzahl Karten verteilt. Die restlichen Karten bleiben im Talon |
hands[0].setVerso(false) | Die Karten in der hand[0] werden jeweils aufgedeckt |
Beispiel 3: Bei echten Kartenspielen werden Karten in der Regel zu dritt verteilt. Die Karten-Verteilung erfolg dadurch schneller. Alle Karten werden zuerst in den Tallo gesetzt, dann wurde eine einzige Karte gezeigt, die sich vom Talon auf die Hand des Spielers bewegt und drei Karten in die Hand eingefügt. Dann ist die Hand mit Rangpriorität angezeigt. Im folgenden Beispiel wird jeder Spieler bekommen insgesamt 9 Karten, nur die Karten einer Hand aufgedeckt. .
|
![]() |
Programmcode:
// CardGameEx12.java import ch.aplu.jcardgame.*; import ch.aplu.jgamegrid.*; public class CardGameEx12 extends CardGame { public enum Suit { KREUZ, HERZ, KARO, PIK } public enum Rank { ASS, KOENIG, DAME, BAUER, ZEHN, NEUN, ACHT, SIEBEN, SECHS } private final Deck deck = new Deck(Suit.values(), Rank.values(), "cover"); private final int nbPlayers = 4; private final Location[] handLocations = { new Location(300, 525), new Location(75, 300), new Location(300, 75), new Location(525, 300), }; private final Location talonLocation = new Location(300, 300); private final int packetSize = 3; private Hand[] hands; private Hand talon; public CardGameEx12() { super(600, 600); dealingOut(); setStatusText("Dealing Out...done."); } private void dealingOut() { setStatusText("Dealing Out In Packets..."); hands = deck.dealingOut(nbPlayers, 0); // All cards in shuffled talon talon = hands[nbPlayers]; for (int i = 0; i < nbPlayers; i++) { hands[i].setView(this, new RowLayout(handLocations[i], 280, 90 * i)); hands[i].draw(); } CardCover talonCover = new CardCover(this, talonLocation, deck, 1, 0); while (!talon.isEmpty()) { for (int i = 0; i < nbPlayers; i++) { for (int k = 0; k < packetSize; k++) { hands[i].insert(talon.getLast(), false); talon.removeLast(false); } if (talon.isEmpty()) talonCover.removeSelf(); CardCover cardCover = new CardCover(this, talonLocation, deck, 1, 0); cardCover.slideToTarget(handLocations[i], 20, true, true); cardCover.removeSelf(); if (i == 0) { hands[i].setVerso(false); hands[i].sort(Hand.SortType.RANKPRIORITY, false); } else hands[i].setVerso(true); hands[i].draw(); } } hands[0].setTouchEnabled(true); } public static void main(String[] args) { new CardGameEx12(); } } |
CardCover cardCover = new CardCover(this, talonLocation, deck, 1, 0) cardCover.slideToTarget(handLocations[i], 20, true, true) cardCover.removeSelf() |
Anstelle von Karten wird beim Verschieben der CardCpver angezeigt |
for (int k = 0; k < packetSize; k++) | Es werden jeweils drei Karten in die Spielerhand verschoben. Anzezeigt werden aber nur cover |
hands[i].sort(Hand.SortType.RANKPRIORITY, false) | Die Karten werden in der Hand nach der Rangpriorität geordnet |