简体   繁体   English

在BlackJack游戏中,如何将我绘制的2张卡的值添加到我刚刚绘制的卡值中

[英]How can I add the value of 2 cards that I drew, into the card value that i just draw, in a game of BlackJack

import java.util.Random;
import java.util.Stack;
import java.util.Scanner;

public class Blackjack {
  public static void main(String[] args) {
    int cardValue; /* card value is from 2 to 11 */
    int MaxCard = 50;
    Stack < Integer > Player1 = new Stack < Integer > ();
    Stack < Integer > Addition = new Stack < Integer > ();
    Stack < Integer > Dealer = new Stack < Integer > ();
    Scanner keyboard = new Scanner(System.in);
    String input;
    Random r = new Random();

    System.out.println("Welcome to Mitchell's blackjack program!");

    for (int a = 1; a <= 2; a++) { // Start's the game by assigning 2 cards each, to the players
      int Player1draw = 2 + r.nextInt(11);
      int Dealerdraw = 2 + r.nextInt(11);
      //System.out.print("\nPushing in " + Player1draw + " to Player 1."); - used for checking
      Player1.push(Player1draw);
      //System.out.print("\nPushing in " + Dealerdraw + " to Dealer."); - used for checking
      Dealer.push(Dealerdraw);
    }

    Integer pop1 = (int) Player1.pop();
    System.out.print("\nYou get a " + pop1);
    Integer pop2 = (int) Player1.pop();
    System.out.print(" and " + pop2);

    int sum = pop1 + pop2;

    System.out.print("\nYour total is " + sum);

    if (sum > 21) {
      System.out.print("\nYou LOST! ");
      System.exit(0);
    }

    Integer pop3 = (int) Dealer.pop();
    System.out.print("\nThe dealer has a " + pop3 + " showing, and a hidden card.\n");
    Integer pop4 = (int) Dealer.pop();
    System.out.print("\nHis total is hidden, too.\n");

    for (int i = 0; i < MaxCard; i++) {
      System.out.print("\nWould you like to \"hit\" or \"stay\"? ");
      input = keyboard.next();

      if (input.equals("hit")) {
        int draw2 = 2 + r.nextInt(11); // draw -> 'hit' for another card
        int sum3 = sum + draw2;
        System.out.print("\nYou drew a " + draw2);
        System.out.print("\nYour total is " + (sum3) + ".");

        if ((sum + draw2) > 21) {
          System.out.print("\nThe Dealer WON and you LOST!");
          System.exit(0);
        }
      } else if (input.equals("stay")) {
        System.out.print("\nOkay, dealer's turn.");
        System.out.print("\nHis hidden card was a " + pop4);

        int sum2 = pop3 + pop4;
        System.out.print("\nHis total was " + sum2);

        if (sum2 > 21) {
          System.out.print("\nThe Dealer LOST and you WON!");
          System.exit(0);
        }
      }
    }
  }
}

So when the code runs, what it does is that it will draw 2 cards for Player1 (both of the card is shown) and the dealer(1 of the card is shown). 因此,当代码运行时,它的作用是为Player1抽取2张卡(显示了卡的两面),并且发牌者(显示了卡的1张)。 It will then display the total number of points player1 has. 然后它将显示玩家1拥有的总点数。 So after i press hit, to draw another card, it calculates the total value just fine because it sums up the current drawn card value into the previous 2 card. 因此,在我按下命中键之后,要抽出另一张牌,它会计算总值就好了,因为它会将当前抽出的牌值汇总到前两张牌中。

Now the problem is, how can I code it so whenever I type the 'hit' button again it will than add up the previous 3 cards and the newly drawn card? 现在的问题是,我该如何编码,以便每当我再次输入“命中”按钮时,它将比之前的3张卡片和新绘制的卡片相加?

int sum = 0;
for(Integer card : Player1)
   sum += card;

if(sum > 21) system.err.println("Over 21; you lost");

Looking for something like that... ? 寻找类似的东西...?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为该二十一点游戏增加可重玩性? - How can i add replayability to this blackjack game? 我希望我的发牌者在选择何时在BlackJack游戏中抽牌时更加“智能” - I want my dealer to be bit more 'intelligent' when choosing when to draw a card in BlackJack game 如何将随机数加到Java(二十一点)的总数中? - How can i add the random number to my total for java(blackjack)? 当我绘制另一个矩形时,如何保留我在 Jpanel 上绘制的矩形? - How to keep the rectangle that I drew on the Jpanel when I draw another rectangle? 试图在Java中在我已经绘制过的图像上画线,但是我无法在图像上绘制它? - Trying to draw lines in java over an image i already drew but i can't get it on top of the image? 如何计算二十一点游戏的分数? 我要去哪里错了? - How to calculate the score for Blackjack game? Where am I going wrong? 如何使J掌握找到此二十一点游戏的主要方法? - How can I make it so that J grasp finds the main method in this blackjack game? 在动态创建的卡片视图上,如果按下特定于卡片的删除按钮,如何删除单个卡片? - On a card view created dynamically, how can I remove individual cards if a delete button specific to a card is pressed? 如果卡值超过21,如何更改二十一点中ace的值 - how to change value of ace in blackjack if card value goes over 21 我应该如何在游戏中画图? - How should I draw this in a game?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM