Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step2 - 블랙젝 #672

Merged
merged 54 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
aabf9e4
Refactor: Skills, Language의 builder를 사용하여 가독성 향상
oyj7677 Nov 21, 2023
67bf9ca
Docs: 기능 목록 초기 구성
oyj7677 Nov 23, 2023
58a82f8
Refactor: Skills, language 구현 변경
oyj7677 Nov 23, 2023
15c50de
Feat: 카드 번호, 카드 무늬 유효성 검증 구현
oyj7677 Nov 23, 2023
6b33e03
Feat: 카드 포인트 반환 기능 구현, 카드 무늬 명 반환 기능 구현
oyj7677 Nov 23, 2023
b88d964
Feat: 카드 클래스 생성 및 카드 구성 데이터 캐싱
oyj7677 Nov 25, 2023
0d06426
Feat: 카드팩 클래스 생성 및 캐싱 구현
oyj7677 Nov 25, 2023
c1a5504
Docs: 블랙젝 게임 기능 추가 (카드팩 기능 이동), 플레이어 기능 목록 변경
oyj7677 Nov 25, 2023
257482f
Docs: 블랙젝 게임 기능 목록 추가.
oyj7677 Nov 25, 2023
e384c31
Feat: 블랙젝 게임 클래스 생성, 카드팩 검증 로직 구현(52장, 중복X)
oyj7677 Nov 25, 2023
683ccfd
Docs: 카드인텍스 기능 목록 추가.
oyj7677 Nov 25, 2023
d261000
Docs: 카드 인텍스 검증 기능 목록 추가
oyj7677 Nov 25, 2023
8aca69f
Feat: 카드 배포 구현
oyj7677 Nov 25, 2023
e897c27
Feat: 플레이어 상태 추가, 플레이어 상태 추가 및 변경 구현
oyj7677 Nov 25, 2023
18317d6
Feat: 플레이어 카드 저장 기능 구현
oyj7677 Nov 25, 2023
88e3b66
Refactor: CardNumber 명칭 추가, class PlayingCard @override toString(), 카…
oyj7677 Nov 25, 2023
c5b5adf
Refactor: 블랙젝 게임 디폴트 파라미터 추가 - playerList: List<Player>
oyj7677 Nov 25, 2023
aae5ec2
Docs: 블랙젝 게임 기능 추가, 블랙젝 계산기 추가.
oyj7677 Nov 26, 2023
84b7ea9
Feat: 카드 초기 세팅 기능 추가(게임 시작시 카드 2장 받음)
oyj7677 Nov 26, 2023
f4fef00
Refactor: CardIndex -> GameIndex로 역할 범위 확장. Index 값 생성자 파라미터 -> Backi…
oyj7677 Nov 27, 2023
231ba04
Refactor: 참여 가능 플레이어 조건 변경, 카드팩 검증 로직 역할 변경(BlackjackGame -> CardPack…
oyj7677 Nov 27, 2023
07f695a
Chore: 프로덕트 클래스, 테스트 클래스 패키지 구조 동기화
oyj7677 Nov 27, 2023
5b83654
Feat: 블랙젝 계산기 추가(승리에 유리한 계산 결과 반환)
oyj7677 Nov 27, 2023
00d42dd
Refactor: CardNumber파라미터 접근 제한 변경 private -> publid
oyj7677 Nov 27, 2023
18c0aea
Docs: 기능 목록 체크
oyj7677 Nov 27, 2023
eccd3a8
Refactor: GameIndex의 index 변수 명 변경 -> cardIndex
oyj7677 Nov 27, 2023
05dbca5
Feat: 게임 인덱스, 플레이어 인덱스 추가 및 증가 기능 구현
oyj7677 Nov 27, 2023
9280506
Feat: 플레이어 카드 저장 함수 추가
oyj7677 Nov 27, 2023
3cf1551
 Feat: 플레이어 상태 업데이트 기능 구현
oyj7677 Nov 27, 2023
d056703
Feat: PlayerGroup 일급 컬렉션 추가,
oyj7677 Nov 27, 2023
70a6c84
Refactor: 카드 반환 기능 CardPack으로 이동
oyj7677 Nov 27, 2023
a8acb07
Refactor: 카드 배포 역할 변경(BlackjackGame -> CardPack), 게임 진행 로직 변경
oyj7677 Nov 27, 2023
9c0d5e9
Refactor: 불필요한 코드 제거 및 수정
oyj7677 Nov 29, 2023
cfe4fde
Refactor
oyj7677 Nov 29, 2023
63171c3
Feat: CardDeck생성(카드 캐싱)
oyj7677 Nov 29, 2023
23fd469
Refactor:
oyj7677 Nov 29, 2023
5854bb5
Refactor:
oyj7677 Nov 29, 2023
335541a
Feat: 플레이어 카드 점수 계산 기능 추가.
oyj7677 Nov 30, 2023
a041fe2
Refactor:
oyj7677 Nov 30, 2023
2b1857d
chore
oyj7677 Nov 30, 2023
ac1c262
Refactor: 플레이어 상태 업데이트 로직 변경
oyj7677 Nov 30, 2023
c70c516
chore: GameDeckTest 주석 추가.
oyj7677 Nov 30, 2023
30d7837
Feat: Input, Output View 구현
oyj7677 Dec 2, 2023
01d4259
Feat: InputData to PlayerList 를 위한 플래이어 팩토리 구현
oyj7677 Dec 2, 2023
e8c4212
Docs: README.md 변경
oyj7677 Dec 2, 2023
9dd1499
Refactor: 게임 덱 셔플 기능 추가, 생성자 public으로 변경, 플레이어 덱 override toString()
oyj7677 Dec 2, 2023
07ed6eb
Refactor: 블랙잭 게임 로직 재구현
oyj7677 Dec 2, 2023
3bd337a
Refactor: 플레이어 관련 불필요한 함수 제거 및 매직 넘버 제거
oyj7677 Dec 2, 2023
fa7ef73
Feat: 메인 함수 구현
oyj7677 Dec 2, 2023
31afda1
style: 코드 컨벤션 정리 (feat: Gradle)
oyj7677 Dec 2, 2023
1c2fd96
Refactor: CardDeck interface 삭제, 플레이어 생성자 추가, 카드팩 재사용, 게임덱 리셋 기능 구현 등…
oyj7677 Dec 6, 2023
4396f52
Chore: 클래스 명 변경 GameDeck -> CardDeck
oyj7677 Dec 6, 2023
aacff6c
Refactor: ViewInterfacd생성 및 BlackjackGame 생성자 추가.
oyj7677 Dec 6, 2023
ff53d99
BlackjackGame 생성자로 ViewInterFace 추가.
oyj7677 Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
# kotlin-blackjack
# kotlin-blackjack

## 기능 요구 사항

블랙잭 게임을 변형한 프로그램을 구현한다. 블랙잭 게임은 딜러와 플레이어 중 카드의 합이 21 또는 21에 가장 가까운 숫자를 가지는 쪽이 이기는 게임이다.

- 카드의 숫자 계산은 카드 숫자를 기본으로 하며, 예외로 Ace는 1 또는 11로 계산할 수 있으며, King, Queen, Jack은 각각 10으로 계산한다.
- 게임을 시작하면 플레이어는 두 장의 카드를 지급 받으며, 두 장의 카드 숫자를 합쳐 21을 초과하지 않으면서 21에 가깝게 만들면 이긴다. 21을 넘지 않을 경우 원한다면 얼마든지 카드를 계속 뽑을 수 있다.

## 기능 목록

### 플레이어

- [O] 플레이어의 초기 상태는 START이다.
- [O] 플레이어가 카드 받는 것을 끝내면 상태는 STAND가 된다.
- [O] 플레이어는 받은 카드를 저장한다.
- [O] 플레이어는 상태를 업데이트 한다.
- [O] 카드의 합이 20 이하 라면 PLAYING
- [O] 카드의 합이 21을 이상 이라면 BUST
- [O] 카드를 받지 않는다 하면 STAND
- [O] 첫 2장의 카드 합이 21이라면 BLACK_JACK

### 플레이어 그룹

- [O] 플레이어는 최소 2명 이상 26명 이하여야 한다.

### 플레이어 펙토리
- [O] 문자열을 받아서 플레이어 리스트 생성.
- [O] 공백은 플레어어로 생성하지 않는다.

### 트럼프 카드(플레잉 카드)

- [O] 카드의 포인트를 반환한다.
- [O] 카드의 무늬 명을 반환한다.
- [O] 카드의 번호를 반환한다.

### 게임 덱
- [O] 카드를 순차적으로 반환한다.

### 플레이어 덱
- [O] 카드를 추가할 수 있다.
- [O] 점수를 반환한다.
- [O] A는 블랙잭에 유리하게 점수를 반영한다.

### inputView

- [O] 플레이어를 입력받는다.
- [O] 플레이어가 입력되지 않으면 예외를 던진다.

### output

- [O] 게임 시작 시 초기 카드 출력
- [O] 플레이어가 가지고 있는 카드를 출력
- [O] 플레이어 카드 계산값을 출력
60 changes: 60 additions & 0 deletions src/main/kotlin/BlackjackGame.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import card.deck.CardDeck
import player.Player
import player.PlayerGroup
import player.Status
import view.InputViewInterface
import view.OutputViewInterface

class BlackjackGame(
private val cardDeck: CardDeck,
val playerGroup: PlayerGroup,
private val inputView: InputViewInterface,
private val outputView: OutputViewInterface,
) {

init {
for (player in playerGroup.playerList) {
settingCard(player)
}
}

fun start() {
for (player in playerGroup.playerList) {
gamePlay(player)
}
}

private fun gamePlay(player: Player) {
while (player.status == Status.PLAYING) {
val response = inputView.askForHit(player.name)
handleForResponse(response, player)
outputView.showPlayingCard(player)
}
}

private fun handleForResponse(response: String, player: Player) {
when (response.uppercase()) {
Comment on lines +35 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response가 String으로 들어오는 것은 Console View의 의존성이 여전히 묻어나보여요.
입력 뷰가 조금 바뀌어서 y, n가 아닌, YES, NO 혹은 예,아니오로 변경된다면, 이 모든 String에서 검증해주어야할까요?

view에서는 예 혹은 아니오에 대한 도메인 객체를 반환하도록 만들어보는 건 어떨까요?

TEXT_ANSWER_YES -> {
player.saveCard(cardDeck.getCardWithIncrease())
}
TEXT_ANSWER_NO -> {
player.playDone()
}
else -> {
println(TEXT_RETRY_INPUT)
}
Comment on lines +43 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 여전히 Console View의 의존관계를 가지고있습니다 :)
output View에 대한 추상화를 만드셨으니 이쪽으로 옮겨주세요!

}
}

private fun settingCard(player: Player) {
repeat(2) {
player.saveCard(cardDeck.getCardWithIncrease())
}
}

companion object {
private const val TEXT_RETRY_INPUT = "Y 혹은 N만 입력 가능합니다."
private const val TEXT_ANSWER_YES = "Y"
private const val TEXT_ANSWER_NO = "N"
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/BlackjackMain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import card.CardPack
import card.deck.CardDeck
import player.PlayerFactory
import player.PlayerGroup
import view.InputView
import view.OutputView

fun main() {

val playerNames = InputView.inputPlayerName()
val playerGroup = PlayerGroup(PlayerFactory.createPlayerList(playerNames))
val cardDeck = CardDeck(CardPack.cards.toMutableList())

val game = BlackjackGame(cardDeck = cardDeck, playerGroup = playerGroup, InputView, OutputView)

OutputView.showGameStart(game.playerGroup)

game.start()

OutputView.showGameEnd(game.playerGroup)
}
9 changes: 9 additions & 0 deletions src/main/kotlin/card/CardPack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package card

object CardPack {
val cards: List<PlayingCard> = CardRank.values().flatMap { rank ->
Suit.values().map { suit ->
PlayingCard.of(suit = suit, cardRank = rank)
}
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/card/CardRank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package card

enum class CardRank(val point: Int, val symbol: String) {
ACE(1, "A"),
TWO(2, "2"),
TREE(3, "3"),
FOUR(4, "4"),
FIVE(5, "5"),
SIX(6, "6"),
SEVEN(7, "7"),
EIGHT(8, "8"),
NINE(9, "9"),
TEN(10, "10"),
JACK(10, "J"),
QUEEN(10, "Q"),
KING(10, "K");
}
35 changes: 35 additions & 0 deletions src/main/kotlin/card/PlayingCard.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package card

data class PlayingCard(val suit: Suit, val cardRank: CardRank) {
fun getPoint(): Int {
return cardRank.point
}

fun getSuitName(): String {
return suit.koName
}

override fun toString(): String {
return "${cardRank.symbol}${suit.koName}"
}

companion object {
private val CARDS: MutableMap<String, PlayingCard> = mutableMapOf()

fun of(suit: Suit, cardRank: CardRank): PlayingCard {
return CARDS[toKey(suit, cardRank)] ?: throw NoSuchElementException()
}

private fun toKey(suit: Suit, cardRank: CardRank): String {
return suit.name + cardRank.name
}

init {
for (suit in Suit.values()) {
for (cardRank in CardRank.values()) {
CARDS[toKey(suit, cardRank)] = PlayingCard(suit, cardRank)
}
}
}
Comment on lines +27 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코틀린 확장함수를 활용해보는 건 어떨까요?
람다로 표현된 식은 바로 멤벼변수에 할당해서, 멤버변수를 mutable로 유지하지 않을 수 있겠어요 :)

}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/card/Suit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package card
enum class Suit(val koName: String) {
SPADE("스페이드"),
HEART("하트"),
DIAMOND("다이아몬드"),
CLUB("클러버");
}
26 changes: 26 additions & 0 deletions src/main/kotlin/card/deck/CardDeck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package card.deck

import card.PlayingCard

class CardDeck(private val cardList: MutableList<PlayingCard>) {
Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자 파라미터로 Mutable Collection을 받는 것은 지양하는 것이 좋습니다 :)
외부에 남아있는 참조를 실수로 조작한다면 이 객체가 문제가 생길 수 있는데요,
아래 코드를 실행하면 어떤 일이 발생할까요?

val mutableList = mutableListOf()
val cardDeck = CardDeck(mutableList)

mutableList.clear()


init {
cardList.shuffle()
}

private var index = 0

Comment on lines +10 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

멤버변수는 생성자 상단에 위치하는 것이 코틀린 컨벤션입니다 :)

fun getCardWithIncrease(): PlayingCard {
if (isMaxIndexOfCard()) resetCard()
return cardList[index++]
}

private fun isMaxIndexOfCard(): Boolean {
return index == cardList.size
}

private fun resetCard() {
cardList.shuffle()
index = 0
}
}
50 changes: 50 additions & 0 deletions src/main/kotlin/card/deck/Hands.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package card.deck

import card.CardRank
import card.PlayingCard

class Hands(val cardList: MutableList<PlayingCard> = mutableListOf()) {

fun addCard(playingCard: PlayingCard) {
cardList.add(playingCard)
}

fun getResultPoint(): Int {
var point = cardList.sumOf { it.getPoint() }

if (isContainAce()) {
point += addAcePoint(point)
}

return point
}

fun cardDeckSize() = cardList.size

private fun isContainAce(): Boolean {
return cardList.any { it.cardRank == CardRank.ACE }
}

private fun addAcePoint(point: Int): Int {
return if (point <= 11) {
ADD_ACE_POINT
} else {
ADD_ACE_POINT_NONE
}
}

companion object {
fun create() = Hands(mutableListOf())
private const val ADD_ACE_POINT = 10
private const val ADD_ACE_POINT_NONE = 0
}

override fun toString(): String {
val sb = StringBuilder()
for (card in cardList) {
sb.append("$card, ")
}
sb.deleteCharAt(sb.lastIndex - 1)
return sb.toString()
}
}
52 changes: 52 additions & 0 deletions src/main/kotlin/player/Player.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package player

import card.PlayingCard
import card.deck.Hands

class Player(
val name: String,
status: Status = Status.START,
hands: Hands = Hands(),
) {

var status = status
private set

var playerDeck = hands
private set

fun playDone() {
this.status = Status.STAND
}

fun saveCard(card: PlayingCard) {
playerDeck.addCard(card)
updateStatus()
}

fun updateStatus() {
val totalPoint = playerDeck.getResultPoint()

if (totalPoint > BLACKJACK_NUMBER) {
this.status = Status.BUST
} else if (isBlackJack()) {
this.status = Status.BLACK_JACK
} else {
this.status = Status.PLAYING
}
}

fun getResultPoint(): Int {
return playerDeck.getResultPoint()
}

private fun isBlackJack(): Boolean {
val totalPoint = playerDeck.getResultPoint()
return playerDeck.cardDeckSize() == BLACKJACK_CARD_COUNT && totalPoint == BLACKJACK_NUMBER
}

companion object {
private const val BLACKJACK_CARD_COUNT = 2
private const val BLACKJACK_NUMBER = 21
}
}
19 changes: 19 additions & 0 deletions src/main/kotlin/player/PlayerFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package player

object PlayerFactory {

private const val DELIMITERS = ','

fun createPlayerList(playerNames: String): List<Player> {
val playerNameList = splitInputData(playerNames)
val playerList = mutableListOf<Player>()
playerNameList.forEach { name ->
playerList.add(Player(name))
}
return playerList
}

private fun splitInputData(inputData: String): List<String> {
return inputData.split(DELIMITERS).map { it.trim() }.filter { it.isNotBlank() }
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/player/PlayerGroup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package player

class PlayerGroup(val playerList: List<Player>) {

init {
validatePlayer()
}

private fun validatePlayer() {
require(playerList.size in MIN_PLAYER_CNT..MAM_PLAYER_CNT) { "참여 가능한 플레이어의 범위를 넘어섰습니다." }
}

companion object {
private const val MIN_PLAYER_CNT = 2
private const val MAM_PLAYER_CNT = 26
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/player/Status.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package player

enum class Status {
START, PLAYING, STAND, BLACK_JACK, BUST
}
Loading