我正在尝试开发一款纸牌游戏,如图所示:
- 在游戏开始时,每个玩家轮流从洗牌的52张牌中抽一张牌。玩家应该有相同数量的牌
- 例如,如果有5名玩家,那么每个玩家将获得10张牌。然后比赛分回合进行
- 在每一轮比赛中,每个玩家都会打一张牌。打出最大牌的玩家获胜。玩家玩的牌的顺序就是拿到牌的顺序
- 例如,如果爱丽丝按顺序抽到S5、D2、HK、SA、C4,她将在第一轮打S5,在第二轮打D2,依此类推
问题:在类CardGame中,我对play((方法有问题。在//为每一轮部分开发循环中,我的方法是创建2个数组列表,并最终打印出本轮获胜者的姓名:
- compare1数组列表,用于存储所玩的牌
- 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;
}
}
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
,则它在位置0
到S - 1
处具有元素
您的l
从0
变为S - 1
。但您正在比较l
处的元素和l + 1
处的元素。CCD_ 11上升到CCD_ 12,因此CCD_。
因此出现了例外。
解决方案:
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());
}
}
正如您所看到的,所需要的只是读取堆栈跟踪以找出问题发生的位置。。。以及一些逻辑。
调试器可能是找到问题位置的另一种方法。