学校のブラックジャックゲームを仕上げる最終段階。ロジックをプログラムしようとしているときに問題が発生しているようです。
基本的に、ディーラーは自分のカードが 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();
}
}
他にご不明な点がございましたら、お気軽にお問い合わせください。よろしくお願いいたします。