刚学Java要按要求做一个21点的app,有没有大神能指教一二????

![gan](https://img-blog.csdnimg.cn/20191029110534631.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xlb1hVbg==,size_16,color_FFFFFF,t_70)
在这里插入图片描述
在这里插入图片描述

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class BlackJackApplication extends Application{
	
	/*
	 These are the 'model' elements.
	 */
	Hand dealerHand, playerHand;
	Deck deck;

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("BlackJack!");

        //TODO: You should have two types of Panes that you have made, one
		//TODO: for the dealer and one for the player. You want it to display their
		//TODO: respective hands


        //TODO: Make a dealCard button that, when clicked, deals one card to the dealer and
		//TODO: one to the player. Don't forget to update the view.

        
        //TODO: Make a newGame Button that, when clicked, resets the player and
        //TODO: dealer hands to empty hands and updates the view.
        
        
        FlowPane aPane = new FlowPane();
        //TODO: add elements to the FlowPane in the order you wish them to appear
		//TODO: feel free to use a different layout if you wish.
        
    
        primaryStage.setScene(new Scene(aPane, 400, 400));
        primaryStage.show();
		
	}
	
	public void update() {
		//TODO: update the GUI. Presumably the dealer's and player's hands have
		//TODO: changed since this was last called.
		
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}
import java.util.ArrayList;

//hand of either the player or the dealer. We will subclass them later and implement dealer logic.
//For now there is no dealer AI.
public class Hand {
	//An ArrayList is like an array with a flexible size.
	ArrayList<Card> hand;
	int total;

	/*
	Aces can count as 1 or 11. Initially we count them as 11. If the hand is over 21,
	we reduce any aces to 1 so that the player does not bust.
	 */
	int totalAces;
	
	public Hand() {
		hand = new ArrayList<>();
		total = 0;
		totalAces = 0;
	}
	
	public void newHand() {
		hand.clear();
		total = 0;
		totalAces = 0;
	}

	/*
	We add this card to the player's hand and update the value.
	 */
	public int dealCard(Card card) {
		hand.add(card);
		if (card.getCardValue() == 1) {
			totalAces ++;
			total += 11;
		} else {
			total += card.getCardValue();
		}
		//if we busted, but there are aces, we make those aces worth 1 instead of 11
		while(total >21) {
			if (totalAces <1) {
				break;
			}
			totalAces --;
			total -= 10;
		}
		return total;
	}
	
	public boolean busted() {
		return total > 21;
	}
	
	public int getTotal() {
		return total;
	}

	/*
	Output the hand as a String (that could go into a TextField, for instance).
	 */
	public String toString() {
		StringBuilder str = new StringBuilder();
		for (Card card: hand) {
			str.append(card);
			str.append(", ");
		}
		if (str.length()>3) {
			str.delete(str.length()-1, str.length());
		}
		return str.toString();
	}
}

import java.util.Random;

public class Card {
	
	//we use String instead of char because "10" is two characters. 
	public static final String[] cardNumbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
	
	//unicode values in hex
	static int diamond = 0x2662;
	static int heart = 0x2661;
	static int club = 0x2663;
	static int spade = 0x2660;

	//we cast the unicode values to char to get the suits.
	public static final char[] suits = {(char)heart,(char)diamond,(char)club,(char)spade};

	//initialize to useless values
	int cardNumber;
	int cardSuit;
	
	//to generate random cards
	Random random = new Random();
	
	//must be a number from 0 to 12 and 0 to 3
	public Card(int cardNumber, int cardSuit) {
		if (cardNumber > 12||cardNumber <0) {
			this.cardNumber = random.nextInt(13);
		}else {
			this.cardNumber = cardNumber;
		}
		if (cardSuit > 3||cardSuit <0) {
			this.cardSuit = random.nextInt(4);
		}else {
			this.cardSuit = cardSuit;
		}
	}
	
	//no argument constructor makes this a random card
	public Card() {
		cardNumber = random.nextInt(13);
		cardSuit = random.nextInt(4);
	}

	//return a random card by making a new card using the no argument constructor
	public static Card randomCard() {
		return new Card();
	}
	
	public String toString() {
		return Card.cardNumbers[cardNumber]+Card.suits[cardSuit];
	}

	/*
	A little test method that prints out all the possible cards.
	 */
	public static void main (String[] args) {
		for(int i = 0; i < 13; i++) {
			for (int j = 0; j < 4; j ++) {
				System.out.println(cardNumbers[i]+suits[j]);
			}
		}
	}

	/*
	The value of the card (in the game of BlackJack, Aces return 1).
	 */
	public int getCardValue() {
		if (cardNumber >=9) {
			return 10;
		}else {
			return cardNumber+1;
		}
	}

}

import java.util.Random;


/*
 * For resource management purposes, instead of allocating and deallocating memory constantly (an 
 * expensive operation), we use the same 52 cards and keep them in memory.
 *
 * This class holds 52 cards and when asked, returns one at random. No card is ever removed
 * from the Deck, so you may receive the same card on subsequent calls.
 */
public class Deck {
	
	Card[] deck;
	Random random;
	
	public Deck() {
		random = new Random();
		deck = new Card[52];
		int count = 0;
		for(int i = 0; i < 13; i++) {
			for (int j = 0; j < 4; j ++) {
				deck[count++]= new Card(i,j);
			}
		} 
	}
	
	public Card dealCard() {
		return deck[random.nextInt(52)];
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值