有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何为一副扑克牌返回数组列表的最大值?

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

  • In the beginning of the game, each player takes turn to draw a card from a shuffled deck of 52 cards. The players should have the same number of cards.
  • For example, if there are 5 players, then each player will get 10 cards. The game then proceeds in rounds.
  • In each round, each player plays a card. The player who plays the largest card wins. The order of the cards that a player plays is the order of getting the card.
  • For example, if Alice draws S5, D2, HK, SA, C4 in this order, she will play S5 in the first round, D2 in the second round, and so on.

问题:在课堂上,我对play()方法有问题。在//为每一轮部分开发循环中,我的方法是创建两个数组列表,并最终打印出该轮获胜者的姓名:

  • 比较1数组列表以存储所玩的卡
  • compare2数组列表以存储比较数组列表中比较的卡

但是,我得到的错误是:

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;
  }
}

共 (1) 个答案

  1. # 1 楼答案

    stacktrace显示问题是由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的位置

    你的l0S - 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());
        }
    }
    

    如您所见,所需的只是读取stacktrace以找出问题发生的位置。。。还有一点逻辑

    调试器可能是找到问题所在位置的另一种方法