Make Blackjack in Java

TL;DR I recreated the blackjack created with TypeScript in Java during the Golden Week period last year, so that work memo.

Public repository

GitHub

doxygen documentation

doxygen

Various classes

Card

////////////////////////////////////////////////////////////////////////////////
///	@file			Card.java
///	@brief card class
///	@author			Yuta Yoshinaga
///	@date			2019.04.27
///	$Version:		$
///	$Revision:		$
///
/// (c) 2019 Yuta Yoshinaga.
///
/// -Copying (copying) part or all of this software without permission
///This is a copyright infringement and is prohibited.
/// -Regarding infringement or infringement of patent rights or other rights caused by the use of this product
///We do not take any responsibility.
///
////////////////////////////////////////////////////////////////////////////////

package jp.gr.java_conf.yuta_yoshinaga.java_trumpcards;

////////////////////////////////////////////////////////////////////////////////
///	@class		Card
///	@brief card class
///
////////////////////////////////////////////////////////////////////////////////
public class Card {
	public static final int DEF_CARD_TYPE_JOKER = 0;
	public static final int DEF_CARD_TYPE_SPADE = 1;
	public static final int DEF_CARD_TYPE_CLOVER = 2;
	public static final int DEF_CARD_TYPE_HEART = 3;
	public static final int DEF_CARD_TYPE_DIAMOND = 4;
	public static final int DEF_CARD_TYPE_MIN = DEF_CARD_TYPE_JOKER;
	public static final int DEF_CARD_TYPE_MAX = DEF_CARD_TYPE_DIAMOND;

	public static final int DEF_CARD_VALUE_JOKER = 0;
	public static final int DEF_CARD_VALUE_MIN = 0;
	public static final int DEF_CARD_VALUE_MAX = 13;

	private int type; //!<Card type
	private int value; //!<Card value
	private boolean drowFlag; //!<Card payout flag
	private String ext; //!<Card expansion information, etc.(When sending a message for each card, etc.)

