尝试用java调试非常容易出错的21点游戏

尝试用java调试非常容易出错的21点游戏,java,algorithm,blackjack,Java,Algorithm,Blackjack,首先,感谢您阅读本文,我将首先发布所有与程序相关的代码,您现在可以浏览或完全跳过这些代码 试着忽略我在整个代码中的所有讽刺评论,顺便说一句,它们不是针对堆栈溢出的:p 21点档案: import java.util.Scanner; import java.util.InputMismatchException; public class BlackJack { //i should probably put a betting thing somewhere..... //also s

首先,感谢您阅读本文,我将首先发布所有与程序相关的代码,您现在可以浏览或完全跳过这些代码

试着忽略我在整个代码中的所有讽刺评论,顺便说一句,它们不是针对堆栈溢出的:p

21点档案:

import java.util.Scanner; 
import java.util.InputMismatchException; 
public class BlackJack { //i should probably put a betting thing somewhere.....
  //also should probably add a thing that tells them one of the dealers cards
  //should also put something that stops 2 players from having the same name

    static Scanner input = new Scanner(System.in); //honestly have no idea if i can put this here but it seems to work. keyword; seems
//good lucking counting cards with all this shuffling lol

  public void absolutelyNothing() { //maybe put a betting method here instead of a method that does nothing???
    //Do NOT Delete This Method!
  }

  public static void displayHands(Hand[] hands, String[] playerNames, int players) {

    for (int i = 1; i < players; i++) {
      display(hands, playerNames, i);
    }
    displayDealer(hands);
  }

  public static void display(Hand[] hands, String[] playerNames, int i) {
    System.out.println(playerNames[i] + " has " + hands[i]);
  }

  public static void displayDealer(Hand[] hands) {
    System.out.println("Dealer has " + hands[0].dealerDisplay());
  }

  public static boolean win(Hand[] hands, int i) {

    int value = worth(hands[i]);
    int dealer = worth(hands[0]);
      if (value == -1) {
        return false;
      }
      if (value > dealer) {
        return true;
      }
      return false;
  }

  public static void winners(Hand[] hands, String[] playerNames) {

    for (int i = 1; i < hands.length; i++) {
      if (win(hands, i) == true) {
        System.out.println(playerNames[i] + " beat the dealer");
      }
      else {
        System.out.println(playerNames[i] + " lost to the dealer");
      }
    }
  }

  public static int worth(Hand hand) {

    int sum = 0;
    int aces = 0;

    for (int i = 0; i < hand.howManyCards(); i++) {
      Card card = hand.getCard(i);
      int value;
      if (card.getNumber() == -10) {
        aces++;
        value = 11;
    }
    else {
      value = card.getNumber();
    }
      sum+= value;
  }
  for (int i = 0; aces > i; i++) {

    if (sum >= 22) {
      sum = sum - 10;
    }
  }



    if (sum >= 22) {
      return -1;
    }
    return sum;
  }

  public static void dealerTurn(Deck deck, Hand[] hands) {

    int worth = worth(hands[0]);

    while (worth < 16) {
      dealCard(deck, hands, 0);
    }

  }

  public static void playTurn(Deck deck, Hand[] hands, int i, String[] playerNames) {

  //  System.out.println(playerNames[i] + " hit?" + " Your hand is worth " + worth(hands[i]) );
    boolean hit;

    displayHands(hands, playerNames, hands.length);

    while (true) {

      if (worth(hands[i]) == -1) {
        System.out.println("Bust");
        break;
      }

      System.out.println(playerNames[i] + " hit?" + " Your hand is worth " + worth(hands[i]) );
      try {
     hit = input.nextBoolean();
    }

    catch (InputMismatchException exception) {
      System.out.println("Please enter \"true\" or \"false\"");
      continue; //pretty sure this continue is causing the glitch where if you don't enter a boolean it goes insane
    }

      if (hit == true) {
        dealCard(deck, hands, i);
      }

      else {
        break;
      }

    }
  }

  public static void everyoneGo(Deck deck, Hand[] hands, int players, String[] playerNames) {

    for (int j = 1; j < players + 1; j++) { //players go
      playTurn(deck, hands, j, playerNames);
    }
      dealerTurn(deck, hands);
  }

  public static Deck newHand(Deck deck) {

    Deck newDeck = new Deck();
    return newDeck;
  }

  public static void dealCard(Deck deck, Hand[] hands, int i) {

    deck.shuffleDeck();
    //goodluck counting cards
    hands[i].addCard(deck.getCard(0));
    deck.removeCard(0);
  }

  public static void giveNextCard(Hand[] hands, Deck deck, int i) {
    hands[i].addCard(deck.getCard(0));
    deck.removeCard(0);
  }

  public static Hand[] firstTwoCards(int players, String[] playerNames, Deck deck) { //gives dealer cards first an I'm too lazy to fix

    deck.shuffleDeck();
    //seriously good luck
    Hand[] hands = new Hand[players + 1]; //dealer is hands[0]

      for (int i = 0; i < players + 1; i++) {
        hands[i] = new Hand(playerNames[i]);
      }

      for (int j = 0; j < 2; j++) {
        for (int i = 0; i < players + 1; i++) {
          giveNextCard(hands, deck, i);
        }
      }

    return hands;
  }

  public static String[] getNames(int players) {

    System.out.println("What are the names of the players?");
    String[] playerNames = new String[players + 1];
    playerNames[0] = "Dealer";
    for (int i = 1; i < players + 1; i++) {
      playerNames[i] = input.next(); //something with this line the last time you use it
    }
    return playerNames;
  }

  public static int peoplePlaying() {

    System.out.println("How many people are playing?");

    int players = input.nextInt();

    return players;
  }

  public static void main(String[] args) {

    int players = peoplePlaying();

    String[] playerNames = getNames(players);

    Deck deck = new Deck();

    Hand[] hands = firstTwoCards(players, playerNames, deck);

    everyoneGo(deck, hands, players, playerNames);

    winners(hands, playerNames);
  }
}

//if you're smart you would have realized all this shuffling has absolutely no effect on counting cards lol now figure out why
顺便说一句,如果有人能解释一下,在653(第一条消息)这样不存在的行中出现异常意味着什么,那将非常有帮助

它也会在某些时候抛出同样的异常,即使你没有破发,我假设这是庄家在自动遵守庄家的常规赌场规则时破坏庄家


如果有人能找到答案,我们将不胜感激,谢谢

你想从空牌堆中发一张牌。是时候进行一些调试和修复了。使用调试器加快运行速度,并仔细查看异常stacktrace提供给您的行。@HoverCraftfullOfels我删除了注释以避免混淆,但我没有说错误在Java中,我只是说异常stacktrace引用的第653行在ArrayList.Java中,在回答“653等不存在的行的异常意味着什么”的问题时@samgak:对不起,我误读了您的陈述。对于OP,请查看stacktrace中引用的代码行,包括
CardList.java:31
BlackJack.java:151
,等等,运行程序时,使用调试器查看变量的状态。Eclipse和NetBeans都附带了很好的调试器,这些调试器将允许您查看正在发生的事情。
public class Hand extends CardList {

  public Hand(String label) {
    super(label);
  }

  public void display() {
    System.out.println(getLabel() + ": ");
    for (int i = 0; i < size(); i++) {
        System.out.println(getCard(i));
    }
    System.out.println();
  }

}
public class Deck extends CardList {

  public Deck(String label) {
    super(label);

    for (int suit = 0; suit <= 3; suit++) {
      for (int rank = 1; rank <= 13; rank++) {
        addCard(new Card(rank, suit));
      }
    }
  }
}
import java.util.ArrayList;
import java.util.Random;

public class CardList {

  public String name;
  public ArrayList<Card> cards;

  public CardList(String name) {
    this.name = name;
    this.cards = new ArrayList<Card>();
  }

  public String toString() {
    String s = "";
    for (int i = 0; i < cards.size(); i++) {
      s = s + cards.get(i) + ", ";
    }
    return s;
  }

  public String dealerDisplay() {
    return cards.get(0).toString();
  }

  public String getName() {
    return name;
  }

  public Card getCard(int i) {
    return cards.get(i);
  }

  public int howManyCards() {
    return cards.size();
  }

  public void addCard(Card card) {
    cards.add(card);
  }

  public void removeCard(int i) {
    cards.remove(i);
  }

  public void swapCards(int i, int j) {
    Card wait = cards.get(i);
    cards.set(i, cards.get(j));
    cards.set(j, wait);
  }

  public void shuffleDeck() {

    for (int k = 0; k < 10; k++) { //not sure how well the algorithm works but if you do it 10 times it shouldn't matter how bad it is
      for (int i = 0; i < howManyCards(); i++) {
        int j = (int) (Math.random() * howManyCards());
        swapCards(i, j);
      }
    }
  }

}
public class Card {

  int number;
  int suit;

  public static final String[] NUMBERS = {null, null, "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
  public static final String[] SUITS = {"Diamonds", "Clubs", "Spades", "Hearts"};

  public Card(int number, int suit) {
    this.number = number;
    this.suit = suit;
  }

  @Override
  public String toString() {
    return NUMBERS[number] + " of " + SUITS[suit];
  }

  public int getNumber() {

    if (number == 14) { //used to tell if its an ace in blackjack file
      return -10;
    }
    if (number < 11) {
      return number;
    }
    else {
      return 10;
    }
  }
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at CardList.getCard(CardList.java:31)
    at BlackJack.dealCard(BlackJack.java:151)
    at BlackJack.dealerTurn(BlackJack.java:93)
    at BlackJack.everyoneGo(BlackJack.java:138)
    at BlackJack.main(BlackJack.java:209)