Skip to content
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

Step3 - 지뢰 찾기(게임 실행) #421

Open
wants to merge 7 commits into
base: oyj7677
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ fun main() {
val height = InputView.inputHeight()
val width = InputView.inputWidth()
val mineCnt = InputView.inputMineCnt()

val mapInfo = MapInfo(height, width, mineCnt)
val game = MinesWeeperGame(Board(mapInfo), InputView, OutputView)

val board = Board(mapInfo)
OutputView.drawBoard(board)
game.startGame()
// OutputView.drawBoard(board)
}
72 changes: 72 additions & 0 deletions src/main/kotlin/MinesWeeperGame.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import map.Board
import map.Position
import view.InputViewInterface
import view.OutputViewInterface
import java.util.LinkedList
import java.util.Queue

class MinesWeeperGame(
private val board: Board,
private val inputView: InputViewInterface,
private val outputView: OutputViewInterface,
) {

fun startGame() {
while (true) {
val positionStr: String = inputView.inputSelect()

val position = positionStr.toPosition()
if (!board.validatePosition(position)) {
continue
}

aroundOpenWithNotMine(position)

if (isLose(position)) break

outputView.drawBoard(board)

if (isWin()) break
}
}

private fun isWin(): Boolean {
if (board.getMineCnt() == board.getCloseCellCnt()) {
outputView.printGameClear()
return true
}
return false
}

private fun isLose(position: Position): Boolean {
if (board.isMine(position)) {
outputView.printGameOver()
return true
}
return false
}

private fun aroundOpenWithNotMine(position: Position) {
// 첫 오픈 셀.
val queue: Queue<Position> = LinkedList()
queue.add(position)

while (queue.isNotEmpty()) {
val newPosition = queue.poll()

board.open(newPosition)
queue.addAll(board.getAroundPosition(newPosition))
queue.distinct()
}
}

private fun String.toPosition(): Position {
val positionList = splitWithComma(this)
return Position(positionList[1], positionList[0])
}

private fun splitWithComma(input: String): List<Int> {
return input.split(',')
.map { it.trim().toInt() }
}
}
95 changes: 69 additions & 26 deletions src/main/kotlin/map/Board.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,60 @@ class Board(private val mapInfo: MapInfo) {
settingBoard()
}

fun open(position: Position) {
mineBoard[position.y][position.x].openCell()
}

fun getAroundPosition(position: Position): List<Position> {
val aroundPosition = mutableListOf<Position>()
val searchingPosition = mineBoard[position.y][position.x].searchAround(mapInfo.height, mapInfo.width)
.filter { closeNoneCell(it) }

aroundPosition.addAll(searchingPosition)

return aroundPosition
}

fun validatePosition(position: Position): Boolean {
// 1. 보드를 넘어가는 셀.
if (!validateIndex(position)) {
return false
}

// 2. 이미 오픈된 셀
if (validateOpen(position)) {
return false
}

return true
}

fun isMine(position: Position): Boolean {
return mineBoard[position.y][position.x] is Mine
}

fun getMineCnt(): Int {
return mapInfo.mineCnt
}

fun getCloseCellCnt(): Int {
return mineBoard.sumOf { it.count { cell -> !cell.isOpen() } }
}

private fun validateOpen(position: Position): Boolean {
return mineBoard[position.y][position.x].isOpen()
}

private fun validateIndex(position: Position): Boolean {
return position.y >= INDEX_ZERO &&
position.y < mapInfo.height &&
position.x >= INDEX_ZERO &&
position.x < mapInfo.width
}

private fun closeNoneCell(position: Position) =
mineBoard[position.y][position.x] is None && !mineBoard[position.y][position.x].isOpen()

