Welcome to Blackjack! Your first card is the Q of the heart The dealer's first card is Ace of Diamond Your second card is a heart A The dealer's second card is a secret Your current point is 11 Do you draw a card? Yes: y or No: n
―――――――――――――――――――――――――― I want to use the method to set the number of the player to 1, 2, 3 ...
Welcome to Blackjack! Your 0th card is the Q of the heart The dealer's first card is Ace of Diamond Your 0th card is a heart A The dealer's second card is a secret Your current point is 11 Do you draw a card? Yes: y or No: n
package blackjack;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MainGame {
	public static void main(String[] args) {
		System.out.println("Welcome to Blackjack!");
		List<Integer> deck = new ArrayList<Integer>(52);
		Deck.shuffleDeck(deck); //Call the method
		//Show shuffled cards. For debugging
		//		for(Integer i : deck) {
		//	            System.out.println(i);
		//	        }
		List<Integer> player = new ArrayList<>(); //Create a player's hand list
		List<Integer> dealer = new ArrayList<>(); //Create a dealer's hand list
		//Player dealer draws two cards
		player.add(deck.get(0));
		dealer.add(deck.get(1));
		player.add(deck.get(2));
		dealer.add(deck.get(3));
		//Show points in the player / dealer's hand
		System.out.println("Your"
				+ Player.getPlayerHands() + "The first card is"
				+ Deck.toDescription(player.get(0)));
		System.out.println("The dealer's first card is"
				+ Deck.toDescription(dealer.get(0)));
		System.out.println("Your"
				+ Player.getPlayerHands() + "The first card is"
				+ Deck.toDescription(player.get(1)));
		System.out.println("The dealer's second card is a secret");
		//Aggregate player and dealer points
		int playerPoint = Deck.sumPoint(player);
		int dealerPoint = Deck.sumPoint(dealer);
		System.out.println("Your current point is"
				+ playerPoint + "is");
		//Phase in which the player draws a card
		while (true) {
			System.out.println("Do you draw a card? Yes:y or No:n");
			//Accepts keyboard input and assigns it to the variable str
			Scanner scan = new Scanner(System.in);
			String str = scan.next();
			if ("n".equals(str)) {
				break;
			} else if ("y".equals(str)) {
				//Add 1 card from the deck to your hand
				player.add(deck.get(Deck.deckCount));
				//Advance one deck and one hand
				Deck.deckCount++;
				Player.setPlayerHands(Player.getPlayerHands() + 1);
				System.out.println("your"
						+ Player.getPlayerHands() + "The first card is"
						+ Deck.toDescription(player.get(Player.getPlayerHands() - 1)));
				playerPoint = Deck.sumPoint(player);
				System.out.println("The current total is" + playerPoint);
				//Player burst check
				if (Deck.isBusted(playerPoint)) {
					System.out.println("Unfortunately, it burst. The dealer wins.");
					return;
				}
			} else {
				System.out.println("Your input is"
						+ str + "is. Enter y or n.");
			}
		}
		//Phase to draw cards until the dealer has 17 or more in hand
		while (true) {
			//Break if you have 17 or more in your hand
			if (dealerPoint >= 17) {
				break;
			} else {
				//Add 1 card from the deck to your hand
				dealer.add(deck.get(Deck.deckCount));
				//Advance one deck
				Deck.deckCount++;
				//Calculate the total points of the dealer
				dealerPoint = Deck.sumPoint(dealer);
				//Dealer burst check
				if (Deck.isBusted(dealerPoint)) {
					System.out.println("The dealer has burst. You win!");
					return;
				}
			}
		}
		//Compare points
		System.out.println("Your point is" + playerPoint);
		System.out.println("The point of the dealer is" + dealerPoint);
		if (playerPoint == dealerPoint) {
			System.out.println("It's a draw.");
		} else if (playerPoint > dealerPoint) {
			System.out.println("Won!");
		} else {
			System.out.println("lost···");
		}
	}
}
package blackjack;
import java.util.Collections;
import java.util.List;
public class Deck {
	static int deckCount; //Define a variable deckCount that records the progress of the deck
	//A method to put a value in the deck and shuffle it
	public static void shuffleDeck(List<Integer> deck) {
		//1 on the list-Substitute 52 serial numbers
		for (int i = 1; i <= 52; i++) {
			deck.add(i);
		}
		//Shuffle the deck
		Collections.shuffle(deck);
	}
	//Method to calculate current total points
	public static int sumPoint(List<Integer> list) {
		int sum = 0;
		for (int i = 0; i < list.size(); i++) {
			sum = sum + toPoint(toNumber(list.get(i)));
		}
		return sum;
	}
	//A method that replaces the number of decks with the number of cards
	private static int toNumber(int cardNumber) {
		int number = cardNumber % 13;
		if (number == 0) {
			number = 13;
		}
		return number;
	}
	//Method J that converts the serial number of the deck into points for score calculation/Q/K is 10
	public static int toPoint(int num) {
		if (num == 11 || num == 12 || num == 13) {
			num = 10;
		}
		return num;
	}
	//A method that replaces the number of decks with the (rank) string of (suit)
	public static String toDescription(int cardNumber) {
		String rank = toRank(toNumber(cardNumber));
		String suit = toSuit(cardNumber);
		return suit + "of" + rank;
	}
	//Method to convert card number to rank (A,J,Q,K etc.)
	private static String toRank(int number) {
		switch (number) {
		case 1:
			return "A";
		case 11:
			return "J";
		case 12:
			return "Q";
		case 13:
			return "K";
		default:
			String str = String.valueOf(number);
			return str;
		}
	}
	//Method to replace the number of decks with suits
	private static String toSuit(int cardNumber) {
		switch ((cardNumber - 1) / 13) {
		case 0:
			return "club";
		case 1:
			return "Diamond";
		case 2:
			return "heart";
		case 3:
			return "spade";
		default:
			return "Exception";
		}
	}
	//Method to determine if your hand is bursting
	public static boolean isBusted(int point) {
		if (point <= 21) {
			return false;
		} else {
			return true;
		}
	}
}
package blackjack;
public class Player extends Deck {
	private static int playerHands; //Define the variable playerHands that records the number of cards in the player's hand
	public static int getPlayerHands() {
		return playerHands;
	}
	public static void setPlayerHands(int playerHands) {
		Player.playerHands = playerHands;
	}
}
I looked it up on the internet, but I felt the limit. .. .. Please teach me if you like.
Recommended Posts