在while循环中使用If语句,二十一点游戏

时间:2013-09-25 23:08:27

标签: java if-statement while-loop boolean

在完成学校黑杰克游戏的最后阶段。 在尝试编写逻辑时,我似乎遇到了问题。

基本上,只有当他们的牌数<= 16时,经销商才会被击中,由于某种原因,if的声明不起作用且经销商只是一直打击或一直停留,无论他们的价值如何卡片...我知道它可能很简单,但我似乎无法找到它。

当卡的价值超过21时,经销商也没有破产。

主要(问题在这里)

import java.util.Scanner;
import java.util.ArrayList;

public class game {
public static String name;

public static void main(String[]args){

    deck doc = new deck();
    dealer dealer = new dealer(doc);

    Scanner nameScanner = new Scanner(System.in);

    int money = 200;

    System.out.println("Welcome to BlackJack!" + "\n\n" + "Your balance: $" + money + "\n\n" + "What is your name? ");
    name = nameScanner.nextLine();

    dealer.start();

    dealer.dealDealer();
    dealer.dealPlayer();
    dealer.dealDealer();
    dealer.dealPlayer();


    boolean dealerStay = false;
    boolean playerStay = false;
    boolean allStay = false;
    boolean tie = false;

    while(true){

        System.out.println(dealer.toString());
        Scanner gameScanner = new Scanner(System.in);   

        System.out.println("\nWould you like to Hit(H) or Stay(S)? ");

        String hos = gameScanner.nextLine();

        //Players & Dealer hit logic
        if(hos.equalsIgnoreCase("h")) {
            dealer.dealPlayer();
        }
        //Player bust
        if(dealer.getPlayerHandValue() > 21){
            System.out.println("*****YOU LOSE\n*****You bust with " + dealer.getPlayerHandValue() + "\n*****Dealer wins by default");
            break;
        }
        //Stay logic
        if(hos.equalsIgnoreCase("s")){
            playerStay = true;
        }
        //Dealer hit logic PROBLEM HERE
        if(playerStay == true && dealer.getDealerHandValue() <= 16){
            dealer.dealDealer();
            System.out.println("\n*****Dealer hit\n");
        }
        //Dealer stay logic PROBLEM HERE
        if (playerStay == true && dealer.getDealerHandValue() > 16){
            System.out.println("\n*****Dealer stayed\n");
            dealerStay = true;
        }
        //Dealer bust PROBLEM HERE
        if(dealer.getDealerHandValue() > 21){
            System.out.println("*****YOU WIN\n *****Dealer busts");
            break;
        }





        }
    }
}

构造函数(如果您需要)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class dealer {

deck deck = new deck();
String dealerTopCardString = "";
card playerHandCard;
card dealerHandCard;
ArrayList<card> dealerHand = new ArrayList<card>();
ArrayList<card> playerHand = new ArrayList<card>();

public dealer(deck cards) {
    deck = cards;

}

public void start() {
    dealerHand = new ArrayList<card>();
    playerHand = new ArrayList<card>();
}

public String getPlayerHand() {

    String playerHandString = "";
    for (int i = 0; i < playerHand.size(); i++) {
        playerHandCard = playerHand.get(i);
        card card = new card(playerHandCard.getValue(),
                playerHandCard.getSuit());
        playerHandString += "\n";
        playerHandString += card.getValue();
        playerHandString += "\t";
        playerHandString += card.getSuit();
    }
    return playerHandString;

}

public String getDealerHand() {

    String dealerHandString = "";
    for (int i = 0; i < dealerHand.size(); i++) {
        dealerHandCard = dealerHand.get(i);
        card card = new card(dealerHandCard.getValue(),
                dealerHandCard.getSuit());
        dealerHandString += "\n";
        dealerHandString += card.getValue();
        dealerHandString += "\t";
        dealerHandString += card.getSuit();
    }
    return dealerHandString;
}

public void dealPlayer() {

    playerHand.add(deck.deal());

}

public void dealDealer() {

    dealerHand.add(deck.deal());

}

public String getDealerTopCard() {
    String dealerTopCard = "";
    card cardTop = dealerHand.get(0);
    card card = new card(cardTop.getValue(), cardTop.getSuit());
    dealerTopCard += "\n";
    dealerTopCard += card.getValue();
    dealerTopCard += "\t";
    dealerTopCard += card.getSuit();
    return dealerTopCard;

}

@Override
public String toString() {

    return  "\n" + game.name + "'s cards : " + getPlayerHand() + "\n\n " + game.name + "'s total: "
            + getPlayerHandValue() + "\n\n" + "Dealer is showing : "
            + getDealerHand();
}

public int getPlayerHandValue() {
    int playerHandValue = 0;        

    for (card a : playerHand) {
        String c = a.getValue();
        if (c.equals("J")) {
            playerHandValue += 10;
        }

        else if (c.equals("Q")) {
            playerHandValue += 10;
        }

        else if (c.equals("K")) {
            playerHandValue += 10;
        }

        else if (c.equals("A")) {
            playerHandValue += 11;
        }

        else {
            playerHandValue += Integer.parseInt(a.getValue());

        }
    }

    int valueOfCards = playerHandValue;
    return valueOfCards;
}

public int getDealerHandValue() {
    int playerHandValue = 0;        

    for (card a : playerHand) {
        String c = a.getValue();
        if (c.equals("J")) {
            playerHandValue += 10;
        }

        else if (c.equals("Q")) {
            playerHandValue += 10;
        }

        else if (c.equals("K")) {
            playerHandValue += 10;
        }

        else if (c.equals("A")) {
            playerHandValue += 11;
        }

        else {
            playerHandValue += Integer.parseInt(a.getValue());

        }
    }

    int valueOfCards = playerHandValue;
    return valueOfCards;
}

public void resetDealerHand() {
    dealerHand.clear();
}

public void resetPlayerHand() {
    playerHand.clear();
}

}

如果我能为您提供其他任何帮助,请告诉我,提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您的方法getDealerHandValue()正在使用ArrayList playerHand

它应该使用dealerHand ArrayList。

更好的方法是定义一个方法,将列表作为争论,然后用适当的列表调用它,比如

public int getHandValue(List<card> theHand) {
    int handValue = 0;        

    for (card a : theHand) {
        // Your logic
    }
    return handValue;
}

然后拨打getHandValue(playerHand)getHandValue(dealerHand)