diff --git a/README.md b/README.md index 4c354962f..3cf20f3b2 100644 --- a/README.md +++ b/README.md @@ -1 +1,39 @@ -# kotlin-minesweeper \ No newline at end of file +# kotlin-minesweeper + +### 기능 요구사항 +지뢰 찾기를 변형한 프로그램을 구현한다. + +- 높이와 너비, 지뢰 개수를 입력받을 수 있다. +- 지뢰는 눈에 잘 띄는 것으로 표기한다. +- 지뢰는 가급적 랜덤에 가깝게 배치한다. + +### 실행 결과 +``` +높이를 입력하세요. +10 + +너비를 입력하세요. +10 + +지뢰는 몇 개인가요? +10 + +지뢰찾기 게임 시작 +C C C * C C C * C C +C C * C * C C C C C +C C C C C C C C C C +C C C C C C C C C C +* C C C C C C C C C +C C C C C C * C C C +C C * C C C * C C C +C C C C C C * C C * +C C C C C C C C C C +C C C C C C C C C C +``` + +### 기능 목록 +- land + - height, width, mines +- mine + - x, y +- 입출력 \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index e78e72956..43c17967b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,7 +13,7 @@ repositories { dependencies { testImplementation("org.junit.jupiter", "junit-jupiter", "5.8.2") testImplementation("org.assertj", "assertj-core", "3.22.0") - testImplementation("io.kotest", "kotest-runner-junit5", "5.2.3") + testImplementation("io.kotest", "kotest-runner-junit5", "5.5.0") } tasks { diff --git a/src/main/kotlin/MineSweeperApplication.kt b/src/main/kotlin/MineSweeperApplication.kt new file mode 100644 index 000000000..a7f48557a --- /dev/null +++ b/src/main/kotlin/MineSweeperApplication.kt @@ -0,0 +1,18 @@ +import domain.Land +import view.InputConsoleView +import view.OutputConsoleView + +object MineSweeperApplication { + private val inputConsoleView = InputConsoleView() + private val outputConsoleView = OutputConsoleView() + + @JvmStatic + fun main(args: Array) { + val height = inputConsoleView.inputHeight() + val width = inputConsoleView.inputWidth() + val count = inputConsoleView.inputMineCount() + + val land = Land(height, width, count) + outputConsoleView.displayLand(land) + } +} diff --git a/src/main/kotlin/domain/Land.kt b/src/main/kotlin/domain/Land.kt new file mode 100644 index 000000000..8be6ab62d --- /dev/null +++ b/src/main/kotlin/domain/Land.kt @@ -0,0 +1,21 @@ +package domain + +class Land(val height: Int, val width: Int, val count: Int) { + val mines = mutableListOf() + + init { + repeat(count) { + var newMine = makeRandomPosition() + while (mines.contains(newMine)) { + newMine = makeRandomPosition() + } + mines.add(newMine) + } + } + + private fun makeRandomPosition(): Mine { + val x = (1..width).random() + val y = (1..height).random() + return Mine(x, y) + } +} diff --git a/src/main/kotlin/domain/Mine.kt b/src/main/kotlin/domain/Mine.kt new file mode 100644 index 000000000..0cba3ddd8 --- /dev/null +++ b/src/main/kotlin/domain/Mine.kt @@ -0,0 +1,3 @@ +package domain + +data class Mine(val x: Int, val y: Int) diff --git a/src/main/kotlin/view/InputConsoleView.kt b/src/main/kotlin/view/InputConsoleView.kt new file mode 100644 index 000000000..9dc706215 --- /dev/null +++ b/src/main/kotlin/view/InputConsoleView.kt @@ -0,0 +1,18 @@ +package view + +class InputConsoleView { + fun inputHeight(): Int { + println("높이를 입력하세요.") + return readln().toInt() + } + + fun inputWidth(): Int { + println("너비를 입력하세요.") + return readln().toInt() + } + + fun inputMineCount(): Int { + println("지뢰는 몇 개인가요?") + return readln().toInt() + } +} diff --git a/src/main/kotlin/view/OutputConsoleView.kt b/src/main/kotlin/view/OutputConsoleView.kt new file mode 100644 index 000000000..44bb7799b --- /dev/null +++ b/src/main/kotlin/view/OutputConsoleView.kt @@ -0,0 +1,20 @@ +package view + +import domain.Land +import domain.Mine + +class OutputConsoleView { + fun displayLand(land: Land) { + println("지뢰찾기 게임 시작") + for (y in 1..land.height) { + for (x in 1..land.width) { + if (land.mines.contains(Mine(x, y))) { + print("* ") + } else { + print("C ") + } + } + println() + } + } +} diff --git a/src/test/kotlin/domain/LandTest.kt b/src/test/kotlin/domain/LandTest.kt new file mode 100644 index 000000000..6a424509b --- /dev/null +++ b/src/test/kotlin/domain/LandTest.kt @@ -0,0 +1,12 @@ +package domain + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class LandTest : StringSpec({ + "Land 생성 성공" { + val count = 10 + val land = Land(10, 10, count) + land.mines.size shouldBe count + } +}) diff --git a/src/test/kotlin/domain/MineTest.kt b/src/test/kotlin/domain/MineTest.kt new file mode 100644 index 000000000..c3ebd3b1d --- /dev/null +++ b/src/test/kotlin/domain/MineTest.kt @@ -0,0 +1,10 @@ +package domain + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class MineTest : StringSpec({ + "mine 생성 성공" { + Mine(1, 2) shouldBe Mine(1, 2) + } +})