-
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
[Step2]: π 2λ¨κ³ - μ§λ’° μ°ΎκΈ°(μ§λ’° κ°μ) #414
base: dlwnsgus777
Are you sure you want to change the base?
Changes from all commits
33b75e5
4384cfe
937bfd1
87e088f
d7f3900
f7c94a6
3f540f9
8bfbda7
e70df0b
f7eebaf
57ec133
12754be
875d734
140ec03
31e02a4
fdbda58
cfdce4b
0a4a2ae
632971f
227cf4c
90a2b65
7e62807
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,32 +1,28 @@ | ||
package mineswipper.domain.map | ||
|
||
import mineswipper.domain.map.position.Position | ||
import mineswipper.domain.map.position.Row | ||
import mineswipper.domain.map.position.Size | ||
|
||
class Field( | ||
private val size: Size, | ||
positions: List<Position> | ||
val field: Map<Row, Pedals> | ||
) { | ||
val field: Map<Int, Pedals> | ||
fun findPedal(position: Position): Pedal { | ||
val pedals = field[position.toRow()] | ||
require(pedals != null) { VALID_MESSAGE } | ||
|
||
init { | ||
val initField: MutableMap<Int, Pedals> = mutableMapOf() | ||
repeat(size.height) { x -> | ||
initField[x] = pedalSetting(x, positions) | ||
} | ||
field = initField.toMap() | ||
return pedals.get(position.x) | ||
} | ||
|
||
private fun pedalSetting(x: Int, positions: List<Position>): Pedals { | ||
val pedalList = (0 until size.width).map { y -> | ||
val position = Position(x, y) | ||
createPedal(positions, position) | ||
} | ||
return Pedals(pedalList) | ||
fun getSize(): Size { | ||
val pedals = field[Row(FIRST_ROW_FOR_SIZE)] | ||
require(pedals != null) { VALID_MESSAGE } | ||
|
||
return Size(field.size, pedals.value.size) | ||
} | ||
|
||
private fun createPedal( | ||
positions: List<Position>, | ||
position: Position | ||
) = when (positions.contains(position)) { | ||
true -> Mine() | ||
false -> NormalPedal() | ||
companion object { | ||
private const val VALID_MESSAGE: String = "ν΄λΉ μμΉμ κ°μ΄ μ‘΄μ¬νμ§ μμ΅λλ€." | ||
private const val FIRST_ROW_FOR_SIZE: Int = 0 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package mineswipper.domain.map | ||
|
||
@JvmInline | ||
value class Mark( | ||
val value: String | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package mineswipper.domain.map | ||
|
||
class Mine : Pedal | ||
class Mine( | ||
override var mark: Mark? = Mark("*") | ||
) : Pedal |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package mineswipper.domain.map | ||
|
||
class NormalPedal : Pedal | ||
class NormalPedal( | ||
override var mark: Mark? = null | ||
) : Pedal |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
package mineswipper.domain.map | ||
|
||
sealed interface Pedal { | ||
} | ||
var mark: Mark? | ||
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. nullable λ³μλ‘ μ μ ν μ΄μ κ° μμκΉμ? |
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package mineswipper.domain.map.position | ||
|
||
data class Position( | ||
val x: Int, | ||
val y: Int | ||
) { | ||
fun getAroundPositions(size: Size): Positions { | ||
return (x - 1..x + 1).flatMap { xVal -> | ||
(y - 1..y + 1).map { yVal -> Position(xVal, yVal) } | ||
Comment on lines
+8
to
+9
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. x - 1, x - 1, y+1,y-1 λ±μ΄ 무μμ μλ―Έν κΉμ? |
||
}.filter { it.isValid(size.width, size.height) && it != this }.toPositions() | ||
} | ||
|
||
fun toRow(): Row { | ||
return Row(y) | ||
} | ||
|
||
private fun isValid(width: Int, height: Int): Boolean { | ||
return x in 0 until width && y in 0 until height | ||
} | ||
} | ||
|
||
fun List<Position>.toPositions(): Positions = Positions(this) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mineswipper.domain.map.position | ||
|
||
data class Positions( | ||
val positions: List<Position> | ||
) { | ||
fun contains(position: Position): Boolean { | ||
return positions.contains(position) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package mineswipper.domain.map.position | ||
|
||
@JvmInline | ||
value class Row(val value: Int) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package mineswipper.domain.map | ||
package mineswipper.domain.map.position | ||
|
||
data class Size( | ||
val width: Int, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package mineswipper.domain.map.util | ||
|
||
import mineswipper.domain.map.Field | ||
import mineswipper.domain.map.Mine | ||
import mineswipper.domain.map.NormalPedal | ||
import mineswipper.domain.map.Pedal | ||
import mineswipper.domain.map.Pedals | ||
import mineswipper.domain.map.position.Position | ||
import mineswipper.domain.map.position.Positions | ||
import mineswipper.domain.map.position.Row | ||
import mineswipper.domain.map.position.Size | ||
|
||
class FieldFactory( | ||
private val size: Size | ||
) { | ||
fun createField( | ||
// size: Size, | ||
minePositions: Positions | ||
): Field { | ||
val initField: MutableMap<Row, Pedals> = mutableMapOf() | ||
repeat(size.height) { x -> | ||
initField[Row(x)] = pedalSetting(x, minePositions) | ||
} | ||
|
||
val fakeField = Field(initField.toMap()) | ||
|
||
MarkGenerator.markGeneration(fakeField) | ||
|
||
return fakeField | ||
} | ||
private fun createPedal( | ||
minePositions: Positions, | ||
position: Position | ||
): Pedal { | ||
return if (minePositions.contains(position)) Mine() else NormalPedal() | ||
} | ||
|
||
private fun pedalSetting(x: Int, minePositions: Positions): Pedals { | ||
val pedalList = (0 until size.width).map { y -> | ||
val position = Position(x, y) | ||
createPedal(minePositions, position) | ||
} | ||
return Pedals(pedalList) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mineswipper.domain.map.util | ||
|
||
import mineswipper.domain.map.Field | ||
import mineswipper.domain.map.Mark | ||
import mineswipper.domain.map.Mine | ||
import mineswipper.domain.map.position.Position | ||
|
||
object MarkGenerator { | ||
fun markGeneration(field: Field, position: Position = Position(0, 0)): Mark { | ||
val pedal = field.findPedal(position) | ||
if (pedal.mark != null) return pedal.mark!! | ||
|
||
val aroundPositions = position.getAroundPositions(field.getSize()) | ||
val count = aroundPositions.positions.count { | ||
val findPedal = field.findPedal(it) | ||
findPedal is Mine | ||
} | ||
|
||
val mark = Mark(count.toString()) | ||
pedal.mark = mark | ||
|
||
aroundPositions.positions.forEach { | ||
markGeneration(field, it) | ||
} | ||
|
||
return mark | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package mineswipper.domain.map.util | ||
|
||
import mineswipper.domain.map.Position | ||
import mineswipper.domain.map.Size | ||
import mineswipper.domain.map.position.Positions | ||
import mineswipper.domain.map.position.Size | ||
|
||
interface MinePositionStrategy { | ||
fun createMinePosition(size: Size, mineAmount: Int): List<Position> | ||
} | ||
fun createMinePosition(size: Size, mineAmount: Int): Positions | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package mineswipper.domain.map | ||
|
||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
import mineswipper.domain.map.position.Position | ||
import mineswipper.domain.map.position.Positions | ||
import mineswipper.domain.map.position.Size | ||
import mineswipper.domain.map.util.FieldFactory | ||
import mineswipper.domain.map.util.MinePositionStrategy | ||
import org.junit.jupiter.api.Test | ||
|
||
class FieldTest { | ||
|
||
@Test | ||
fun `νλμλ μ§λ’° λ°νμ΄ μλ€`() { | ||
val size = Size(2, 2) | ||
val mine = 2 | ||
val field = FieldFactory(size).createField( | ||
MineMarkTestStrategy().createMinePosition(size, mine) | ||
) | ||
|
||
field.findPedal(Position(1, 0)).shouldBeInstanceOf<Mine>() | ||
} | ||
} | ||
|
||
class MineMarkTestStrategy : MinePositionStrategy { | ||
override fun createMinePosition(size: Size, mineAmount: Int): Positions { | ||
return Positions( | ||
listOf( | ||
Position(1, 0), | ||
Position(0, 1) | ||
) | ||
) | ||
} | ||
} |
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.
var λ‘ μ μν μ΄μ κ° μμκΉμ?
μΈλΆμμ μ½κ² λ³κ²½μ΄ κ°λ₯ν ꡬ쑰λ€μ!