	////////////////////////////////////////////////////////////////////////////////
	///	@brief constructor
	///	@fn				public Card()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public Card() {
		this.type = DEF_CARD_TYPE_JOKER;
		this.value = DEF_CARD_VALUE_JOKER;
		this.drowFlag = false;
		this.ext = "";
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public int getType()
	///	@return card type
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int getType() {
		return type;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setType(int type)
	///	@param[in]int type card type
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setType(int type) {
		if(DEF_CARD_TYPE_MIN <= type && type <= DEF_CARD_TYPE_MAX){
			this.type = type;
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public getValue(): number
	///	@return card value
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int getValue() {
		return value;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setValue(int value)
	///	@param[in]int value card value
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setValue(int value) {
		if(DEF_CARD_VALUE_MIN <= value && value <= DEF_CARD_VALUE_MAX){
			this.value = value;
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public boolean getDrowFlag()
	///	@return card payout flag
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public boolean getDrowFlag() {
		return drowFlag;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setDrowFlag(boolean drowFlag)
	///	@param[in]boolean drowFlag Card payout flag
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setDrowFlag(boolean drowFlag) {
		this.drowFlag = drowFlag;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public String getExt()
	///	@return card payout flag
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public String getExt() {
		return ext;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setExt(String ext)
	///	@param[in]String ext card extension information, etc.(When sending a message for each card, etc.)
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setExt(String ext) {
		this.ext = ext;
	}

}

A class that holds playing card information.

TrumpCards

////////////////////////////////////////////////////////////////////////////////
///	@file			TrumpCards.java
///	@brief playing card class
///	@author			Yuta Yoshinaga
///	@date			2019.04.27
///	$Version:		$
///	$Revision:		$
///
/// (c) 2019 Yuta Yoshinaga.
///
/// -Copying (copying) part or all of this software without permission
///This is a copyright infringement and is prohibited.
/// -Regarding infringement or infringement of patent rights or other rights caused by the use of this product
///We do not take any responsibility.
///
////////////////////////////////////////////////////////////////////////////////
package jp.gr.java_conf.yuta_yoshinaga.java_trumpcards;

import java.util.ArrayList;
import java.util.Collections;

////////////////////////////////////////////////////////////////////////////////
///	@class		TrumpCards
///	@brief playing card class
///
////////////////////////////////////////////////////////////////////////////////
public class TrumpCards {
	public static final int DEF_CARD_CNT = (13 * 4);
	private ArrayList<Card> deck; //!<Deck
	private int deckDrowCnt; //!<Number of cards distributed
	private int deckCnt; //!<Number of decks

	////////////////////////////////////////////////////////////////////////////////
	///	@brief constructor
	///	@fn				public TrumpCards(int jokerCnt)
	///	@param[in]int jokerCnt Number of jokers
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public TrumpCards(int jokerCnt) {
		this.deckCnt = DEF_CARD_CNT + jokerCnt;
		this.cardsInit();
		this.deckInit();
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public ArrayList<Card> getDeck()
	///	@return deck
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public ArrayList<Card> getDeck() {
		return deck;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setDeck(ArrayList<Card> deck)
	///	@param[in]		ArrayList<Card>deck deck
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setDeck(ArrayList<Card> deck) {
		this.deck = deck;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public int getDeckDrowCnt()
	///	@return Number of cards dealt
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int getDeckDrowCnt() {
		return deckDrowCnt;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setDeckDrowCnt(int deckDrowCnt)
	///	@param[in]int deckDrowCnt Number of decks dealt
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setDeckDrowCnt(int deckDrowCnt) {
		this.deckDrowCnt = deckDrowCnt;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public int getDeckCnt()
	///	@return Number of decks
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int getDeckCnt() {
		return deckCnt;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setDeckCnt(int deckCnt)
	///	@param[in]int deckCnt Number of decks
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setDeckCnt(int deckCnt) {
		this.deckCnt = deckCnt;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief card initialization
	///	@fn				private void cardsInit()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	private void cardsInit() {
		this.deck = new ArrayList<Card>();
		for (int i = 0; i < this.deckCnt; i++) {
			Card curCard = new Card();
			curCard.setDrowFlag(false);
			if (0 <= i && i <= 12) {
				// ***spade*** //
				curCard.setType(Card.DEF_CARD_TYPE_SPADE);
				curCard.setValue(i + 1);
			} else if (13 <= i && i <= 25) {
				// ***Clover*** //
				curCard.setType(Card.DEF_CARD_TYPE_CLOVER);
				curCard.setValue((i - 13) + 1);
			} else if (26 <= i && i <= 38) {
				// ***heart*** //
				curCard.setType(Card.DEF_CARD_TYPE_HEART);
				curCard.setValue((i - 26) + 1);
			} else if (39 <= i && i <= 51) {
				// ***Diamond*** //
				curCard.setType(Card.DEF_CARD_TYPE_DIAMOND);
				curCard.setValue((i - 39) + 1);
			} else {
				// ***Joker*** //
				curCard.setType(Card.DEF_CARD_TYPE_JOKER);
				curCard.setValue((i - 52) + 1);
			}
			this.deck.add(curCard);
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Initialization of deck
	///	@fn				private void deckInit()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	private void deckInit() {
		this.deckDrowFlagInit();
		this.deckDrowCnt = 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Initialize deck draw flag
	///	@fn				private deckDrowFlagInit(): void
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	private void deckDrowFlagInit() {
		for (int i = 0; i < this.deckCnt; i++) {
			this.deck.get(i).setDrowFlag(false);
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Deck shuffle
	///	@fn				public shuffle(): void
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void shuffle() {
		Collections.shuffle(this.deck);
		this.deckDrowFlagInit();
		this.deckDrowCnt = 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Distribute the deck
	///	@fn				public drowCard(): Card
	///	@return card class
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public Card drowCard() {
		Card res = null;
		if (this.deckDrowCnt < this.deckCnt) {
			this.deck.get(this.deckDrowCnt).setDrowFlag(true);
			res = this.deck.get(this.deckDrowCnt++);
		}
		return res;
	}

}

A class that manages decks of playing cards.

Player

////////////////////////////////////////////////////////////////////////////////
///	@file			Player.java
///	@brief player class
///	@author			Yuta Yoshinaga
///	@date			2019.04.27
///	$Version:		$
///	$Revision:		$
///
/// (c) 2019 Yuta Yoshinaga.
///
/// -Copying (copying) part or all of this software without permission
///This is a copyright infringement and is prohibited.
/// -Regarding infringement or infringement of patent rights or other rights caused by the use of this product
///We do not take any responsibility.
///
////////////////////////////////////////////////////////////////////////////////
package jp.gr.java_conf.yuta_yoshinaga.java_trumpcards;

import java.util.ArrayList;

////////////////////////////////////////////////////////////////////////////////
///	@class		Player
///	@brief player class
///
////////////////////////////////////////////////////////////////////////////////
public class Player {
	private ArrayList<Card> cards; //!<Player card
	private int cardsCnt; //!<Number of player cards
	private int score; //!<Player score

	////////////////////////////////////////////////////////////////////////////////
	///	@brief constructor
	///	@fn				public Player()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public Player() {
		this.cards = new ArrayList<Card>();
		this.cardsCnt = 0;
		this.score = 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				ArrayList<Card> getCards()
	///	@return player card
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public ArrayList<Card> getCards() {
		return cards;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setCards(ArrayList<Card> cards)
	///	@param[in]		ArrayList<Card>cards player cards
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setCards(ArrayList<Card> cards) {
		this.cards = cards;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public int getCardsCnt()
	///	@return Number of player cards
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int getCardsCnt() {
		return cardsCnt;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setCardsCnt(int cardsCnt)
	///	@param[in]int cardsCnt number of player cards
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setCardsCnt(int cardsCnt) {
		this.cardsCnt = cardsCnt;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public int getScore()
	///	@return player score
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int getScore() {
		return score;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setScore(int score)
	///	@param[in]int score player score
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setScore(int score) {
		this.score = score;
	}

}

A class that holds blackjack player information

BlackJack

////////////////////////////////////////////////////////////////////////////////
///	@file			BlackJack.java
///	@brief blackjack class
///	@author			Yuta Yoshinaga
///	@date			2019.04.27
///	$Version:		$
///	$Revision:		$
///
/// (c) 2019 Yuta Yoshinaga.
///
/// -Copying (copying) part or all of this software without permission
///This is a copyright infringement and is prohibited.
/// -Regarding infringement or infringement of patent rights or other rights caused by the use of this product
///We do not take any responsibility.
///
////////////////////////////////////////////////////////////////////////////////
package jp.gr.java_conf.yuta_yoshinaga.java_trumpcards;

import java.util.ArrayList;

////////////////////////////////////////////////////////////////////////////////
///	@class		BlackJack
///	@brief blackjack class
///
////////////////////////////////////////////////////////////////////////////////
public class BlackJack {
	public static final int DEF_SHUFFLE_CNT = 10;
	private TrumpCards trumpCards; //!<Playing cards
	private Player player; //!<player
	private Player dealer; //!<dealer
	private boolean gameEndFlag; //!<Game end flag

	////////////////////////////////////////////////////////////////////////////////
	///	@brief constructor
	///	@fn				public BlackJack()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public BlackJack() {
		this.trumpCards = new TrumpCards(0);
		this.player = new Player();
		this.dealer = new Player();
		this.gameInit();
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				TrumpCards getTrumpCards()
	///	@return playing cards
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public TrumpCards getTrumpCards() {
		return trumpCards;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setTrumpCards(TrumpCards trumpCards)
	///	@param[in]TrumpCards trumpCards playing cards
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setTrumpCards(TrumpCards trumpCards) {
		this.trumpCards = trumpCards;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public Player getPlayer()
	///	@return player
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public Player getPlayer() {
		return player;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setPlayer(Player player)
	///	@param[in]Player player player
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setPlayer(Player player) {
		this.player = player;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public Player getDealer()
	///	@return dealer
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public Player getDealer() {
		return dealer;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setDealer(Player dealer)
	///	@param[in]Player dealer
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setDealer(Player dealer) {
		this.dealer = dealer;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief getter
	///	@fn				public boolean getGameEndFlag()
	///	@return dealer
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public boolean getGameEndFlag() {
		return gameEndFlag;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief setter
	///	@fn				public void setGameEndFlag(boolean gameEndFlag)
	///	@param[in]boolean gameEndFlag Game end flag
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void setGameEndFlag(boolean gameEndFlag) {
		this.gameEndFlag = gameEndFlag;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Game initialization
	///	@fn				public void gameInit()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void gameInit() {
		this.gameEndFlag = false;
		// ***Deck shuffle*** //
		for (int i = 0; i < DEF_SHUFFLE_CNT; i++) {
			this.trumpCards.shuffle();
		}
		// ***Player / Dealer Initialization*** //
		this.player.setCards(new ArrayList<Card>());
		this.player.setCardsCnt(0);
		this.dealer.setCards(new ArrayList<Card>());
		this.dealer.setCardsCnt(0);
		// ***Deal 2 players / dealers each*** //
		for (int i = 0; i < 2; i++) {
			this.player.getCards().add(this.trumpCards.drowCard());
			this.player.setCardsCnt(this.player.getCardsCnt() + 1);
			this.dealer.getCards().add(this.trumpCards.drowCard());
			this.dealer.setCardsCnt(this.dealer.getCardsCnt() + 1);
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Player hit
	///	@fn				public void playerHit()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void playerHit() {
		if (this.gameEndFlag == false) {
			this.player.getCards().add(this.trumpCards.drowCard());
			this.player.setCardsCnt(this.player.getCardsCnt() + 1);
			int score = this.getScore(this.player.getCards(), this.player.getCardsCnt());
			if (22 <= score)
				this.playerStand(); //Forced termination because it burst
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief player stand
	///	@fn				public void playerStand()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void playerStand() {
		this.dealerHit();
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Dealer hit
	///	@fn				private void dealerHit()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	private void dealerHit() {
		for (;;) {
			int score = this.getScore(this.dealer.getCards(), this.dealer.getCardsCnt());
			if (score < 17) {
				// ***The dealer will have until the total number of cards in his possession is "17" or higher.*** //
				// ***Keep hitting (keep drawing cards)*** //
				this.dealer.getCards().add(this.trumpCards.drowCard());
				this.dealer.setCardsCnt(this.dealer.getCardsCnt() + 1);
			} else {
				// ***Dealers should have a total of "17" or more on their cards*** //
				// ***Stay (do not draw a card).*** //
				this.dealerStand();
				break;
			}
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief dealer stand
	///	@fn				private void dealerStand()
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	private void dealerStand() {
		this.gameEndFlag = true;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Get the current score from your hand
	///	@fn				public int getScore(ArrayList<Card> cards,int cardsCnt)
	///	@param[in]		ArrayList<Card>cards hand
	///	@param[in]int cardsCnt Number of cards in hand
	///	@return current score
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int getScore(ArrayList<Card> cards, int cardsCnt) {
		int res = 0;
		boolean aceFlag = false;
		for (int i = 0; i < cardsCnt; i++) {
			if (2 <= cards.get(i).getValue() && cards.get(i).getValue() <= 10) {
				// *** 2~10 *** //
				res += cards.get(i).getValue();
			} else if (11 <= cards.get(i).getValue() && cards.get(i).getValue() <= 13) {
				// *** 11~13 *** //
				res += 10;
			} else {
				if (aceFlag) {
					// ***The second ace is forcibly converted to 1.*** //
					res += 1;
				} else {
					// ***Ace will be calculated later*** //
					aceFlag = true;
				}
			}
		}
		if (aceFlag) {
			// ***Ace calculation*** //
			var tmpScore1 = res + 1;
			var tmpScore2 = res + 11;
			var diff1 = 21 - tmpScore1;
			var diff2 = 21 - tmpScore2;
			if ((22 <= tmpScore1) && (22 <= tmpScore2)) {
				// ***1 ace if both are bursting*** //
				res = tmpScore1;
			} else if ((22 <= tmpScore1) && (tmpScore2 <= 21)) {
				// ***If the ace is bursting at 1, then the ace is 11*** //
				res = tmpScore2;
			} else if ((tmpScore1 <= 21) && (22 <= tmpScore2)) {
				// ***If the ace is bursting at 11, 1 ace*** //
				res = tmpScore1;
			} else {
				// ***If neither is bursting, use the one with the smaller difference from 21.*** //
				if (diff1 < diff2)
					res = tmpScore1;
				else
					res = tmpScore2;
			}
		}
		return res;
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Game win / loss judgment
	///	@fn				public int gameJudgment()
	///	@return Game win / loss judgment
	///					- 1 :Player victory
	///					- 0 :draw
	///					- -1 :Player defeat
	///
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public int gameJudgment() {
		int res = 0;
		int score1 = this.getScore(this.player.getCards(), this.player.getCardsCnt());
		int score2 = this.getScore(this.dealer.getCards(), this.dealer.getCardsCnt());
		int diff1 = 21 - score1;
		int diff2 = 21 - score2;
		if (22 <= score1 && 22 <= score2) {
			// ***Losing because both players and dealers are bursting*** //
			res = -1;
		} else if (22 <= score1 && score2 <= 21) {
			// ***Losing because the player is bursting*** //
			res = -1;
		} else if (score1 <= 21 && 22 <= score2) {
			// ***Win because the dealer is bursting*** //
			res = 1;
		} else {
			if (diff1 == diff2) {
				// ***Draw if the score is the same*** //
				res = 0;
				if (score1 == 21 && this.player.getCardsCnt() == 2 && this.dealer.getCardsCnt() != 2) {
					// ***If only the player is pure blackjack, the player wins*** //
					res = 1;
				}
			} else if (diff1 < diff2) {
				// ***The player is closer to 21 so he wins*** //
				res = 1;
			} else {
				// ***The dealer is closer to 21, so I lose*** //
				res = -1;
			}
		}
		return res;
	}

}

Blackjack A class that manages the game and player status.

BlackJackMain

////////////////////////////////////////////////////////////////////////////////
///	@file			BlackJackMain.java
///	@brief blackjack main class
///	@author			Yuta Yoshinaga
///	@date			2019.04.27
///	$Version:		$
///	$Revision:		$
///
/// (c) 2019 Yuta Yoshinaga.
///
/// -Copying (copying) part or all of this software without permission
///This is a copyright infringement and is prohibited.
/// -Regarding infringement or infringement of patent rights or other rights caused by the use of this product
///We do not take any responsibility.
///
////////////////////////////////////////////////////////////////////////////////
package jp.gr.java_conf.yuta_yoshinaga.java_trumpcards;

import java.util.ArrayList;
import java.util.Scanner;

////////////////////////////////////////////////////////////////////////////////
///	@class		BlackJackMain
///	@brief blackjack main class
///
////////////////////////////////////////////////////////////////////////////////
public class BlackJackMain {

	////////////////////////////////////////////////////////////////////////////////
	///	@brief main method
	///	@fn				public static void main(String[] args)
	///	@param[in]		String[] args
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public static void main(String[] args) {
		BlackJackMain blackJackMain = new BlackJackMain();
		BlackJack blackJack = new BlackJack();
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.println("Please enter a command.");
			System.out.println("q ・ ・ ・ quit");
			System.out.println("r ・ ・ ・ reset");
			System.out.println("h ・ ・ ・ hit");
			System.out.println("s ・ ・ ・ stand");
			blackJackMain.showStatus(blackJack);
			String inputStr = sc.nextLine();
			switch (inputStr) {
			case "q":
			case "quit":
				// quit
				System.out.println("bye.");
				sc.close();
				System.exit(0);
				break;
			case "r":
			case "reset":
				// reset
				blackJack.gameInit();
				break;
			case "h":
			case "hit":
				// hit
				blackJack.playerHit();
				break;
			case "s":
			case "stand":
				// stand
				blackJack.playerStand();
				break;
			default:
				// Unsupported command
				System.out.println("Unsupported command.");
				break;
			}
		}

	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief status display
	///	@fn				public void showStatus(BlackJack blackJack)
	///	@param[in]		BlackJack blackJack
	///	@no return
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public void showStatus(BlackJack blackJack) {
		System.out.println("----------");
		// dealer
		ArrayList<Card> dc = blackJack.getDealer().getCards();
		int dcc = blackJack.getDealer().getCardsCnt();
		System.out.println("dealer score " + (blackJack.getGameEndFlag() ? blackJack.getScore(dc, dcc) : ""));
		String cardStr = "";
		if (blackJack.getGameEndFlag()) {
			for (int i = 0; i < dcc; i++) {
				if (i != 0)
					cardStr += ",";
				cardStr += getCardStr(dc.get(i));
			}
		} else {
			cardStr = getCardStr(dc.get(0)) + ",*";
		}
		System.out.println(cardStr);
		System.out.println("----------");
		// player
		ArrayList<Card> pc = blackJack.getPlayer().getCards();
		int pcc = blackJack.getPlayer().getCardsCnt();
		System.out.println("player score " + blackJack.getScore(pc, pcc));
		cardStr = "";
		for (int i = 0; i < pcc; i++) {
			if (i != 0)
				cardStr += ",";
			cardStr += getCardStr(pc.get(i));
		}
		System.out.println(cardStr);
		System.out.println("----------");
		if (blackJack.getGameEndFlag()) {
			if (blackJack.gameJudgment() == 1) {
				System.out.println("You are the winner.");
			} else if (blackJack.gameJudgment() == 0) {
				System.out.println("It is a draw.");
			} else {
				System.out.println("It is your loss.");
			}
			System.out.println("----------");
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	///	@brief Get card information string
	///	@fn				public String getCardStr(Card card)
	///	@param[in]		Card card
	///	@return Card information string acquisition
	///	@author			Yuta Yoshinaga
	///	@date			2019.04.27
	///
	////////////////////////////////////////////////////////////////////////////////
	public String getCardStr(Card card) {
		String res = "";
		switch (card.getType()) {
		case Card.DEF_CARD_TYPE_SPADE:
			res = "SPADE ";
			break;
		case Card.DEF_CARD_TYPE_CLOVER:
			res = "CLOVER ";
			break;
		case Card.DEF_CARD_TYPE_HEART:
			res = "HEART ";
			break;
		case Card.DEF_CARD_TYPE_DIAMOND:
			res = "DIAMOND ";
			break;
		default:
			res = "Unsupported card";
			break;
		}
		res = res + card.getValue();
		return res;
	}
}

A class for playing blackjack games on the console. Take the corresponding action in response to input from the console.

Impressions

--The class was designed to be as object-oriented as possible. ――Since I am currently involved in a Java console application, I created it with a console application. --If possible, I would like to make a Servlet version and make it a Web application in the future. ――On the first day of the GW10 consecutive holidays, I started work with a strange tension that made me take a nap too much and couldn't sleep at night. ――Working time is about 2 hours. It's good because you can easily create a console application.

Recommended Posts

Make Blackjack in Java
Refactoring: Make Blackjack in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Easy to make Slack Bot in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Make "I'm not a robot" in Java EE (Jakarta EE)
Java beginners make poker games in 4 days (3rd day)
[Personal memo] Make a simple deep copy in Java
I tried to make a login function in Java
Java beginners make poker games in 4 days (2nd day)
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
Java --How to make JTable
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①