Skip to content

Step 5 - Car racing(Refactoring) #1816

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

Open
wants to merge 24 commits into
base: aparnapattathil
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c7cec37
update readme
AparnaPattathil Mar 25, 2025
e00f3c7
move person to a separate package
AparnaPattathil Mar 25, 2025
fec28f3
move operator related functions to Operator class
AparnaPattathil Mar 25, 2025
8273b5f
use require instead of throwing exceptions
AparnaPattathil Mar 25, 2025
9646e45
delete gitkeep
AparnaPattathil Mar 30, 2025
445215e
docs - update readme
AparnaPattathil Mar 30, 2025
c974519
feat - implement car racing
AparnaPattathil Mar 30, 2025
5f89321
test - added unit tests
AparnaPattathil Mar 30, 2025
99083f0
style - run detekt
AparnaPattathil Mar 30, 2025
9d89940
refactor - use more constant values
AparnaPattathil Mar 30, 2025
47e1620
step3: moving validation to init block
aparna-pattathil Apr 2, 2025
aef3dce
step3: views into separate package
aparna-pattathil Apr 2, 2025
a66de49
step3: extract car generation resposibility from CarRace class
aparna-pattathil Apr 2, 2025
ce081f0
step3: extract random number generation responsibility from CarRace c…
aparna-pattathil Apr 2, 2025
67f09a7
step4: update readme
aparna-pattathil Apr 7, 2025
2ad675d
step4: refactor to accept input as strings
aparna-pattathil Apr 7, 2025
60d096c
step4: find winner and display
aparna-pattathil Apr 7, 2025
6b6506a
step4: test for winner logic
aparna-pattathil Apr 7, 2025
8d08560
Merge branch 'aparnapattathil' into AparnaPattathil-step-4
AparnaPattathil Apr 7, 2025
6491ac2
step5: delete duplicated view after merge
aparna-pattathil Apr 14, 2025
bab40a5
step5: update readme
aparna-pattathil Apr 14, 2025
e1c490c
step5: Refactor CarRace to only focus on contolling the race flow
aparna-pattathil Apr 14, 2025
b46b0cd
step5: Refactor winner logic
aparna-pattathil Apr 14, 2025
1c148a7
Merge branch 'aparnapattathil' into AparnaPattathil-step-5
AparnaPattathil Apr 14, 2025
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
12 changes: 7 additions & 5 deletions src/main/kotlin/carracing/CarRace.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package carracing

import carracing.RandomNumberGenerator.getRandom
import carracing.view.ResultView

typealias RaceHistory = List<List<Car>>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continued with #1813 (comment)

So if I understand correct, the RaceHistory will be List<List>, right?

Yes, That's right!

Just a thought: what would change if RaceHistory were its own class rather than a typealias?
Would it give you more flexibility in handling race data or expressing domain logic more clearly?


class CarRace(val cars: List<Car>, val numRounds: Int) {

Expand All @@ -10,15 +11,16 @@ class CarRace(val cars: List<Car>, val numRounds: Int) {
require(numRounds > MIN_NUMBER_OF_ROUNDS) { "Select at least one round to start the race" }
}

fun startRace() {
private val raceHistory = mutableListOf<List<Car>>()
fun startRace(): RaceHistory {
repeat(numRounds) {
cars.forEach { it.move(random = getRandom()) }
ResultView.showStatus(cars)
raceHistory.add(cars.map { Car(it.name, it.position) })
}
ResultView.displayWinners(winners = getWinners())
return raceHistory
}

fun getWinners(): List<Car> {
fun getWinners(): List<Car> {
val maxPosition = cars.maxOf { it.position }
return cars.filter { it.position == maxPosition }
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/carracing/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package carracing

import carracing.view.InputView.getCarNames
import carracing.view.InputView.getRoundInput
import carracing.view.ResultView

fun main() {
val carsInput =
Expand All @@ -10,6 +11,7 @@ fun main() {

val cars = CarGenerator.generateCars(carsInput)
val race = CarRace(cars = cars, numRounds = numRounds)

race.startRace()
val raceHistory: RaceHistory = race.startRace()
raceHistory.forEach { _ -> ResultView.showStatus(cars) }
ResultView.displayWinners(winners = race.getWinners())
}