gpt4 book ai didi

java - 程序无法正常运行

转载 作者:行者123 更新时间:2023-12-01 10:51:34 24 4
gpt4 key购买 nike

我正在尝试编写一个代码,该代码应该构造一个 52 张牌堆,然后将牌发给 n 个玩家(某些玩家可能有一张额外的牌)。获胜者是持有黑桃 A 牌的人。

public class CardGame {
public static void main(String[] args) {

int numberOfPlayers = Integer.parseInt(args[0]);
CardPile deck = CardPile.makeFullDeck();
CardPile [] players = new CardPile[numberOfPlayers];

for (int i=0;i<numberOfPlayers;i++) {
int numofnum = i%numberOfPlayers;

players[i] = new CardPile();
}

for (int i=0;i<52;i++) {
int numofnum =i%numberOfPlayers;

CardPile curPlayer = players[i%numberOfPlayers];
Card nextCard = deck.get(i);
players[numofnum].addToBottom(nextCard);

}

for (int i=0;i<numberOfPlayers;i++) {

if (players[i].find(Suit.SPADES, Value.ACE) != -1) {

System.out.println("Player " + i + " is the winner");
}
}

}
}

这是 CardPile

public class CardPile{
private Card[] cards;
private int numCards;


public static void main(String[] args){


}


public CardPile(){
Card[] constructor = new Card[52];
cards = constructor;
numCards = 0;
}

public boolean isEmpty(){
return this.numCards==0;
}

public void addToBottom(Card c){
int i = 0;
for(i = 0;i<52;i++){
if(cards[i]==null){
break;
}
}
numCards = i;
cards[i] = c;
}


public Card get(int i){
return cards[i];
}

public Card remove(int i){
Card removed = cards[i];
for(i = i;i<51;i++){
cards[i]=cards[i+1];
}
cards[51]= null;
numCards=numCards-1;
return removed;
}

public int find(Suit a,Value b){
int i = 0;
for(i=0;i<52;i++){
if(cards[i].getSuit()==a && cards[i].getValue()==b){
break;
}
else{
i = -1;
return i;
}
}
return i;
}

public String toString(){
String x = "";
String y = "";

for(int i=0;i<numCards;i++){
y = cards[i].toString();
x = x + i + "." + y +" ";
}
return x;

}

public static CardPile makeFullDeck(){

CardPile pile = new CardPile();
for(Value v: Value.values()){
for(Suit s: Suit.values()){
pile.addToBottom(new Card(s,v));
}
}

UtilityCode.shuffle(pile.cards, pile.numCards);

return pile;

}


}

我的问题是,有时当我运行 CardGame 时,它​​会打印出“玩家 x 是获胜者”,但有时它不会打印任何内容。有人可以帮我吗?谢谢

最佳答案

CardPile.find() 将返回 -1,除非第一张卡片是正在搜索的卡片。将其更改为:

public int find(Suit a,Value b){
for(int i=0;i<52;i++){
if(cards[i].getSuit()==a && cards[i].getValue()==b){
return i;
}
}
return -1;
}

关于java - 程序无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33849621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com