Skip to content

Commit 8278288

Browse files
committed
feat: 초기 카드 배분을 구현한다.
- 두 장씩 배분했는지 확인하는 테스트 작성 - 게임 시작하며 카드 풀을 먼저 생성하고 두 장씩의 카드를 배분하도록 구현 - 린트 포맷팅
1 parent 6bbe688 commit 8278288

File tree

9 files changed

+107
-9
lines changed

9 files changed

+107
-9
lines changed
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
11
package blackjack.controller
22

3+
import blackjack.model.Card
4+
import blackjack.model.CardInfo
5+
import blackjack.model.CardType
6+
import blackjack.model.Participant
37
import blackjack.ui.InputView
48
import blackjack.ui.ResultView
59

6-
class BlackJackGame {
10+
class BlackJackGame(
11+
val participants: List<Participant>,
12+
) {
13+
14+
private val cardsPool = mutableSetOf<Card>()
15+
16+
init {
17+
makeCardsPool()
18+
}
19+
20+
private fun makeCardsPool() {
21+
CardType.values().forEach { type ->
22+
CardInfo.values().forEach { cardInfo ->
23+
cardsPool.add(Card(type, cardInfo))
24+
}
25+
}
26+
}
27+
28+
fun allocateDefaultCards() {
29+
participants.forEach {
30+
it.cards.addAll(pickRandomCards(DEFAULT_CARD_COUNTS))
31+
}
32+
}
33+
34+
private fun pickRandomCards(count: Int): List<Card> {
35+
val pickedCards = cardsPool.shuffled().take(count)
36+
pickedCards.forEach { pickedCard ->
37+
cardsPool.remove(pickedCard)
38+
}
39+
return pickedCards
40+
}
41+
42+
companion object {
43+
const val DEFAULT_CARD_COUNTS = 2
44+
}
745
}
846

947
fun main() {
1048
val participants = InputView.registerParticipants()
49+
50+
val blackJackGame = BlackJackGame(participants)
51+
1152
ResultView.showInitialStatusOfParticipants(participants)
12-
}
53+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package blackjack.model
22

33
data class Card(
4-
val name: String,
4+
val type: CardType,
5+
val value: CardInfo,
56
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package blackjack.model
2+
3+
enum class CardInfo(
4+
val displayName: String,
5+
val value1: Int,
6+
val value2: Int = value1,
7+
) {
8+
Ace("ace", 1, 10),
9+
One("1", 1),
10+
Two("2", 2),
11+
Three("3", 3),
12+
Four("4", 4),
13+
Five("5", 5),
14+
Six("6", 6),
15+
Seven("7", 7),
16+
Eight("8", 8),
17+
Nine("9", 9),
18+
King("King", 10),
19+
Queen("Queen", 10),
20+
Jack("Jack", 10),
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package blackjack.model
2+
3+
enum class CardType {
4+
Diamond,
5+
Spade,
6+
Heart,
7+
Clover,
8+
;
9+
}

src/main/kotlin/blackjack/model/Participant.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package blackjack.model
22

33
data class Participant(
44
val name: String,
5-
val cards: List<Card>? = null,
5+
val cards: MutableList<Card> = mutableListOf(),
66
)

src/main/kotlin/blackjack/ui/InputView.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ object InputView {
2525
}
2626
} ?: throw IllegalArgumentException("카드 받기 여부는 null을 허용하지 않습니다.")
2727
}
28-
29-
}
28+
}

src/main/kotlin/blackjack/ui/ResultView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ object ResultView {
1111
}
1212
println("에게 2장의 카드를 나눠주었습니다.")
1313
}
14-
}
14+
}

src/test/kotlin/DslTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class DslTest {
3434
)
3535
}
3636
}
37-
3837
}
3938

4039
fun introduce(block: PersonBuilder.() -> Unit): Person {
@@ -100,4 +99,4 @@ data class Person(
10099
sealed interface Skill {
101100
data class SoftSkill(val name: String) : Skill
102101
data class HardSkill(val name: String) : Skill
103-
}
102+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package blackjack.controller
2+
3+
import blackjack.controller.BlackJackGame.Companion.DEFAULT_CARD_COUNTS
4+
import blackjack.model.Participant
5+
import io.kotest.matchers.shouldBe
6+
import org.junit.jupiter.api.Test
7+
8+
class BlackJackGameTest {
9+
10+
@Test
11+
fun checkDefaultAllocatedCards() {
12+
val blackJackGame = BlackJackGame(participants)
13+
blackJackGame.allocateDefaultCards()
14+
15+
blackJackGame.participants.forEach {
16+
it.cards.size shouldBe DEFAULT_CARD_COUNTS
17+
}
18+
}
19+
20+
companion object {
21+
private val participants = listOf(
22+
Participant("Liam"),
23+
Participant("Noel"),
24+
Participant("Gem"),
25+
Participant("Andy"),
26+
)
27+
}
28+
}

0 commit comments

Comments
 (0)