交易者在用户破产时仍在播放……如果交易者不在时重置,找不到用户的手存储方式

问题描述 投票:0回答:1

[就像标题说的那样,我一直在迷你赌场里玩各种游戏。然而,二十一点,无论用户是否破产,庄家仍然会玩。我尝试使用hand.getScore和DealerHand.GetScore允许我使用一会儿循环,如果用户<= 21,则发牌者会玩。

我现在完全感到困惑,这使我不知所措,任何建议都很好!预先非常感谢你们,伙计们!

我将附加正在引用的代码类。

应用程序部分

public class App {
//public static void main(String[] args) {
    public void playNewBlackjack(User player) {
    Deck deck = new Deck();
    Hand hand = new Hand();
    Dealer dealer = new Dealer();
    Game game = new Game();
    Scoring scoring = new Scoring();
    User user = new User();
    Hand dealerHand = new Hand();

    user.setFirstName("What is your first name?");
    user.setSurname("What is your Last name?");
    user.setBalance("How much would you like to deposit?");

    while (user.getBalance() >= 1) {
        user.setBet("How much do you want to bet this time?");
        deck.shuffle();
        int[] ArrayScore = game.main(deck, hand, dealer);
        int CheckWin = scoring.scoringSystem(ArrayScore);

        if (CheckWin == 1) {
            user.updateBalance(user.getBalance() + user.getBet());
            System.out.println("Your total balance is now " + user.getBalance());

        } else if (CheckWin == 2 || CheckWin == 3 || CheckWin == 4) {
            user.updateBalance(user.getBalance() - (user.getBet()));
            System.out.println("Your total balance is now " + user.getBalance());

        }
        deck.resetDeck();
        hand.resetHand();
        dealerHand.resetHand();
    }

主游戏区

public class Game {
public static int[] main(Deck deck, Hand hand, Dealer dealer){
    Scanner user_input = new Scanner(System.in);

    hand.add(deck.deal());
    hand.add(deck.deal());
    System.out.println(hand);
    System.out.println(hand.getScore());

    while(hand.getScore() <= 21) {
        System.out.println("Do you want to hit or stick?");
        String hitOrStick = user_input.nextLine();

        if (hitOrStick.equals("hit")){
            hitOption(hand, deck);

        } else if(hitOrStick.equals("stick")){
            System.out.println("Your total hand is " + hand.getScore());
            System.out.println("");
            break;
        }
        if (hand.getScore() == 21){
            System.out.println("Your total hand is " + hand.getScore() + " you must stick");
            break;
        }
        if(hand.getScore() > 21){
            System.out.println("You bust");
            break;
        }
    }

    int dealerScore = dealer.dealerTurn(deck);
    int playerScore = hand.getScore();
    int[] scoreArray = {dealerScore,playerScore};

    return scoreArray;

}

public static void hitOption(Hand hand,Deck deck){
    hand.add(deck.deal());
    System.out.println(hand);
    System.out.println(hand.getScore());

}

经销商

public class Dealer {
public static int dealerTurn(Deck deck) {
    Hand hand = new Hand();
    Game game = new Game();
    Hand dealerHand = new Hand();

    dealerHand.add(deck.deal());
    dealerHand.add(deck.deal());
    System.out.println("The dealers cards are:");
    System.out.println(dealerHand);
    System.out.println(dealerHand.getScore());
    System.out.println(hand.getScore()); // to see whether the value is being stored


    while (hand.getScore() <= 21) {  //still assigned as 0
        if (dealerHand.getScore() < 17) {
            System.out.println("The dealer will hit");
            game.hitOption(hand, deck);
        }
        else if (dealerHand.getScore() == 21) {
            System.out.println("The dealer will hits 21");
        }
        else if (dealerHand.getScore() >= 17){
            System.out.println("The dealer will stick");
        }
        else {
            System.out.println("The dealer will stand");
            System.out.println(dealerHand);
            System.out.println("The dealers score is " + dealerHand.getScore());
            break;
        }
    }
    return hand.getScore();
}

public class Hand {
private List<Card> cards = new ArrayList<>();

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

public int getScore(){
    int total = 0;
    for(Card card: cards){
        total += card.getScore();
    }
    return total;
}

public String toString() {
    return cards.toString();
}

public void resetHand(){
    cards.clear();
}

得分系统(临时,在主游戏修复后需要正确执行)

public class Scoring {
public static int scoringSystem(int[] ArrayScore){

     //Dealer = 0
     //User = 1
    if (ArrayScore[0] > 21 && ArrayScore[1] <= 21) {
        System.out.println("The dealer has bust, you win!");
        return 1;

    }
    else if (ArrayScore[1] > 21){
        System.out.println("You have bust, the dealer wins!");
        return 2;

    }
    else if (ArrayScore[0] > ArrayScore[1] && ArrayScore[0] <= 21 || ArrayScore[0] == ArrayScore[1] && ArrayScore[0] <= 21){
        System.out.println("The dealer wins!");
        return 3;
    }
    else {
        return 4;
    }
}
java
1个回答
0
投票

您的while循环后,您正在为dealer.turn()呼叫用户。我的解决方案不是完美的(您需要添加一些行/函数以使其完全按照您的要求工作),但它至少可以向您显示错误的位置。

while(hand.getScore() <= 21) {
    System.out.println("Do you want to hit or stick?");
    String hitOrStick = user_input.nextLine();

    if (hitOrStick.equals("hit")){
        hitOption(hand, deck);

    } else if(hitOrStick.equals("stick")){
        System.out.println("Your total hand is " + hand.getScore());
        System.out.println("");
        break;
    }
    if (hand.getScore() == 21){
        System.out.println("Your total hand is " + hand.getScore() + " you must stick");
        break;
    }
    if(hand.getScore() > 21){
        System.out.println("You bust");
        int playerScore = hand.getScore(); // changes
        int[] scoreArray = {0,playerScore}; // changes

        return scoreArray; // changes
    }
}

int dealerScore = dealer.dealerTurn(deck);
int playerScore = hand.getScore();
int[] scoreArray = {dealerScore,playerScore};

return scoreArray;
© www.soinside.com 2019 - 2024. All rights reserved.