private fun settingBoard() {
settingMines(mapInfo.mineCnt)
settingMineCntInfo(mapInfo)
Expand All @@ -20,22 +74,22 @@ class Board(private val mapInfo: MapInfo) {
private fun createBoard(): MutableList<MutableList<Cell>> {
val height = mapInfo.height
val width = mapInfo.width
return MutableList(height) { x -> MutableList(width) { y -> None(x, y) } }
return MutableList(height) { y -> MutableList(width) { x -> None(Position(y, x)) } }
}

private fun settingMines(count: Int) {
val maxValue = getBoardMaxValue()
val positions = randomLogic.createRandomNumList(count, maxValue)

positions.forEach { position ->
val rowIndex = linearIndexToRowIndex(position)
val columnIndex = linearIndexToColumIndex(position)
setMine(rowIndex, columnIndex)
val y = linearIndexToRowIndex(position)
val x = linearIndexToColumIndex(position)
setMine(y, x)
}
}

private fun setMine(rowIndex: Int, columnIndex: Int) {
mineBoard[rowIndex][columnIndex] = Mine
private fun setMine(y: Int, x: Int) {
mineBoard[y][x] = Mine()
}

private fun getBoardMaxValue(): Int {
Expand All @@ -61,50 +115,39 @@ class Board(private val mapInfo: MapInfo) {
}

private fun settingMineCntInfo(mapInfo: MapInfo) {
for (x in INDEX_ZERO until mapInfo.height) {
setMineCntInfoRow(mapInfo, x)
for (y in INDEX_ZERO until mapInfo.height) {
setMineCntInfoRow(mapInfo, y)
}
}

private fun setMineCntInfoRow(mapInfo: MapInfo, x: Int) {
for (y in INDEX_ZERO until mapInfo.width) {
setMineCntInfo(x, y)
private fun setMineCntInfoRow(mapInfo: MapInfo, y: Int) {
for (x in INDEX_ZERO until mapInfo.width) {
setMineCntInfo(y, x)
}
}

private fun setMineCntInfo(x: Int, y: Int) {
val cell = mineBoard[x][y]
private fun setMineCntInfo(y: Int, x: Int) {
val cell = mineBoard[y][x]
if (cell is None) {
val mineCnt = getMineCnt(cell)
cell.mineCnt = mineCnt
}
}

private fun getMineCnt(cell: None): Int {

val cellX = cell.x
val cellY = cell.y
var mineCnt = 0

for (addIndex in RelativeDirection.values()) {
val newX = cellX + addIndex.x
val newY = cellY + addIndex.y

mineCnt = increaseMineCnt(mineCnt, newX, newY)
for (around in cell.searchAround(mapInfo.height, mapInfo.width)) {
mineCnt = increaseMineCnt(mineCnt, around.y, around.x)
}

return mineCnt
}

private fun increaseMineCnt(mineCnt: Int, newX: Int, newY: Int): Int {
if (!checkIndex(newX, newY)) return mineCnt
return if (mineBoard[newX][newY] is Mine) mineCnt + 1 else mineCnt
}

private fun checkIndex(newX: Int, newY: Int): Boolean {
return newX >= INDEX_ZERO && newX < mapInfo.width && newY >= INDEX_ZERO && newY < mapInfo.height
}

companion object {
private const val INDEX_OFFSET = 1
private const val INDEX_ZERO = 0
Expand Down
51 changes: 48 additions & 3 deletions src/main/kotlin/map/Cell.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
package map

sealed class Cell
class None(val x: Int, val y: Int, var mineCnt: Int = 0) : Cell()
sealed class Cell(private var isOpen: Boolean = false) : CellFunc {

object Mine : Cell()
override fun openCell() {
isOpen = true
}

override fun isOpen() = isOpen
}

class None(private val position: Position, var mineCnt: Int = 0) : Cell() {

override fun searchAround(maxHeight: Int, maxWidth: Int): List<Position> {
val aroundPosition = mutableListOf<Position>()
val maxPosition = Position(maxHeight, maxWidth)

for (addIndex in RelativeDirection.values()) {
val newPosition = Position(position.y + addIndex.x, position.x + addIndex.y)
if (checkIndex(newPosition, maxPosition)) aroundPosition.add(newPosition)
}

return aroundPosition
}

private fun checkIndex(newPosition: Position, maxPosition: Position): Boolean {
return newPosition.y >= INDEX_ZERO &&
newPosition.y < maxPosition.y &&
newPosition.x >= INDEX_ZERO &&
newPosition.x < maxPosition.x
}

companion object {
private const val INDEX_ZERO = 0
}
}

class Mine : Cell() {

override fun searchAround(maxHeight: Int, maxWidth: Int): List<Position> {
return emptyList()
}
}

interface CellFunc {
fun searchAround(maxHeight: Int, maxWidth: Int): List<Position>

fun openCell()

fun isOpen(): Boolean
}
3 changes: 3 additions & 0 deletions src/main/kotlin/map/Position.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package map

data class Position(val y: Int, val x: Int)
28 changes: 27 additions & 1 deletion src/main/kotlin/view/InputView.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package view

object InputView {
object InputView : InputViewInterface {
private const val ERR_MSG_INVALID_NUMERIC_FORMAT = "입력된 값의 포맷이 숫자가 압니다."
private const val ERR_MSG_MINIMUM_SIZE = "최소 1 이상을 입력 해야 합니다."
private const val TEXT_INPUT_HEIGHT = "높이를 입력하세요."
private const val TEXT_INPUT_WEIGHT = "너비를 입력하세요."
private const val TEXT_INPUT_MINE = "지뢰는 몇 개 인가요?"
private const val TEXT_SELECT_CELL = "오픈할 위치를 입력해 주세요 : "

fun inputHeight(): Int {
println(TEXT_INPUT_HEIGHT)
Expand All @@ -28,6 +29,13 @@ object InputView {
return inputData.toInt()
}

override fun inputSelect(): String {
println(TEXT_SELECT_CELL)
val inputData = readln()
validateInputPosition(inputData)
return inputData
}

private fun validateInputData(inputData: String) {
validateNumericFormat(inputData)
validateMinimumValue(inputData.toInt())
Expand All @@ -40,4 +48,22 @@ object InputView {
private fun validateMinimumValue(inputData: Int) {
require(inputData > 0) { ERR_MSG_MINIMUM_SIZE }
}

private fun validateInputPosition(inputData: String) {
// position x,y
validateSplitSize(inputData)
splitWithComma(inputData).map { validateNumericFormat(it) }
}

private fun validateSplitSize(inputData: String) {
val positionList = splitWithComma(inputData)
require(positionList.size == 2)
}

private fun splitWithComma(input: String): List<String> {

return input.split(',')
.map { it.trim() }
.filter { it.isNotBlank() }
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/view/InputViewInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package view

interface InputViewInterface {
fun inputSelect(): String
}
Loading