Last active
November 19, 2021 06:15
-
-
Save shiracamus/e4464c0a4a0b074b8409843917f57610 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Collections; | |
import java.util.stream.Collectors; | |
import java.util.Scanner; | |
public class BlackJack { | |
public static void main(String[] args) { | |
System.out.println("ブラックジャックゲーム"); | |
Deck deck = new Deck(); | |
Dealer dealer = new Dealer(); | |
Player player = Human.create(); | |
dealer.deal(deck, player); | |
} | |
} | |
abstract class Player { | |
protected final String name; | |
protected final List<Card> cards; | |
public Player(String name) { | |
this.name = name; | |
this.cards = new ArrayList<>(); | |
} | |
public void hold(Card card) { | |
cards.add(card); | |
} | |
public abstract boolean hit(); | |
public int score() { | |
int score = cards.stream().mapToInt(card -> Math.min(card.rank.number, 10)).sum(); | |
return score <= 11 && hasAce() ? score + 10 : score; | |
} | |
private boolean hasAce() { | |
return cards.stream() | |
.filter(card -> card.isAce()) | |
.findFirst() | |
.isPresent(); | |
} | |
public boolean isBust() { | |
return score() > 21; | |
} | |
public boolean isStrongerThan(Player other) { | |
return !isBust() && (other.isBust() || score() > other.score()); | |
} | |
public void showCards() { | |
System.out.println(name + "の手:" + | |
cards.stream() | |
.map(card -> card.toString()) | |
.collect(Collectors.joining("、"))); | |
System.out.println(name + "の点数: " + score()); | |
} | |
} | |
class Human extends Player { | |
private static final Scanner scanner = new Scanner(System.in); | |
public Human(String name) { | |
super(name); | |
} | |
public static Human create() { | |
System.out.print("名前を入力してください > "); | |
return new Human(scanner.nextLine()); | |
} | |
@Override | |
public boolean hit() { | |
showCards(); | |
while (isBust()) { | |
return false; | |
} | |
System.out.print("もう一枚挽くなら y 、ひかないなら n を入力してください > "); | |
while (true) { | |
String line = scanner.nextLine(); | |
if (line.equals("y")) { | |
return true; | |
} else if (line.equals("n")) { | |
return false; | |
} | |
System.out.println("y か n を入力してください"); | |
} | |
} | |
} | |
class Dealer extends Player { | |
public Dealer() { | |
super("ディーラー"); | |
} | |
public boolean hit() { | |
showCards(); | |
return score() < 17; // 合計点が17未満ならカードが欲しい | |
} | |
public void deal(Deck deck, Player player) { | |
player.hold(deck.draw()); | |
player.hold(deck.draw()); | |
this.hold(deck.draw()); | |
this.hold(deck.draw()); | |
System.out.println(name + "のカード1枚目: " + cards.get(0)); | |
while (player.hit()) { | |
player.hold(deck.draw()); | |
} | |
while (this.hit()) { | |
this.hold(deck.draw()); | |
} | |
judge(player); | |
} | |
private void judge(Player player) { | |
if (this.isStrongerThan(player)) { | |
System.out.println(this.name + "の勝ちです!"); | |
} else if (player.isStrongerThan(this)) { | |
System.out.println(player.name + "の勝ちです!"); | |
} else { | |
System.out.println("引き分けです。"); | |
} | |
} | |
} | |
class Deck { | |
private final List<Card> cards = new ArrayList<>(); | |
public Deck() { | |
for (Suit suit: Suit.values()) { | |
for (int number = 1; number <= 13; number++) { | |
cards.add(new Card(suit, new Rank(number))); | |
} | |
} | |
Collections.shuffle(cards); | |
} | |
public Card draw() { | |
return cards.remove(0); | |
} | |
} | |
class Card { | |
public final Suit suit; | |
public final Rank rank; | |
public final String label; | |
public Card(Suit suit, Rank rank) { | |
this.suit = suit; | |
this.rank = rank; | |
this.label = suit.label + "の" + rank.label; | |
} | |
@Override | |
public String toString() { | |
return this.label; | |
} | |
boolean isAce() { | |
return this.rank.label.equals("A"); | |
} | |
} | |
enum Suit { | |
SPADE("スペード"), | |
HEART("ハート"), | |
DIAMOND("ダイヤ"), | |
CLUB("クラブ"); | |
public final String label; | |
Suit(final String label) { | |
this.label = label; | |
} | |
} | |
class Rank { | |
public static final String[] LABELS = { | |
"?", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" | |
}; | |
public final int number; | |
public final String label; | |
Rank(int number) { | |
if (number < 1 || number > 13) { | |
throw new IllegalArgumentException(); | |
} | |
this.number = number; | |
this.label = LABELS[number]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment