绘制新卡时出现总和问题-BlackJack

时间:2018-10-21 20:01:22

标签: java blackjack

我在一个类似于Swing中的BlackJack的简单游戏的代码中遇到了问题。用户可以绘制两张卡,程序可以正确计算两张卡,但是当用户想要按下按钮(我使用JButton)来获得新卡时,它并没有按计划进行。

注意:这是一个非常简单的版本。没有超过10的卡。

例如:第一张牌= 2 + 5 = 7 用户按下“新卡”以获取新卡。 新卡是4->即7 + 4 = 11 ====甚至还没有被破坏,但是程序有时会告诉用户它已被破坏。

我的代码中的错误在哪里?我在哪里可以改进和修复它?

	/*
	 *Write a program that draws a card of value 1 to 10. 
	 *Determine the sum of the cards so far. 
	 *Keep drawing cards while the sum is less than 16. Output the total sum. 
	 *If the sum is greater than 21 output “Bust”. 
	 */
import java.awt.*;
import java.awt.event.*; 
import javax.swing.*;
import java.util.Random;//random number generator
import java.util.ArrayList;//Array lists

class CardGame extends JFrame implements ActionListener
{
	
	JLabel välkommenMessage = new JLabel ("Welcome to BlackJack   ");
	JTextArea spelRegler = new JTextArea("Klicka på 'Draw Card' för att få dina två första kort.\nOm kortsumman är under 16, fortsätt dra kort.\nKommer du över 21 förlorar du. ");
	
	JButton cardButton = new JButton ("Draw Card");
	JButton newCard = new JButton ("New Card");
    JLabel kortText = new JLabel("");
	static JLabel blackJackSumma = new JLabel ("");
	static JLabel displayNewCard = new JLabel("");
	JLabel stödlinjenText = new JLabel ("18+. Stödlinjen: 020-819 100");
	
	static Random randomCard = new Random(); 
	
	
	public CardGame (){
		super("Card Game");
		setSize (270,250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible (true);
		Container contentArea = getContentPane();
		contentArea.setBackground(Color.white);
		FlowLayout flowManager = new FlowLayout();
		contentArea.setLayout(flowManager);
		spelRegler.setEditable(false);
		
		
		cardButton.addActionListener(this);
		newCard.addActionListener(this);
		
		contentArea.add(välkommenMessage);
		contentArea.add(spelRegler);
		contentArea.add(stödlinjenText);
		contentArea.add(cardButton);
		contentArea.add(kortText);
		
		contentArea.add(displayNewCard);
		contentArea.add(blackJackSumma);
        contentArea.add(newCard);
		setContentPane(contentArea);
		
	}	
	
	public static int cardDraw () {
		 int card = randomCard.nextInt(10) + 1;
		 return card;
	}

     public static ArrayList<Integer> drawTwoCards(){
	       ArrayList<Integer> cards = new ArrayList<Integer>();
	       for(int i = 0; i < 2; i++)
	    {
	        cards.add(cardDraw());
	    }
	       return cards;
	  }
     

   public static int cardsSumma(ArrayList<Integer> cards){
	   int total = 0;
	   for(int card :cards){ 
	      total = total + card;
	    }
	   return total;
	  }
    
    public static void spelaKort(ArrayList<Integer> cards){
    	
    	int newCard = cardDraw(); //call for method
	    cards.add(newCard); 
	    displayNewCard.setText("Your card: " + newCard);
	    blackJackSumma.setText("Total: "+ cardsSumma(cards));
	
	if (cardsSumma(cards)== 21){
		blackJackSumma.setText("You win");
		return; 
		}
	else if (cardsSumma(cards)> 21){
		blackJackSumma.setText("BUSTED!");
		return;
	}
	else{                             
		blackJackSumma.setText("Total: " + cardsSumma(cards));
		System.out.print(cardsSumma(cards));
		spelaKort(cards);
		}
    }
 
    
	public void actionPerformed (ActionEvent event){ 

	    if (event.getSource()== cardButton){
	     ArrayList<Integer> cards = drawTwoCards();
		 kortText.setText("Card: " + cards.get(0) +" and " + cards.get(1)+ "   Total: " + cardsSumma(cards));
		 if (cardsSumma(cards)< 21){
               spelRegler.setText("Press 'New Card' to continue");
              }
		 
    }
		if (event.getSource()==newCard){
			ArrayList<Integer> cards = drawTwoCards();
			spelaKort(cards); 
		}
		}
	    
	}


public class Worksheet37a {
	public static void main(String[] args) {
		
		CardGame Win = new CardGame();
		
	}

}

0 个答案:

没有答案