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

🚀 1단계 - 지뢰 찾기(그리기) #351

Open
wants to merge 2 commits into
base: rladowl92
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
# kotlin-minesweeper
# 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
- 입출력
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/MineSweeperApplication.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
val height = inputConsoleView.inputHeight()
val width = inputConsoleView.inputWidth()
val count = inputConsoleView.inputMineCount()

val land = Land(height, width, count)
outputConsoleView.displayLand(land)
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/domain/Land.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package domain

class Land(val height: Int, val width: Int, val count: Int) {
val mines = mutableListOf<Mine>()

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)
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/domain/Mine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package domain

data class Mine(val x: Int, val y: Int)
18 changes: 18 additions & 0 deletions src/main/kotlin/view/InputConsoleView.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/view/OutputConsoleView.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
12 changes: 12 additions & 0 deletions src/test/kotlin/domain/LandTest.kt
Original file line number Diff line number Diff line change
@@ -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
}
})
10 changes: 10 additions & 0 deletions src/test/kotlin/domain/MineTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
})