我正在使用数组列表创建纸牌游戏“战争”的Java版本。
这些都是文件,主要基于Paul Deiel的“ Java How to Program,Early Objects”https://github.com/eldar101/EldarRep/tree/master/Game_Of_War/src
游戏会询问两个玩家的名字,并将一个牌组分成两个26个牌组。我添加了JPanel消息,以通知牌组的大小以及谁在每个回合中获胜。它通常会转弯甚至达到几百转,但由于某种原因,它永远不会结束并发出:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 14 out of bounds for length 14
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:373)
at java.base/java.util.ArrayList.get(ArrayList.java:425)
at DeckOfCards.getCard(DeckOfCards.java:69)
at Game.gamePlay(Game.java:51)
at Game.<init>(Game.java:31)
at main.main(main.java:5)
有人可以告诉我为什么我在很多转弯后随机越界吗?我不知道自己缺少什么,因为游戏正常进行,只是没有尽头而出现异常。
如果您想在此处快速浏览,这是游戏本身的代码:
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Game {
String p1, p2;
int p1DeckSize = 0, p2DeckSize = 0;
DeckOfCards deck, deckP1, deckP2;
public Game() {
Scanner input = new Scanner(System.in);
deck = new DeckOfCards(); // crate a new deck
deck.shuffle(); // Shuffle the deck
deckP1 = new DeckOfCards();
deckP1.clearDeck();
deckP2 = new DeckOfCards();
deckP2.clearDeck();
for (int i = 0; i < 26; i++) {
deckP1.addICard(i,deck.getCard(i));
}
for (int i = 0; i < 26; i++) {
deckP2.addICard(i,deck.getCard(i+26));
}
deck.clearDeck();
JOptionPane.showMessageDialog(null, "Welcome to a game of \"War\"!");
this.p1 = JOptionPane.showInputDialog(null, "Please enter player 1's name:");
this.p2 = JOptionPane.showInputDialog(null, "Please enter player 2's name:");
JOptionPane.showMessageDialog(null, this.p1 + " is player 1 \n" + this.p2 + " is player 2");
gamePlay();
}
public void gamePlay() {
int turn = 1, i = 0;
//int indexP1 = 0, indexP2 = 0;
while (deckP1.deckSize() != 0 && deckP2.deckSize() != 0) {
JOptionPane.showMessageDialog(null, "Turn number " + turn++);
JOptionPane.showMessageDialog(null, p1 + " has " + deckP1.getCard(i).toString() + "\n" + p2 + " has " + deckP2.getCard(i).toString());
if (deckP1.getCard(i).cardValue() > deckP2.getCard(i).cardValue()) {
JOptionPane.showMessageDialog(null, p1 + " wins this turn!");
turnWin(deckP1, deckP2, i);
JOptionPane.showMessageDialog(null, p1 + " : " +deckP1.deckSize() +" " + p2+ " : " +deckP2.deckSize() );
} else if (deckP1.getCard(i).cardValue() < deckP2.getCard(i).cardValue()) {
JOptionPane.showMessageDialog(null, p2 + " wins this turn!");
turnWin(deckP2, deckP1, i);
JOptionPane.showMessageDialog(null, p1 + " : " +deckP1.deckSize() +" " + p2+ " : " +deckP2.deckSize() );
} else {
JOptionPane.showMessageDialog(null, "The cards are equal! time for war!");
JOptionPane.showMessageDialog(null, p1 + " : " +deckP1.deckSize() +" " + p2+ " : " +deckP2.deckSize() );
JOptionPane.showMessageDialog(null, p1 + "'s third card is " + deckP1.getCard(i+3) + "\n" + p2 + "'s third card is " + deckP2.getCard(i+3));
warStage(deckP1, deckP2, i);
i++;
JOptionPane.showMessageDialog(null, p1 + " : " +deckP1.deckSize() +" " + p2+ " : " +deckP2.deckSize() );
}
if (deckP1.deckSize() == 0)
JOptionPane.showMessageDialog(null, p1 + " Lost the game!" + p2 + " is the winner!");
else if (deckP2.deckSize() == 0)
JOptionPane.showMessageDialog(null, p2 + " Lost the game!" + p1 + " is the winner!");
}
}
public void turnWin(DeckOfCards d1, DeckOfCards d2, int i) {
Card temp1 = new Card(d1.getCard(i).face, d1.getCard(i).suit);
Card temp2 = new Card(d2.getCard(i).face, d2.getCard(i).suit);
d1.removeICard(i);
d2.removeICard(i);
d1.addCard(temp1);
d1.addCard(temp2);
} //end method turnWin
public void warStage(DeckOfCards d1, DeckOfCards d2, int i) {
Card temp1 = new Card(d1.getCard(i + 3).face, d1.getCard(i + 3).suit);
Card temp2 = new Card(d2.getCard(i + 3).face, d2.getCard(i + 3).suit);
if (temp1.cardValue() > temp2.cardValue())
{
for (int j = 0; j < 3; j++){
turnWin(d1, d2,i+1);}
} else if (temp1.cardValue() < temp2.cardValue()) {
for (int j = 0; j < 3; j++){
turnWin(d2, d1, i+1);}
}
else
{
warStage(d1,d2,i+3);
}
} //end method warStage
}
感谢您的帮助。
[最有可能在gameplay()
方法中,您的控件转到warStage()
方法,如果我没记错的话,您从“卡片组”中删除“卡”,并且此DeckOfCards
由某种列表支持,并且您只需从列表中删除一个条目。然后增加计数器变量i
。
另一方面,Deck的大小正在减小,这是游戏逻辑。但是,当控件返回顶部时,DeckOfCards
可能具有Card
,但不在您指定的索引i
中。
首先简单地执行各个卡座尺寸检查,然后继续执行逻辑。意思是,在gameplay()
方法中,分别将后两个方法作为前两个条件,然后将第一个条件作为第三个条件并将其设为else if
所以:if deckP1.deckSize() check
else if deckP2.deckSize() check
else if deckP1.getCard(i).cardValue() > deckP2.getCard(i).cardValue()
依此类推
查看相关行:
JOptionPane.showMessageDialog(null, p1 + "'s third card is " + deckP1.getCard(i+3) + "\n" + p2 + "'s third card is " + deckP2.getCard(i+3));
您将到达游戏中的地步:
玩家X:除了3张卡片外,其余全部玩家Y:有3张牌
[两个玩家都玩5:
[当您尝试获取(x + 3)该卡不存在时,您会看到异常。没有谷歌搜索,我不知道应该如何结束,因为我从未真正体验过它:
您可能应该考虑做一些事情:
1)对面和西服选项使用枚举,通过执行以下操作可能会更安全一些:
for all Suits for all Faces deck.add(new Card(suit,face));
2)开始使用数组列表,例如堆栈或队列。基本上,您应该只做
Deck.deal()
这将始终占据最高的牌(无论您从顶部还是底部看)。在我所能想到的99%的纸牌游戏中,您只会从最高处发出纸牌,因此能够向下访问第三张纸牌而不丢弃前三张纸牌毫无意义。现在,某些RPG类型的游戏允许您搜索套牌等,但这是另一套规则。
3)创建一个>
public class Player { private Deck active; private Deck discard; }
这意味着您可以从活动卡座中进行deal()并添加到废弃卡座中。例如,您将拥有(类似):
p1Card = p1.deal(); p2Card = p2.deal(); if (p1Card > p2Card) p2.getDiscard().addAll(p1Card, p2Card);
这也意味着您可以检查p1Card是否为null或DeckEmptyException并执行类似的操作:
p1.shuffleDiscard();
这会随机播放,并将所有卡从丢弃状态添加到活动状态。我认为这是打仗的正确方法,而不仅仅是将获胜卡牌添加到当前卡组的底部。