带有数组的百家乐游戏在几轮后抛出异常
Baccarat game with arrays throws exception after a few rounds
我已经快通关了,但不知道为什么玩了几轮就卡住了。我相信这与牌组 class 的书写方式有关。它是老师提供的,但我可能需要修复它。
我想也许在几轮之后,数组变得太满了之类的,所以我尝试添加一种方法来清除它们。这没有用,我不认为这是问题所在,但我不确定。
class deck{
private String [] cards = {"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
private int cardCount = 1;
private boolean isShuffled = false;
//**********Shuffle cards method****************************
private void shuffleCards(){//shuffle cards method
for (int i = 0; i < cards.length; i++) {
int index = (int)(Math.random() * 13);
String temp = cards[i];
cards[i] = cards[index];
cards[index] = temp;
}
isShuffled = true;
}
//********Ensure the cards get shuffled before dealing******
public String getCards(){
if (isShuffled == true){
cardCount++;
return cards[cardCount];
}
else {
shuffleCards();//shuffle if they have not
cardCount++;
return cards[cardCount];
}
}
//********Show cards method*********************************
public void showCards(String [] theirCards){
for (int i = 0; i < theirCards.length; i++) {
if(theirCards[i] == null){
continue;
}
System.out.print(theirCards[i] + " ");
}
}
public void clearCards(){
cards = null;
cards = null;
}
//*******Card value method**********************************
public int getCardValues(String tempChar){
int indValues = 1;
switch (tempChar){
case "A": indValues = 1; break;
blah blah blah
case "3": indValues = 3; break;
case "2": indValues = 2;
}
return indValues;
}
}
我希望游戏一直循环,直到我退出或 运行 没钱为止,但在大约 3 场比赛后,我收到此错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at deck.getCards(Baccarat.java:248)
at playerObjects.setComputersCards(Baccarat.java:177)
at Baccarat.main(Baccarat.java:19)
两个明显的问题:
首先,cardCount被初始化为1(如前所述)。
其次,在方法 getCards() 中,cardCount 在返回卡片之前递增。
要解决此问题,您应该将 cardCount 初始化为 -1 而不是 1。
就像现在一样,你总是跳过牌组中的前两张牌,所以你只有 11 张牌,而不是你假设的 13 张,这可能就是为什么你要在大约第三张牌上查看索引的原因round -- 百家乐手牌通常是玩家和庄家各有 2 到 3 张牌,所以加起来。
解决方案
首先,您应该将 cardCount 初始化为 -1。
其次,作为故障保险,您应该修改 getCards() 方法来测试您的 cardCount,如果它是 12,则已经考虑将其设置为 -1 并在执行 increment/return 卡片之前再次洗牌。换句话说,让套牌自行管理以避免索引越界。
我已经快通关了,但不知道为什么玩了几轮就卡住了。我相信这与牌组 class 的书写方式有关。它是老师提供的,但我可能需要修复它。
我想也许在几轮之后,数组变得太满了之类的,所以我尝试添加一种方法来清除它们。这没有用,我不认为这是问题所在,但我不确定。
class deck{
private String [] cards = {"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
private int cardCount = 1;
private boolean isShuffled = false;
//**********Shuffle cards method****************************
private void shuffleCards(){//shuffle cards method
for (int i = 0; i < cards.length; i++) {
int index = (int)(Math.random() * 13);
String temp = cards[i];
cards[i] = cards[index];
cards[index] = temp;
}
isShuffled = true;
}
//********Ensure the cards get shuffled before dealing******
public String getCards(){
if (isShuffled == true){
cardCount++;
return cards[cardCount];
}
else {
shuffleCards();//shuffle if they have not
cardCount++;
return cards[cardCount];
}
}
//********Show cards method*********************************
public void showCards(String [] theirCards){
for (int i = 0; i < theirCards.length; i++) {
if(theirCards[i] == null){
continue;
}
System.out.print(theirCards[i] + " ");
}
}
public void clearCards(){
cards = null;
cards = null;
}
//*******Card value method**********************************
public int getCardValues(String tempChar){
int indValues = 1;
switch (tempChar){
case "A": indValues = 1; break;
blah blah blah
case "3": indValues = 3; break;
case "2": indValues = 2;
}
return indValues;
}
}
我希望游戏一直循环,直到我退出或 运行 没钱为止,但在大约 3 场比赛后,我收到此错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at deck.getCards(Baccarat.java:248)
at playerObjects.setComputersCards(Baccarat.java:177)
at Baccarat.main(Baccarat.java:19)
两个明显的问题:
首先,cardCount被初始化为1(如前所述)。 其次,在方法 getCards() 中,cardCount 在返回卡片之前递增。 要解决此问题,您应该将 cardCount 初始化为 -1 而不是 1。
就像现在一样,你总是跳过牌组中的前两张牌,所以你只有 11 张牌,而不是你假设的 13 张,这可能就是为什么你要在大约第三张牌上查看索引的原因round -- 百家乐手牌通常是玩家和庄家各有 2 到 3 张牌,所以加起来。
解决方案
首先,您应该将 cardCount 初始化为 -1。 其次,作为故障保险,您应该修改 getCards() 方法来测试您的 cardCount,如果它是 12,则已经考虑将其设置为 -1 并在执行 increment/return 卡片之前再次洗牌。换句话说,让套牌自行管理以避免索引越界。