-
Notifications
You must be signed in to change notification settings - Fork 230
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
[숫자 야구 게임] 장은영 미션 제출합니다. #228
base: main
Are you sure you want to change the base?
Changes from all commits
68fe5a5
2c4dce7
69c0e0a
d87543d
6fd78c9
c840134
f565746
d157e29
58a1d22
ba926b2
02ee8bb
3bfd8c8
d67c7aa
3d4b931
87023b3
9c537fe
3ccea4a
ef2ffc8
b2fe5bf
a807399
dd0f3ca
3b1f4d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#기능 목록 | ||
|
||
##Computer | ||
+숫자 야구 게임을 시작합니다. 가 콘솔창에 출력되며 게임 시작 | ||
+컴퓨터는 사용자가 맞춰야 하는 1부터 9까지 서로 다른 임의의 수로 이루어진 3자리 숫자 생성 | ||
-1~9까지의 숫자 | ||
-서로 다른 수로 이루어짐 | ||
-3자리 숫자 | ||
|
||
+사용자로 부터 숫자 입력 받기 | ||
+같은 수가 같은 자리에 있으면 스트라이크 | ||
-스트라이크 수 세기 | ||
+같은 수가 다른 자리에 있으면 볼 | ||
-볼 수 세기 | ||
+같은 수가 전혀 없으면 낫싱 | ||
+결과 출력 | ||
+맞는 답(3스트라이크)인지 확인 | ||
|
||
+과정 반복 | ||
+맞는 답 이면 게임 종료 | ||
+그에 따른 재시작/종료를 구분 하는 입력 받기 | ||
|
||
|
||
##User | ||
+숫자를 입력해주세요 : 가 콘솔 창에 표시 | ||
+숫자로 입력 받기 | ||
-1부터 9까지의 수인지 확인 | ||
-서로 다른 수로 이루어 졌는지 확인 | ||
-3자리 수인지 확인 | ||
+숫자가 아닌 입력인 경우 IllegalArgumentException 발생 시킨 후 종료 | ||
|
||
+게임이 끝난 경우 재시작/종료를 구분 하는 입력 받기 | ||
-1일 경우 재시작 | ||
-2일 경우 종료 | ||
+그 외의 경우 입력 예외 | ||
|
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. | ||
#Mon Oct 23 00:09:44 KST 2023 | ||
sdk.dir=C\:\\Users\\User\\AppData\\Local\\Android\\Sdk |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,120 @@ | ||
package baseball | ||
|
||
import camp.nextstep.edu.missionutils.Randoms | ||
import camp.nextstep.edu.missionutils.Console | ||
import java.lang.IllegalArgumentException | ||
|
||
const val START_MESSAGE = "숫자 야구 게임을 시작합니다." | ||
const val INPUT_NUMBER_MESSAGE = "숫자를 입력해주세요 : " | ||
const val THREE_STRIKE_MESSAGE = "3개의 숫자를 모두 맞히셨습니다! 게임 종료" | ||
const val RESTART_MESSAGE = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." | ||
Comment on lines
+7
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상수화 시켜주셨는데 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum class 생각도 못했네요,,..! 좋은 의견 감사합니다 !
Comment on lines
+7
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥰 |
||
|
||
|
||
|
||
fun main() { | ||
TODO("프로그램 구현") | ||
var computer: List<Int> = listOf() | ||
var user: List<Int> | ||
var areUReady = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 귀엽네요 areUReady ㅎㅎ |
||
var gameRestart = true | ||
|
||
println(START_MESSAGE) | ||
while(areUReady or gameRestart){ | ||
if(gameRestart) { | ||
computer = generateRandomNumber() | ||
} | ||
user = userInput() | ||
areUReady = game(computer,user) | ||
gameRestart = gameOver(areUReady) | ||
} | ||
} | ||
|
||
fun generateRandomNumber(): List<Int> { | ||
|
||
val computer = mutableListOf<Int>() | ||
while (computer.size < 3) { | ||
val randomNumber = Randoms.pickNumberInRange(1, 9) | ||
if (!computer.contains(randomNumber)) { | ||
computer.add(randomNumber) | ||
} | ||
} | ||
println(computer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 디버그용으로 작성하신 출력문인 것 같은데, 커밋 전에는 꼭 지워주세요! |
||
return computer | ||
} | ||
|
||
fun userInput(): List<Int> { | ||
print(INPUT_NUMBER_MESSAGE) | ||
val user = Console.readLine().map {it.digitToInt()} | ||
|
||
if(user.contains(0)) { | ||
throw IllegalArgumentException("1~9 사이의 값을 입력해주세요") | ||
} | ||
if(user.size != 3) { | ||
throw IllegalArgumentException("3자리 수를 입력해주세요") | ||
} | ||
if(user.toSet().size != 3) { | ||
throw IllegalArgumentException("중복 되지 않는 숫자를 입력해주세요") | ||
} | ||
if(user.isEmpty()){ | ||
throw IllegalArgumentException("값을 넣어주세요") | ||
} | ||
Comment on lines
+48
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return user | ||
} | ||
|
||
fun game(computer: List<Int>, user: List<Int>):Boolean { | ||
var ball = 0 | ||
var strike = 0 | ||
|
||
for (i in computer.indices) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 지영님께 배웠는데, forEachIndexed 사용해도 좋을 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다음번엔 forEachIndexed 활용해보겠습니다 👍 감사합니다! |
||
if(user[i] !in computer){ | ||
continue | ||
} | ||
Comment on lines
+68
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 조건문은 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 고려하지 못했던 부분이네요,,감사합니다 👍 |
||
if (computer[i] == user[i]) { | ||
strike++ | ||
continue | ||
} | ||
else { | ||
ball++ | ||
} | ||
} | ||
println(compareNumber(ball, strike)) | ||
|
||
if (strike == 3) { | ||
println(THREE_STRIKE_MESSAGE) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
|
||
|
||
fun compareNumber(ball:Int, strike: Int): String { | ||
|
||
var str = "" | ||
if (ball == 0 && strike == 0) { | ||
print("낫싱") | ||
} | ||
else if (ball == 0 && strike > 0) { | ||
print("${strike}스트라이크") | ||
} | ||
else if (strike == 0 && ball > 0) { | ||
print("${ball}볼") | ||
}else{ | ||
print("${ball}볼 ${strike}스트라이크")} | ||
return str | ||
} | ||
Comment on lines
+90
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 승연님 말씀대로 val str = when() 해서 분기하면 더 좋을 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return 값으로 돌려줘야해서 구현에 급급해서 그렇게 짰었네요..! 다음번엔 when() 사용 해봐야겠습니다 ! |
||
|
||
|
||
fun gameOver(areUDone:Boolean): Boolean { | ||
val userReply : Int | ||
|
||
if(!areUDone){ | ||
println(RESTART_MESSAGE) | ||
userReply = Console.readLine().toInt() | ||
return when (userReply) { | ||
1 -> true | ||
2 -> false | ||
else -> throw IllegalArgumentException("1 또는 2를 선택해주세요") | ||
} | ||
} | ||
return false | ||
} |
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.
기본적으로 변수명과 클래스명은 명사로, 함수명은 동사로 지어주어야 합니다!
코틀린 컨벤션 가이드를 참고하시면 좋을 것 같아요.
코틀린 네이밍 규칙
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.
그리고 자동 정렬을 습관화하시면, 더 쉽게 코틀린 컨벤션을 지킬 수 있답니다!
코드를 읽어보니
:
띄어쓰기가 잘 안 되어 있는 것 같더라고요.자동 정렬 단축키
윈도우:
ctrl+shift+alt+L
맥:
command+option+L
Trailing Commas
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.
좋은 정보 감사합니다 @sseung416 님!
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.
좋은 정보 감사합니다 !