-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: SafeCell 근처에 존재하는 LandMineCell개수를 세어 SafeCell에 할당해주는 로직 구현 * feat: SafeCell은 근처 지뢰 개수를 출력하도록 변경 * build: kotest-framework-datatest 추가 * study: kotest-framework-datatest 학습해보기 * test: withData를 사용하도록 변경 * refactor: LandMineCell -> Mine, Cells -> RowCells로 이름 변경 * refactor: 자기 자신은 제외하고 개수를 세도록 변경
- Loading branch information
1 parent
bca54a2
commit b382e2c
Showing
12 changed files
with
415 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,49 @@ | ||
package domain | ||
|
||
class Board(height: Int, width: Int, landMinesCount: Int) { | ||
val matrix: List<Cells> | ||
class Board(height: Int, width: Int, minesCount: Int) { | ||
val matrix: List<RowCells> | ||
|
||
init { | ||
val totalCountOfCells = height * width | ||
val cells = | ||
Cells.create( | ||
safeCellsCount = totalCountOfCells - landMinesCount, | ||
landMinesCount = landMinesCount, | ||
val rowCells = | ||
RowCells.create( | ||
safeCellsCount = totalCountOfCells - minesCount, | ||
minesCount = minesCount, | ||
shufflingStrategy = DefaultShufflingStrategy | ||
) | ||
|
||
matrix = | ||
(1..height).map { number -> | ||
val fromIndex = (number - 1) * width | ||
val toIndex = number * width | ||
cells.subList(fromIndex, toIndex) | ||
rowCells.subList(fromIndex, toIndex) | ||
} | ||
|
||
countMineNearBySafeCell() | ||
} | ||
|
||
private fun countMineNearBySafeCell() { | ||
matrix.forEachIndexed { index, rowCells -> | ||
rowCells.fillMineCountNearBySafeCell( | ||
previousRowCells = getPreviousCellsOf(index), | ||
nextRowCells = getNextCellsOf(index), | ||
) | ||
} | ||
} | ||
|
||
private fun getPreviousCellsOf(index: Int): RowCells? { | ||
return if (index == 0) { | ||
null | ||
} else { | ||
matrix[index - 1] | ||
} | ||
} | ||
|
||
private fun getNextCellsOf(index: Int): RowCells? { | ||
return if (index == matrix.lastIndex) { | ||
null | ||
} else { | ||
matrix[index + 1] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
package domain | ||
|
||
interface Cell | ||
interface Cell { | ||
fun isMine(): Boolean | ||
} | ||
|
||
class LandMineCell : Cell | ||
class Mine : Cell { | ||
override fun isMine(): Boolean { | ||
return true | ||
} | ||
} | ||
|
||
class SafeCell : Cell | ||
class SafeCell : Cell { | ||
var mineCountNearby: Int = 0 | ||
|
||
override fun isMine(): Boolean { | ||
return false | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package domain | ||
|
||
class RowCells(val elements: List<Cell>) { | ||
/** | ||
* [fromIndex] : 포함 | ||
* [toIndex] : 제외 | ||
*/ | ||
fun subList( | ||
fromIndex: Int, | ||
toIndex: Int, | ||
): RowCells { | ||
return RowCells(elements.subList(fromIndex, toIndex)) | ||
} | ||
|
||
operator fun get(index: Int): Cell { | ||
return elements[index] | ||
} | ||
|
||
fun fillMineCountNearBySafeCell(previousRowCells: RowCells?, nextRowCells: RowCells?) { | ||
elements.forEachIndexed { index, cell -> | ||
if (cell is SafeCell) { | ||
val previousCount = countMines(previousRowCells, index) | ||
val midCount = countMines(this, index) | ||
val nextCount = countMines(nextRowCells, index) | ||
|
||
cell.mineCountNearby = previousCount + midCount + nextCount | ||
} | ||
} | ||
} | ||
|
||
private fun countMines(otherRowCells: RowCells?, currentCellIndex: Int): Int { | ||
return when (otherRowCells) { | ||
null -> 0 | ||
this -> { | ||
val previousIndex = getFromIndex(currentCellIndex) | ||
val nextIndex = getToIndex(currentCellIndex) | ||
|
||
listOf(elements[previousIndex], elements[nextIndex]) | ||
.count { cell -> cell.isMine() } | ||
} | ||
else -> { | ||
val fromIndex = getFromIndex(currentCellIndex) | ||
val toIndex = getToIndex(currentCellIndex) | ||
|
||
otherRowCells.elements | ||
.slice(fromIndex..toIndex) | ||
.count { cell -> cell.isMine() } | ||
} | ||
} | ||
} | ||
|
||
private fun getFromIndex(currentCellIndex: Int): Int { | ||
return if (currentCellIndex == 0) { | ||
0 | ||
} else { | ||
currentCellIndex - 1 | ||
} | ||
} | ||
|
||
private fun getToIndex(currentCellIndex: Int): Int { | ||
return if (currentCellIndex == elements.lastIndex) { | ||
currentCellIndex | ||
} else { | ||
currentCellIndex + 1 | ||
} | ||
} | ||
|
||
companion object { | ||
fun create( | ||
safeCellsCount: Int, | ||
minesCount: Int, | ||
shufflingStrategy: ShufflingStrategy | ||
): RowCells { | ||
require(safeCellsCount > 0 && minesCount > 0) { "safeCellsCount와 minesCount는 0보다 커야 합니다." } | ||
|
||
val safeCells = (1..safeCellsCount).map { SafeCell() } | ||
val mines = (1..minesCount).map { Mine() } | ||
|
||
val cells = safeCells + mines | ||
return RowCells(shufflingStrategy.shuffle(cells)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.