-
Notifications
You must be signed in to change notification settings - Fork 118
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
[둘리] 2단계 자동차 경주 제출합니다. #68
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3e97c71
refactor(CarsHelper): companion object를 object로 수정
hyemdooly cb95bad
refactor(CarsInfo): CarsInfo 삭제, Cars/Car 클래스만 사용
hyemdooly 89d4dd8
refactor(InputView, RaceGame): 반복을 RaceGame이 아닌 InputView에서 하도록 수정
hyemdooly e37d339
refactor(RaceGame): 간접 호출 삭제
hyemdooly affbefb
refactor(Car,Cars): backing property 수정
hyemdooly b6834c8
refactor(Cars,RaceGame): 랜덤값 생성 로직을 RaceGame으로 이동
hyemdooly 1b39386
refactor(CarsHelper): listOf() -> emptyList()로 변경
hyemdooly e818230
refactor(CarsHelper): CarsHelper -> WinnersFinder로 이름 변경
hyemdooly 18e68de
refactor(CarTest): initTest -> checkCarCreateTest 이름 변경
hyemdooly f282093
refactor(InputView,RaceGame): 잘못된 값에 대한 함수 반복 여부 RaceGame에서 결정하도록 수정
hyemdooly 4e8ad55
refactor(Constants, InputView, OutputView, RaceGame): 네이밍 변경
hyemdooly 90c2b1d
refactor(Cars): backing property 삭제
hyemdooly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,49 @@ | ||
package controller | ||
|
||
import generator.RandomGenerator | ||
import model.Cars | ||
import util.CarsHelper | ||
import util.Validator | ||
import util.Constants | ||
import util.WinnersFinder | ||
import view.InputView | ||
import view.OutputView | ||
|
||
class RaceGame(private val outputView: OutputView, private val inputView: InputView) { | ||
|
||
fun run() { | ||
val cars = executeInputCarNames() | ||
val tryNumber = executeInputTryNumber() | ||
outputView.outputCarNames() | ||
val cars = Cars(inputCarNames()) | ||
outputView.outputNumber() | ||
val tryNumber = inputNumber().toInt() | ||
outputView.outputResults() | ||
repeat(tryNumber) { | ||
tryMove(cars) | ||
} | ||
outputView.outputWinners(CarsHelper.findWinners(cars)) | ||
outputView.outputWinners(WinnersFinder.findWinners(cars)) | ||
} | ||
|
||
private fun tryMove(cars: Cars) { | ||
repeat(cars.getCarSize()) { | ||
cars.move(it) | ||
outputView.outputResult(cars.getCarInfo(it)) | ||
cars.move(it, RandomGenerator().getRandomNumber()) | ||
outputView.outputResult(cars.getCar(it)) | ||
} | ||
outputView.outputNextLine() | ||
} | ||
|
||
private fun executeInputTryNumber(): Int { | ||
var result: Int? | ||
do { | ||
outputView.outputTryNumber() | ||
result = getInputTryNumber(inputView.inputTryNumber()) | ||
} while (result == null) | ||
return result | ||
} | ||
|
||
private fun getInputTryNumber(number: String): Int? { | ||
return runCatching { | ||
Validator().checkTryNumber(number) | ||
number.toInt() | ||
}.onFailure { | ||
outputView.outputErrorMessage(it.message ?: "에러가 발생했습니다.") | ||
}.getOrNull() | ||
} | ||
|
||
private fun executeInputCarNames(): Cars { | ||
var result: Cars? | ||
do { | ||
outputView.outputCarNames() | ||
result = getInputCarNames(inputView.inputCarNames()) | ||
} while (result == null) | ||
return result | ||
private fun inputCarNames(): String { | ||
var input = inputView.inputCarNames() | ||
while (input.isNullOrBlank() || input.contains("[ERROR]")) { | ||
outputView.outputErrorMessage(input ?: Constants.INPUT_IS_NULL) | ||
input = inputView.inputCarNames() | ||
} | ||
return input | ||
} | ||
|
||
private fun getInputCarNames(cars: String): Cars? { | ||
return runCatching { | ||
Validator().checkNames(cars) | ||
Cars(cars) | ||
}.onFailure { | ||
outputView.outputErrorMessage(it.message ?: "에러가 발생했습니다.") | ||
}.getOrNull() | ||
private fun inputNumber(): String { | ||
var input = inputView.inputNumber() | ||
while (input.isNullOrBlank() || input.contains("[ERROR]")) { | ||
outputView.outputErrorMessage(input ?: Constants.INPUT_IS_NULL) | ||
input = inputView.inputNumber() | ||
} | ||
return input | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,10 @@ | ||
package model | ||
|
||
import generator.RandomGenerator | ||
|
||
class Cars(private val cars: List<Car>) { | ||
class Cars(val cars: List<Car>) { | ||
constructor(input: String) : this(input.split(",").mapIndexed { _, name -> Car(name.trim()) }) | ||
|
||
fun getCarInfos(): List<CarInfo> { | ||
val carInfos = mutableListOf<CarInfo>() | ||
repeat(cars.size) { | ||
carInfos.add(cars[it].getInfo()) | ||
} | ||
return carInfos | ||
} | ||
|
||
fun getCarInfo(index: Int): CarInfo = cars[index].getInfo() | ||
fun getCar(index: Int): Car = cars[index] | ||
fun getCarSize(): Int = cars.size | ||
fun move(index: Int) { | ||
cars[index].move(RandomGenerator().getRandomNumber()) | ||
fun move(index: Int, condition: Int) { | ||
cars[index].move(condition) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package util | ||
|
||
import model.Cars | ||
|
||
object WinnersFinder { | ||
fun findWinners(cars: Cars): List<String> { | ||
val equalCars = cars.cars.groupBy({ it.position }, { it.name }) | ||
return equalCars[equalCars.keys.max()]?.toList() ?: emptyList() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
package view | ||
|
||
import util.Validator | ||
|
||
class InputView { | ||
fun inputCarNames(): String { | ||
return readLine() ?: "" | ||
fun inputCarNames(): String? { | ||
var input = readlnOrNull() | ||
runCatching { | ||
Validator().checkNames(input) | ||
}.onFailure { | ||
input = it.message | ||
} | ||
return input | ||
} | ||
|
||
fun inputTryNumber(): String { | ||
return readLine() ?: "" | ||
fun inputNumber(): String? { | ||
var input = readlnOrNull() | ||
runCatching { | ||
Validator().checkTryNumber(input) | ||
}.onFailure { | ||
input = it.message | ||
} | ||
return input | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
컨트롤러에서 유효한 입력값을 받을때까지 계속해서 호출하도록 잘 개선해주셨네요. 👍
참고로, 프로그래밍 요구사항에는 유효한 입력값을 받을때까지 계속해서 호출하는 항목은 없습니다. 😅