- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写一个方法来从牌堆顶部删除指定数量的牌并将它们作为数组返回。
这就是我到目前为止所做的。创建牌组、复制牌组的方法、返回数组中指定位置的纸牌的方法、返回纸牌数组大小的方法、洗牌和剪切牌组的方法(不确定这些是否正确)。这是卡片类
public class Card {
private final int suit; // 0, 1, 2, 3 represent Spades, Hearts, Clubs,
// Diamonds, respectively
private final int value; // 1 through 13 (1 is Ace, 11 is jack, 12 is
// queen, 13 is king)
/*
* Strings for use in toString method and also for identifying card images
*/
private final static String[] suitNames = { "s", "h", "c", "d" };
private final static String[] valueNames = { "Unused", "A", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "J", "Q", "K" };
/**
* Standard constructor.
*
* @param value
* 1 through 13; 1 represents Ace, 2 through 10 for numerical
* cards, 11 is Jack, 12 is Queen, 13 is King
* @param suit
* 0 through 3; represents Spades, Hearts, Clubs, or Diamonds
*/
public Card(int value, int suit) {
if (value < 1 || value > 13) {
throw new RuntimeException("Illegal card value attempted. The "
+ "acceptable range is 1 to 13. You tried " + value);
}
if (suit < 0 || suit > 3) {
throw new RuntimeException("Illegal suit attempted. The "
+ "acceptable range is 0 to 3. You tried " + suit);
}
this.suit = suit;
this.value = value;
}
/**
* "Getter" for value of Card.
*
* @return value of card (1-13; 1 for Ace, 2-10 for numerical cards, 11 for
* Jack, 12 for Queen, 13 for King)
*/
public int getValue() {
return value;
}
/**
* "Getter" for suit of Card.
*
* @return suit of card (0-3; 0 for Spades, 1 for Hearts, 2 for Clubs, 3 for
* Diamonds)
*/
public int getSuit() {
return suit;
}
/**
* Returns the name of the card as a String. For example, the 2 of hearts
* would be "2 of h", and the Jack of Spades would be "J of s".
*
* @return string that looks like: value "of" suit
*/
public String toString() {
return valueNames[value] + " of " + suitNames[suit];
}
/**
* [STUDENTS SHOULD NOT BE CALLING THIS METHOD!] Used for finding the image
* corresponding to this Card.
*
* @return path of image file corresponding to this Card.
*/
public String getImageFileName() {
String retValue;
retValue = suitNames[suit];
if (value <= 10)
retValue += value;
else if (value == 11)
retValue += "j";
else if (value == 12)
retValue += "q";
else if (value == 13)
retValue += "k";
else
retValue += "Unknown!";
return "images/" + retValue + ".gif";
}
}
Deck 方法是我需要帮助的方法
public class Deck {
private Card[] cards;
public Deck() {
cards = new Card[52];
int numberOfCard = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int value = 1; value <= 13; value++) {
cards[numberOfCard] = new Card(value, suit);
numberOfCard++;
}
}
}
public Deck(Deck other) {
cards = new Card[other.cards.length];
for (int i = 0; i < other.cards.length; i++) {
cards[i] = other.cards[i];
}
}
public Card getCardAt(int position) {
if (position >= cards.length) {
throw new IndexOutOfBoundsException("Values are out of bounds");
} else {
return cards[position];
}
}
public int getNumCards() {
return cards.length;
}
public void shuffle() {
int temp = 0;
for (int row = 0; row < cards.length; row++) {
int random = (int) (Math.random() * ((cards.length - row) + 1));
Deck.this.cards[temp] = this.getCardAt(row);
cards[row] = cards[random];
cards[random] = cards[temp];
}
}
public void cut(int position) {
Deck tempDeck = new Deck();
int cutNum = tempDeck.getNumCards() / 2;
for (int i = 0; i < cutNum; i++) {
tempDeck.cards[i] = this.cards[52 - cutNum + i];
}
for (int j = 0; j < 52 - cutNum; j++) {
tempDeck.cards[j + cutNum] = this.cards[j];
}
this.cards = tempDeck.cards;
}
public Card[] deal(int numCards) {
return cards;
}
}
最佳答案
如果您不能使用List
,那么使用另一个带有牌组顶牌索引的变量似乎是个好主意:
public class Deck {
private Card[] cards;
private int topCardIndex;
public Deck() {
cards = new Card[52];
int numberOfCard = 0;
for(int suit = 0; suit <= 3; suit++){
for(int value = 1; value <= 13; value++){
cards[numberOfCard] = new Card(value, suit);
numberOfCard++;
}
}
topCardIndex = 0;
}
public Card getCardAt(int position) {
if (position >= cards.length - topCardIndex || position < topCardIndex) {
throw new IndexOutOfBoundsException("Values are out of bounds");
} else {
return cards[topCardIndex + position];
}
}
public Card[] deal(int numCards) {
// FIXME: check bounds, or make method "Card pickCardAt(int position)"
// that removes the card from the deck
Card[] drawnCards = new Card[numCards];
for(int index = 0; index < numCards; index ++) {
drawnCards[index] = cards[topCard];
topCard++;
}
return drawnCards;
}
<小时/>
我认为您最好将 Deck 的 card
集合设为 List
而不是 Card[]
,这样您就可以进行更好的操作,例如 remove()
。
如果您想从牌组中取出前 n
张牌,则只需对结果数组进行 remove()
n 次即可(我也会制作一个列表)。
public List<Card> deal(int numCards) {
List<Card> drawnCards = new ArrayList<Card>(numCards);
for(int index = 0; index < numCards; index++) {
drawnCards.add(cards.remove(0));
}
return drawnCards;
}
关于从一副牌发牌的 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13338932/
1、前言 最近小编在做一个关于德州扑克的小游戏,当然已经做完了,才写的一系列文章来记录一下自己的开发经历 点击链接查看其他文章 python实战之德州扑克第二步-判断牌型 python实战
我需要有关这部分代码的帮助。这部分涉及生成 2 个随机数,并使用该随机数同时在 2 个标签框中的每一个中显示一张卡片。这里的问题是随机数没有正确生成,因此卡片没有正确显示(有重复,有时不显示等)
问题 我以前从未制作过纸牌游戏,目前遇到了一些困难。 但是,我已经设法创建了套牌等。 -(NSMutableArray *)arrayWithDeck:(id)sender { [sender
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我一直致力于使用 F# 对流行的纸牌游戏(情书)进行建模,以了解有关函数式编程的更多信息。 module Game = open Cards open Players type Deck = Card
我只使用 c 几个星期,所以很可能会出现我忽略的明显错误。我看过其他线程,但我不明白我正在读的很多内容。该程序假设有一个无限大的牌组。 已知问题: clearBuffer 当前未使用,我正在尝试不同的
我是一名优秀的程序员,十分优秀!