[英]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);
}
}
}
}
}
因此,當代碼運行時,它的作用是為Player1抽取2張卡(顯示了卡的兩面),並且發牌者(顯示了卡的1張)。 然后它將顯示玩家1擁有的總點數。 因此,在我按下命中鍵之后,要抽出另一張牌,它會計算總值就好了,因為它會將當前抽出的牌值匯總到前兩張牌中。
現在的問題是,我該如何編碼,以便每當我再次輸入“命中”按鈕時,它將比之前的3張卡片和新繪制的卡片相加?
int sum = 0;
for(Integer card : Player1)
sum += card;
if(sum > 21) system.err.println("Over 21; you lost");
尋找類似的東西...?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.