-
Notifications
You must be signed in to change notification settings - Fork 206
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λ¨κ³ - μ§λ’° μ°ΎκΈ°(그리기) #417
base: giibeom
Are you sure you want to change the base?
Changes from all commits
4b9794d
cb274d8
2a0cb93
19f43ca
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 | ||
|
||
## λ―Έμ λ΄μ© | ||
|
||
### STEP 1 | ||
|
||
<details> | ||
<summary>μ κΈ°/νΌμΉκΈ°</summary> | ||
<div markdown="1"> | ||
|
||
#### [μꡬ μ¬ν λΆμ] | ||
|
||
- λμ΄μ λλΉ, μ§λ’° κ°μλ₯Ό μ λ ₯λ°μ μ μλ€. | ||
- μ§λ’°λ λμ μ λλ κ²μΌλ‘ νκΈ°νλ€. | ||
- μ§λ’°λ κ°κΈμ λλ€μ κ°κΉκ² λ°°μΉνλ€. | ||
|
||
#### [κΈ°λ₯ λͺ©λ‘] | ||
|
||
- [x] λμ΄λ₯Ό μ λ ₯ λ°λλ€. | ||
- [x] λλΉλ₯Ό μ λ ₯ λ°λλ€. | ||
- [x] μ§λ’° κ°μλ₯Ό μ λ ₯λ°λλ€. | ||
- [x] 2μ°¨ λ°°μ΄μ μ λ ₯λ μ§λ’° κ°μλ§νΌ λλ€μΌλ‘ μ§λ’°(π£)λ₯Ό μμ±νλ€. | ||
- [x] 2μ°¨ λ°°μ΄μ μΆλ ₯νλ€. | ||
|
||
</div> | ||
</details> | ||
|
||
<br> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package game.minesweeper | ||
|
||
import game.minesweeper.domain.GameBoard | ||
import game.minesweeper.ui.Input | ||
import game.minesweeper.ui.Output | ||
|
||
fun main() { | ||
val height = Input.getHeight() | ||
val width = Input.getWidth() | ||
val minesNumber = Input.getMinesNumber() | ||
|
||
val gameBoard = GameBoard(height, width) | ||
|
||
Output.printStartGamePrompt() | ||
gameBoard.startGame(minesNumber) | ||
|
||
Output.printGameResult(gameBoard) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package game.minesweeper.domain | ||
|
||
class GameBoard(height: Int, width: Int) { | ||
private val board = Array(height) { Array(width) { "C" } } | ||
|
||
fun startGame(minesNumber: Int) { | ||
MineGenerator.create(board, minesNumber) | ||
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. MineGeneratorλΌλ μ±κΈν΄κ°μ²΄μ μ κ·Όνλ ꡬ쑰λ€μ |
||
} | ||
|
||
override fun toString(): String { | ||
return board.joinToString("\n") { row -> | ||
row.joinToString(" ") | ||
} | ||
Comment on lines
+11
to
+13
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. ν΄λΉ λ‘μ§μ Viewκ΄λ ¨ λ‘μ§μ μλκΉμ? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package game.minesweeper.domain | ||
|
||
object MineGenerator { | ||
fun create(board: Array<Array<String>>, minesNumber: Int) { | ||
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. Array<Array>λ 무μμ μλ―Έν κΉμ?
μ νμ©ν΄λ³΄λ©΄ μ΄λ¨κΉμ? |
||
val height = board.size | ||
val width = board[0].size | ||
var minesPlaced = 0 | ||
|
||
while (minesPlaced < minesNumber) { | ||
val randomRow = (0 until height).random() | ||
val randomCol = (0 until width).random() | ||
|
||
if (board[randomRow][randomCol] != "*") { | ||
board[randomRow][randomCol] = "*" | ||
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. ν리미ν°λ‘ μ λ¬λ°μ boardκ° λ³κ²½λλ ꡬ쑰λ€μ, |
||
minesPlaced++ | ||
} | ||
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,56 @@ | ||
package game.minesweeper.ui | ||
|
||
import game.minesweeper.domain.GameBoard | ||
|
||
object Input { | ||
private const val HEIGHT_PROMPT = "λμ΄λ₯Ό μ λ ₯νμΈμ." | ||
private const val WIDTH_PROMPT = "λλΉλ₯Ό μ λ ₯νμΈμ." | ||
private const val NUMBER_OF_MINES_PROMPT = "μ§λ’°λ λͺ κ°μΈκ°μ?" | ||
|
||
private const val INVALID_NUMBER_PROMPT = "μ ν¨ν μ«μλ₯Ό μ λ ₯ν΄μΌ ν©λλ€." | ||
private const val INVALID_HEIGHT_RANGE_PROMPT = "λμ΄λ 0λ³΄λ€ μ»€μΌ ν©λλ€." | ||
private const val INVALID_WIDTH_RANGE_PROMPT = "λλΉλ 0λ³΄λ€ μ»€μΌ ν©λλ€." | ||
private const val INVALID_MINES_NUMBER_PROMPT = "μ§λ’° κ°μλ 0λ³΄λ€ μ»€μΌ ν©λλ€." | ||
|
||
fun getHeight(): Int { | ||
println(HEIGHT_PROMPT) | ||
|
||
val height = readlnOrNull()?.toIntOrNull() | ||
requireNotNull(height) { INVALID_NUMBER_PROMPT } | ||
require(height > 0) { INVALID_HEIGHT_RANGE_PROMPT } | ||
Comment on lines
+18
to
+20
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.
|
||
|
||
return height | ||
} | ||
|
||
fun getWidth(): Int { | ||
println(WIDTH_PROMPT) | ||
|
||
val width = readlnOrNull()?.toIntOrNull() | ||
requireNotNull(width) { INVALID_NUMBER_PROMPT } | ||
require(width > 0) { INVALID_WIDTH_RANGE_PROMPT } | ||
|
||
return width | ||
} | ||
|
||
fun getMinesNumber(): Int { | ||
println(NUMBER_OF_MINES_PROMPT) | ||
|
||
val minesNumber = readlnOrNull()?.toIntOrNull() | ||
requireNotNull(minesNumber) { INVALID_NUMBER_PROMPT } | ||
require(minesNumber > 0) { INVALID_MINES_NUMBER_PROMPT } | ||
|
||
return minesNumber | ||
} | ||
} | ||
|
||
object Output { | ||
private const val GAME_START_PROMPT = "μ§λ’°μ°ΎκΈ° κ²μ μμ" | ||
|
||
fun printStartGamePrompt() { | ||
println(GAME_START_PROMPT) | ||
} | ||
|
||
fun printGameResult(gameBoard: GameBoard) { | ||
println(gameBoard) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package game.minesweeper.domain | ||
|
||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.data.forAll | ||
import io.kotest.data.row | ||
import io.kotest.matchers.shouldBe | ||
|
||
class MineGeneratorTest : StringSpec({ | ||
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. GameBoard κ΄λ ¨ ν μ€νΈ μ½λλ μμ±ν΄λ³΄λ©΄ μ΄λ¨κΉμ? |
||
|
||
"μ§λ’° μμ±μ κ°μ κ³³μ μ€λ³΅ν΄μ μμ±λμ§ μκ³ , μ§λ’° κ°μλ§νΌ λλ€μΌλ‘ μμ±λλ€." { | ||
forAll( | ||
row(10), | ||
row(9), | ||
row(8), | ||
row(7), | ||
row(6), | ||
row(2), | ||
row(1), | ||
) { minesNumber: Int -> | ||
val gameBoard = Array(10) { Array(10) { "C" } } | ||
MineGenerator.create(gameBoard, minesNumber) | ||
|
||
val minesCount = gameBoard.sumOf { row -> row.count { cell -> cell == "*" } } | ||
minesCount shouldBe minesNumber | ||
} | ||
} | ||
}) |
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.
"C"λΌλ건 무μμ μλ―Έν κΉμ?
μꡬμ¬νμ μ§μΌλ³΄μμ!