@Override public void update(Observable paramObservable, Object event) { if (event instanceof CardsWonEvent) { // System.out.println(currentPlayer.toString() + " has won the pile!"); lastWinnerId = players.indexOf(currentPlayer); currentPlayer.addPoints(((CardsWonEvent) event).getTotalPoints()); currentPlayer.addWonCards(((CardsWonEvent) event).getCardSize()); } }
/** * Starts a Pisti game. * * @returns winner bot. */ public Bot startGame() { table.addObserver(this); // to keep scores. table.addObserver(players.get(0)); // observer pattern to notify players via events. table.addObserver(players.get(1)); table.addObserver(players.get(2)); table.addObserver(players.get(3)); table.putInitialCards(deck.getFourCards()); while (!deck.isEmpty()) { // deal cards for (Bot bot : players) { bot.setHand(deck.getFourCards()); } // start playing for (int i = 0; i < 4; i++) { for (Bot bot : players) { currentPlayer = bot; Card cardToThrow = currentPlayer.throwACard(); // System.out.println("Bot "+ currentPlayer.toString() + " threw " + // cardToThrow.toString()); table.putOnPile(cardToThrow); } } } // award last pile winner players.get(lastWinnerId).addPoints(Table.calculatePoints(table.getCardsOnPile())); players.get(lastWinnerId).addWonCards(table.getCardsOnPile().size()); // System.out.println("Last pile goes to ["+players.get(lastWinnerId).toString()+"]"); table.clearPile(); // award most card winner Collections.sort(players, new CompareByWonCardSize()); // sort players by most cards won. players.get(3).addPoints(3); // last one has most cards. // System.out.println("The bot ["+players.get(3).toString()+"] won the most cards so it gets // bonus 3 points!"); // System.out.println("### SCORES ###"); Collections.sort(players, new CompareByPoints()); // sort by points Bot winner = players.get(3); // System.out.println("WINNER IS: "+ winner); return winner; }