感谢您的帮助,我已经设法在所提供的答案的帮助下工作。
这是我的主要课程:
import data.Deck;
import java.util.ArrayList;
public class Game {
public static void main(String[] args) {
// TODO code application logic here
ArrayList<Card> deck = new ArrayList<Card>();
Deck getdeck = new Deck();
deck = getdeck.get32Cards();
for (Card info : deck) {
System.out.println(info); //this just prints all the
}
}
}
这是我的卡类: 包装业务;
public class Card {
public enum Suit{SPADES, CLUBS, HEARTS, DIAMONDS};
public enum Rank{JACK, KING, SEVEN, EIGHT, NINE, TEN, ACE, QUEEN}
private Suit suit;
private Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
@Override
public String toString() {
return getRank() + " of " + getSuit();
}
}
这是我的甲板课程:
package data;
import business.Hand;
import business.Card;
import business.Card.Rank;
import business.Card.Suit;
import java.util.ArrayList;
import java.util.Collections;
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;
}
我还有一个Hand类和一个Player类。我打算让两个玩家各自拥有一手牌。我不知道如何删除'deck'arraylist的前四张牌,并将其插入另一个arraylist。另外我不知道编写代码的类。请指教。
答案 0 :(得分:1)
尝试创建另一个这样的ArrayList newArraLIst
:
ArrayList<Card> newArraLIst = new ArrayList<Card>();
并使用循环到remove()
首先four
元素和add()
它们到新的ArrayList,如下所示:
if(deck.size() >= 4){
for (int i = 0 ; i < 4 ; i++) {
newArraLIst.add(deck.remove(i));
}
}
答案 1 :(得分:0)
我对您的代码有一些建议。首先,将这些卡添加到卡片中看起来有点难看,我建议做这样的事情:
for (Rank rank : Rank.values()){
for (Suit suit : Suit.values()){
deck.add(new Card(suit, rank);
}
}
另外,你的牌组应该是一堆牌,一手牌可以是牌阵列。当你从你的套牌中处理你的牌时,它应该是这样的:
public static final int HAND_SIZE = 4;
for (int i = 0; i < HAND_SIZE; i++){
for (Player player : game.getPlayers()){
player.getHand().add(deck.pop());
}
}
这段代码将甲板上的4张牌交给游戏中的每个玩家。 deck.pop()删除堆栈中的最后一张卡并返回它。这样你就可以在玩家手中添加一张牌,并在一行代码中将其从牌组中删除。
答案 2 :(得分:-1)
我建议你使用Stack而不是ArrayList,它更有意义。然后你可以使用pop()方法同时获取和删除顶部卡。 但是,您可以使用以下n次来继续将甲板的第一个元素移动到手的开始位置。在你的情况下将n替换为4。
ArrayList<Integer> hand = new ArrayList<>();
for(int i = 0; i<n;i++){
hand.add(0, deck.get(0));
deck.remove(0);
}