【问题标题】:How to return the maximum value of an array list for a deck of playing cards?如何返回一副扑克牌的数组列表的最大值?
【发布时间】:2020-07-09 23:05:31
【问题描述】:

我正在尝试开发一个如图所示的纸牌游戏:

  • 在游戏开始时,每位玩家轮流从一副洗好的 52 张牌中抽一张牌。玩家应该拥有相同数量的牌。
  • 例如,如果有 5 个玩家,那么每个玩家将获得 10 张牌。然后比赛进行轮次。
  • 在每一轮中,每个玩家打一张牌。打出最大牌的玩家获胜。玩家出牌的顺序就是拿到牌的顺序。
  • 例如,如果 Alice 依次抽到 S5、D2、HK、SA、C4,那么她将在第一轮打 S5,在第二轮打 D2,以此类推。

问题:在类 CardGame 中,我遇到了 play() 方法的问题。在 //develop the loop for each round 部分,我的方法是创建 2 个数组列表并最终打印出该轮获胜者的姓名:

  • compare1 存储打出的牌的数组列表
  • compare2 数组列表,用于存储 compare 数组列表中的比较卡片

但是,我得到的错误是:

Alice has cards: [S2, DJ, D8, D5, D2, CQ, C9, C6]
Bob has cards: [S10, S7, S4, DK, D10, D7, D4, CA]
Cathy has cards: [H5, H2, SQ, S9, S6, S3, DQ, D9]
David has cards: [HK, H10, H7, H4, SA, SJ, S8, S5]
Emily has cards: [C8, C5, C2, HQ, H9, H6, H3, SK]
Fred has cards: [D3, CK, C10, C7, C4, HA, HJ, H8]
Alice plays S2
Bob plays S10
Cathy plays H5
David plays HK
Emily plays C8
Fred plays D3
The winner of this round is Alice
The winner of this round is Bob
The winner of this round is David
Alice has cards: [D8, D5, D2, CQ, C9, C6]
Bob has cards: [S4, DK, D10, D7, D4, CA]
Cathy has cards: [SQ, S9, S6, S3, DQ, D9]
David has cards: [H7, H4, SA, SJ, S8, S5]
Emily has cards: [C2, HQ, H9, H6, H3, SK]
Fred has cards: [C10, C7, C4, HA, HJ, H8]
Alice plays D8
Bob plays S4
Cathy plays SQ
David plays H7
Emily plays C2
Fred plays C10
The winner of this round is Alice
The winner of this round is Bob
The winner of this round is David
The winner of this round is Fred
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 6
    at CardGame.play(CardGame.java:52)
    at CardGameDriver.main(CardGameDriver.java:8)

我做错了什么?

这是预期的程序输出

Alice has cards: [S2, DJ, D8, D5, D2, CQ, C9, C6]
Bob has cards: [S10, S7, S4, DK, D10, D7, D4, CA]
Cathy has cards: [H5, H2, SQ, S9, S6, S3, DQ, D9]
David has cards: [HK, H10, H7, H4, SA, SJ, S8, S5]
Emily has cards: [C8, C5, C2, HQ, H9, H6, H3, SK]
Fred has cards: [D3, CK, C10, C7, C4, HA, HJ, H8]
Alice plays S2
Bob plays S10
Cathy plays H5
David plays HK
Emily plays C8
Fred plays D3
The winner of this round is David.

Alice has cards: [DJ, D8, D5, D2, CQ, C9, C6]
Bob has cards: [S7, S4, DK, D10, D7, D4, CA]
Cathy has cards: [H2, SQ, S9, S6, S3, DQ, D9]
David has cards: [H10, H7, H4, SA, SJ, S8, S5]
Emily has cards: [C5, C2, HQ, H9, H6, H3, SK]
Fred has cards: [CK, C10, C7, C4, HA, HJ, H8]
Alice plays DJ
Bob plays S7
Cathy plays H2
David plays H10
Emily plays C5
Fred plays CK
The winner of this round is Fred.

