帮助BlackJack游戏 [英] Help with BlackJack game

查看:63
本文介绍了帮助BlackJack游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助以使Jack,Queen和King的值等于10
而不是(如我的程序中的11、12、13)

另外,我需要有关如何使播放器选择的帮助
Ace = 1或11

I need help to make the values of Jack, Queen, and King equal to 10
and not (11,12,13 as in my program)

Also, i need help on how to the player have the option of making
Ace = 1 or 11

import java.util.Scanner;
public class G21
{
    static Deck deck; 
    static int playerHand;
    static int compHand;
    static Scanner scanner;
    public static void main(String[] args)
    {
        scanner = new Scanner(System.in);
        deck = new Deck();
        deck.shuffle();
        boolean deal = true;
        do {
            playerHand = 0;
            compHand = 0;
            start();
            if(playersTurn())
                dealersTurn();
            checkScores();
            System.out.println("Play again? y/n");
            if(scanner.nextLine().charAt(0) != 'y')
                deal = false;
        } while(deal);
    }
    private static void start() {
        for(int j = 0; j < 2; j++) {
            Card card = deck.dealCard();
            String s = (j == 0) ? "1st" : "2nd";
            System.out.println("Your " + s + " card: " + card);
            playerHand += card.getValue();
            // check for blackjack
        }
        for(int j = 0; j < 2; j++) {
            Card card = deck.dealCard();
            String s = (j == 0) ? "1st" : "2nd";
            System.out.println("Dealer's " + s + " card: " + card);
            compHand += card.getValue();
            // check for blackjack
        }
    }
    private static boolean playersTurn() {
        boolean takeHit = true;
        while(takeHit) {
            System.out.println("Do you want a hit? y/n");
            if(scanner.nextLine().charAt(0) == 'y') {
                Card card = deck.dealCard();
                System.out.println("Your next card: " + card);
                playerHand += card.getValue();
                if(playerHand > 21)
                    takeHit = false;
            } else {
                takeHit = false;
            }
        }
        return playerHand <= 21;
    }
    private static void dealersTurn() {
        while(compHand < 16) {
            Card card = deck.dealCard();
            System.out.println("Dealer's next card: " + card);
            compHand += card.getValue();
        }
    }
    private static void checkScores() {
        if(playerHand > 21)
            System.out.println("You lose with " + playerHand);
        else if(compHand > 21)
            System.out.println("Dealer loses with " + compHand);
        else if(compHand == 21)
            System.out.println("Dealer wins with " + compHand);
        else if (playerHand == 21)
            System.out.println("You win with " + playerHand);
        else {
            String result;
            if(playerHand > compHand)
                result = "You win: " + playerHand + " to " + compHand;
            else
                result = "Dealer wins with " + compHand + " to " + playerHand;
            System.out.println(result);
        }
    }
}


甲板课程:


Class for Deck:

public class Deck {
private Card[] deck; 
private int cardsUsed; 
public Deck() {
deck = new Card[52];
int cardCt = 0; 
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new Card(value,suit);
cardCt++;
}
}
cardsUsed = 0;
}
public void shuffle() {
// Put all the used cards back into the deck, and shuffle it into
// a random order.
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public int cardsLeft() {
// As cards are dealt from the deck, the number of cards left
// decreases. This function returns the number of cards that
// are still left in the deck.
return 52 - cardsUsed;
}
public Card dealCard() {
// Deals one card from the deck and returns it.
if (cardsUsed == 52)
shuffle();
cardsUsed++;
return deck[cardsUsed - 1];
}
} // end class Deck



卡类



Class for Cards

public class Deck {
private Card[] deck; 
private int cardsUsed; 
public Deck() {
// Create an unshuffled deck of cards.
deck = new Card[52];
int cardCt = 0; 
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new Card(value,suit);
cardCt++;
}
}
cardsUsed = 0;
}
public void shuffle() {
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public int cardsLeft() {
return 52 - cardsUsed;
}
public Card dealCard() {
if (cardsUsed == 52)
shuffle();
cardsUsed++;
return deck[cardsUsed - 1];
}
} // end class Deck

推荐答案

语录:我需要帮助使Jack,Queen和King的值等于10,而不是(如我的程序中的11、12、13)

怎么样:
Quote: I need help to make the values of Jack, Queen, and King equal to 10 and not (11,12,13 as in my program)

How about:
if (value > 10) value = 10;



至于另外一个,您只需要检查玩家的总和是否超过21就可以将其保留为A,并将Ace的值重置为1.或者为玩家提供一个决定的选项.



As to the other one, you just need to check whether the player holds an Ace if his total goes above 21, and reset the Ace value to 1. Or offer an option for the player to decide.


关于另一个,只要他的总分超过21,您只需检查玩家是否持有A,并将Ace的值重置为1.或者提供一个选项供玩家决定."

我如何让玩家做出决定?或编写if语句以使Ace在超过21岁时破产?
"As to the other one, you just need to check whether the player holds an Ace if his total goes above 21, and reset the Ace value to 1. Or offer an option for the player to decide."

how do i make the player decide? or write the if statement for the Ace to bust if over 21?


这篇关于帮助BlackJack游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