Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环和更新变量_Java_Loops_Blackjack - Fatal编程技术网

Java 循环和更新变量

Java 循环和更新变量,java,loops,blackjack,Java,Loops,Blackjack,在此blackjack程序中,计算运行计数时存在问题(两个代码块以RemainingDeck开头)。基本上,我希望程序在一轮开始时计算运行中的卡数,如果玩家或庄家命中则更新,并保留该计数,以便稍后将其添加到下一轮的最终计数(以及输出)。我似乎不知道要记住多少变量,在代码中放在哪里,以及必要的计算是什么。我已经尝试了很多次,第一轮运行正常,但它不会将其计数添加到下一轮 跑步计数是一种帮助玩家在整个游戏(不是回合)中计算牌数的策略。玩家通过给他们一个+1(值为2,3,4,5,6的牌)、0(值为7,8

在此blackjack程序中,计算运行计数时存在问题(两个代码块以RemainingDeck开头)。基本上,我希望程序在一轮开始时计算运行中的卡数,如果玩家或庄家命中则更新,并保留该计数,以便稍后将其添加到下一轮的最终计数(以及输出)。我似乎不知道要记住多少变量,在代码中放在哪里,以及必要的计算是什么。我已经尝试了很多次,第一轮运行正常,但它不会将其计数添加到下一轮

跑步计数是一种帮助玩家在整个游戏(不是回合)中计算牌数的策略。玩家通过给他们一个+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;
}
import java.io.IOException;
导入java.util.Scanner;
公营21点{
私人静态游戏卡;
专用静态卡[]玩家带;
专用静态卡[]经销商;
私有静态布尔转向;
私有静态布尔博弈;
专用静态双oldCardCounter=0。;
专用静态双纽卡计数器=0。;
私有静态双runningCountOld=0。;
私有静态双runningCountNew=0。;
私人静态双剩余甲板;
public Blackjack()引发异常{
playingDeck=新的deckOfCards();
playingDeck.shuffle();
playerHand=新卡[10];
dealerHand=新卡[10];
}
公共静态void main(字符串[]args)引发IOException{
扫描仪用户输入=新扫描仪(System.in);
21点游戏=新21点();
playerHand[0]=playingDeck.dealCard();
dealerHand[0]=playingDeck.dealCard();
playerHand[1]=playingDeck.dealCard();
dealerHand[1]=playingDeck.dealCard();
//playerMoney持有玩家现金
双人游戏费用=10000.0;
//在玩家有钱的时候玩游戏
//游戏循环
而(playerMoney>0&(playingDeck.getCurrentCard()<(playingDeck.getCard().length-1))){
如果(游戏结束){
//取消手动数组
playerHand=新卡[playerHand.length];
dealerHand=新卡[dealerHand.length];
playerHand[0]=playingDeck.dealCard();
dealerHand[0]=playingDeck.dealCard();
playerHand[1]=playingDeck.dealCard();
dealerHand[1]=playingDeck.dealCard();
}
转身=真;
gameOver=false;
//打赌
System.out.println(“\n您有美元“+playerMoney+”,您想赌多少?”;
System.out.println(“您应该下注$”+betValue(newCardCounter,playerMoney));
double playerBet=userInput.nextDouble();
while((playerBet>playerMoney)和&(turn=true)){
//如果他们下注太多,就破发
System.out.println(“\n您不能下注超过您的赌注。”);
playerBet=userInput.nextDouble();
}
System.out.println(“\n洗牌后,牌组看起来像:”;
//游戏。playingDeck.displayDeck();
System.out.println(“\n刻度…”);
//显示玩家卡
System.out.println(“\n您的手:“+playerHand[0]+”和“+playerHand[1]);
//展示经销商卡
System.out.println(“\n标记手:“+dealerHand[0]+”和[hidden]”+dealerHand[1]);
//显示值
System.out.println(“\n您的手的当前值为:”+分数(玩家手));
如果(分数(演奏者和)=21){
System.out.println(“\n拉克杰克!你赢了$”+玩家组);
playerMoney+=playerBet*1.5;
gameOver=true;
}
remainingDecks=Math.round(remainingCards()/52.);
runningCountOld=(int)(cardCounter(playerHand)+cardCounter(dealerHand));
runningCountNew+=runningCountOld;
oldCardCounter=运行计数新建/保留甲板;
oldCardCounter=Math.round((runningCountNew)/(remainingDecks));
if((数学四舍五入(oldCardCounter*100)/10==5)和&oldCardCounter<0){
oldCardCounter=数学楼层(运行计数新/剩余甲板);
}
System.out.println(remainingCards());
System.out.println(“\n此运行计数为:“+runningCountNew”);
newCardCounter=oldCardCounter;
System.out.println(“\n实际计数为:“+newCardCounter”);
而(!gameOver){
if(决策(新卡计数器)){
System.out.println(“您应该点击”);
}
否则{
System.out.println(“你应该站起来”);
}
//他们想做什么
System.out.println(“\n您想(1)击球还是(2)站立”);
int response=userInput.nextInt();
//他们击中
如果(响应==1){
打(转);
System.out.println(“\n您的新分数是”+分数(playerHand));
如果(得分(演奏者)大于21){
System.out.println(“\n你破产了!你损失了$”+playerBet);
playerMoney-=playerBet;
gameOver=true;
}否则如果(得分(playerHand)==21){
System.out.println(“\n您赢得$”+playerBet);
playerMoney+=playerBet;
gameOver=true;
}
}
//站立
如果(响应==2){
站(转);
System.out.pr