gofishhand.java
来自「java版的数据结构的完全代码 免费提供了 学习数据结构的请下载」· Java 代码 · 共 49 行
JAVA
49 行
// Introduced in Chapter 5import java.util.Iterator;/** Hand of cards for the game of Go Fish. */public class GoFishHand extends ArrayList<Card> { /** * Remove all Cards of the specified rank and add them to the hand * taker. Return true if at least one Card was given. */ public boolean give(int rank, GoFishHand taker) { boolean foundAny = false; Iterator<Card> iter = iterator(); while (iter.hasNext()) { Card card = iter.next(); if (card.getRank() == rank) { iter.remove(); taker.add(card); foundAny = true; } } return foundAny; } /** * Remove all sets of four same-rank Cards from this GoFishHand. * Return the number of sets. */ public int meldSets() { // Count number of Cards of each rank int[] rankCount = new int[14]; // Initialized to zeroes for (Card c : this) { rankCount[c.getRank()] += 1; } // Remove cards in complete sets int cardsRemoved = 0; Iterator<Card> iter = iterator(); while (iter.hasNext()) { if (rankCount[iter.next().getRank()] == 4) { cardsRemoved += 1; iter.remove(); } } // Return number of complete sets return cardsRemoved / 4; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?