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

[숫자 야구 게임] 장민호 미션 제출합니다. #239

Open
wants to merge 1 commit 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
14 changes: 14 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 기능 목록

## 1. 게임 시작
- 메세지 출력
- 컴퓨터 난수 생성, 저장
## 2. 게임 진행
- 사용자 입력 저장
- 사용자 / 컴퓨터 정답 비교
- 스트라이크, 볼 수 출력
## 3. 게임 종료
- 정답 시 새 게임 또는 프로그램 종료
## 4. 게임 비정상 종료
- 정답시 서로 다른 숫자 3개 외 입력
- 새 게임 진행 선택 시 1, 2을 제외한 입력
7 changes: 6 additions & 1 deletion src/main/kotlin/baseball/Application.kt
Original file line number Diff line number Diff line change
@@ -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()
}
32 changes: 32 additions & 0 deletions src/main/kotlin/baseball/controller/BaseBallController.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
35 changes: 35 additions & 0 deletions src/main/kotlin/baseball/model/Computer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package baseball.model


import camp.nextstep.edu.missionutils.Randoms

object Computer {
//컴퓨터 난수 생성, 저장
fun pickNumbers(): 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 baseball(user: MutableList<Int>, computer : MutableList<Int>) : Pair<Int, Int>{

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)
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/baseball/model/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package baseball.model

class User {
//사용자 입력 저장
fun guess( input : String? ): MutableList<Int> {
val user = mutableListOf<Int>()
if (input.isNullOrEmpty()) {
throw IllegalArgumentException()
}
else if (input.matches(Regex("\\d{3}"))) {
val charSet = HashSet<Char>()
for (number in input) {
if (charSet.contains(number)) {
throw IllegalArgumentException()
}
charSet.add(number)
user.add(Character.getNumericValue(number))
}
}
else {
throw IllegalArgumentException()
}
return user
}
}
65 changes: 65 additions & 0 deletions src/main/kotlin/baseball/view/View.kt
Original file line number Diff line number Diff line change
@@ -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() {}
}
3 changes: 2 additions & 1 deletion src/test/kotlin/baseball/ApplicationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down