Skip to content

Commit

Permalink
[step2] refactor: 인접 지뢰 생성시 Move 클래스 사용 안함
Browse files Browse the repository at this point in the history
  • Loading branch information
bong6981 committed Dec 31, 2023
1 parent 010aa66 commit e352119
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 38 deletions.
16 changes: 9 additions & 7 deletions src/main/kotlin/minesweeper/domain/board/MineBoardBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package minesweeper.domain.board

import minesweeper.domain.cell.Cell
import minesweeper.domain.cell.cells
import minesweeper.domain.position.Position
import minesweeper.domain.position.PositionPicker
import minesweeper.domain.position.Positions
import minesweeper.domain.position.positions

fun mineBoard(
minePicker: PositionPicker,
Expand All @@ -26,16 +28,16 @@ class MineBoardBuilder(
mineCount = count
}

fun build(): MineBoard = MineBoard(cells())
fun build(): MineBoard = MineBoard(createCells())

private fun cells(): Map<Position, Cell> =
minesweeper.domain.cell.cells {
positions(positions())
private fun createCells(): Map<Position, Cell> =
cells {
positions(createPositions())
}

private fun positions(): Positions =
minesweeper.domain.position.positions(minePicker) {
allPositions(size.allPositionsOfRowAndColumns)
private fun createPositions(): Positions =
positions(minePicker) {
allPositions(size.allPositions)
mineTotal(mineCount)
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/minesweeper/domain/board/MineBoardSize.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data class MineBoardSize(
val height: Height,
val width: Width,
) {
val allPositionsOfRowAndColumns: Set<Position> by lazy {
val allPositions: Set<Position> by lazy {
createAllPositions(height, width)
}

Expand Down
6 changes: 0 additions & 6 deletions src/main/kotlin/minesweeper/domain/cell/Move.kt

This file was deleted.

9 changes: 6 additions & 3 deletions src/main/kotlin/minesweeper/domain/game/MinesweeperGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ data class MinesweeperGame(
val cell = board.open(position)

if (cell.isZeroMineCount()) {
position.adjacentPositions.forEach { adjacentPosition ->
if (board.isValidPosition(adjacentPosition)) doOpen(adjacentPosition)
}
openAdjacentPositions(cell.position)
}
}

private fun openAdjacentPositions(position: Position) {
val adjacentPositions = position.adjacentPositions.filter { board.isValidPosition(it) }
adjacentPositions.forEach { doOpen(it) }
}
}
33 changes: 13 additions & 20 deletions src/main/kotlin/minesweeper/domain/position/Position.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package minesweeper.domain.position

import minesweeper.domain.cell.Move

data class Position(
val row: Int,
val column: Int,
Expand All @@ -12,27 +10,22 @@ data class Position(
}

val adjacentPositions: Set<Position>
get() = movesToAdjacentPositions.mapNotNull { this.moveOrNull(it) }.toSet()

private fun moveOrNull(move: Move): Position? {
val row = this.row - move.row
val column = this.column - move.column
if (row < MIN_ROW || column < MIN_COLUMN) return null
return Position(row, column)
}
get() = setOfNotNull(
createIfValid(row - 1, column - 1),
createIfValid(row - 1, column),
createIfValid(row - 1, column + 1),
createIfValid(row, column - 1),
createIfValid(row, column + 1),
createIfValid(row + 1, column - 1),
createIfValid(row + 1, column),
createIfValid(row + 1, column + 1),
)

companion object {
private const val MIN_ROW = 0
private const val MIN_COLUMN = 0
private val movesToAdjacentPositions = listOf(
Move(-1, -1),
Move(-1, 0),
Move(-1, 1),
Move(0, -1),
Move(0, 1),
Move(1, -1),
Move(1, 0),
Move(1, 1),
)

private fun createIfValid(row: Int, column: Int): Position? =
takeIf { row >= MIN_ROW && column >= MIN_COLUMN }?.let { Position(row, column) }
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/minesweeper/domain/position/Positions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ data class Positions(
val mineCountByPosition: Map<Position, Int> by lazy {
val adjacentMineCountByPosition = minePositions
.flatMap { it.adjacentPositions }
.filter { it in value }
.groupBy { it }
.mapValues { it.value.size }

Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/domain/board/MineBoardSizeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MineBoardSizeTest : DescribeSpec({
val size = MineBoardSize(Height(3), Width(2))

it("행(0..2)과 열(0..1)에 대한 Position 생성") {
val positions = size.allPositionsOfRowAndColumns
val positions = size.allPositions

val expect = setOf(
Position(0, 0), Position(0, 1),
Expand Down

0 comments on commit e352119

Please sign in to comment.