diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..fd7e28dc9 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,11 @@ +<구현할 기능 목록> + +1. 랜덤 값 추출하기 +2. 게임 시작 문구 출력하기 +3. 서로 다른 수 3자리 입력받기 +4. 입력 받은 값과 랜덤 값 비교하기 +5. 결과 출력하기 (맞힌 개수 표시/정답) +6. 재시작/종료 문구 출력하기 +7. 재시작/종료 선택하기 +8. 재시작 하기 +9. 종료하기 \ No newline at end of file diff --git a/local.properties b/local.properties new file mode 100644 index 000000000..66c9052e6 --- /dev/null +++ b/local.properties @@ -0,0 +1,8 @@ +## This file must *NOT* be checked into Version Control Systems, +# as it contains information specific to your local configuration. +# +# Location of the SDK. This is only used by Gradle. +# For customization when using a Version Control System, please read the +# header note. +#Tue Oct 24 23:56:26 KST 2023 +sdk.dir=C\:\\Users\\HOME\\AppData\\Local\\Android\\Sdk diff --git a/src/main/kotlin/baseball/Application.kt b/src/main/kotlin/baseball/Application.kt index 148d75cc3..72794449a 100644 --- a/src/main/kotlin/baseball/Application.kt +++ b/src/main/kotlin/baseball/Application.kt @@ -1,5 +1,90 @@ package baseball -fun main() { - TODO("프로그램 구현") +import camp.nextstep.edu.missionutils.Console +import camp.nextstep.edu.missionutils.Randoms + +fun inputCheck(inputNumber: String) { + if (inputNumber.length != 3) { + throw IllegalArgumentException () + } +} + +fun randomNum(): MutableList { + val computer = mutableListOf() + while (computer.size < 3) { + val randomNumber = Randoms.pickNumberInRange(1, 9) + if (!computer.contains(randomNumber)) { + computer.add(randomNumber) + } + } + return computer +} + +fun inputNum(): MutableList { + val user = mutableListOf() + print("숫자를 입력해주세요 : ") + val inputNumber = Console.readLine()!! + inputCheck(inputNumber) + for (i in inputNumber) { + user.add(i.digitToInt()) + } + return user +} + +fun hintResult(computerNum: MutableList, userNum: MutableList): Int { + var strike = 0 + var ball = 0 + var not = 0 + for (i in userNum.indices) { + if (userNum[i] == computerNum[i]) { + strike += 1 + } else if (computerNum.contains(userNum[i])) { + ball += 1 + } else { + not += 1 + } + } + if (strike == 3) { + println("${strike}스트라이크") + println("3개의 숫자를 모두 맞히셨습니다! 게임 종료") + } else if (not == 3) { + println("낫싱") + } else if (strike == 0 && ball == 0) { + println("낫싱") + } else if (strike > 0 && ball == 0) { + println("${strike}스트라이크") + } else if (strike == 0 && ball > 0) { + println("${ball}볼") + } else println("${ball}볼 ${strike}스트라이크 ") + return strike +} + +fun restartEnd(): Int { + println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") + val choice = Console.readLine()!!.toInt() + return choice } + +fun playGame() { + while (true) { + val computerNum = randomNum() + while (true) { + val userNum = inputNum() + val strike = hintResult(computerNum, userNum) + if (strike == 3) { + break + } + } + val userChoice = restartEnd() + if (userChoice == 1) { + continue + } else { + break + } + } +} + +fun main() { + println("숫자 야구 게임을 시작합니다.") + playGame() +} \ No newline at end of file