-
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
Changes from 10 commits
3e97c71
cb95bad
89d4dd8
e37d339
affbefb
b6834c8
1b39386
e818230
18e68de
f282093
4e8ad55
90c2b1d
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 |
---|---|---|
@@ -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.outputTryNumber() | ||
val tryNumber = inputTryNumber().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 inputTryNumber(): String { | ||
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. 함수는 행위 혹은 행동을 의미하기 때문에, 동사구로 작성하곤 합니다. |
||
var input = inputView.inputTryNumber() | ||
while (input.isNullOrBlank() || input.contains("[ERROR]")) { | ||
outputView.outputErrorMessage(input ?: Constants.INPUT_IS_NULL) | ||
input = inputView.inputTryNumber() | ||
} | ||
return input | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,21 +1,14 @@ | ||||||||||
package model | ||||||||||
|
||||||||||
import generator.RandomGenerator | ||||||||||
class Cars(cars: List<Car>) { | ||||||||||
private val _cars: List<Car> = cars | ||||||||||
val cars: List<Car> get() = _cars | ||||||||||
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.
Suggested change
Cars 클래스에는 add를 하는 로직이 없습니다. |
||||||||||
|
||||||||||
class Cars(private 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 getCarSize(): Int = cars.size | ||||||||||
fun move(index: Int) { | ||||||||||
cars[index].move(RandomGenerator().getRandomNumber()) | ||||||||||
fun getCar(index: Int): Car = _cars[index] | ||||||||||
fun getCarSize(): Int = _cars.size | ||||||||||
fun move(index: Int, condition: Int) { | ||||||||||
_cars[index].move(condition) | ||||||||||
} | ||||||||||
} |
This file was deleted.
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() | ||
} | ||
} |
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 inputTryNumber(): String? { | ||
var input = readlnOrNull() | ||
runCatching { | ||
Validator().checkTryNumber(input) | ||
}.onFailure { | ||
input = it.message | ||
} | ||
return input | ||
} | ||
} |
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.
컨트롤러에서 유효한 입력값을 받을때까지 계속해서 호출하도록 잘 개선해주셨네요. 👍
참고로, 프로그래밍 요구사항에는 유효한 입력값을 받을때까지 계속해서 호출하는 항목은 없습니다. 😅