在完成我的学校二十一点游戏的最后阶段。在尝试对逻辑进行编程时,我似乎遇到了问题。
基本上,只有当他们的牌 <= 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();
}
}
让我知道我是否可以为您提供其他任何东西,在此先感谢您的帮助!