[JAVA] I want to call a method and count the number

The result I want to output to the console is below

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 ...

The current situation is as follows

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

I will put the source code

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

I want to call a method and count the number
I want to call a method of another class
I want to call the main method using reflection
[Ruby] I want to do a method jump!
I want to pass the argument of Annotation and the argument of the calling method to aspect
[Rough commentary] I want to marry the pluck method
I want to add a delete function to the comment function
I want to recursively get the superclass and interface of a certain class
[Java] I want to convert a byte array to a hexadecimal number
I want to bring Tomcat to the server and start the application
I want to expand the clickable part of the link_to method
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried to explain the method
I want to create a form to select the [Rails] category
I want to use the sanitize method other than View.
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I want to give a class name to the select attribute
I want to get a list of the contents of a zip file and its uncompressed size
I want to give edit and delete permissions only to the poster
I want to create a chat screen for the Swift chat app!
Method to add the number of years and get the end of the month
[Java] I tried to make a maze by the digging method ♪
I want to write a nice build.gradle
I wanted to add @VisibleForTesting to the method
I was addicted to the roll method
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
[Active Admin] I want to customize the default create and update processing
[Ruby] I want to extract only the value of the hash and only the key
[Spring Boot] I stumbled upon a method call count test (Spock framework)
I want to output the day of the week
I want to graph the number of photo AC downloads [MySQL ring cooperation] ~ Coding 10 lines a day ~
I want to var_dump the contents of the intent
I want to download a file on the Internet using Ruby and save it locally (with caution)
I want to simply write a repeating string
When you want to use the method outside
I want to design a structured exception handling
I want to truncate after the decimal point
I want to get the value in Ruby
If it is Ruby, it is efficient to make it a method and stock the processing.
I want to control the start / stop of servers and databases with Alexa
I want to play a GIF image on the Andorid app (Java, Kotlin)
I want to separate the handling of call results according to the API caller (call trigger)
[JDBC ③] I tried to input from the main method using placeholders and arguments.
Count the number of occurrences of a string in Ruby
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I tried to decorate the simple calendar a little
I made a method to ask for Premium Friday
I want to judge the range using the monthly degree
I want to use a little icon in Rails
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
I want to monitor a specific file with WatchService
I want to define a function in Rails Console
How to mock a super method call in PowerMock
I want to transition screens with kotlin and java!
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
I want to be aware of the contents of variables!