[Production progress] BlackJack: 02

This progress

It is a continuation from Last time.

--Change one of the cards to a string like "Ace of Diamond", "Ace of Diamond 2" ... "K of Clover" --Create an array bills that stores playing cards one by one using a for statement --In order to calculate the score, use HashMap to bills the associative array ["Ace of Diamond", 1], ["Ace of Diamond 2", 2] ... ["Clover K", 10]. Created in the For statement that creates (This time, A is judged as only one point.) --Randomize deck bills at the start of the game using Collections.shuffle --Set the player's hand to be stored in a list called handCard using ArrayList (the dealer's part is in handCard_dealer) --Set the variable cardPoints to store the score of the hand (for the dealer, set cardPoints_dealer) --Create a JUDGE class to refer to cardPoints and determine whether it was BlackJack (hereafter BJ) or Burst. --Define the global variable result in the JUDGE class so that the game can be terminated according to the conditions. ――After both hands are complete, the player chooses whether to hit or stand. --Even when you draw a card, it will be judged whether it is BJ or Burst. --Set to loop a series of flows with a While statement and repeat Hit until the player Stands, BJs, or Bursts.

It's a little more. The details are from the following items.

Contents

Expression of one card and score calculation

--One card as a character string. --Create array bills to store playing cards --Create an associative array using HashMap to calculate the score --Shuffled the deck

Until the last time, considering the score calculation, it was expressed in the array ["Diamond", "A"], ["Diamond", "2"] ... ["Clover", "K"]. .. Therefore, I decided to use HasMap to calculate the score. By using the card as an associative array key, one card has been changed to the character strings "Ace of Diamond" and "Ace of Diamond 2". I decided to create the array bills that will be the deck and the Points of the associative array for score calculation together with a For statement. I used Collection.shuffle to shuffle and randomize the deck at the beginning of the game.

