diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..6ff1628bd --- /dev/null +++ b/docs/README.md @@ -0,0 +1,14 @@ +# 기능 목록 + + ## 1. 게임 시작 +- 메세지 출력 +- 컴퓨터 난수 생성, 저장 + ## 2. 게임 진행 +- 사용자 입력 저장 +- 사용자 / 컴퓨터 정답 비교 +- 스트라이크, 볼 수 출력 + ## 3. 게임 종료 +- 정답 시 새 게임 또는 프로그램 종료 + ## 4. 게임 비정상 종료 +- 정답시 서로 다른 숫자 3개 외 입력 +- 새 게임 진행 선택 시 1, 2을 제외한 입력 \ No newline at end of file diff --git a/src/main/kotlin/baseball/Application.kt b/src/main/kotlin/baseball/Application.kt index 148d75cc3..bf29ee7cf 100644 --- a/src/main/kotlin/baseball/Application.kt +++ b/src/main/kotlin/baseball/Application.kt @@ -1,5 +1,10 @@ package baseball +import baseball.controller.BaseBallController +import camp.nextstep.edu.missionutils.Randoms +import kotlin.system.exitProcess + fun main() { - TODO("프로그램 구현") + val newGame = BaseBallController() + newGame.play() } diff --git a/src/main/kotlin/baseball/controller/BaseBallController.kt b/src/main/kotlin/baseball/controller/BaseBallController.kt new file mode 100644 index 000000000..fabc3d93b --- /dev/null +++ b/src/main/kotlin/baseball/controller/BaseBallController.kt @@ -0,0 +1,32 @@ +package baseball.controller +import baseball.model.Computer +import baseball.model.User +import baseball.view.View + +class BaseBallController { + fun play() { + val newUser = User() + val newView = View() + try { + var restart : String = "1" + newView.gameStart() + while (restart == "1") { + val computer = Computer.pickNumbers() + var breakIdx : Boolean = false + + while (!breakIdx) { + newView.requireInput() + val input = readLine() + val user = newUser.guess(input) + val baseball = Computer.baseball(user, computer) + val strikes = baseball.first + val balls = baseball.second + breakIdx = newView.check(strikes, balls) + } + restart = newView.restart() + } + } catch (e: IllegalArgumentException) { + throw IllegalArgumentException() + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/baseball/model/Computer.kt b/src/main/kotlin/baseball/model/Computer.kt new file mode 100644 index 000000000..aaeee4ee5 --- /dev/null +++ b/src/main/kotlin/baseball/model/Computer.kt @@ -0,0 +1,35 @@ +package baseball.model + + +import camp.nextstep.edu.missionutils.Randoms + +object Computer { + //컴퓨터 난수 생성, 저장 + fun pickNumbers(): MutableList { + + val computer = mutableListOf() + while (computer.size < 3) { + val randomNumber = Randoms.pickNumberInRange(1, 9) + + if (!computer.contains(randomNumber)) { + computer.add(randomNumber) + } + } + return computer + } + // 사용자 / 컴퓨터 정답 비교 + fun baseball(user: MutableList, computer : MutableList) : Pair{ + + var strikes : Int = 0 + var balls : Int = 0 + for (i in 0..2) { + if (user[i] == computer[i]) { + strikes++ + } + else if (user[i] in computer.filterIndexed { index, _ -> index != i }) { + balls++ + } + } + return Pair(strikes, balls) + } +} \ No newline at end of file diff --git a/src/main/kotlin/baseball/model/User.kt b/src/main/kotlin/baseball/model/User.kt new file mode 100644 index 000000000..1faad62ba --- /dev/null +++ b/src/main/kotlin/baseball/model/User.kt @@ -0,0 +1,25 @@ +package baseball.model + +class User { + //사용자 입력 저장 + fun guess( input : String? ): MutableList { + val user = mutableListOf() + if (input.isNullOrEmpty()) { + throw IllegalArgumentException() + } + else if (input.matches(Regex("\\d{3}"))) { + val charSet = HashSet() + for (number in input) { + if (charSet.contains(number)) { + throw IllegalArgumentException() + } + charSet.add(number) + user.add(Character.getNumericValue(number)) + } + } + else { + throw IllegalArgumentException() + } + return user + } +} \ No newline at end of file diff --git a/src/main/kotlin/baseball/view/View.kt b/src/main/kotlin/baseball/view/View.kt new file mode 100644 index 000000000..c8725082a --- /dev/null +++ b/src/main/kotlin/baseball/view/View.kt @@ -0,0 +1,65 @@ +package baseball.view + +class View { + // 스트라이크, 볼 수 출력 + fun check(strikes: Int, balls: Int) : Boolean { + var breakIdx : Boolean = false + //판정 + // no balls + if (balls == 0) { + //낫싱 + if (strikes == 0) { + println("낫싱") + } + //온리 스트라이크 + else { + println( "${strikes}스트라이크" ) + if (strikes == 3) { + println("3개의 숫자를 모두 맞히셨습니다! 게임 종료\n" + + "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") + breakIdx = true + } + } + } + // balls, strikes + else if (strikes != 0) { + println("${balls}볼 ${strikes}스트라이크") + } + // no strikes + else { + println("${balls}볼") + } + return breakIdx + } + + fun restart() : String { + val restart = readLine().toString() + when (restart) + { + "1" -> + { + doNothing() + } + "2" -> + { + println("종료") + } + else -> + { + throw IllegalArgumentException() + } + } + return restart + } + + // 메세지 출력 함수 + fun gameStart() = println(GAME_START) + fun requireInput() = println(REQUIRE_INPUT) + + companion object { + const val GAME_START = "숫자 야구 게임을 시작합니다." + const val REQUIRE_INPUT = "숫자를 입력해주세요 : " + } + + fun doNothing() {} +} \ No newline at end of file diff --git a/src/test/kotlin/baseball/ApplicationTest.kt b/src/test/kotlin/baseball/ApplicationTest.kt index 5c4ad5c23..e7597f962 100644 --- a/src/test/kotlin/baseball/ApplicationTest.kt +++ b/src/test/kotlin/baseball/ApplicationTest.kt @@ -7,7 +7,8 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -class ApplicationTest : NsTest() { +class +ApplicationTest : NsTest() { @Test fun `게임종료 후 재시작`() { assertRandomNumberInRangeTest(