-
Notifications
You must be signed in to change notification settings - Fork 180
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
Step1 - 지뢰 찾기(그리기) #333
Open
sumniy
wants to merge
10
commits into
next-step:sumniy
Choose a base branch
from
sumniy:step1
base: sumniy
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step1 - 지뢰 찾기(그리기) #333
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f63f5b
docs: 요구사항 정의
turner-lee a30cf29
feat: 지뢰찾기에 필요한 정보 입력 받기
turner-lee 88f25ef
feat: 입력 받은 정보로 지도 정보를 생성한다
turner-lee c73e707
chore: lint 적용
turner-lee 4ba37f9
feat: 지뢰 개수만큼 지도에 지뢰를 설치
turner-lee 7dcf403
feat: 지뢰 찾기 게임 출력
turner-lee 0d1adf3
refactor: 객체지향원칙 적용
turner-lee 1ace61c
refactor: 객체지향원칙 적용
turner-lee 802cd55
refactor: 네이밍을 자연스럽게 변경
turner-lee af2ae00
refactor: 지뢰의 위치를 생성할 때 랜덤으로 만들지않고 변수로 받도록 변경
turner-lee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
# kotlin-minesweeper | ||
# kotlin-minesweeper | ||
|
||
- [x] 지뢰 찾기에 필요한 정보(높이, 너비, 지뢰 개수)를 입력 받는다 | ||
- [x] 높이 * 너비 만큼의 좌표 정보를 생성한다 | ||
- [x] 지뢰 개수만큼 랜덤한 지뢰의 위치를 구해 해당 좌표에 지뢰를 설치한다 | ||
- [x] 지뢰 찾기 게임을 출력한다 |
Empty file.
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,22 @@ | ||
package minesweeper | ||
|
||
import minesweeper.domain.Height | ||
import minesweeper.domain.MineIndexes | ||
import minesweeper.domain.MineMap | ||
import minesweeper.domain.MineMapSize | ||
import minesweeper.domain.RandomIndexesGenerator | ||
import minesweeper.domain.Width | ||
import minesweeper.view.InputView | ||
import minesweeper.view.ResultView | ||
|
||
fun main() { | ||
val height = Height(InputView.receiveHeight()) | ||
val width = Width(InputView.receiveWidth()) | ||
val mineMapSize = MineMapSize(width, height) | ||
val mineCount = InputView.receiveMineCount() | ||
val mineIndexes = MineIndexes(RandomIndexesGenerator.generate(mineCount, mineMapSize.size())) | ||
|
||
val mineMap = MineMap(mineMapSize, mineIndexes) | ||
|
||
ResultView.printMineGame(mineMap) | ||
} |
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,11 @@ | ||
package minesweeper.domain | ||
|
||
class Height(val value: Int) { | ||
init { | ||
require(value >= MIN_HEIGHT_VALUE) { "height는 $MIN_HEIGHT_VALUE 이상만 허용됩니다." } | ||
} | ||
|
||
companion object { | ||
const val MIN_HEIGHT_VALUE = 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package minesweeper.domain | ||
|
||
class MineIndexes(private val indexes: List<Int>) { | ||
init { | ||
require(indexes.size >= MIN_MINE_COUNT_VALUE) { "지뢰 개수는 $MIN_MINE_COUNT_VALUE 이상만 허용됩니다." } | ||
} | ||
|
||
fun contains(index: Int): Boolean { | ||
return indexes.contains(index) | ||
} | ||
|
||
fun size(): Int { | ||
return indexes.size | ||
} | ||
|
||
companion object { | ||
private const val MIN_MINE_COUNT_VALUE = 0 | ||
} | ||
} |
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,49 @@ | ||
package minesweeper.domain | ||
|
||
class MineMap(private val mineMapSize: MineMapSize, mineIndexes: MineIndexes) { | ||
private val size: Int = mineMapSize.size() | ||
val map: List<Point> | ||
|
||
init { | ||
require(size >= mineIndexes.size()) { "지도 크기는 지뢰 개수이상이어야 합니다." } | ||
|
||
val height = mineMapSize.height() | ||
val width = mineMapSize.width() | ||
|
||
map = points(height, width, mineIndexes) | ||
} | ||
|
||
fun width(): Int { | ||
return mineMapSize.width() | ||
} | ||
|
||
fun getPoint(index: Int): Point { | ||
return map[index] | ||
} | ||
|
||
private fun points( | ||
height: Int, | ||
width: Int, | ||
mineIndexes: MineIndexes | ||
): List<Point> { | ||
return (1..height).flatMap { y -> | ||
generateRow(width, mineIndexes, y) | ||
} | ||
} | ||
|
||
private fun generateRow( | ||
width: Int, | ||
mineIndexes: MineIndexes, | ||
y: Int | ||
) = (1..width).map { x -> | ||
point(mineIndexes.contains(mineMapSize.getIndex(y, x)), x, y) | ||
} | ||
|
||
private fun point(isMine: Boolean, x: Int, y: Int): Point { | ||
if (isMine) { | ||
return MinePoint(x, y) | ||
} | ||
|
||
return Point(x, y) | ||
} | ||
} |
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,19 @@ | ||
package minesweeper.domain | ||
|
||
class MineMapSize(private val width: Width, private val height: Height) { | ||
fun size(): Int { | ||
return width.value * height.value | ||
} | ||
|
||
fun width(): Int { | ||
return width.value | ||
} | ||
|
||
fun height(): Int { | ||
return height.value | ||
} | ||
|
||
fun getIndex(heightIndex: Int, widthIndex: Int): Int { | ||
return (heightIndex - 1) * width() + (widthIndex - 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package minesweeper.domain | ||
|
||
class MinePoint(x: Int, y: Int) : Point(x, y) { | ||
override val symbol = MINE_SYMBOL | ||
|
||
companion object { | ||
private const val MINE_SYMBOL = "*" | ||
} | ||
} |
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,18 @@ | ||
package minesweeper.domain | ||
|
||
open class Point(x: Int, y: Int) { | ||
open val symbol: String = POINT_SYMBOL | ||
|
||
init { | ||
require(x >= Width.MIN_WIDTH_VALUE) { "x 좌표 값은 width의 최소 값 이상이어야합니다." } | ||
require(y >= Height.MIN_HEIGHT_VALUE) { "y 좌표 값은 height의 최소 값 이상이어야합니다." } | ||
} | ||
|
||
fun symbol(): String { | ||
return symbol | ||
} | ||
|
||
companion object { | ||
private const val POINT_SYMBOL = "C" | ||
} | ||
} |
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,9 @@ | ||
package minesweeper.domain | ||
|
||
object RandomIndexesGenerator { | ||
fun generate(size: Int, maximum: Int): List<Int> { | ||
return (0..maximum) | ||
.shuffled() | ||
.take(size) | ||
} | ||
} |
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,11 @@ | ||
package minesweeper.domain | ||
|
||
class Width(val value: Int) { | ||
init { | ||
require(value >= MIN_WIDTH_VALUE) { "width는 $MIN_WIDTH_VALUE 이상만 허용됩니다." } | ||
} | ||
|
||
companion object { | ||
const val MIN_WIDTH_VALUE = 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package minesweeper.view | ||
|
||
object InputView { | ||
fun receiveHeight(): Int { | ||
println("높이를 입력하세요.") | ||
|
||
return receiveNotNullNumber() | ||
} | ||
|
||
fun receiveWidth(): Int { | ||
println() | ||
println("너비를 입력하세요.") | ||
|
||
return receiveNotNullNumber() | ||
} | ||
|
||
fun receiveMineCount(): Int { | ||
println() | ||
println("지뢰는 몇 개인가요?") | ||
|
||
return receiveNotNullNumber() | ||
} | ||
|
||
private fun receiveString(): String { | ||
var input: String? = null | ||
|
||
do { | ||
input = readlnOrNull() | ||
} while (input.isNullOrBlank()) | ||
|
||
return input | ||
} | ||
|
||
private fun receiveNotNullNumber(): Int { | ||
var input: Int? = receiveString().toIntOrNull() | ||
|
||
while (input == null) { | ||
input = receiveString().toIntOrNull() | ||
} | ||
|
||
return input | ||
} | ||
} |
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,16 @@ | ||
package minesweeper.view | ||
|
||
import minesweeper.domain.MineMap | ||
|
||
object ResultView { | ||
fun printMineGame(mineMap: MineMap) { | ||
println() | ||
println("지뢰찾기 게임 시작") | ||
|
||
mineMap.map | ||
.chunked(mineMap.width()) | ||
.map { line -> | ||
println(line.joinToString(" ") { it.symbol }) | ||
} | ||
} | ||
} |
Empty file.
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,12 @@ | ||
package minesweeper | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import minesweeper.domain.Height | ||
import org.junit.jupiter.api.Test | ||
|
||
class HeightTest { | ||
@Test | ||
fun `height는 음수 값으로 생성 시 예외가 발생한다`() { | ||
shouldThrow<IllegalArgumentException> { Height(0) } | ||
} | ||
} |
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,18 @@ | ||
package minesweeper | ||
|
||
import io.kotest.matchers.shouldBe | ||
import minesweeper.domain.Height | ||
import minesweeper.domain.MineMapSize | ||
import minesweeper.domain.Width | ||
import org.junit.jupiter.api.Test | ||
|
||
class MineMapSizeTest { | ||
@Test | ||
fun `size는 width와 height의 value의 곱이다`() { | ||
val width = Width(5) | ||
val height = Height(5) | ||
val mineMapSize = MineMapSize(width, height) | ||
|
||
mineMapSize.size() shouldBe 25 | ||
} | ||
} |
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,36 @@ | ||
package minesweeper | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.matchers.types.shouldBeTypeOf | ||
import minesweeper.domain.Height | ||
import minesweeper.domain.MineIndexes | ||
import minesweeper.domain.MineMap | ||
import minesweeper.domain.MineMapSize | ||
import minesweeper.domain.MinePoint | ||
import minesweeper.domain.Width | ||
import org.junit.jupiter.api.Test | ||
|
||
class MineMapTest { | ||
@Test | ||
fun `지뢰 개수가 지도 크기를 초과하면 예외가 발생한다`() { | ||
val height = Height(1) | ||
val width = Width(1) | ||
val mineMapSize = MineMapSize(width, height) | ||
val mineIndexes = MineIndexes(listOf(1, 2, 3)) | ||
|
||
shouldThrow<IllegalArgumentException> { MineMap(mineMapSize, mineIndexes) } | ||
} | ||
|
||
@Test | ||
fun `mineIndexes에 표시된 위치에 지뢰가 심어진다`() { | ||
val height = Height(10) | ||
val width = Width(10) | ||
val mineMapSize = MineMapSize(width, height) | ||
val mineIndexes = MineIndexes(listOf(1, 2, 3)) | ||
val mineMap = MineMap(mineMapSize, mineIndexes) | ||
|
||
mineMap.getPoint(1).shouldBeTypeOf<MinePoint>() | ||
mineMap.getPoint(2).shouldBeTypeOf<MinePoint>() | ||
mineMap.getPoint(3).shouldBeTypeOf<MinePoint>() | ||
} | ||
} |
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,13 @@ | ||
package minesweeper | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import minesweeper.domain.Point | ||
import org.junit.jupiter.api.Test | ||
|
||
class PointTest { | ||
@Test | ||
fun `좌표정보는 height와 width 의 최소 값 조건을 따른다`() { | ||
shouldThrow<IllegalArgumentException> { Point(0, 1) } | ||
shouldThrow<IllegalArgumentException> { Point(1, 0) } | ||
} | ||
} |
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,12 @@ | ||
package minesweeper | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import minesweeper.domain.Width | ||
import org.junit.jupiter.api.Test | ||
|
||
class WidthTest { | ||
@Test | ||
fun `width는 음수 값으로 생성 시 예외가 발생한다`() { | ||
shouldThrow<IllegalArgumentException> { Width(0) } | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
내가 원하는 좌표에 지뢰를 심어보고, 그 좌표에 정말 지뢰가 있는지 확인해볼 수 있도록 테스트 코드를 작성해보면 어떨까요?