BlackJack_main.java


		//Deck list
		List<String> bills = new ArrayList<String>();

		//Hash map for score calculation
		HashMap<String, Integer> points = new HashMap<String, Integer>();

		//Card generation array
		String[] strMark={"Diamond","heart","spade","Clover"};
		String[] strNumber={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

		//Create a card with a For statement and add it to the deck list
		//Add cards and points to hashmap
		for (int i=0;i<strMark.length;i++){
			for (int j=0;j<strNumber.length;j++){
				String card = strMark[i]+"of"+strNumber[j];
				bills.add(card);
				if(j>9) {
					points.put(card, 10);
				}else {
					points.put(card, j+1);
				}
			}
		}

		//Shuffle deck
		Collections.shuffle(bills);

Expression of hand

--Express the cards in your hand as an array using ArrayList --Store your score in the int type variable cardPoints

When I draw a card, I pull it out one by one from bills, so I used ArrayList to store the drawn cards in a list called handCard. I used a list instead of an array because it's uncertain how many cards I'll draw. In addition, I defined the int type variable cardPoitns to calculate the score from points using the cards in my hand and keep it pooled. Both of these also define a list / variable with "_dealer" added like a dealer.

BlackJack_main.java


		//Start the game
		//Player's hand
		ArrayList<String> handCard =new ArrayList<String>();
		handCard.add(bills.get(0));
		System.out.println("Your first card is["+handCard.get(0)+"]is");
		int cardPoints = points.get(bills.get(0));
		bills.remove(0);
		handCard.add(bills.get(0));
		System.out.println("Your second card is["+handCard.get(1)+"]is");
		cardPoints += points.get(bills.get(0));
		bills.remove(0);
		System.out.println("Your hand is"+handCard+"is");
		System.out.println("Your score is"+cardPoints+"Is a point");

		//Dealer's hand
		ArrayList<String> handCard_dealer = new ArrayList<String>();
		handCard_dealer.add(bills.get(0));
		System.out.println("The dealer's first card is["+handCard_dealer.get(0)+"]is");
		int cardPoints_dealer = points.get(bills.get(0));
		bills.remove(0);
		handCard_dealer.add(bills.get(0));
		System.out.println("I don't know the dealer's second card");
		cardPoints_dealer += points.get(bills.get(0));
		bills.remove(0);

Judgment of BJ or Burst

--Create a JUDGE class to refer to cardPoints and determine whether it was BJ or Burst --Conditional branching so that the game can be ended by defining the global variable result in the JUDGE class.

I want to judge whether it is BJ or Burst every time the player and the dealer draw a card, so I created a JUDGE class and made it available as a method. In addition, we have defined a global variable result in the JUDGE class so that the game can be terminated conditionally.

JUDGE.java


public class JUDGE {
	public static String result = "";

	public static void judge(int cardPoints,int cardPoints_dealer) {
		if(cardPoints>=22) {
			System.out.println("----Burst!!!----");
			System.out.println("----You lose----");
			result="lose";
		} else if(cardPoints==21){
			System.out.println("☆★☆★☆★☆★BLACKJACK!!!☆★☆★☆★☆★");
			System.out.println("----Is your victory----");
			result="win";
		} else if(cardPoints_dealer>=22) {
			System.out.println("----DEALER'S Burst!!!----");
			System.out.println("The dealer's score is"+cardPoints_dealer+"Because it is a point, it burst");
			System.out.println("----You win----");
			result="win";
		} else if(cardPoints_dealer==21){
			System.out.println("☆★☆★☆★☆★DEALER'S BLACKJACK!!!☆★☆★☆★☆★");
			System.out.println("----You lose----");
			result="lose";
		}
	}		//judge method closing
}			//class tightening

BlackJack_main.java


		//BlackJack or Burst processing
		JUDGE.judge(cardPoints,cardPoints_dealer);
		if(JUDGE.result!="") {
			return;
		}

Do you want to hit? Do you want to stand?

――After both hands are complete, the player chooses whether to hit or stand. --Even when you draw a card, it will be judged whether it is BJ or Burst. --Set to loop and repeat a series of flows with a While statement

From here, it is the part where the player actually selects the action. Choose between Hit (draw a card) or Stand (pass the turn to the dealer without drawing a card) depending on your score. When Hit is selected, one card is taken out from bills, handcard is added, and the score of the drawn card is also added to cardPoints. After that, the judgment is also made using the above judge method. Finally, the action selection and Hit behavior are looped in a While statement.

BlackJack_main.java


		//Hit?Stand?
		Scanner scanner = new Scanner(System.in);
		int PlayerRoop = 0;
		while(PlayerRoop==0) {
			int choice_roop =0;
			String choicePlay ="";

			while (choice_roop==0) {
				System.out.print("Do you want to hit? Do you want to stand?(H/S?)>");
				String choice = scanner.next();

				if (choice.contains("H") || choice.contains("h")) {
					System.out.println("I chose Hit");
					choice_roop+=1;
					choicePlay="Hit";
				}else if(choice.contains("S") || choice.contains("s")){
					System.out.println("I chose Stand");
					choice_roop+=1;
					choicePlay="Stand";
				}else {
					System.out.println("Please select Hit or Stand");
				}
			}			//while statement closing

			//Action selection result
			if (choicePlay.contains("Hit")) {
				handCard.add(bills.get(0));
				System.out.println("The card you drew["+handCard.get(handCard.size()-1)+"]is");
				cardPoints += points.get(handCard.get(handCard.size()-1));
				bills.remove(0);
				System.out.println("The current score is"+cardPoints+"Is a point");
				System.out.println("Your hand is"+handCard+"is");
				JUDGE.judge(cardPoints,cardPoints_dealer);
				if(JUDGE.result != "") {
					return;
				}
			}else {
				System.out.println("Move on to the dealer's turn");
				PlayerRoop+=1;
			}		//if statement closing
		}			//while statement closing

from now on

I'm wondering if I'll add the dealer's actions after that.

At the end

The code has become quite long, but I feel relieved when the end is visible. This time there were too many updates and it was quite long, but I would like to hear any advice.

Recommended Posts

[Production progress] BlackJack: 03
[Production progress] BlackJack: 01
[Production progress] BlackJack: 02
[Production progress] BlackJack: 04
Implemented "Blackjack".