尝试在java

时间:2016-02-24 04:30:10

标签: java algorithm blackjack

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

尝试忽略我在代码中的所有讽刺评论,因为他们没有针对堆栈溢出:P

BlackJack文件:

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

手档:

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

Cardlist文件:

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

好吧,所以希望你没有通过尝试阅读所有内容来杀死自己,并且我意识到我的代码风格/组织可以使用一些工作。

无论如何,他们有一些非常奇怪的事情,我似乎无法弄清楚。有时程序运行正常,你可以玩很多玩家的完整游戏,但有时候它会抛出一堆例外。

基本上如果你破产(超过21),程序会100%的时间抛出这个:

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)

顺便说一句,如果有人可以解释在不存在的行中的例外情况,例如653(第一条消息)会非常有帮助。

它也会在某些时候抛出同样的例外情况,即使你没有破坏我认为经销商正在破坏,因为经销商会自动遵守经销商的常规赌场规则。

如果有人能搞清楚,我们将不胜感激,谢谢!

0 个答案:

没有答案