在这个二十一点程序中,计算运行计数存在问题(两个代码块以remainingDecks开头)。基本上,我希望程序在一轮开始时计算运行卡计数,如果玩家或经销商点击则更新它并保留该计数以便稍后将其添加到下一轮的最终计数(输出) 。我似乎不知道要记住多少变量以及将它们放在代码中的位置以及必要的计算内容。我已经尝试了多次,第一轮正常工作,但它不会将其计数添加到下一轮#。
运行计数是一种策略,可以帮助玩家统计整个游戏(不是轮次)中处理的牌。玩家通过给出+1值(对于值为2,3,4,5,6的牌),0(对于值为7,8,9的牌)和-1(对于卡值)来跟踪高牌和低牌。价值10,J,Q,K,A)的卡片。当牌被发出时,玩家在整个游戏中将值加到起始数0并使用该计数来确定鞋中的牌是什么类型(未发牌)。例如,在任何给定时刻玩家的计数为+10让他知道鞋子现在有更多的高牌,反之亦然。
import java.io.IOException;
import java.util.Scanner;
public class Blackjack{
private static deckOfCards playingDeck;
private static Card[] playerHand;
private static Card[] dealerHand;
private static boolean turn;
private static boolean gameOver;
private static double oldCardCounter = 0.;
private static double newCardCounter = 0.;
private static double runningCountOld = 0.;
private static double runningCountNew = 0.;
private static double remainingDecks;
public Blackjack() throws IOException{
playingDeck = new deckOfCards();
playingDeck.shuffle();
playerHand = new Card[10];
dealerHand = new Card[10];
}
public static void main(String[] args) throws IOException{
Scanner userInput = new Scanner(System.in);
Blackjack theGame = new Blackjack();
playerHand[0] = playingDeck.dealCard();
dealerHand[0] = playingDeck.dealCard();
playerHand[1] = playingDeck.dealCard();
dealerHand[1] = playingDeck.dealCard();
//playerMoney holds players cash
double playerMoney = 10000.0;
//Play the game while the player has money
//Game loop
while(playerMoney>0 && (playingDeck.getCurrentCard() < (playingDeck.getCard().length-1))){
if(gameOver){
//nullify the hand arrays
playerHand = new Card[playerHand.length];
dealerHand = new Card[dealerHand.length];
playerHand[0] = playingDeck.dealCard();
dealerHand[0] = playingDeck.dealCard();
playerHand[1] = playingDeck.dealCard();
dealerHand[1] = playingDeck.dealCard();
}
turn = true;
gameOver = false;
//Take Bet
System.out.println("\nYou have $" + playerMoney + ", how much would you like to bet?");
System.out.println("You should bet $" + betValue(newCardCounter, playerMoney));
double playerBet = userInput.nextDouble();
while((playerBet > playerMoney) && (turn = true)){
//Break if they bet too much
System.out.println("\nYou cannot bet more than you have.");
playerBet = userInput.nextDouble();
}
System.out.println("\nAfter shuffling, the deck looks like: ");
//theGame.playingDeck.displayDeck();
System.out.println("\nDealing...");
//Display player cards
System.out.println("\nYour Hand:" + playerHand[0] + " and " + playerHand[1]);
//Display dealer cards
System.out.println("\nDealer Hand: " + dealerHand[0] + " and [hidden]" +dealerHand[1]);
//Display Value
System.out.println("\nYour hand is currently valued at: " + score(playerHand));
if(score(playerHand) == 21){
System.out.println("\nBlackjack! You win $" + playerBet);
playerMoney += playerBet * 1.5;
gameOver = true;
}
remainingDecks = Math.round(remainingCards()/52.);
runningCountOld = (int) (cardCounter(playerHand) + cardCounter(dealerHand));
runningCountNew += runningCountOld;
oldCardCounter = runningCountNew/remainingDecks;
oldCardCounter = Math.round((runningCountNew)/(remainingDecks));
if((Math.round(oldCardCounter*100)/10 == 5) && oldCardCounter < 0){
oldCardCounter = Math.floor(runningCountNew/remainingDecks);
}
System.out.println(remainingCards());
System.out.println("\nThis running count is: " + runningCountNew);
newCardCounter = oldCardCounter;
System.out.println("\nThe true count is: " +newCardCounter);
while(!gameOver){
if(decisionMaking(newCardCounter)){
System.out.println("You should hit");
}
else{
System.out.println("You should stand");
}
//What do they want to do
System.out.println("\nWould you like to (1)Hit or (2)Stand");
int response = userInput.nextInt();
//They hit
if(response == 1){
hit(turn);
System.out.println("\nYour new score is " + score(playerHand));
if(score(playerHand) > 21){
System.out.println("\nYou bust! You lose $" + playerBet);
playerMoney -= playerBet;
gameOver = true;
}else if(score(playerHand) == 21){
System.out.println("\nYou win $" + playerBet);
playerMoney += playerBet;
gameOver = true;
}
}
//Stand
if(response == 2){
stand(turn);
System.out.println("\nYou stand! Dealer's turn");
System.out.println("\nDealer Hand: " + dealerHand[0] + " and " + dealerHand[1]);
while(score(dealerHand) < 17){
hit(stand(turn));
}
System.out.println("\nDealer's score is " + score(dealerHand));
if(score(dealerHand) > 21){
System.out.println("\nDealer busts! You win $" + playerBet);
playerMoney += playerBet;
gameOver = true;
}else if(score(playerHand) > score(dealerHand)){
System.out.println("\nDealer loses! You win $" + playerBet);
playerMoney += playerBet;
gameOver = true;
}else if(score(dealerHand) > score(playerHand)){
System.out.println("\nDealer wins! You lose $" + playerBet);
playerMoney -= playerBet;
gameOver = true;
}else if(score(dealerHand) == score(playerHand)){
System.out.println("\nTie! Game over!");
gameOver = true;
}
stand(gameOver);
}
remainingDecks = Math.round(remainingCards()/52.);
runningCountOld = (int) (cardCounter(playerHand) + cardCounter(dealerHand));
runningCountNew += oldCardCounter;
oldCardCounter = runningCountNew/remainingDecks;
oldCardCounter = Math.round((runningCountNew)/(remainingDecks));
if((Math.round(oldCardCounter*100)/10 == 5) && oldCardCounter < 0){
oldCardCounter = Math.floor(runningCountNew/remainingDecks);
}
System.out.println(remainingCards());
System.out.println("\nThis running count is: " + runningCountNew);
newCardCounter = oldCardCounter;
System.out.println("\nThe true count is: " +newCardCounter);
}
}
}
public static int score(Card[] hand){
int aces = 0;
int counter = 0;
int totalValue = 0;
while(hand[counter] != null){
if(hand[counter].getCardValue() == 11){
if((totalValue + 1) <= 11){
aces = 1;
totalValue += 11;
}else{
totalValue += 1;
}
}else{
totalValue += hand[counter].getCardValue();
}
counter++;
}
if((totalValue > 21) && (aces!=0)){
totalValue -= 10;
}
return totalValue;
}
public static Card[] hit(boolean turn){
if(turn){
for (int i=0; i < playerHand.length; i++) {
if(playerHand[i] == null){
playerHand[i] = playingDeck.dealCard();
System.out.println("\nYou get a " + playerHand[i]);
break;
}
}
return playerHand;
}else{
for (int i=0; i < dealerHand.length; i++) {
if(dealerHand[i] == null){
dealerHand[i] = playingDeck.dealCard();
System.out.println("\nDealer draws a " + dealerHand[i]);
break;
}
}
return dealerHand;
}
}
//added
public static boolean stand(boolean turns){
if(turns){
turns = false;
return turns;
}else{
gameOver = true;
return gameOver;
}
}
//AI Code
public static int cardCounter(Card[] hand){
int i = 0;
int cardCount = 0;
while(hand[i] != null){
if(hand[i].getCardValue() >= 2 && hand[i].getCardValue() <= 6){
cardCount += 1;
}else if(hand[i].getCardValue() >= 7 && hand[i].getCardValue() <= 9){
cardCount += 0;
}else if(hand[i].getCardValue() >= 10 && hand[i].getCardValue() <= 14){
cardCount += -1;
}
i++;
if( i == 312){
break;
}
}
return cardCount;
}
public static int remainingCards(){
int remainingCardCount = 0;
remainingCardCount = (playingDeck.getCard().length) - playingDeck.getCurrentCard();
return remainingCardCount;
}
public static boolean decisionMaking(double trueCount){
if (score(playerHand) >= 2 && score(playerHand) <= 11) {
return true;
}else if (score(playerHand) == 12) {
if (dealerHand[0].getCardValue() >=7 && dealerHand[0].getCardValue() <= 11) {
return true;
}else if(dealerHand[0].getCardValue() == 2) {
return (trueCount < 3.0);
}else if(dealerHand[0].getCardValue() == 3) {
return (trueCount < 1.0);
}else if(dealerHand[0].getCardValue() == 4) {
return (trueCount < 0.0);
}else if(dealerHand[0].getCardValue() == 5) {
return (trueCount < -1.0);
}else{
return (trueCount < 0.0);
}
}else if (score(playerHand) == 13) {
if (dealerHand[0].getCardValue() >=7 && dealerHand[0].getCardValue() <= 11) {
return true;
}else if(dealerHand[0].getCardValue() == 2) {
return (trueCount < 0.0);
}else if(dealerHand[0].getCardValue() == 3) {
return (trueCount < -1.0);
}else{
return false;
}
}else if (score(playerHand) == 14) {
if (dealerHand[0].getCardValue() >=7 && dealerHand[0].getCardValue() <= 11) {
return true;
}else{
return false;
}
}else if (score(playerHand) == 15) {
if (dealerHand[0].getCardValue() >=7 && dealerHand[0].getCardValue() <= 11 && dealerHand[0].getCardValue() != 10) {
return true;
}else if (dealerHand[0].getCardValue() == 10) {
return (trueCount < 4.0);
}else{
return false;
}
}else if (score(playerHand) == 16) {
if (dealerHand[0].getCardValue() >=7 && dealerHand[0].getCardValue() <= 11 && dealerHand[0].getCardValue() != 10 && dealerHand[0].getCardValue()!= 9) {
return true;
}else if (dealerHand[0].getCardValue() == 10) {
return (trueCount < 0.0);
}else if (dealerHand[0].getCardValue() == 9) {
return (trueCount < 5.0);
}else{
return false;
}
}else if (score(playerHand) == 17) {
return false;
}else if (score(playerHand) == 18) {
return false;
}else if (score(playerHand) == 19) {
return false;
}else{
return false;
}
}
public static double betValue(double trueCount, double totalMoney){
double advantage = -0.005;
double betPercent = 0.76;
double optimalBet;
optimalBet = totalMoney * ((trueCount * Math.abs(advantage) + advantage)*betPercent);
return optimalBet;
}
}