1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ブラックジャック実装

Last updated at Posted at 2024-02-01

ブラックジャックをやったことがないから、そもそもルールをしらない...

掛け金の上限は設定してなくて、プレイヤー側が負けても損失はでない
優しいブラックジャックにしました。

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("賭けますか? yes:y or no:n");
		String select = scan.next();
		
		int bet =0;
		if("y".equals(select)) {
			System.out.println("賭け額を入力してください:");
			int betNum = scan.nextInt();
			bet = betNum;
			
		}
		
		System.out.println("ゲームスタート");

		List<Integer> deck = new ArrayList<>(52);

		shuffleDeck(deck);

		List<Integer> player = new ArrayList<>();
		List<Integer> dealer = new ArrayList<>();

		player.add(deck.get(0));
		dealer.add(deck.get(1));
		player.add(deck.get(2));
		dealer.add(deck.get(3));

		int deckCount = 4;
		int playerHands = 2;

		System.out.println("あなたの1枚目のカード" + toDescription(player.get(0)));
		System.out.println("ディーラーの1枚目のカード" + toDescription(dealer.get(0)));
		System.out.println("あなたの2枚目のカード" + toDescription(player.get(1)));
		System.out.println("ディーラーの二枚目は秘密です");

		int playerPoint = sumPoint(player);
		int dealerPoint = sumPoint(dealer);

		System.out.println("playerPoint" + playerPoint);
		System.out.println("dealerPoint" + dealerPoint);

		while (true) {

			System.out.println("カードを引きますかyes:y or no:n");
			String str = scan.next();

			if ("n".equals(str)) {
				break;
			} else if ("y".equals(str)) {
				player.add(deck.get(deckCount));
				deckCount++;
				playerHands++;

				System.out.println("あなたの" + playerHands + "枚目のカード" + player.get(playerHands - 1));
				playerPoint = sumPoint(player);
				System.out.println("playerPoint" + playerPoint);

				if (isBusted(playerPoint)) {
					System.out.println("バーストしてしまいました");
					return;
				}
			}
		}
		while (true) {
			if (dealerPoint >= 17) {
				break;
			} else if (dealerPoint <= 17) {
				dealer.add(deck.get(deckCount));
				deckCount++;
				dealerPoint = sumPoint(dealer);
			}
			
			if (isBusted(dealerPoint)) {
				System.out.println("ディーラーがバーストしました。あなたの勝ちです");
				bet *= 5;
				System.out.println("配当金"+bet);
				return;
			}
		}
		
		System.out.println("playerPoint" + playerPoint);
		System.out.println("dealerPoint" + dealerPoint);
		
		if(playerPoint-dealerPoint>=5) {
			bet *= 10;
		}else if(playerPoint-dealerPoint>=10) {
			bet *= 100;
		}else if(playerPoint-dealerPoint<5) {
			bet *= 2;
		}
		
		if(playerPoint == dealerPoint) {
			System.out.println("ひきわけ");
		}else if(playerPoint > dealerPoint) {
			System.out.println("あなたのかち");
			System.out.println("配当金"+bet);
		}else if(playerPoint<dealerPoint) {
			System.out.println("ディーラーのかち");
		}
		
		
		
		

	}

	public static void shuffleDeck(List<Integer> deck) {

		for (int i = 1; i <= 52; i++) {
			deck.add(i);
		}

		Collections.shuffle(deck);

		//		for(int j = 0;j<deck.size();j++) {
		//			System.out.println(deck.get(j));
		//		}

	}

	public static String toSuit(int cardNumber) {

		switch ((cardNumber - 1) / 13) {

		case 0:
			return "♣";

		case 1:
			return "♦";

		case 2:
			return "♥";

		case 3:
			return "♠";

		default:
			return "例外です";

		}
	}

	public 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;

		}
	}

	public static int toNumber(int cardNumber) {

		int number = cardNumber % 13;

		if (number % 13 == 0) {

			number = 13;
		}
		return number;

	}

	public static String toDescription(int cardNumber) {

		String rank = toRank(toNumber(cardNumber));
		String suit = toSuit(cardNumber);

		return suit + "の" + rank;
	}

	public static int point(int number) {

		if (number == 11 || number == 12 || number == 13) {

			number = 10;

		}
		return number;
	}

	public static int sumPoint(List<Integer> list) {
		int sum = 0;

		for (int i = 0; i < list.size(); i++) {
			sum += point(toNumber(list.get(i)));
		}
		return sum;
	}

	public static boolean isBusted(int point) {

		if (point <= 21) {
			return false;
		} else {
			return true;
		}
	}


}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?