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

[step1] minesweeper(draw) #437

Merged
merged 15 commits into from
Dec 14, 2024
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# kotlin-minesweeper
# kotlin-minesweeper

## step1

### requirements

- [x] 높이와 너비, 지뢰 개수를 입력받을 수 있다
- [x] 지뢰는 눈에 잘 띄는 것으로 표기한다
- 지뢰(*), 일반(C)
- [x] 지뢰는 가급적 랜덤에 가깝게 배치한다

### Board

- [x] 높이와 너비, 지뢰 갯수를 가지고 있어야 한다
- [x] Board 라인(Row)들을 가지고 있어야 한다

### Cell

- [x] Island, Mine 두개로 구분된다

### BoardLine

- [x] Cell들을 가지고 있다(너비)

### BoardLines

- [x] BoardLine들을 가지고 있다(높이)

Empty file removed src/main/kotlin/.gitkeep
Empty file.
23 changes: 23 additions & 0 deletions src/main/kotlin/minsweeper/MinesweeperRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package minsweeper

import minsweeper.domain.Board
import minsweeper.view.InputView
import minsweeper.view.ResultView

class MinesweeperRunner {

fun run() {
val board = Board(
InputView.showAndGetHeight(),
InputView.showAndGetWidth(),
InputView.showAndGetMineCount(),
)

ResultView.printBoard(board.boardLines)
}

}

fun main() {
MinesweeperRunner().run()
}
11 changes: 11 additions & 0 deletions src/main/kotlin/minsweeper/domain/Board.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package minsweeper.domain

class Board(
height: Int,
width: Int,
mineCount: Int,
) {

Choose a reason for hiding this comment

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

프로그래밍 요구사항에는 아래와 같은 항목이 있습니다.

3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다.

이 요구사항을 반영해보면 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

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


val boardLines: BoardLines = BoardLinesGenerator.generate(height, width, mineCount)

}
3 changes: 3 additions & 0 deletions src/main/kotlin/minsweeper/domain/BoardLine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package minsweeper.domain

data class BoardLine(val cells: List<Cell>)
3 changes: 3 additions & 0 deletions src/main/kotlin/minsweeper/domain/BoardLines.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package minsweeper.domain

data class BoardLines(val lines: List<BoardLine>)
18 changes: 18 additions & 0 deletions src/main/kotlin/minsweeper/domain/BoardLinesGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package minsweeper.domain

object BoardLinesGenerator {

fun generate(height: Int, width: Int, mineCount: Int): BoardLines {

Choose a reason for hiding this comment

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

내가 원하는 좌표에 지뢰를 심을 수 있는지 테스트 코드로 확인해볼 수 있도록 개선해보면 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

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

1e6b1ec
BoardLinesGeneratorTest 에서 지뢰를 내가 원하는 위치에 심는 로직 테스트 해보는데 mineCount를 7이 아닌 다른 값으로 넣어도 테스트가 통과가 됩니다.

mineCount도 Param이 가지고 있어야 하고 내가 원하는 만큼 원하는 위치에 지뢰를 심어야 하다보니 둘 사이에 괴리가 생겨난거 같습니다. 혹시 관련하여 좋은 방법이 있을까요?

Choose a reason for hiding this comment

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

@jihoi-kang
mineCount를 Param에서 꼭 가져야하는 정보일까요? 🤔

Copy link
Author

Choose a reason for hiding this comment

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

@BeokBeok
큰 힌트가 되었습니다. 감사합니다🙏🙏

val minePositions = createMinePositions(height, width, mineCount)
return BoardLines(List(height) { row ->
BoardLine(List(width) { col ->
val position = row * width + col
if (position in minePositions) Cell.Mine else Cell.Island
})
})
}

private fun createMinePositions(height: Int, width: Int, mineCount: Int) =
(0 until height * width).shuffled().take(mineCount)

}
6 changes: 6 additions & 0 deletions src/main/kotlin/minsweeper/domain/Cell.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package minsweeper.domain

sealed interface Cell {
data object Island : Cell
data object Mine : Cell
}
20 changes: 20 additions & 0 deletions src/main/kotlin/minsweeper/view/InputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package minsweeper.view

object InputView {

fun showAndGetWidth(): Int {
println("너비를 입력하세요.")
return readln().toInt()
}

fun showAndGetHeight(): Int {
println("높이를 입력하세요.")
return readln().toInt()
}

fun showAndGetMineCount(): Int {
println("지뢰는 몇 개인가요?")
return readln().toInt()

Choose a reason for hiding this comment

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

너비와 높이를 곱한 값보다 큰 수치의 값을 입력하게 되면 어떻게 될까요? 🤔

Copy link
Author

Choose a reason for hiding this comment

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

}

}
22 changes: 22 additions & 0 deletions src/main/kotlin/minsweeper/view/ResultView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package minsweeper.view

import minsweeper.domain.BoardLines
import minsweeper.domain.Cell

object ResultView {

fun printBoard(boardLines: BoardLines) {
println(buildString {
append("지뢰찾기 게임 시작\n")
append(boardLines.lines.joinToString(separator = "\n") { boardLine ->
boardLine.cells.joinToString(separator = " ") { it.print() }
})
})
}

private fun Cell.print(): String = when (this) {
Cell.Island -> "*"
Cell.Mine -> "C"
}

}