Es ist sinnvoll, für sämtliche Kartenablagen Hands einzurichten. In diesem Beispiel verwenden wir folgende Bezeichnungen für die Hand-Arrays:
Die Karten werden zuerst aus dem Talon an die Spieler verteilt (Stack-Darstellung), jeder Spieler erhält neun Karten. Beim unteren Spieler sind die Karten aufgedeckt. Danach spielt jeder Spieler mit einem linken Doppelklick auf seine Hand eine Karte aus. Die ausgespielten Karten werden in die bids verschoben. In diesem Beispiel kann jeder Spieler mit einem rechten Mausklick auf seine hand den Stich zu seinem stock verschieben. Die Verschiebung erfolgt für alle 4 Karten gleichzeitig.
|
![]() |
Programmcode:
// CardGameEx14.java import ch.aplu.jcardgame.*; import ch.aplu.jgamegrid.*; import ch.aplu.util.*; public class CardGameEx14 extends CardGame { public enum Suit { KREUZ, KARO, HERZ, PIK } public enum Rank { ASS, KOENIG, DAME, BAUER, ZEHN } private 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 final Location[] bidLocations = { new Location(300, 350), new Location(250, 300), new Location(300, 250), new Location(350, 300), }; private final Location[] stockLocations = { new Location(400, 500), new Location(100, 400), new Location(200, 100), new Location(500, 200), }; private Hand[] hands; private Hand[] bids = new Hand[nbPlayers]; private Hand[] stocks = new Hand[nbPlayers]; private Hand talon; private int targetCount; public CardGameEx14() { super(600, 600, 30); dealingOut(); setStatusText("Double click on hand to play. Double click on own bid to collect cards."); initBids(); initStocks(); } private void dealingOut() { setStatusText("Dealing out..."); hands = deck.dealingOut(nbPlayers, 0); talon = hands[nbPlayers]; talon.setVerso(true); for (int i = 0; i < nbPlayers; i++) { hands[i].setView(this, new StackLayout(handLocations[i], 90 * i)); hands[i].setVerso(i == 0 ? false : true); hands[i].draw(); hands[i].setTouchEnabled(true); } talon.setView(this, new StackLayout(handLocations[nbPlayers])); talon.draw(); while (!talon.isEmpty()) { for (int i = 0; i < nbPlayers; i++) { Card top = talon.getLast(); top.setVerso(i == 0 ? false : true); talon.transfer(top, hands[i], true); } } } private void initBids() { for (int i = 0; i < nbPlayers; i++) { bids[i] = new Hand(deck); bids[i].setView(this, new StackLayout(bidLocations[i])); hands[i].setTargetArea(new TargetArea(bidLocations[i])); hands[i].setTouchEnabled(true); final int k = i; hands[i].addCardListener(new CardAdapter() { public void leftDoubleClicked(Card card) { card.setVerso(false); card.transfer(bids[k], true); } }); bids[i].setTouchEnabled(true); bids[i].addCardListener(new CardAdapter() { public void leftDoubleClicked(Card card) { targetCount = 0; transferToStock(k); } public void atTarget(Card card, Location loc) { targetCount++; if (targetCount == nbPlayers) Monitor.wakeUp(); // All cards in stock->continue } }); } } private void initStocks() { for (int i = 0; i < nbPlayers; i++) { stocks[i] = new Hand(deck); double rotationAngle; if (i == 0 || i == 2) rotationAngle = 0; else rotationAngle = 90; stocks[i].setView(this, new StackLayout(stockLocations[i], rotationAngle)); } } private void transferToStock(int player) { for (int i = 0; i < nbPlayers; i++) { bids[i].setTargetArea(new TargetArea(stockLocations[player])); Card c = bids[i].getLast(); if (c == null) continue; c.setVerso(true); bids[i].transferNonBlocking(c, stocks[player]); } Monitor.putSleep(); // Wait until all cards are transferred to stock stocks[player].draw(); } public static void main(String[] args) { new CardGameEx14(); } } |
hands[i].setVerso(i == 0 ? false : true) |
Karten in der hands[0] werden aufgedeckt, die übrigen mit der Rückseite angezeigt |
public void leftDoubleClicked(Card card) { card.setVerso(false); card.transfer(bids[k], true); } |
Callbackmethode des CardListeners. Bei einem Doppelklick wird die Karte aufgedeckt und in die Bidhand verschobern |
Hand eval = new Hand(deck) |
Die vier ausgespielten Karten werden in die Hand eval verschobern. Hier kann die Nummer des Spielers mit der höchsten Karten ermittelt werden. Der rechte Klick wird nur dann ausgeführt, wenn der Winner die Karten zu sich nehmen will |
public void rightClicked(Card card) { transferToStock(k); } |
Callbackmethode des CardListeners. Ruft die Methode trnsferToStock() auf, welche alle Karten des Stichs zum Stock verschiebt |
Anstatt die Karten mit rechtem Mausklick zum Spieler mir der höchsten Karte zu verschieben, kann dieses automatisch durch den Computer erledigt werden. Die vier ausgespielten Karten werden in eine neue Hand eval eingefügt und dort nach der Rangpriority sortiert. Die Methode transferToWinner() gibt die Nummer des Spielers mit der höchsten Karten zurück. Anschliessend werden alle Karten aus der Hand eval in die Kartenablage dieses Spielers verschoben.
|
![]() |
Programmcode:
import ch.aplu.jcardgame.*; import ch.aplu.jgamegrid.*; import ch.aplu.util.Monitor; public class CardGameEx15 extends CardGame { public enum Suit { KREUZ, KARO, HERZ, PIK } public enum Rank { ASS, KOENIG, DAME, BAUER, ZEHN } 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 final Location[] bidLocations = { new Location(300, 350), new Location(250, 300), new Location(300, 250), new Location(350, 300), }; private final Location[] stockLocations = { new Location(400, 500), new Location(100, 400), new Location(200, 100), new Location(500, 200), }; private Hand[] hands; private Hand[] bids = new Hand[nbPlayers]; private Hand[] stocks = new Hand[nbPlayers]; private Hand talon; private int currentPlayer = 0; private int nbMovesInRound = 0; private int targetCount = 0; public CardGameEx15() { super(600, 600, 30); dealingOut(); setStatusText("Dealing out...done. Starting player: " + currentPlayer); initBids(); initStocks(); hands[0].setTouchEnabled(true); } private void dealingOut() { setStatusText("Dealing out..."); hands = deck.dealingOut(nbPlayers, 0); // All cards in talon talon = hands[nbPlayers]; talon.setVerso(true); for (int i = 0; i < nbPlayers; i++) { hands[i].setView(this, new StackLayout(handLocations[i], 90 * i)); hands[i].draw(); } talon.setView(this, new StackLayout(handLocations[nbPlayers])); talon.draw(); while (!talon.isEmpty()) { for (int i = 0; i < nbPlayers; i++) { Card top = talon.getLast(); top.setVerso(i == 0 ? false : true); talon.setTargetArea(new TargetArea(handLocations[i])); talon.transfer(top, hands[i], true); } } } private void initBids() { for (int i = 0; i < nbPlayers; i++) { bids[i] = new Hand(deck); bids[i].setView(this, new StackLayout(bidLocations[i])); bids[i].addCardListener(new CardAdapter() { public void atTarget(Card card, Location loc) { targetCount++; if (targetCount == nbPlayers) Monitor.wakeUp(); // All cards in stock->continue } }); hands[i].setTargetArea(new TargetArea(bidLocations[i])); final int k = i; hands[i].addCardListener(new CardAdapter() { public void leftDoubleClicked(Card card) { card.setVerso(false); card.transfer(bids[k], true); hands[currentPlayer].setTouchEnabled(false); currentPlayer = (currentPlayer + 1) % nbPlayers; if (nbMovesInRound == 3) { setStatusText("Evaluating round..."); nbMovesInRound = 0; currentPlayer = transferToWinner(); } else nbMovesInRound++; if (hands[currentPlayer].isEmpty()) setStatusText("Game over"); else { setStatusText("Current player: " + currentPlayer); hands[currentPlayer].setTouchEnabled(true); } } }); } } private void initStocks() { for (int i = 0; i < nbPlayers; i++) { stocks[i] = new Hand(deck); double rotationAngle; if (i == 0 || i == 2) rotationAngle = 0; else rotationAngle = 90; stocks[i].setView(this, new StackLayout(stockLocations[i], rotationAngle)); } } private int transferToWinner() { delay(1500); Hand eval = new Hand(deck); for (int i = 0; i < nbPlayers; i++) eval.insert(bids[i].getFirst(), false); int nbWinner = eval.getMaxPosition(Hand.SortType.RANKPRIORITY); setStatusText("Round winner: " + nbWinner); transferToStock(nbWinner); return nbWinner; } private void transferToStock(int player) { targetCount = 0; for (int i = 0; i < nbPlayers; i++) { bids[i].setTargetArea(new TargetArea(stockLocations[player])); Card c = bids[i].getLast(); if (c == null) continue; c.setVerso(true); bids[i].transferNonBlocking(c, stocks[player]); } Monitor.putSleep(); // Wait until all cards are transferred to stock stocks[player].draw(); } public static void main(String[] args) { new CardGameEx15(); } } |