-
Notifications
You must be signed in to change notification settings - Fork 196
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
Changes from 9 commits
1b888f0
8ffddef
f3468eb
ff98a52
442fd93
31b8821
b1d0dde
6b17947
6c7688d
888c3ac
1e6b1ec
3593718
3d2f7a2
6c2557a
addd195
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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들을 가지고 있다(높이) | ||
|
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() | ||
} |
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, | ||
) { | ||
|
||
val boardLines: BoardLines = BoardLinesGenerator.generate(height, width, mineCount) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package minsweeper.domain | ||
|
||
data class BoardLine(val cells: List<Cell>) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package minsweeper.domain | ||
|
||
data class BoardLines(val lines: List<BoardLine>) |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 내가 원하는 좌표에 지뢰를 심을 수 있는지 테스트 코드로 확인해볼 수 있도록 개선해보면 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1e6b1ec
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jihoi-kang There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
} |
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 | ||
} |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 너비와 높이를 곱한 값보다 큰 수치의 값을 입력하게 되면 어떻게 될까요? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
} |
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" | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
프로그래밍 요구사항에는 아래와 같은 항목이 있습니다.
이 요구사항을 반영해보면 어떨까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
888c3ac