这是我的甲板课程:
public class Deck {
ArrayList<Card> deck = new ArrayList<Card>();
public ArrayList<Card> get32Cards() {
deck.add(new Card(Suit.SPADES, Rank.JACK));
deck.add(new Card(Suit.SPADES, Rank.KING));
deck.add(new Card(Suit.SPADES, Rank.SEVEN));
deck.add(new Card(Suit.SPADES, Rank.EIGHT));
deck.add(new Card(Suit.SPADES, Rank.NINE));
deck.add(new Card(Suit.SPADES, Rank.TEN));
deck.add(new Card(Suit.SPADES, Rank.ACE));
deck.add(new Card(Suit.SPADES, Rank.QUEEN));
deck.add(new Card(Suit.HEARTS, Rank.JACK));
deck.add(new Card(Suit.HEARTS, Rank.KING));
deck.add(new Card(Suit.HEARTS, Rank.SEVEN));
deck.add(new Card(Suit.HEARTS, Rank.EIGHT));
deck.add(new Card(Suit.HEARTS, Rank.NINE));
deck.add(new Card(Suit.HEARTS, Rank.TEN));
deck.add(new Card(Suit.HEARTS, Rank.ACE));
deck.add(new Card(Suit.HEARTS, Rank.QUEEN));
deck.add(new Card(Suit.CLUBS, Rank.JACK));
deck.add(new Card(Suit.CLUBS, Rank.KING));
deck.add(new Card(Suit.CLUBS, Rank.SEVEN));
deck.add(new Card(Suit.CLUBS, Rank.EIGHT));
deck.add(new Card(Suit.CLUBS, Rank.NINE));
deck.add(new Card(Suit.CLUBS, Rank.TEN));
deck.add(new Card(Suit.CLUBS, Rank.ACE));
deck.add(new Card(Suit.CLUBS, Rank.QUEEN));
deck.add(new Card(Suit.DIAMONDS, Rank.JACK));
deck.add(new Card(Suit.DIAMONDS, Rank.KING));
deck.add(new Card(Suit.DIAMONDS, Rank.SEVEN));
deck.add(new Card(Suit.DIAMONDS, Rank.EIGHT));
deck.add(new Card(Suit.DIAMONDS, Rank.NINE));
deck.add(new Card(Suit.DIAMONDS, Rank.TEN));
deck.add(new Card(Suit.DIAMONDS, Rank.ACE));
deck.add(new Card(Suit.DIAMONDS, Rank.QUEEN));
Collections.shuffle(deck); //this shuffles the deck
return deck;
}
}
这是我的主要课程:
public class Game {
public static void main(String[] args) {
ArrayList<Card> deck = new ArrayList<Card>();
Deck getdeck = new Deck();
deck = getdeck.get32Cards();
for (Card info : deck) {
System.out.println(info);
}
}
}
我有一段代码,我认为它会起作用,但不确定如何实现它,或者它是否正确:
public boolean giveFourCards(Card c) {
for (int x = 1; x < 3; x++) {
if (deck.contains(c)) {
deck.remove(c);
Hand1.add(c);
return true;
} else
return false;
}
return false;
}
我想要复制&#39;套牌的前4个元素。 arraylist进入另一个名为&#39; hand1&#39;的arraylist,然后从&#39; deck&#39;中删除这4个元素。数组列表。
答案 0 :(得分:1)
您的giveFourCards()方法接收一个Card实例作为参数:为什么?
你的for循环似乎错了:应该是for (int x=0; x<4; x++) {
。
您似乎已经复制了一些您尚未理解的代码块。
据我所知,你的问题应该是:
public boolean giveFourCards() {
for (int x=0; x<4; x++) {
if (!deck.isEmpty()) {
Hand1.add(deck.removeFirst());
} else {
return false;
}
}
return true;
}
答案 1 :(得分:1)
如果你想从甲板上获得前4张牌,那么你可以
public ArrayList<Card> giveFourCards() {
ArrayList<Card> hand=new ArrayList<Card>();
for (int x = 0; x < 3; x++) {
hand.add(deck.get(x));
}
return hand;
}
答案 2 :(得分:0)
两个问题:
这可能会解决问题:
public giveFourCards(Hand h) {
for (int x = 0; x < 3; x++) {
h.add(deck.remove(0)); // push the removed card into the hand
}
}