Alice has cards: [D8, D5, D2, CQ, C9, C6]
Bob has cards: [S4, DK, D10, D7, D4, CA]
Cathy has cards: [SQ, S9, S6, S3, DQ, D9]
David has cards: [H7, H4, SA, SJ, S8, S5]
Emily has cards: [C2, HQ, H9, H6, H3, SK]
Fred has cards: [C10, C7, C4, HA, HJ, H8]
Alice plays D8
Bob plays S4
Cathy plays SQ
David plays H7
Emily plays C2
Fred plays C10
The winner of this round is Cathy.

Alice has cards: [D5, D2, CQ, C9, C6]
Bob has cards: [DK, D10, D7, D4, CA]
Cathy has cards: [S9, S6, S3, DQ, D9]
David has cards: [H4, SA, SJ, S8, S5]
Emily has cards: [HQ, H9, H6, H3, SK]
Fred has cards: [C7, C4, HA, HJ, H8]
Alice plays D5
Bob plays DK
Cathy plays S9
David plays H4
Emily plays HQ
Fred plays C7
The winner of this round is Bob.

Alice has cards: [D2, CQ, C9, C6]
Bob has cards: [D10, D7, D4, CA]
Cathy has cards: [S6, S3, DQ, D9]
David has cards: [SA, SJ, S8, S5]
Emily has cards: [H9, H6, H3, SK]
Fred has cards: [C4, HA, HJ, H8]
Alice plays D2
Bob plays D10
Cathy plays S6
David plays SA
Emily plays H9
Fred plays C4
The winner of this round is David.

Alice has cards: [CQ, C9, C6]
Bob has cards: [D7, D4, CA]
Cathy has cards: [S3, DQ, D9]
David has cards: [SJ, S8, S5]
Emily has cards: [H6, H3, SK]
Fred has cards: [HA, HJ, H8]
Alice plays CQ
Bob plays D7
Cathy plays S3
David plays SJ
Emily plays H6
Fred plays HA
The winner of this round is Fred.

Alice has cards: [C9, C6]
Bob has cards: [D4, CA]
Cathy has cards: [DQ, D9]
David has cards: [S8, S5]
Emily has cards: [H3, SK]
Fred has cards: [HJ, H8]
Alice plays C9
Bob plays D4
Cathy plays DQ
David plays S8
Emily plays H3
Fred plays HJ
The winner of this round is Cathy.

Alice has cards: [C6]
Bob has cards: [CA]
Cathy has cards: [D9]
David has cards: [S5]
Emily has cards: [SK]
Fred has cards: [H8]
Alice plays C6
Bob plays CA
Cathy plays D9
David plays S5
Emily plays SK
Fred plays H8
The winner of this round is Bob.

这是我的代码

// YOU CANNOT MODIFY THIS FILE
public class CardGameDriver {

  public static void main(String[] args) {

    Player[] players = InitializePlayer.getPlayers();
    CardGame game = new CardGame(players);
    game.play();
  }
}
import java.util.ArrayList;

public class CardGame {

  // YOU CANNOT DEFINE OTHER INSTANCE VARIABLES
  private Player[] players; // players in this game
  private int numRounds;    // number of rounds in this game
  private Deck deck;        // deck of cards

  // constructor
  // YOU SHOULD NOT MODIFY THIS METHOD
  public CardGame(Player[] players) {

    this.players = players;
    deck = new Deck(5);

  }

  // implement this method
  public void play() {

    // find out the number of rounds in this game
      numRounds = 52/players.length;

    // distribute cards among players
      for (int x=0; x<numRounds; x++) {
          for (int y=0; y<players.length; y++) {
              players[y].addCard(Deck.drawCard());
          }
      }


    // develop the loop for each round
    // (1) print out the cards each player has
    // (2) print out the cards each player plays
    // (3) identify the winner of this round
      ArrayList<Card> compare1 = new ArrayList<Card>();
      ArrayList<Card> compare2 = new ArrayList<Card>();


      for (int i=0; i<numRounds; i++) {
          for (int j=0; j<players.length; j++) {
              players[j].printHand();
          }
          for (int k=0; k<players.length; k++) {
              System.out.print(players[k].getName() + " plays" + " ");
              System.out.println(players[k].playCard());
              compare1.add(players[k].playCard());
          }
          for (int l=0; l<compare1.size()-1; l++) {
              if (compare1.get(l).compareTo(compare1.get(l+1)) == 1) {
                System.out.println("The winner of this round is " + players[l].getName());
              }
          }
      }
  }

}
import java.util.ArrayList;

