gpt4 book ai didi

java - 为什么我的方法没有清空 ArrayList

转载 作者:行者123 更新时间:2023-12-02 05:22:40 25 4
gpt4 key购买 nike

我正在尝试清空 ArrayList 手牌,以便可以玩新一轮。我不确定为什么我的 for 循环没有清空“手”,而是只删除手中的第一张牌。这是我的代码:

import java.util.ArrayList;

public class Game {

private Deck aDeck;
private InputReader reader;
private ArrayList<Card> hand;
private String commandChoice;
private int handValue;

/**
* Method to run the game.
* First while loop will run until the player chooses "no" for another round.
* Second while look will keep running until the player chooses to stand, has 21 or busts.
* the last while loop is to make sure that the player chooses either "Hit" or "Stand". If neither is choosen, it will keep requesting it.
*/
public void Play(){
int playerPoints = 0;
int totalRounds = 0;
commandChoice = "";
hand = new ArrayList<Card>();
reader = new InputReader();

intro();
aDeck = new Deck();
aDeck.loadDeck();
aDeck.shuffle();
aDeck.shuffle();

while(anotherRound() == false){
dealCard();
dealCard();
report();

playTurn();
totalRounds ++;

emptyHand();
endRound();
anotherRound();

}
System.out.println("Player Points: " + playerPoints);
System.out.println("Total Rounds: " + totalRounds);
}

/**
* intro message to player
*/
private void intro(){
System.out.println("Welcome to 1451 Blackjack!");
System.out.println("You will start with two cards.");
System.out.println("You will be prompted to 'hit' or 'stand' 'hit' means you want another card, 'stand' not.");
System.out.println("");
System.out.println("You are trying to get Blackjack with exactly 21 points.");

}
/**
* deals a card to the player
*/
private void dealCard(){
int deckSize = aDeck.deckSize();
if(deckSize == 0){
System.out.println("Time for some more cards");
aDeck.loadDeck();
aDeck.shuffle();
aDeck.shuffle();
} else {
Card tempCard = aDeck.takeCard();
hand.add(tempCard);
}
}

/**
* calculates the hand value of the player
* @return handValue
*/
private int getHandValue(){
handValue = 0;
for(Card eachCard : hand) {
int tempValue = eachCard.getValue();
handValue = handValue + tempValue;
}
return handValue;
}

/**
* displays contents of hand
*/
private void showHand(){
System.out.println("Your cards:");
for(Card eachCard : hand) {
System.out.println(eachCard.getDescription()+
" of " + eachCard.getSuit());
}
}

private void emptyHand(){
for(int count = 0; count < hand.size(); count++) {
hand.remove(count);
}
}

显然我的“emptyHand()”方法中的代码有问题,但我错过了什么?!让我发疯。

最佳答案

该问题是由选择删除哪个索引以及列表大小变化引起的。除了“不清除”列表之外,这还可能导致 IndexOutOfBounds 异常。

考虑一手牌{A, B, C, D, E},循环流程如下所示;这也可以通过使用调试器单步执行循环来看到。

i      size()    result after remove(i)
----- -------- ----------------------
0 5 {B, C, D, E}
1 4 {B, D, E}
2 3 {B, D}
3 2 -> done (2 cards left!)

简单的修复方法是使用 hand.clear()尽管其他解决方案包括向后迭代索引或始终“弹出”末尾。

for (int i = hand.size() - 1; i >= 0; i--) {
hand.remove(i);
}

while (hand.size() > 0) {
hand.remove(hand.size() - 1);
// or hand.remove(0); but this causes bad complexity bounds on an ArrayList
}

创建一个与上面类似的表格来证明这些方法的有效性。

关于java - 为什么我的方法没有清空 ArrayList<Card>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26374449/

25 4 0