我正在用 Java 编写纸牌游戏。玩家2的Hands类的第二个对象为玩家提供了与玩家1相同的牌组,为什么?
我是编程初学者,这是我的第一个问题。我必须用 Java 编写一款纸牌游戏 Gin Rummy。
我创建了 Card 类、Deck 类和 Hands 类。牌组类构造函数初始化一个包含 52 张牌的 Arraylist。游戏开始时,每个玩家必须抽 10 张牌,因此在 Hands 类构造函数中,会抽出这副牌的前 10 张牌,并将其添加到 Hands 类中的数组列表中。
当我检查第一名玩家的牌时,这是真的。 玩家 1 抽完 10 张牌后,牌堆中还剩下多少张牌?确实,卡片已经不存在了。 玩家2的牌?不正确,玩家 2 的牌是 52 副牌的前 10 张牌(与玩家 1 相同)。 我花了几个小时试图找出问题所在。
我在玩家 2 拍摄后打印了牌组,并且实际上抽出了前 20 张牌。我不知道为什么,当我创建 Hand 类的第二个对象时,它不会拿下接下来的 10 张牌,而是拿第一个对象(玩家 1)已经拿的牌。
这是 CardsDeck 类:它将 52 张卡片集添加到数组列表中,效果很好。
public class CardsDeck {
private final int totalCardsNumber = 52;
private static List<Card> deckOfCards;
public CardsDeck(){
deckOfCards = new ArrayList<>(totalCardsNumber);
int index=0;
for (int suit = 0; suit <Card.Suit.values().length; suit++){
for(int rank = 0; rank <Card.Rank.values().length; rank++){
deckOfCards.add(new Card(Card.Rank.values()[rank], Card.Suit.values()[suit]));
index++;
}
}
// Collections.shuffle(deckOfCards);
}
public void print(){
for (Card card: deckOfCards){ System.out.println(card);}
}
public void setDeckOfCards(List<Card> updatedDeckOfCards) { this.deckOfCards = new ArrayList<>(updatedDeckOfCards); }
protected static List<Card> getDeckOfCards() {
return deckOfCards;
}
}
这是 Hands 类:
public class CardsInHands extends CardsDeck{
protected static List<Card> updatedCardList = new ArrayList<>(getDeckOfCards());
protected List<Card> playerCards = new ArrayList<>();
public CardsInHands(){
for (int i=0; i<10; i++){
playerCards.add(getDeckOfCards().get(i));
updatedCardList.remove(0);
}
setDeckOfCards(updatedCardList);
}
public void printPlayerCards() {
for (Card card: playerCards){ System.out.print(card + " - ");}
}
public List<Card> getPlayerCards() {
return playerCards;
}
public void setPlayerCards(List<Card> playerCards) { this.playerCards = playerCards;}
public void removeCard(int cardIndex){ playerCards.remove(cardIndex);}
public void addCard(Card card){playerCards.add(card);}
为了完整起见,这是 Card 类:
public class Card {
public enum Rank {
ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5),
SIX(6), SEVEN(7), EIGHT(8), NINE(9),
TEN(10),
JACK(10), QUEEN(10), KING(10);
int value;
Rank(int value) { this.value = value; }
public int getValue() { return value; }
}
public enum Suit {SPADES,HEARTS, CLUBS, DIAMONDS;}
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit){
this.rank = rank;
this.suit = suit;
}
public Rank getRank() { return rank; }
public Suit getSuit() { return suit; }
public int getValue(){ return rank.getValue(); }
public String toString(){
return this.rank+ " of " +this.suit;
}
}
这是我测试代码的主要方法:
public class Runner {
public static void main(String[] args){
CardsDeck cards = new CardsDeck();
//cards.print();
CardsInHands playerOne = new CardsInHands();
//playerOne.printPlayerCards(); //prints first 10 cards of the deck
// cards.print(); //correct, first 10 cards are gone
CardsInHands playerTwo = new CardsInHands();
playerTwo.printPlayerCards(); //shows same 10 cards of player 1!
//cards.print(); //correct, second set of 10 cards are gone
}
}
谁能告诉我我做错了什么导致 Hands 类的第二个对象无法正常工作?
I am a beginner in programming, and it is my first question here. I have to code a card game, Gin Rummy, in Java.
I created the Card class, Deck class, and Hands class. The deck class constructor initialize an Arraylist of 52 cards. At the start of the game each player has to draw 10 cards, so in the hands class constructor, the first 10 cards of the deck is drawn and added to an array list in the Hands class.
When I check the the first players cards, it is true.
The cards left in the deck after player 1 draw 10 cards? true, cards are no longer there.
Player 2 cards? Not true, player 2 cards are the first 10 cards of the 52 deck (the same as player 1).
I spent hours trying to figure out what is wrong.
I printed the cards deck after player 2 take, and the first 20 cards are actually drawn. I do not know why, when I create a second object of the Hand class, it does not take the next 10 cards, but take the cards that the first object, player 1, already took.
This the CardsDeck class: it adds 52 card set to the array list, which works well.
public class CardsDeck {
private final int totalCardsNumber = 52;
private static List<Card> deckOfCards;
public CardsDeck(){
deckOfCards = new ArrayList<>(totalCardsNumber);
int index=0;
for (int suit = 0; suit <Card.Suit.values().length; suit++){
for(int rank = 0; rank <Card.Rank.values().length; rank++){
deckOfCards.add(new Card(Card.Rank.values()[rank], Card.Suit.values()[suit]));
index++;
}
}
// Collections.shuffle(deckOfCards);
}
public void print(){
for (Card card: deckOfCards){ System.out.println(card);}
}
public void setDeckOfCards(List<Card> updatedDeckOfCards) { this.deckOfCards = new ArrayList<>(updatedDeckOfCards); }
protected static List<Card> getDeckOfCards() {
return deckOfCards;
}
}
This is the Hands class:
public class CardsInHands extends CardsDeck{
protected static List<Card> updatedCardList = new ArrayList<>(getDeckOfCards());
protected List<Card> playerCards = new ArrayList<>();
public CardsInHands(){
for (int i=0; i<10; i++){
playerCards.add(getDeckOfCards().get(i));
updatedCardList.remove(0);
}
setDeckOfCards(updatedCardList);
}
public void printPlayerCards() {
for (Card card: playerCards){ System.out.print(card + " - ");}
}
public List<Card> getPlayerCards() {
return playerCards;
}
public void setPlayerCards(List<Card> playerCards) { this.playerCards = playerCards;}
public void removeCard(int cardIndex){ playerCards.remove(cardIndex);}
public void addCard(Card card){playerCards.add(card);}
For sake of completion, this is the Card class:
public class Card {
public enum Rank {
ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5),
SIX(6), SEVEN(7), EIGHT(8), NINE(9),
TEN(10),
JACK(10), QUEEN(10), KING(10);
int value;
Rank(int value) { this.value = value; }
public int getValue() { return value; }
}
public enum Suit {SPADES,HEARTS, CLUBS, DIAMONDS;}
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit){
this.rank = rank;
this.suit = suit;
}
public Rank getRank() { return rank; }
public Suit getSuit() { return suit; }
public int getValue(){ return rank.getValue(); }
public String toString(){
return this.rank+ " of " +this.suit;
}
}
This is the main method I test the code from:
public class Runner {
public static void main(String[] args){
CardsDeck cards = new CardsDeck();
//cards.print();
CardsInHands playerOne = new CardsInHands();
//playerOne.printPlayerCards(); //prints first 10 cards of the deck
// cards.print(); //correct, first 10 cards are gone
CardsInHands playerTwo = new CardsInHands();
playerTwo.printPlayerCards(); //shows same 10 cards of player 1!
//cards.print(); //correct, second set of 10 cards are gone
}
}
Could anyone tell me what I am doing wrong that makes the second object of Hands class not work properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为您已将 DeckOfCards 标记为静态,这意味着它是类级别变量。
停止使用 static 那里,问题就会解决。
有关静态变量的更多信息。
<一href="https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/#:%7E:text=Java%20Static%20Varia bles,%20the%20class 的%20instances%20。&text=Static%20variables%20are%20also%20known%20as%20Class%20Variables" rel="nofollow noreferrer">https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/#:~:text=Java%20Static%20Varia bles,%20the%20class 的%20instances%20。&text=Static%20variables%20are%20also%20known%20as%20Class%20Variables。
This is happening because you have marked deckOfCards a static, which means it is a class level variable.
Stop using static there and the problem will be solved.
More on static variable.
https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/#:~:text=Java%20Static%20Variables,the%20instances%20of%20the%20class.&text=Static%20variables%20are%20also%20known%20as%20Class%20Variables.