From a07ca4418b1833f025dc8ad868cdd2468b7184ea Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 11 May 2022 20:38:33 +0900 Subject: [PATCH 01/24] =?UTF-8?q?docs:=20README.md=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index b4b1377..9234edd 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,22 @@ # λ―Έμ…˜ - μ›Œλ“€ +## πŸ“ κΈ°λŠ₯ λͺ©λ‘ + +### μ½”ν‹€λ¦° μ›Œλ“€ κΈ°λŠ₯λͺ©λ‘ / 페퍼, μ‘°μ‘°κ·Έλ¦° + +- [ ] μ‹œμž‘λ©”μ‹œμ§€ 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ +- [ ] λ‹΅μ•ˆμ„ μž…λ ₯λ°›λŠ” κΈ°λŠ₯ + - [ ] μž…λ ₯값은 5자리 λ¬Έμžμ—΄λ‘œ μ œν•œν•˜λŠ” κΈ°λŠ₯ + - [ ] μž…λ ₯값은 `words.txt` 에 μ‘΄μž¬ν•˜λŠ” λ‹¨μ–΄λ§Œ κ°€λŠ₯ + - [ ] λ‹΅μ•ˆμ€ 6번 만 μž…λ ₯ κ°€λŠ₯ +- [ ] 였늘의 정닡을 μ„ νƒν•˜λŠ” κΈ°λŠ₯ + - [ ] 정닡은 `words.txt` 의 ((ν˜„μž¬ λ‚ μ§œ - 2021λ…„ 6μ›” 19일) % λ°°μ—΄μ˜ 크기) 번째의 단어 +- [ ] λ‹΅μ•ˆμ΄ 각 λ¬Έμžκ°€ μ •λ‹΅μ˜ λ¬Έμžμ™€ μ–΄λ–»κ²Œ λ§€μΉ­λ˜λŠ”μ§€ μ°ΎλŠ” κΈ°λŠ₯ + - [ ] 두 개의 λ™μΌν•œ 문자λ₯Ό μž…λ ₯ν•˜κ³  그쀑 ν•˜λ‚˜κ°€ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œλ˜λ©΄ ν•΄λ‹Ή 문자 쀑 ν•˜λ‚˜λ§Œ μ΅œμ’… 단어에 ν‘œμ‹œ +- [ ] ν™•μΈλœ 정보에 맞게 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ + - [ ] λ§žλŠ” κΈ€μžλŠ” μ΄ˆλ‘μƒ‰, μœ„μΉ˜κ°€ 틀리면 λ…Έλž€μƒ‰, μ—†μœΌλ©΄ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œ +- [ ] κ²Œμž„ κ²°κ³Όλ₯Ό 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ + ## πŸ” 진행 방식 - λ―Έμ…˜μ€ **κΈ°λŠ₯ μš”κ΅¬ 사항, ν”„λ‘œκ·Έλž˜λ° μš”κ΅¬ 사항, 과제 진행 μš”κ΅¬ 사항** μ„Έ κ°€μ§€λ‘œ κ΅¬μ„±λ˜μ–΄ μžˆλ‹€. From 13a13cfb19a027a24ba9ed33bc1294004422919f Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 11 May 2022 21:17:24 +0900 Subject: [PATCH 02/24] =?UTF-8?q?feat:=20Answer=20=EA=B8=80=EC=9E=90?= =?UTF-8?q?=EA=B8=B8=EC=9D=B4=EB=A5=BC=20=EA=B2=80=EC=A6=9D=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/kotlin/wordle/domain/Answer.kt | 8 ++++++++ src/test/kotlin/wordle/domain/AnswerTest.kt | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/wordle/domain/Answer.kt create mode 100644 src/test/kotlin/wordle/domain/AnswerTest.kt diff --git a/README.md b/README.md index 9234edd..0a92e48 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ - [ ] μ‹œμž‘λ©”μ‹œμ§€ 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ - [ ] λ‹΅μ•ˆμ„ μž…λ ₯λ°›λŠ” κΈ°λŠ₯ - - [ ] μž…λ ₯값은 5자리 λ¬Έμžμ—΄λ‘œ μ œν•œν•˜λŠ” κΈ°λŠ₯ + - [x] μž…λ ₯값은 5자리 λ¬Έμžμ—΄λ‘œ μ œν•œν•˜λŠ” κΈ°λŠ₯ - [ ] μž…λ ₯값은 `words.txt` 에 μ‘΄μž¬ν•˜λŠ” λ‹¨μ–΄λ§Œ κ°€λŠ₯ - [ ] λ‹΅μ•ˆμ€ 6번 만 μž…λ ₯ κ°€λŠ₯ - [ ] 였늘의 정닡을 μ„ νƒν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt new file mode 100644 index 0000000..6a462d4 --- /dev/null +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -0,0 +1,8 @@ +package wordle.domain + +class Answer(val answer: String) { + + init { + require(answer.length == 5) { "[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€."} + } +} diff --git a/src/test/kotlin/wordle/domain/AnswerTest.kt b/src/test/kotlin/wordle/domain/AnswerTest.kt new file mode 100644 index 0000000..4e85a87 --- /dev/null +++ b/src/test/kotlin/wordle/domain/AnswerTest.kt @@ -0,0 +1,14 @@ +package wordle.domain + +import io.kotest.matchers.throwable.shouldHaveMessage +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +internal class AnswerTest { + + @Test + fun κΈ€μžκΈΈμ΄κ°€_5κ°€_μ•„λ‹Œ_경우_μ˜ˆμ™Έλ°œμƒ() { + assertThrows { Answer("abcdef") } + .shouldHaveMessage("[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€.") + } +} From c1d7d33055c714b7cb66247a027b49bc82aa4de1 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 12 May 2022 11:30:13 +0900 Subject: [PATCH 03/24] =?UTF-8?q?test:=20AnswerTest=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/domain/Answer.kt | 5 +++-- src/test/kotlin/wordle/domain/AnswerTest.kt | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index 6a462d4..278b82f 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -1,8 +1,9 @@ package wordle.domain -class Answer(val answer: String) { +class Answer(private val answer: String) { init { - require(answer.length == 5) { "[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€."} + require(answer.length == 5) { "[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€." } +// require(Words.contains(answer)) {} } } diff --git a/src/test/kotlin/wordle/domain/AnswerTest.kt b/src/test/kotlin/wordle/domain/AnswerTest.kt index 4e85a87..00bddf0 100644 --- a/src/test/kotlin/wordle/domain/AnswerTest.kt +++ b/src/test/kotlin/wordle/domain/AnswerTest.kt @@ -11,4 +11,10 @@ internal class AnswerTest { assertThrows { Answer("abcdef") } .shouldHaveMessage("[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€.") } + + @Test + fun 주어진_단어λͺ©λ‘μ—_μ‘΄μž¬ν•˜μ§€_μ•ŠλŠ”_경우_μ˜ˆμ™Έλ°œμƒ() { + assertThrows { Answer("abcde") } + .shouldHaveMessage("[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€.") + } } From 778f9c1ebf36d3845dd7f637200d06f1b9adf5e2 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 12 May 2022 13:30:58 +0900 Subject: [PATCH 04/24] =?UTF-8?q?feat:=20Answer=20=EA=B0=80=20`words.txt`?= =?UTF-8?q?=20=EC=97=90=20=EC=A1=B4=EC=9E=AC=ED=95=98=EB=8A=94=20=EB=8B=A8?= =?UTF-8?q?=EC=96=B4=EC=9D=B8=EC=A7=80=20=EA=B2=80=EC=A6=9D=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/.gitkeep | 0 src/main/kotlin/wordle/domain/Answer.kt | 2 +- src/main/kotlin/wordle/domain/Words.kt | 15 +++++++++++++++ src/test/kotlin/.gitkeep | 0 4 files changed, 16 insertions(+), 1 deletion(-) delete mode 100644 src/main/kotlin/.gitkeep create mode 100644 src/main/kotlin/wordle/domain/Words.kt delete mode 100644 src/test/kotlin/.gitkeep diff --git a/src/main/kotlin/.gitkeep b/src/main/kotlin/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index 278b82f..ff45b06 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -4,6 +4,6 @@ class Answer(private val answer: String) { init { require(answer.length == 5) { "[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€." } -// require(Words.contains(answer)) {} + require(Words.contains(answer)) { "[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€." } } } diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt new file mode 100644 index 0000000..bb6f6b7 --- /dev/null +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -0,0 +1,15 @@ +package wordle.domain + +import java.nio.file.Files +import java.nio.file.Paths + +class Words { + + companion object { + private val value: List = Files.readAllLines(Paths.get("src/main/resources/words.txt")) + + fun contains(word: String): Boolean { + return value.contains(word) + } + } +} diff --git a/src/test/kotlin/.gitkeep b/src/test/kotlin/.gitkeep deleted file mode 100644 index e69de29..0000000 From 0782fbf6eeb9ccf74c71d5a984ed781df5b109c1 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 12 May 2022 13:46:35 +0900 Subject: [PATCH 05/24] =?UTF-8?q?feat:=20=EC=98=A4=EB=8A=98=EC=9D=98=20?= =?UTF-8?q?=EC=A0=95=EB=8B=B5=EC=9D=84=20=EC=84=A0=ED=83=9D=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- src/main/kotlin/wordle/domain/Words.kt | 13 ++++++++++--- src/test/kotlin/wordle/domain/WordsTest.kt | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/test/kotlin/wordle/domain/WordsTest.kt diff --git a/README.md b/README.md index 0a92e48..c102fb2 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,10 @@ - [ ] μ‹œμž‘λ©”μ‹œμ§€ 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ - [ ] λ‹΅μ•ˆμ„ μž…λ ₯λ°›λŠ” κΈ°λŠ₯ - [x] μž…λ ₯값은 5자리 λ¬Έμžμ—΄λ‘œ μ œν•œν•˜λŠ” κΈ°λŠ₯ - - [ ] μž…λ ₯값은 `words.txt` 에 μ‘΄μž¬ν•˜λŠ” λ‹¨μ–΄λ§Œ κ°€λŠ₯ + - [x] μž…λ ₯값은 `words.txt` 에 μ‘΄μž¬ν•˜λŠ” λ‹¨μ–΄λ§Œ κ°€λŠ₯ - [ ] λ‹΅μ•ˆμ€ 6번 만 μž…λ ₯ κ°€λŠ₯ -- [ ] 였늘의 정닡을 μ„ νƒν•˜λŠ” κΈ°λŠ₯ - - [ ] 정닡은 `words.txt` 의 ((ν˜„μž¬ λ‚ μ§œ - 2021λ…„ 6μ›” 19일) % λ°°μ—΄μ˜ 크기) 번째의 단어 +- [x] 였늘의 정닡을 μ„ νƒν•˜λŠ” κΈ°λŠ₯ + - [x] 정닡은 `words.txt` 의 ((ν˜„μž¬ λ‚ μ§œ - 2021λ…„ 6μ›” 19일) % λ°°μ—΄μ˜ 크기) 번째의 단어 - [ ] λ‹΅μ•ˆμ΄ 각 λ¬Έμžκ°€ μ •λ‹΅μ˜ λ¬Έμžμ™€ μ–΄λ–»κ²Œ λ§€μΉ­λ˜λŠ”μ§€ μ°ΎλŠ” κΈ°λŠ₯ - [ ] 두 개의 λ™μΌν•œ 문자λ₯Ό μž…λ ₯ν•˜κ³  그쀑 ν•˜λ‚˜κ°€ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œλ˜λ©΄ ν•΄λ‹Ή 문자 쀑 ν•˜λ‚˜λ§Œ μ΅œμ’… 단어에 ν‘œμ‹œ - [ ] ν™•μΈλœ 정보에 맞게 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt index bb6f6b7..d63b36e 100644 --- a/src/main/kotlin/wordle/domain/Words.kt +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -2,14 +2,21 @@ package wordle.domain import java.nio.file.Files import java.nio.file.Paths +import java.time.LocalDate +import java.time.temporal.ChronoUnit class Words { - companion object { - private val value: List = Files.readAllLines(Paths.get("src/main/resources/words.txt")) + private val VALUE: List = Files.readAllLines(Paths.get("src/main/resources/words.txt")) + private val BASIC_DATE = LocalDate.of(2021, 6, 19) fun contains(word: String): Boolean { - return value.contains(word) + return VALUE.contains(word) + } + + fun pick(date: LocalDate): String { + val index = ChronoUnit.DAYS.between(BASIC_DATE, date) % VALUE.size + return VALUE[index.toInt()] } } } diff --git a/src/test/kotlin/wordle/domain/WordsTest.kt b/src/test/kotlin/wordle/domain/WordsTest.kt new file mode 100644 index 0000000..fd44718 --- /dev/null +++ b/src/test/kotlin/wordle/domain/WordsTest.kt @@ -0,0 +1,15 @@ +package wordle.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.time.LocalDate + +internal class WordsTest { + + @Test + fun 였늘의_단어λ₯Ό_선택() { + val date = LocalDate.of(2022, 5, 12) + + assertThat(Words.pick(date)).isEqualTo("fetus") + } +} From 58cbf753f039e27128fa1362cb4399fca7c0bde3 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 12 May 2022 15:13:17 +0900 Subject: [PATCH 06/24] =?UTF-8?q?feat:=20=EB=8B=B5=EC=95=88=EA=B3=BC=20?= =?UTF-8?q?=EC=A0=95=EB=8B=B5=EC=9D=84=20=EB=B9=84=EA=B5=90=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- src/main/kotlin/wordle/domain/Answer.kt | 71 ++++++++++++++++++++- src/main/kotlin/wordle/domain/Mark.kt | 8 +++ src/test/kotlin/wordle/domain/AnswerTest.kt | 10 +++ 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/wordle/domain/Mark.kt diff --git a/README.md b/README.md index c102fb2..2f353c8 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ - [ ] λ‹΅μ•ˆμ€ 6번 만 μž…λ ₯ κ°€λŠ₯ - [x] 였늘의 정닡을 μ„ νƒν•˜λŠ” κΈ°λŠ₯ - [x] 정닡은 `words.txt` 의 ((ν˜„μž¬ λ‚ μ§œ - 2021λ…„ 6μ›” 19일) % λ°°μ—΄μ˜ 크기) 번째의 단어 -- [ ] λ‹΅μ•ˆμ΄ 각 λ¬Έμžκ°€ μ •λ‹΅μ˜ λ¬Έμžμ™€ μ–΄λ–»κ²Œ λ§€μΉ­λ˜λŠ”μ§€ μ°ΎλŠ” κΈ°λŠ₯ - - [ ] 두 개의 λ™μΌν•œ 문자λ₯Ό μž…λ ₯ν•˜κ³  그쀑 ν•˜λ‚˜κ°€ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œλ˜λ©΄ ν•΄λ‹Ή 문자 쀑 ν•˜λ‚˜λ§Œ μ΅œμ’… 단어에 ν‘œμ‹œ +- [x] λ‹΅μ•ˆμ΄ 각 λ¬Έμžκ°€ μ •λ‹΅μ˜ λ¬Έμžμ™€ μ–΄λ–»κ²Œ λ§€μΉ­λ˜λŠ”μ§€ μ°ΎλŠ” κΈ°λŠ₯ + - [x] 두 개의 λ™μΌν•œ 문자λ₯Ό μž…λ ₯ν•˜κ³  그쀑 ν•˜λ‚˜κ°€ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œλ˜λ©΄ ν•΄λ‹Ή 문자 쀑 ν•˜λ‚˜λ§Œ μ΅œμ’… 단어에 ν‘œμ‹œ - [ ] ν™•μΈλœ 정보에 맞게 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ - [ ] λ§žλŠ” κΈ€μžλŠ” μ΄ˆλ‘μƒ‰, μœ„μΉ˜κ°€ 틀리면 λ…Έλž€μƒ‰, μ—†μœΌλ©΄ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œ - [ ] κ²Œμž„ κ²°κ³Όλ₯Ό 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index ff45b06..f3b904d 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -1,9 +1,78 @@ package wordle.domain +private const val WORD_SIZE = 5 + class Answer(private val answer: String) { init { - require(answer.length == 5) { "[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€." } + require(answer.length == WORD_SIZE) { "[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€." } require(Words.contains(answer)) { "[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€." } } + + fun match(word: String): MutableList { + val wordTable = createWordTable(word) + val result = MutableList(WORD_SIZE) { Mark.NONE } + matchExact(word, result, wordTable) + matchExist(result, wordTable) + return result + } + + private fun createWordTable(word: String): HashMap { + val wordTable = HashMap() + for (it in word) { + wordTable[it] = wordTable.getOrDefault(it, 0) + 1 + } + return wordTable + } + + private fun matchExact( + word: String, + result: MutableList, + wordTable: HashMap, + ) { + for (i in 0 until WORD_SIZE) { + markExact(word, i, result, wordTable) + } + } + + private fun markExact( + word: String, + i: Int, + result: MutableList, + wordTable: HashMap, + ) { + val key = word[i] + if (key == answer[i]) { + result[i] = Mark.EXACT + wordTable.computeIfPresent(key) { _, v -> v - 1 } + } + } + + private fun matchExist( + result: MutableList, + wordTable: HashMap, + ) { + for (i in 0 until WORD_SIZE) { + markExist(i, result, wordTable) + } + } + + private fun markExist( + i: Int, + result: MutableList, + wordTable: HashMap, + ) { + val key = answer[i] + if (isExist(result, i, wordTable, key)) { + result[i] = Mark.EXIST + wordTable.computeIfPresent(key) { _, v -> v - 1 } + } + } + + private fun isExist( + result: MutableList, + i: Int, + wordTable: HashMap, + key: Char, + ) = result[i] == Mark.NONE && wordTable.containsKey(key) && wordTable[key] != 0 } diff --git a/src/main/kotlin/wordle/domain/Mark.kt b/src/main/kotlin/wordle/domain/Mark.kt new file mode 100644 index 0000000..9922112 --- /dev/null +++ b/src/main/kotlin/wordle/domain/Mark.kt @@ -0,0 +1,8 @@ +package wordle.domain + +enum class Mark { + + NONE, + EXIST, + EXACT +} diff --git a/src/test/kotlin/wordle/domain/AnswerTest.kt b/src/test/kotlin/wordle/domain/AnswerTest.kt index 00bddf0..1452b33 100644 --- a/src/test/kotlin/wordle/domain/AnswerTest.kt +++ b/src/test/kotlin/wordle/domain/AnswerTest.kt @@ -1,8 +1,10 @@ package wordle.domain import io.kotest.matchers.throwable.shouldHaveMessage +import org.assertj.core.api.Assertions import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows +import wordle.domain.Mark.* internal class AnswerTest { @@ -17,4 +19,12 @@ internal class AnswerTest { assertThrows { Answer("abcde") } .shouldHaveMessage("[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€.") } + + @Test + fun λ‹΅μ•ˆκ³Ό_정닡을_비ꡐ() { + val answer = Answer("death") + + Assertions.assertThat(answer.match("fresh")) + .isEqualTo(listOf(NONE, EXIST, NONE, NONE, EXACT)) + } } From 551f64f0831c8299e716b743bce6975690314b0d Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 12 May 2022 16:26:15 +0900 Subject: [PATCH 07/24] =?UTF-8?q?feat:=20=EC=9E=85=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/view/View.kt | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/kotlin/wordle/view/View.kt diff --git a/src/main/kotlin/wordle/view/View.kt b/src/main/kotlin/wordle/view/View.kt new file mode 100644 index 0000000..e69b3c6 --- /dev/null +++ b/src/main/kotlin/wordle/view/View.kt @@ -0,0 +1,41 @@ +package wordle.view + +import wordle.domain.Mark +import wordle.domain.Results + +fun printStartMessage() { + println("WORDLE을 6번 λ§Œμ— 맞좰 λ³΄μ„Έμš”.\nμ‹œλ„μ˜ κ²°κ³ΌλŠ” νƒ€μΌμ˜ 색 λ³€ν™”λ‘œ λ‚˜νƒ€λ‚©λ‹ˆλ‹€.") +} + +fun printInputMessage(): String { + println("정닡을 μž…λ ₯ν•΄ μ£Όμ„Έμš”.") + return readln() +} + +fun printResults(results: Results, isPlaying: Boolean, tryCount: Int) { + println() + if (!isPlaying) { + printTryCount(tryCount) + } + results.value.forEach { + printResult(it) + } + println() +} + +fun printTryCount(tryCount: Int) { + println("$tryCount/6\n") +} + +private fun printResult(result: List) { + val stringBuilder = StringBuilder() + result.forEach { + when (it) { + Mark.NONE -> stringBuilder.append("⬜") + Mark.EXIST -> stringBuilder.append("\uD83D\uDFE8") + Mark.EXACT -> stringBuilder.append("\uD83D\uDFE9") + } + } + println(stringBuilder.toString()) +} + From f0c74c9b1ddee0dd3497d7b7f31de61020f24e2b Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 12 May 2022 16:27:20 +0900 Subject: [PATCH 08/24] =?UTF-8?q?feat:=20Wordle=20=EA=B2=8C=EC=9E=84?= =?UTF-8?q?=EC=9D=84=20=EC=8B=A4=ED=96=89=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/domain/Answer.kt | 8 +++----- src/main/kotlin/wordle/domain/Game.kt | 22 ++++++++++++++++++++++ src/main/kotlin/wordle/domain/Results.kt | 16 ++++++++++++++++ src/main/kotlin/wordle/domain/Words.kt | 2 ++ 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/wordle/domain/Game.kt create mode 100644 src/main/kotlin/wordle/domain/Results.kt diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index f3b904d..aae6450 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -1,7 +1,5 @@ package wordle.domain -private const val WORD_SIZE = 5 - class Answer(private val answer: String) { init { @@ -10,8 +8,8 @@ class Answer(private val answer: String) { } fun match(word: String): MutableList { - val wordTable = createWordTable(word) val result = MutableList(WORD_SIZE) { Mark.NONE } + val wordTable = createWordTable(word) matchExact(word, result, wordTable) matchExist(result, wordTable) return result @@ -19,8 +17,8 @@ class Answer(private val answer: String) { private fun createWordTable(word: String): HashMap { val wordTable = HashMap() - for (it in word) { - wordTable[it] = wordTable.getOrDefault(it, 0) + 1 + for (char in word) { + wordTable[char] = wordTable.getOrDefault(char, 0) + 1 } return wordTable } diff --git a/src/main/kotlin/wordle/domain/Game.kt b/src/main/kotlin/wordle/domain/Game.kt new file mode 100644 index 0000000..bd9a27e --- /dev/null +++ b/src/main/kotlin/wordle/domain/Game.kt @@ -0,0 +1,22 @@ +package wordle.domain + +class Game(private val word: String, val results: Results) { + + var isPlaying: Boolean = true + private set + + fun playRound(answer: Answer) { + val result = answer.match(word) + results.add(result) + if (isOver(result)) { + isPlaying = false + } + } + + private fun isOver(result: MutableList) = + results.isLimit() || result.all { mark -> mark == Mark.EXACT } + + fun findTryCount(): Int { + return results.value.size + } +} diff --git a/src/main/kotlin/wordle/domain/Results.kt b/src/main/kotlin/wordle/domain/Results.kt new file mode 100644 index 0000000..211a3e9 --- /dev/null +++ b/src/main/kotlin/wordle/domain/Results.kt @@ -0,0 +1,16 @@ +package wordle.domain + +private const val LIMIT_SIZE = 6 + +class Results { + + val value: MutableList> = mutableListOf() + + fun add(result: List) { + value.add(result) + } + + fun isLimit(): Boolean { + return value.size >= LIMIT_SIZE + } +} diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt index d63b36e..6536133 100644 --- a/src/main/kotlin/wordle/domain/Words.kt +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -5,6 +5,8 @@ import java.nio.file.Paths import java.time.LocalDate import java.time.temporal.ChronoUnit +const val WORD_SIZE = 5 + class Words { companion object { private val VALUE: List = Files.readAllLines(Paths.get("src/main/resources/words.txt")) From d9a554dd7c7bf5c08b31d0fc78350a6e859c88c2 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 12 May 2022 16:27:47 +0900 Subject: [PATCH 09/24] =?UTF-8?q?feat:=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=EC=9D=84=20=EC=8B=A4=ED=96=89?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++++----- src/main/kotlin/Application.kt | 5 ++++ .../wordle/controller/WordleController.kt | 23 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/Application.kt create mode 100644 src/main/kotlin/wordle/controller/WordleController.kt diff --git a/README.md b/README.md index 2f353c8..c8fcbd4 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,18 @@ ### μ½”ν‹€λ¦° μ›Œλ“€ κΈ°λŠ₯λͺ©λ‘ / 페퍼, μ‘°μ‘°κ·Έλ¦° -- [ ] μ‹œμž‘λ©”μ‹œμ§€ 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ -- [ ] λ‹΅μ•ˆμ„ μž…λ ₯λ°›λŠ” κΈ°λŠ₯ +- [x] μ‹œμž‘λ©”μ‹œμ§€ 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ +- [x] λ‹΅μ•ˆμ„ μž…λ ₯λ°›λŠ” κΈ°λŠ₯ - [x] μž…λ ₯값은 5자리 λ¬Έμžμ—΄λ‘œ μ œν•œν•˜λŠ” κΈ°λŠ₯ - [x] μž…λ ₯값은 `words.txt` 에 μ‘΄μž¬ν•˜λŠ” λ‹¨μ–΄λ§Œ κ°€λŠ₯ - - [ ] λ‹΅μ•ˆμ€ 6번 만 μž…λ ₯ κ°€λŠ₯ + - [x] λ‹΅μ•ˆμ€ 6번 만 μž…λ ₯ κ°€λŠ₯ - [x] 였늘의 정닡을 μ„ νƒν•˜λŠ” κΈ°λŠ₯ - [x] 정닡은 `words.txt` 의 ((ν˜„μž¬ λ‚ μ§œ - 2021λ…„ 6μ›” 19일) % λ°°μ—΄μ˜ 크기) 번째의 단어 - [x] λ‹΅μ•ˆμ΄ 각 λ¬Έμžκ°€ μ •λ‹΅μ˜ λ¬Έμžμ™€ μ–΄λ–»κ²Œ λ§€μΉ­λ˜λŠ”μ§€ μ°ΎλŠ” κΈ°λŠ₯ - [x] 두 개의 λ™μΌν•œ 문자λ₯Ό μž…λ ₯ν•˜κ³  그쀑 ν•˜λ‚˜κ°€ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œλ˜λ©΄ ν•΄λ‹Ή 문자 쀑 ν•˜λ‚˜λ§Œ μ΅œμ’… 단어에 ν‘œμ‹œ -- [ ] ν™•μΈλœ 정보에 맞게 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ - - [ ] λ§žλŠ” κΈ€μžλŠ” μ΄ˆλ‘μƒ‰, μœ„μΉ˜κ°€ 틀리면 λ…Έλž€μƒ‰, μ—†μœΌλ©΄ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œ -- [ ] κ²Œμž„ κ²°κ³Όλ₯Ό 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ +- [x] ν™•μΈλœ 정보에 맞게 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ + - [x] λ§žλŠ” κΈ€μžλŠ” μ΄ˆλ‘μƒ‰, μœ„μΉ˜κ°€ 틀리면 λ…Έλž€μƒ‰, μ—†μœΌλ©΄ νšŒμƒ‰μœΌλ‘œ ν‘œμ‹œ +- [x] κ²Œμž„ κ²°κ³Όλ₯Ό 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ ## πŸ” 진행 방식 diff --git a/src/main/kotlin/Application.kt b/src/main/kotlin/Application.kt new file mode 100644 index 0000000..bd12ffe --- /dev/null +++ b/src/main/kotlin/Application.kt @@ -0,0 +1,5 @@ +import wordle.controller.WordleController + +fun main() { + WordleController().run() +} diff --git a/src/main/kotlin/wordle/controller/WordleController.kt b/src/main/kotlin/wordle/controller/WordleController.kt new file mode 100644 index 0000000..1945b79 --- /dev/null +++ b/src/main/kotlin/wordle/controller/WordleController.kt @@ -0,0 +1,23 @@ +package wordle.controller + +import wordle.domain.Answer +import wordle.domain.Game +import wordle.domain.Results +import wordle.domain.Words +import wordle.view.printInputMessage +import wordle.view.printResults +import wordle.view.printStartMessage +import java.time.LocalDate + +class WordleController { + + fun run() { + printStartMessage() + val game = Game(Words.pick(LocalDate.now()), Results()) + while (game.isPlaying) { + val answer = Answer(printInputMessage()) + game.playRound(answer) + printResults(game.results, game.isPlaying, game.findTryCount()) + } + } +} From 8f1f5f04205ec61dac843e13165670b3d3fa059d Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Fri, 13 May 2022 15:09:18 +0900 Subject: [PATCH 10/24] =?UTF-8?q?test:=20Game=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wordle/controller/WordleController.kt | 3 +- src/main/kotlin/wordle/domain/Answer.kt | 44 +++++------------ src/main/kotlin/wordle/domain/Game.kt | 5 +- src/main/kotlin/wordle/domain/Words.kt | 1 + src/main/kotlin/wordle/view/View.kt | 2 +- src/test/kotlin/wordle/domain/AnswerTest.kt | 3 +- src/test/kotlin/wordle/domain/GameTest.kt | 49 +++++++++++++++++++ 7 files changed, 70 insertions(+), 37 deletions(-) create mode 100644 src/test/kotlin/wordle/domain/GameTest.kt diff --git a/src/main/kotlin/wordle/controller/WordleController.kt b/src/main/kotlin/wordle/controller/WordleController.kt index 1945b79..2d3509a 100644 --- a/src/main/kotlin/wordle/controller/WordleController.kt +++ b/src/main/kotlin/wordle/controller/WordleController.kt @@ -2,7 +2,6 @@ package wordle.controller import wordle.domain.Answer import wordle.domain.Game -import wordle.domain.Results import wordle.domain.Words import wordle.view.printInputMessage import wordle.view.printResults @@ -13,7 +12,7 @@ class WordleController { fun run() { printStartMessage() - val game = Game(Words.pick(LocalDate.now()), Results()) + val game = Game(Words.pick(LocalDate.now())) while (game.isPlaying) { val answer = Answer(printInputMessage()) game.playRound(answer) diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index aae6450..780c2c2 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -7,7 +7,7 @@ class Answer(private val answer: String) { require(Words.contains(answer)) { "[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€." } } - fun match(word: String): MutableList { + fun compareToWord(word: String): MutableList { val result = MutableList(WORD_SIZE) { Mark.NONE } val wordTable = createWordTable(word) matchExact(word, result, wordTable) @@ -23,54 +23,36 @@ class Answer(private val answer: String) { return wordTable } - private fun matchExact( - word: String, - result: MutableList, - wordTable: HashMap, - ) { + private fun matchExact(word: String, result: MutableList, wordTable: HashMap) { for (i in 0 until WORD_SIZE) { - markExact(word, i, result, wordTable) + markExact(i, word, result, wordTable) } } - private fun markExact( - word: String, - i: Int, - result: MutableList, - wordTable: HashMap, - ) { - val key = word[i] - if (key == answer[i]) { + private fun markExact(i: Int, word: String, result: MutableList, wordTable: HashMap) { + if (word[i] == answer[i]) { result[i] = Mark.EXACT - wordTable.computeIfPresent(key) { _, v -> v - 1 } + wordTable.computeIfPresent(word[i]) { _, v -> v - 1 } } } - private fun matchExist( - result: MutableList, - wordTable: HashMap, - ) { + private fun matchExist(result: MutableList, wordTable: HashMap) { for (i in 0 until WORD_SIZE) { markExist(i, result, wordTable) } } - private fun markExist( - i: Int, - result: MutableList, - wordTable: HashMap, - ) { - val key = answer[i] - if (isExist(result, i, wordTable, key)) { + private fun markExist(i: Int, result: MutableList, wordTable: HashMap) { + if (isExist(i, result, wordTable, answer[i])) { result[i] = Mark.EXIST - wordTable.computeIfPresent(key) { _, v -> v - 1 } + wordTable.computeIfPresent(answer[i]) { _, v -> v - 1 } } } private fun isExist( - result: MutableList, i: Int, + result: MutableList, wordTable: HashMap, - key: Char, - ) = result[i] == Mark.NONE && wordTable.containsKey(key) && wordTable[key] != 0 + charOfAnswer: Char, + ) = result[i] == Mark.NONE && wordTable.containsKey(charOfAnswer) && wordTable[charOfAnswer] != 0 } diff --git a/src/main/kotlin/wordle/domain/Game.kt b/src/main/kotlin/wordle/domain/Game.kt index bd9a27e..e992b4a 100644 --- a/src/main/kotlin/wordle/domain/Game.kt +++ b/src/main/kotlin/wordle/domain/Game.kt @@ -1,12 +1,13 @@ package wordle.domain -class Game(private val word: String, val results: Results) { +class Game(private val word: String) { + val results: Results = Results() var isPlaying: Boolean = true private set fun playRound(answer: Answer) { - val result = answer.match(word) + val result = answer.compareToWord(word) results.add(result) if (isOver(result)) { isPlaying = false diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt index 6536133..9aba971 100644 --- a/src/main/kotlin/wordle/domain/Words.kt +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -8,6 +8,7 @@ import java.time.temporal.ChronoUnit const val WORD_SIZE = 5 class Words { + companion object { private val VALUE: List = Files.readAllLines(Paths.get("src/main/resources/words.txt")) private val BASIC_DATE = LocalDate.of(2021, 6, 19) diff --git a/src/main/kotlin/wordle/view/View.kt b/src/main/kotlin/wordle/view/View.kt index e69b3c6..5cd562f 100644 --- a/src/main/kotlin/wordle/view/View.kt +++ b/src/main/kotlin/wordle/view/View.kt @@ -4,7 +4,7 @@ import wordle.domain.Mark import wordle.domain.Results fun printStartMessage() { - println("WORDLE을 6번 λ§Œμ— 맞좰 λ³΄μ„Έμš”.\nμ‹œλ„μ˜ κ²°κ³ΌλŠ” νƒ€μΌμ˜ 색 λ³€ν™”λ‘œ λ‚˜νƒ€λ‚©λ‹ˆλ‹€.") + println("WORDLE 을 6번 λ§Œμ— 맞좰 λ³΄μ„Έμš”.\nμ‹œλ„μ˜ κ²°κ³ΌλŠ” νƒ€μΌμ˜ 색 λ³€ν™”λ‘œ λ‚˜νƒ€λ‚©λ‹ˆλ‹€.") } fun printInputMessage(): String { diff --git a/src/test/kotlin/wordle/domain/AnswerTest.kt b/src/test/kotlin/wordle/domain/AnswerTest.kt index 1452b33..1d8f92d 100644 --- a/src/test/kotlin/wordle/domain/AnswerTest.kt +++ b/src/test/kotlin/wordle/domain/AnswerTest.kt @@ -2,6 +2,7 @@ package wordle.domain import io.kotest.matchers.throwable.shouldHaveMessage import org.assertj.core.api.Assertions +import org.assertj.core.api.Assertions.* import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import wordle.domain.Mark.* @@ -24,7 +25,7 @@ internal class AnswerTest { fun λ‹΅μ•ˆκ³Ό_정닡을_비ꡐ() { val answer = Answer("death") - Assertions.assertThat(answer.match("fresh")) + assertThat(answer.compareToWord("fresh")) .isEqualTo(listOf(NONE, EXIST, NONE, NONE, EXACT)) } } diff --git a/src/test/kotlin/wordle/domain/GameTest.kt b/src/test/kotlin/wordle/domain/GameTest.kt new file mode 100644 index 0000000..4f4e7d2 --- /dev/null +++ b/src/test/kotlin/wordle/domain/GameTest.kt @@ -0,0 +1,49 @@ +package wordle.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll + +internal class GameTest { + + @Test + fun κ²Œμž„μ„_ν•œ_λΌμš΄λ“œμ”©_진행() { + val game = Game("fetus") + repeat(3) { game.playRound(Answer("apple")) } + + assertAll( + {assertThat(game.findTryCount()).isEqualTo(3)}, + {assertThat(game.isPlaying).isTrue()} + ) + } + + @Test + fun κ²Œμž„μ„_ν•œ_λΌμš΄λ“œμ”©_μ§„ν–‰ν•˜λ‹€_6λΌμš΄λ“œμ—_λ„λ‹¬ν•˜λ©΄_κ²Œμž„μ’…λ£Œ() { + val game = Game("fetus") + repeat(6) { game.playRound(Answer("apple")) } + + assertAll( + {assertThat(game.findTryCount()).isEqualTo(6)}, + {assertThat(game.isPlaying).isFalse()} + ) + } + + @Test + fun κ²Œμž„μ„_ν•œ_λΌμš΄λ“œμ”©_μ§„ν–‰ν•˜λ‹€_정닡을_λ§žμΆ”λ©΄_κ²Œμž„μ’…λ£Œ() { + val game = Game("fetus") + repeat(3) { game.playRound(Answer("fetus")) } + + assertAll( + {assertThat(game.findTryCount()).isEqualTo(3)}, + {assertThat(game.isPlaying).isFalse()} + ) + } + + @Test + fun λͺ‡_번째_μ‹œλ„μΈμ§€_계산() { + val game = Game("fetus") + repeat(6) { game.playRound(Answer("apple")) } + + assertThat(game.findTryCount()).isEqualTo(6) + } +} From 5422144d39713b12cf7e76b1eef810716561fb5d Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Fri, 13 May 2022 15:41:54 +0900 Subject: [PATCH 11/24] =?UTF-8?q?test:=20=EC=A0=95=EB=8B=B5=EA=B3=BC=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=ED=95=98=EB=8A=94=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/wordle/domain/AnswerTest.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/test/kotlin/wordle/domain/AnswerTest.kt b/src/test/kotlin/wordle/domain/AnswerTest.kt index 1d8f92d..450b5bb 100644 --- a/src/test/kotlin/wordle/domain/AnswerTest.kt +++ b/src/test/kotlin/wordle/domain/AnswerTest.kt @@ -1,8 +1,7 @@ package wordle.domain import io.kotest.matchers.throwable.shouldHaveMessage -import org.assertj.core.api.Assertions -import org.assertj.core.api.Assertions.* +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import wordle.domain.Mark.* @@ -22,10 +21,18 @@ internal class AnswerTest { } @Test - fun λ‹΅μ•ˆκ³Ό_정닡을_비ꡐ() { - val answer = Answer("death") + fun λ‹΅μ•ˆκ³Ό_정닡을_비ꡐ_CASE_μ€‘λ³΅λ˜λŠ”_문자_쀑_ν•˜λ‚˜λ§Œ_일치_ν• _λ•Œ() { + val answer = Answer("groom") - assertThat(answer.compareToWord("fresh")) - .isEqualTo(listOf(NONE, EXIST, NONE, NONE, EXACT)) + assertThat(answer.compareToWord("goose")) + .isEqualTo(listOf(EXACT, NONE, EXACT, EXIST, NONE)) + } + + @Test + fun λ‹΅μ•ˆκ³Ό_정닡을_비ꡐ_CASE_μ€‘λ³΅λ˜λŠ”_λ¬Έμžκ°€_μ‘΄μž¬ν•˜μ§€λ§Œ_μ •λ‹΅μ˜_κ°œμˆ˜κ°€_더_λ§Žμ„_λ•Œ() { + val answer = Answer("eerie") + + assertThat(answer.compareToWord("sheen")) + .isEqualTo(listOf(EXIST, EXIST, NONE, NONE, NONE)) } } From b496176b157896e126dbf4d88b94a1a08f5d6427 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 17:43:07 +0900 Subject: [PATCH 12/24] =?UTF-8?q?test:=20DslTest=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/study/DslTest.kt | 97 ++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/test/kotlin/study/DslTest.kt diff --git a/src/test/kotlin/study/DslTest.kt b/src/test/kotlin/study/DslTest.kt new file mode 100644 index 0000000..072f117 --- /dev/null +++ b/src/test/kotlin/study/DslTest.kt @@ -0,0 +1,97 @@ +package study + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class DslTest { + + @Test + fun introduce() { + val person = introduce { + name("λ°•μž¬μ„±") + company("μš°μ•„ν•œν˜•μ œλ“€") + skills { + soft("A passion for problem solving") + soft("Good communication skills") + hard("Kotlin") + } + languages { + "Korean" level 5 + "English" level 3 + } + } + + assertThat(person.toString()).isEqualTo( + "Person(name=λ°•μž¬μ„±, company=μš°μ•„ν•œν˜•μ œλ“€, " + + "skills=Skills(softSkills=[A passion for problem solving, Good communication skills], hardSkills=[Kotlin]), " + + "languages=Languages(languages=[Korean level : 5, English level : 3]))") + } +} + +fun introduce(builder: PersonBuilder.() -> Unit): Person { + return PersonBuilder().apply(builder).build() +} + +class PersonBuilder { + lateinit var name: String + lateinit var company: String + lateinit var skills: Skills + lateinit var languages: Languages + + fun name(input: String) { + name = input + } + + fun company(input: String) { + company = input + } + + fun skills(builder: SkillsBuilder.() -> Unit) { + skills = SkillsBuilder().apply(builder).build() + } + + fun languages(builder: LanguagesBuilder.() -> Unit) { + languages = LanguagesBuilder().apply(builder).build() + } + + fun build(): Person { + return Person(name, company, skills, languages) + } +} + +class SkillsBuilder { + private val softSkills: MutableList = mutableListOf() + private val hardSkills: MutableList = mutableListOf() + + fun soft(input: String) { + this.softSkills.add(input) + } + + fun hard(input: String) { + this.hardSkills.add(input) + } + + fun build(): Skills { + return Skills(softSkills, hardSkills) + } +} + +class LanguagesBuilder { + var languages: MutableList = mutableListOf() + + infix fun String.level(value: Int) { + languages.add(plus(" level : ") + value) + } + + fun build(): Languages { + return Languages(languages) + } +} + +data class Person(val name: String, val company: String, val skills: Skills, val languages: Languages) + +data class Skills(val softSkills: List, val hardSkills: List) + +data class Languages(val languages: List) + + From baf4d46c586c359fccb86446016160d50f94b52c Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 17:44:51 +0900 Subject: [PATCH 13/24] =?UTF-8?q?chore:=20=ED=8F=AC=EB=A7=A4=ED=8C=85=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/study/DslTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/kotlin/study/DslTest.kt b/src/test/kotlin/study/DslTest.kt index 072f117..e34a9c5 100644 --- a/src/test/kotlin/study/DslTest.kt +++ b/src/test/kotlin/study/DslTest.kt @@ -93,5 +93,3 @@ data class Person(val name: String, val company: String, val skills: Skills, val data class Skills(val softSkills: List, val hardSkills: List) data class Languages(val languages: List) - - From 1645893c55cc1bd06c999a37b0ec250a0b08bb2d Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 17:52:37 +0900 Subject: [PATCH 14/24] =?UTF-8?q?test:=20DslTest=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/study/DslTest.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/kotlin/study/DslTest.kt b/src/test/kotlin/study/DslTest.kt index e34a9c5..e08a96b 100644 --- a/src/test/kotlin/study/DslTest.kt +++ b/src/test/kotlin/study/DslTest.kt @@ -8,12 +8,12 @@ class DslTest { @Test fun introduce() { val person = introduce { - name("λ°•μž¬μ„±") - company("μš°μ•„ν•œν˜•μ œλ“€") + name("μ‘°μ‘°κ·Έλ¦°") + company("μš°μ•„ν•œν…Œν¬μ½”μŠ€") skills { soft("A passion for problem solving") soft("Good communication skills") - hard("Kotlin") + hard("Java") } languages { "Korean" level 5 @@ -22,8 +22,8 @@ class DslTest { } assertThat(person.toString()).isEqualTo( - "Person(name=λ°•μž¬μ„±, company=μš°μ•„ν•œν˜•μ œλ“€, " + - "skills=Skills(softSkills=[A passion for problem solving, Good communication skills], hardSkills=[Kotlin]), " + + "Person(name=μ‘°μ‘°κ·Έλ¦°, company=μš°μ•„ν•œν…Œν¬μ½”μŠ€, " + + "skills=Skills(softSkills=[A passion for problem solving, Good communication skills], hardSkills=[Java]), " + "languages=Languages(languages=[Korean level : 5, English level : 3]))") } } From c945e41c4ad2d64369caba859ffd6f72470f5ebf Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 17:58:56 +0900 Subject: [PATCH 15/24] =?UTF-8?q?test:=20DslTest=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/study/DslTest.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/kotlin/study/DslTest.kt b/src/test/kotlin/study/DslTest.kt index e08a96b..ba3f34d 100644 --- a/src/test/kotlin/study/DslTest.kt +++ b/src/test/kotlin/study/DslTest.kt @@ -33,10 +33,10 @@ fun introduce(builder: PersonBuilder.() -> Unit): Person { } class PersonBuilder { - lateinit var name: String - lateinit var company: String - lateinit var skills: Skills - lateinit var languages: Languages + private lateinit var name: String + private lateinit var company: String + private lateinit var skills: Skills + private lateinit var languages: Languages fun name(input: String) { name = input @@ -77,7 +77,7 @@ class SkillsBuilder { } class LanguagesBuilder { - var languages: MutableList = mutableListOf() + private var languages: MutableList = mutableListOf() infix fun String.level(value: Int) { languages.add(plus(" level : ") + value) From edb8423cca9597556fd11d101cbf53d55c1754ce Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 19:19:13 +0900 Subject: [PATCH 16/24] =?UTF-8?q?refactor:=20=EB=9E=8C=EB=8B=A4=20?= =?UTF-8?q?=EB=A7=A4=EA=B0=9C=EB=B3=80=EC=88=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/domain/Game.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/wordle/domain/Game.kt b/src/main/kotlin/wordle/domain/Game.kt index e992b4a..226523f 100644 --- a/src/main/kotlin/wordle/domain/Game.kt +++ b/src/main/kotlin/wordle/domain/Game.kt @@ -15,7 +15,7 @@ class Game(private val word: String) { } private fun isOver(result: MutableList) = - results.isLimit() || result.all { mark -> mark == Mark.EXACT } + results.isLimit() || result.all { it == Mark.EXACT } fun findTryCount(): Int { return results.value.size From ababa6c1d25309609345d24d5409f64fe9cd9d45 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 19:39:24 +0900 Subject: [PATCH 17/24] =?UTF-8?q?refactor:=20view=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wordle/controller/WordleController.kt | 16 ++++++++------ src/main/kotlin/wordle/view/View.kt | 21 ++++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/wordle/controller/WordleController.kt b/src/main/kotlin/wordle/controller/WordleController.kt index 2d3509a..4495190 100644 --- a/src/main/kotlin/wordle/controller/WordleController.kt +++ b/src/main/kotlin/wordle/controller/WordleController.kt @@ -3,9 +3,7 @@ package wordle.controller import wordle.domain.Answer import wordle.domain.Game import wordle.domain.Words -import wordle.view.printInputMessage -import wordle.view.printResults -import wordle.view.printStartMessage +import wordle.view.* import java.time.LocalDate class WordleController { @@ -14,9 +12,15 @@ class WordleController { printStartMessage() val game = Game(Words.pick(LocalDate.now())) while (game.isPlaying) { - val answer = Answer(printInputMessage()) - game.playRound(answer) - printResults(game.results, game.isPlaying, game.findTryCount()) + game.playRound(Answer(inputAnswer())) + printResults(game) } } + + private fun printResults(game: Game) { + if (!game.isPlaying) { + printTryCount(game.findTryCount()) + } + printResults(game.results) + } } diff --git a/src/main/kotlin/wordle/view/View.kt b/src/main/kotlin/wordle/view/View.kt index 5cd562f..e96d87d 100644 --- a/src/main/kotlin/wordle/view/View.kt +++ b/src/main/kotlin/wordle/view/View.kt @@ -7,26 +7,28 @@ fun printStartMessage() { println("WORDLE 을 6번 λ§Œμ— 맞좰 λ³΄μ„Έμš”.\nμ‹œλ„μ˜ κ²°κ³ΌλŠ” νƒ€μΌμ˜ 색 λ³€ν™”λ‘œ λ‚˜νƒ€λ‚©λ‹ˆλ‹€.") } -fun printInputMessage(): String { +private fun printInputMessage() { println("정닡을 μž…λ ₯ν•΄ μ£Όμ„Έμš”.") +} + +fun inputAnswer(): String { + printInputMessage() return readln() } -fun printResults(results: Results, isPlaying: Boolean, tryCount: Int) { +fun printTryCount(tryCount: Int) { + println("$tryCount/6\n") +} + +fun printResults(results: Results) { println() - if (!isPlaying) { - printTryCount(tryCount) - } results.value.forEach { printResult(it) } println() } -fun printTryCount(tryCount: Int) { - println("$tryCount/6\n") -} - +// μˆ˜μ • κ°€λŠ₯ ν•  지도? private fun printResult(result: List) { val stringBuilder = StringBuilder() result.forEach { @@ -38,4 +40,3 @@ private fun printResult(result: List) { } println(stringBuilder.toString()) } - From 9c78be6478e9fec5b67c71a5c2ced5e115167dbe Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 20:02:54 +0900 Subject: [PATCH 18/24] =?UTF-8?q?refactor:=20printTile=20=EC=9D=84=20apply?= =?UTF-8?q?()=20=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wordle/controller/WordleController.kt | 7 ---- src/main/kotlin/wordle/view/View.kt | 34 ++++++++++++------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/wordle/controller/WordleController.kt b/src/main/kotlin/wordle/controller/WordleController.kt index 4495190..6203384 100644 --- a/src/main/kotlin/wordle/controller/WordleController.kt +++ b/src/main/kotlin/wordle/controller/WordleController.kt @@ -16,11 +16,4 @@ class WordleController { printResults(game) } } - - private fun printResults(game: Game) { - if (!game.isPlaying) { - printTryCount(game.findTryCount()) - } - printResults(game.results) - } } diff --git a/src/main/kotlin/wordle/view/View.kt b/src/main/kotlin/wordle/view/View.kt index e96d87d..e79c044 100644 --- a/src/main/kotlin/wordle/view/View.kt +++ b/src/main/kotlin/wordle/view/View.kt @@ -1,6 +1,8 @@ package wordle.view +import wordle.domain.Game import wordle.domain.Mark +import wordle.domain.Mark.* import wordle.domain.Results fun printStartMessage() { @@ -16,27 +18,35 @@ fun inputAnswer(): String { return readln() } +fun printResults(game: Game) { + if (!game.isPlaying) { + printTryCount(game.findTryCount()) + } + printTiles(game.results) +} + fun printTryCount(tryCount: Int) { - println("$tryCount/6\n") + println() + println("$tryCount/6") } -fun printResults(results: Results) { +fun printTiles(results: Results) { println() results.value.forEach { - printResult(it) + printTile(it) } println() } -// μˆ˜μ • κ°€λŠ₯ ν•  지도? -private fun printResult(result: List) { - val stringBuilder = StringBuilder() - result.forEach { - when (it) { - Mark.NONE -> stringBuilder.append("⬜") - Mark.EXIST -> stringBuilder.append("\uD83D\uDFE8") - Mark.EXACT -> stringBuilder.append("\uD83D\uDFE9") +private fun printTile(result: List) { + StringBuilder().apply { + result.forEach { + when (it) { + NONE -> append("⬜") + EXIST -> append("\uD83D\uDFE8") + EXACT -> append("\uD83D\uDFE9") + } } + println(toString()) } - println(stringBuilder.toString()) } From 3d66ad914db3785d930c0949caf5505cec3fb943 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 20:37:07 +0900 Subject: [PATCH 19/24] =?UTF-8?q?test:=20DslTest=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/wordle/controller/WordleController.kt | 4 +++- src/main/kotlin/wordle/domain/Words.kt | 12 ++++++------ src/main/kotlin/wordle/view/View.kt | 6 ++++-- src/test/kotlin/study/DslTest.kt | 5 +---- src/test/kotlin/wordle/domain/AnswerTest.kt | 6 ++++-- src/test/kotlin/wordle/domain/GameTest.kt | 12 ++++++------ 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/wordle/controller/WordleController.kt b/src/main/kotlin/wordle/controller/WordleController.kt index 6203384..e62114c 100644 --- a/src/main/kotlin/wordle/controller/WordleController.kt +++ b/src/main/kotlin/wordle/controller/WordleController.kt @@ -3,7 +3,9 @@ package wordle.controller import wordle.domain.Answer import wordle.domain.Game import wordle.domain.Words -import wordle.view.* +import wordle.view.inputAnswer +import wordle.view.printResults +import wordle.view.printStartMessage import java.time.LocalDate class WordleController { diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt index 9aba971..b98dc8a 100644 --- a/src/main/kotlin/wordle/domain/Words.kt +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -1,7 +1,6 @@ package wordle.domain -import java.nio.file.Files -import java.nio.file.Paths +import java.io.FileReader import java.time.LocalDate import java.time.temporal.ChronoUnit @@ -10,12 +9,11 @@ const val WORD_SIZE = 5 class Words { companion object { - private val VALUE: List = Files.readAllLines(Paths.get("src/main/resources/words.txt")) + private val VALUE: List = FileReader("src/main/resources/words.txt").readLines() private val BASIC_DATE = LocalDate.of(2021, 6, 19) - fun contains(word: String): Boolean { - return VALUE.contains(word) - } + fun contains(word: String): Boolean = + VALUE.contains(word) fun pick(date: LocalDate): String { val index = ChronoUnit.DAYS.between(BASIC_DATE, date) % VALUE.size @@ -23,3 +21,5 @@ class Words { } } } + +fun LocalDate.pick6() = this.toString() diff --git a/src/main/kotlin/wordle/view/View.kt b/src/main/kotlin/wordle/view/View.kt index e79c044..f9f6086 100644 --- a/src/main/kotlin/wordle/view/View.kt +++ b/src/main/kotlin/wordle/view/View.kt @@ -1,9 +1,11 @@ package wordle.view +import wordle.domain.Mark.EXACT +import wordle.domain.Mark.NONE +import wordle.domain.Mark.EXIST +import wordle.domain.Results import wordle.domain.Game import wordle.domain.Mark -import wordle.domain.Mark.* -import wordle.domain.Results fun printStartMessage() { println("WORDLE 을 6번 λ§Œμ— 맞좰 λ³΄μ„Έμš”.\nμ‹œλ„μ˜ κ²°κ³ΌλŠ” νƒ€μΌμ˜ 색 λ³€ν™”λ‘œ λ‚˜νƒ€λ‚©λ‹ˆλ‹€.") diff --git a/src/test/kotlin/study/DslTest.kt b/src/test/kotlin/study/DslTest.kt index ba3f34d..88224ce 100644 --- a/src/test/kotlin/study/DslTest.kt +++ b/src/test/kotlin/study/DslTest.kt @@ -21,10 +21,7 @@ class DslTest { } } - assertThat(person.toString()).isEqualTo( - "Person(name=μ‘°μ‘°κ·Έλ¦°, company=μš°μ•„ν•œν…Œν¬μ½”μŠ€, " + - "skills=Skills(softSkills=[A passion for problem solving, Good communication skills], hardSkills=[Java]), " + - "languages=Languages(languages=[Korean level : 5, English level : 3]))") + assertThat(person.toString()).isEqualTo("Person(name=μ‘°μ‘°κ·Έλ¦°, company=μš°μ•„ν•œν…Œν¬μ½”μŠ€, skills=Skills(softSkills=[A passion for problem solving, Good communication skills], hardSkills=[Java]), languages=Languages(languages=[Korean level : 5, English level : 3]))") } } diff --git a/src/test/kotlin/wordle/domain/AnswerTest.kt b/src/test/kotlin/wordle/domain/AnswerTest.kt index 450b5bb..09f5d24 100644 --- a/src/test/kotlin/wordle/domain/AnswerTest.kt +++ b/src/test/kotlin/wordle/domain/AnswerTest.kt @@ -1,10 +1,12 @@ package wordle.domain +import org.junit.jupiter.api.assertThrows +import wordle.domain.Mark.EXACT +import wordle.domain.Mark.NONE +import wordle.domain.Mark.EXIST import io.kotest.matchers.throwable.shouldHaveMessage import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.junit.jupiter.api.assertThrows -import wordle.domain.Mark.* internal class AnswerTest { diff --git a/src/test/kotlin/wordle/domain/GameTest.kt b/src/test/kotlin/wordle/domain/GameTest.kt index 4f4e7d2..077eb81 100644 --- a/src/test/kotlin/wordle/domain/GameTest.kt +++ b/src/test/kotlin/wordle/domain/GameTest.kt @@ -12,8 +12,8 @@ internal class GameTest { repeat(3) { game.playRound(Answer("apple")) } assertAll( - {assertThat(game.findTryCount()).isEqualTo(3)}, - {assertThat(game.isPlaying).isTrue()} + { assertThat(game.findTryCount()).isEqualTo(3) }, + { assertThat(game.isPlaying).isTrue() } ) } @@ -23,8 +23,8 @@ internal class GameTest { repeat(6) { game.playRound(Answer("apple")) } assertAll( - {assertThat(game.findTryCount()).isEqualTo(6)}, - {assertThat(game.isPlaying).isFalse()} + { assertThat(game.findTryCount()).isEqualTo(6) }, + { assertThat(game.isPlaying).isFalse() } ) } @@ -34,8 +34,8 @@ internal class GameTest { repeat(3) { game.playRound(Answer("fetus")) } assertAll( - {assertThat(game.findTryCount()).isEqualTo(3)}, - {assertThat(game.isPlaying).isFalse()} + { assertThat(game.findTryCount()).isEqualTo(3) }, + { assertThat(game.isPlaying).isFalse() } ) } From e2a65119a481643db478a9cb3eb46897cb966937 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 20:48:36 +0900 Subject: [PATCH 20/24] =?UTF-8?q?refactor:=20Words=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wordle/controller/WordleController.kt | 4 ++-- src/main/kotlin/wordle/domain/Answer.kt | 2 +- src/main/kotlin/wordle/domain/Words.kt | 20 ++++++------------- src/test/kotlin/wordle/domain/WordsTest.kt | 2 +- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/wordle/controller/WordleController.kt b/src/main/kotlin/wordle/controller/WordleController.kt index e62114c..6a2227d 100644 --- a/src/main/kotlin/wordle/controller/WordleController.kt +++ b/src/main/kotlin/wordle/controller/WordleController.kt @@ -2,7 +2,7 @@ package wordle.controller import wordle.domain.Answer import wordle.domain.Game -import wordle.domain.Words +import wordle.domain.pickTodayWord import wordle.view.inputAnswer import wordle.view.printResults import wordle.view.printStartMessage @@ -12,7 +12,7 @@ class WordleController { fun run() { printStartMessage() - val game = Game(Words.pick(LocalDate.now())) + val game = Game(LocalDate.now().pickTodayWord()) while (game.isPlaying) { game.playRound(Answer(inputAnswer())) printResults(game) diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index 780c2c2..0d54eb0 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -4,7 +4,7 @@ class Answer(private val answer: String) { init { require(answer.length == WORD_SIZE) { "[ERROR] λΆ€μ μ ˆν•œ κΈ€μž κΈΈμ΄μž…λ‹ˆλ‹€." } - require(Words.contains(answer)) { "[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€." } + require(answer.isInWords()) { "[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€." } } fun compareToWord(word: String): MutableList { diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt index b98dc8a..9046f13 100644 --- a/src/main/kotlin/wordle/domain/Words.kt +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -6,20 +6,12 @@ import java.time.temporal.ChronoUnit const val WORD_SIZE = 5 -class Words { +val BASIC_DATE = LocalDate.of(2021, 6, 19)!! - companion object { - private val VALUE: List = FileReader("src/main/resources/words.txt").readLines() - private val BASIC_DATE = LocalDate.of(2021, 6, 19) +val WORDS: List = FileReader("src/main/resources/words.txt").readLines() - fun contains(word: String): Boolean = - VALUE.contains(word) +fun LocalDate.pickTodayWord() = + WORDS[(ChronoUnit.DAYS.between(BASIC_DATE, this) % WORDS.size).toInt()] - fun pick(date: LocalDate): String { - val index = ChronoUnit.DAYS.between(BASIC_DATE, date) % VALUE.size - return VALUE[index.toInt()] - } - } -} - -fun LocalDate.pick6() = this.toString() +fun String.isInWords() = + WORDS.contains(this) diff --git a/src/test/kotlin/wordle/domain/WordsTest.kt b/src/test/kotlin/wordle/domain/WordsTest.kt index fd44718..fb49f1b 100644 --- a/src/test/kotlin/wordle/domain/WordsTest.kt +++ b/src/test/kotlin/wordle/domain/WordsTest.kt @@ -10,6 +10,6 @@ internal class WordsTest { fun 였늘의_단어λ₯Ό_선택() { val date = LocalDate.of(2022, 5, 12) - assertThat(Words.pick(date)).isEqualTo("fetus") + assertThat(date.pickTodayWord()).isEqualTo("fetus") } } From a89d19072cdf5616982c166dbca656c4b5f70ffe Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 21:14:49 +0900 Subject: [PATCH 21/24] =?UTF-8?q?refactor:=20Results=20=EC=9D=98=20get=20?= =?UTF-8?q?=EC=97=90=20=EB=B0=A9=EC=96=B4=EC=A0=81=20=EB=B3=B5=EC=82=AC=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/domain/Answer.kt | 22 ++++++++++++---------- src/main/kotlin/wordle/domain/Results.kt | 3 +++ src/main/kotlin/wordle/domain/Words.kt | 4 +--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index 0d54eb0..5856f25 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -1,5 +1,7 @@ package wordle.domain +import wordle.domain.Mark.* + class Answer(private val answer: String) { init { @@ -8,7 +10,7 @@ class Answer(private val answer: String) { } fun compareToWord(word: String): MutableList { - val result = MutableList(WORD_SIZE) { Mark.NONE } + val result = MutableList(WORD_SIZE) { NONE } val wordTable = createWordTable(word) matchExact(word, result, wordTable) matchExist(result, wordTable) @@ -31,28 +33,28 @@ class Answer(private val answer: String) { private fun markExact(i: Int, word: String, result: MutableList, wordTable: HashMap) { if (word[i] == answer[i]) { - result[i] = Mark.EXACT + result[i] = EXACT wordTable.computeIfPresent(word[i]) { _, v -> v - 1 } } } private fun matchExist(result: MutableList, wordTable: HashMap) { - for (i in 0 until WORD_SIZE) { - markExist(i, result, wordTable) + for (index in 0 until WORD_SIZE) { + markExist(index, result, wordTable) } } - private fun markExist(i: Int, result: MutableList, wordTable: HashMap) { - if (isExist(i, result, wordTable, answer[i])) { - result[i] = Mark.EXIST - wordTable.computeIfPresent(answer[i]) { _, v -> v - 1 } + private fun markExist(index: Int, result: MutableList, wordTable: HashMap) { + if (isExist(index, result, wordTable, answer[index])) { + result[index] = EXIST + wordTable.computeIfPresent(answer[index]) { _, v -> v - 1 } } } private fun isExist( - i: Int, + index: Int, result: MutableList, wordTable: HashMap, charOfAnswer: Char, - ) = result[i] == Mark.NONE && wordTable.containsKey(charOfAnswer) && wordTable[charOfAnswer] != 0 + ) = result[index] == NONE && wordTable.containsKey(charOfAnswer) && wordTable[charOfAnswer] != 0 } diff --git a/src/main/kotlin/wordle/domain/Results.kt b/src/main/kotlin/wordle/domain/Results.kt index 211a3e9..6705d76 100644 --- a/src/main/kotlin/wordle/domain/Results.kt +++ b/src/main/kotlin/wordle/domain/Results.kt @@ -1,10 +1,13 @@ package wordle.domain +import java.util.* + private const val LIMIT_SIZE = 6 class Results { val value: MutableList> = mutableListOf() + get() = Collections.unmodifiableList(field) fun add(result: List) { value.add(result) diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt index 9046f13..079d53b 100644 --- a/src/main/kotlin/wordle/domain/Words.kt +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -5,10 +5,8 @@ import java.time.LocalDate import java.time.temporal.ChronoUnit const val WORD_SIZE = 5 - val BASIC_DATE = LocalDate.of(2021, 6, 19)!! - -val WORDS: List = FileReader("src/main/resources/words.txt").readLines() +val WORDS = FileReader("src/main/resources/words.txt").readLines() fun LocalDate.pickTodayWord() = WORDS[(ChronoUnit.DAYS.between(BASIC_DATE, this) % WORDS.size).toInt()] From eb826ab020553b5fc8d404aba72ae27c6dc252df Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 21:35:40 +0900 Subject: [PATCH 22/24] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/domain/Answer.kt | 2 +- src/main/kotlin/wordle/domain/Game.kt | 7 +++---- src/main/kotlin/wordle/domain/Results.kt | 13 ++++++------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/wordle/domain/Answer.kt b/src/main/kotlin/wordle/domain/Answer.kt index 5856f25..a570c50 100644 --- a/src/main/kotlin/wordle/domain/Answer.kt +++ b/src/main/kotlin/wordle/domain/Answer.kt @@ -9,7 +9,7 @@ class Answer(private val answer: String) { require(answer.isInWords()) { "[ERROR] λͺ©λ‘μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ‹¨μ–΄μž…λ‹ˆλ‹€." } } - fun compareToWord(word: String): MutableList { + fun compareToWord(word: String): List { val result = MutableList(WORD_SIZE) { NONE } val wordTable = createWordTable(word) matchExact(word, result, wordTable) diff --git a/src/main/kotlin/wordle/domain/Game.kt b/src/main/kotlin/wordle/domain/Game.kt index 226523f..e18fbd0 100644 --- a/src/main/kotlin/wordle/domain/Game.kt +++ b/src/main/kotlin/wordle/domain/Game.kt @@ -14,10 +14,9 @@ class Game(private val word: String) { } } - private fun isOver(result: MutableList) = + private fun isOver(result: List) = results.isLimit() || result.all { it == Mark.EXACT } - fun findTryCount(): Int { - return results.value.size - } + fun findTryCount() = + results.findTryCount() } diff --git a/src/main/kotlin/wordle/domain/Results.kt b/src/main/kotlin/wordle/domain/Results.kt index 6705d76..811c11e 100644 --- a/src/main/kotlin/wordle/domain/Results.kt +++ b/src/main/kotlin/wordle/domain/Results.kt @@ -1,19 +1,18 @@ package wordle.domain -import java.util.* - private const val LIMIT_SIZE = 6 class Results { val value: MutableList> = mutableListOf() - get() = Collections.unmodifiableList(field) - fun add(result: List) { + fun add(result: MutableList) { value.add(result) } - fun isLimit(): Boolean { - return value.size >= LIMIT_SIZE - } + fun findTryCount() = + value.size + + fun isLimit() = + value.size >= LIMIT_SIZE } From cd01e8f925a52dbe6e6074abb90a10cc73a41008 Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Wed, 18 May 2022 23:43:24 +0900 Subject: [PATCH 23/24] =?UTF-8?q?refactor:=20Results=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/domain/Results.kt | 13 ++++++++----- src/main/kotlin/wordle/domain/Words.kt | 1 + src/main/kotlin/wordle/view/View.kt | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/wordle/domain/Results.kt b/src/main/kotlin/wordle/domain/Results.kt index 811c11e..7073717 100644 --- a/src/main/kotlin/wordle/domain/Results.kt +++ b/src/main/kotlin/wordle/domain/Results.kt @@ -4,15 +4,18 @@ private const val LIMIT_SIZE = 6 class Results { - val value: MutableList> = mutableListOf() + private val results: MutableList> = mutableListOf() - fun add(result: MutableList) { - value.add(result) + fun add(result: List) { + results.add(result) } fun findTryCount() = - value.size + results.size fun isLimit() = - value.size >= LIMIT_SIZE + results.size >= LIMIT_SIZE + + fun getReadOnlyResults(): List> = + results } diff --git a/src/main/kotlin/wordle/domain/Words.kt b/src/main/kotlin/wordle/domain/Words.kt index 079d53b..91af58a 100644 --- a/src/main/kotlin/wordle/domain/Words.kt +++ b/src/main/kotlin/wordle/domain/Words.kt @@ -5,6 +5,7 @@ import java.time.LocalDate import java.time.temporal.ChronoUnit const val WORD_SIZE = 5 + val BASIC_DATE = LocalDate.of(2021, 6, 19)!! val WORDS = FileReader("src/main/resources/words.txt").readLines() diff --git a/src/main/kotlin/wordle/view/View.kt b/src/main/kotlin/wordle/view/View.kt index f9f6086..4d6db24 100644 --- a/src/main/kotlin/wordle/view/View.kt +++ b/src/main/kotlin/wordle/view/View.kt @@ -34,7 +34,7 @@ fun printTryCount(tryCount: Int) { fun printTiles(results: Results) { println() - results.value.forEach { + results.getReadOnlyResults().forEach { printTile(it) } println() From e48beb293a00f8b29dfbdffe22b96123baf867ae Mon Sep 17 00:00:00 2001 From: jojogreen91 Date: Thu, 19 May 2022 00:29:32 +0900 Subject: [PATCH 24/24] =?UTF-8?q?refactor:=20Results=20=EC=97=90=20Backing?= =?UTF-8?q?=20properties=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/wordle/domain/Results.kt | 13 ++++++------- src/main/kotlin/wordle/view/View.kt | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/wordle/domain/Results.kt b/src/main/kotlin/wordle/domain/Results.kt index 7073717..434018a 100644 --- a/src/main/kotlin/wordle/domain/Results.kt +++ b/src/main/kotlin/wordle/domain/Results.kt @@ -4,18 +4,17 @@ private const val LIMIT_SIZE = 6 class Results { - private val results: MutableList> = mutableListOf() + private val _results: MutableList> = mutableListOf() + val results: List> + get() = _results fun add(result: List) { - results.add(result) + _results.add(result) } fun findTryCount() = - results.size + _results.size fun isLimit() = - results.size >= LIMIT_SIZE - - fun getReadOnlyResults(): List> = - results + _results.size >= LIMIT_SIZE } diff --git a/src/main/kotlin/wordle/view/View.kt b/src/main/kotlin/wordle/view/View.kt index 4d6db24..7391b51 100644 --- a/src/main/kotlin/wordle/view/View.kt +++ b/src/main/kotlin/wordle/view/View.kt @@ -34,7 +34,7 @@ fun printTryCount(tryCount: Int) { fun printTiles(results: Results) { println() - results.getReadOnlyResults().forEach { + results.results.forEach { printTile(it) } println()