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

[숫자 야구 게임] 임가희 미션 제출합니다. #236

Open
wants to merge 7 commits into
base: main
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
11 changes: 11 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<구현할 기능 목록>

1. 랜덤 값 추출하기
2. 게임 시작 문구 출력하기
3. 서로 다른 수 3자리 입력받기
4. 입력 받은 값과 랜덤 값 비교하기
5. 결과 출력하기 (맞힌 개수 표시/정답)
6. 재시작/종료 문구 출력하기
7. 재시작/종료 선택하기
8. 재시작 하기
9. 종료하기
8 changes: 8 additions & 0 deletions local.properties
Original file line number Diff line number Diff line change
@@ -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
89 changes: 87 additions & 2 deletions src/main/kotlin/baseball/Application.kt
Original file line number Diff line number Diff line change
@@ -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<Int> {
val computer = mutableListOf<Int>()
while (computer.size < 3) {
val randomNumber = Randoms.pickNumberInRange(1, 9)
if (!computer.contains(randomNumber)) {
computer.add(randomNumber)
}
}
return computer
}

fun inputNum(): MutableList<Int> {
val user = mutableListOf<Int>()
print("숫자를 입력해주세요 : ")
val inputNumber = Console.readLine()!!
inputCheck(inputNumber)
for (i in inputNumber) {
user.add(i.digitToInt())
}
return user
}

fun hintResult(computerNum: MutableList<Int>, userNum: MutableList<Int>): 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()
}