public class Player {

  // NO MORE INSTANCE VARIABLE CAN BE DEFINED
  private String name;  // name of the player
  private ArrayList<Card> cards; // cards the player has

  // YOU CANNOT MODIFY THIS FUNCTION
  public Player(String name) {
    this.name = name;
    cards = new ArrayList<Card>();
  }

  // YOU CANNOT MODIFY THIS FUNCTION
  public String getName() {
    return name;
  }

  // implement this method
  void addCard(Card card) {
        cards.add(card);
  }

  // implement this method
  public Card playCard() {
        Card removed = cards.remove(0);
        return removed;
  }

  // YOU CANNOT MODIFY THIS FUNCTION
  public void printHand() {
    System.out.print(name);
    System.out.println(" has cards: " + cards);
  }
}
public class Card {
    private Rank rank;
    private Suit suit;

    public Card (Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public String toString() {
        return suit + "" + rank;
    }

    public int compareTo(Card c) {
        int diffRank = rank.ordinal() - c.rank.ordinal();
        if (diffRank < 0) {
            return -1;
        }
        else if (diffRank > 0) {
            return 1;
        }
        else if (diffRank == 0) {
            int diffSuit = suit.ordinal() - c.suit.ordinal();
            if (diffSuit < 0) {
                return -1;
            }
            else if (diffSuit > 0) {
                return 1;
            } 
        }
        return 0;
    }
}
// YOU CANNOT MODIFY THIS FILE
public class InitializePlayer {

  public static Player[] getPlayers() {
    Player players[] = new Player[6];

    players[0] = new Player("Alice");
    players[1] = new Player("Bob");
    players[2] = new Player("Cathy");
    players[3] = new Player("David");
    players[4] = new Player("Emily");
    players[5] = new Player("Fred");

    return players;
  }
}

【问题讨论】:

  • 我猜您没有按照之前的要求花时间阅读minimal reproducible example 链接,而是发布了大量代码,其中大部分与手头的问题无关,让你的问题更难回答
  • 调试索引越界异常的最佳方法是通过调试器运行代码,查看异常发生的位置和原因。
  • @HovercraftFullOfEels 感谢您的链接。我编辑了帖子并删除了旧帖子。

标签: java arrays loops arraylist compare


【解决方案1】:

堆栈跟踪显示问题是由在您的play() 方法中调用ArrayList.get 触发的。只有一个地方发生这种情况:

for (int l = 0; l < compare1.size(); l++) {
    if (compare1.get(l).compareTo(compare1.get(l + 1)) == 1) {
        System.out.println("The winner of this round is " + players[l].getName());
    }
}

如果列表的大小为S,则它的元素位于0S - 1 的位置

您的l0 变为S - 1。但是您将l 的元素与l + 1 的元素进行比较。 l + 1 上升到 S,因此 get(S) 正在尝试获取索引超出范围的元素。

因此例外。

解决方案:

for (int l = 0; l < compare1.size() - 1; l++) {
    if (compare1.get(l).compareTo(compare1.get(l + 1)) == 1) {
        System.out.println("The winner of this round is " + players[l].getName());
    }
}

如您所见,所需要的只是读取堆栈跟踪以找出问题发生的位置......以及一些逻辑。

调试器是查找问题位置的另一种方法。

【讨论】:

  • 感谢您的建议。我进行了更改,但得到了输出:Alice has cards: [S2, DJ, D8, D5, D2, CQ, C9, C6] Player@2ff4acd0 playsS2 Player@54bedef2 playsS10 The winner of this round is Alice Player@5caf905d playsH5 The winner of this round is Alice The winner of this round is Bob 它一直持续到我收到此错误:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 6 at CardGame.play(CardGame.java:49) at CardGameDriver.main(CardGameDriver.java:8)
  • 所以现在你遇到了一个不同的错误。您是否尝试过按照我在查找第一个方法时使用的方法? 1)阅读堆栈跟踪以查找问题发生的位置。 2)阅读代码并应用简单的逻辑来确定索引如何/为什么超出范围。 3) 如有必要,使用调试器获取额外信息。 (提示:你是需要学习如何发现错误的人......不是我。)
猜你喜欢
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
  • 2023-03-14
  • 2014-04-09
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
相关资源
最近更新 更多