HighTide
10/18/2023, 5:42 PMStephan Schröder
10/18/2023, 6:27 PMjvmtoolchain
. I tend to disable the autodownload of JDKs by adding org.gradle.java.installations.auto-download=false
into my gradle.properties file, since my installed JDK should already be able to handle JVM8 bytecode.
• you don't have to specify MenuOption<out Actions>
if you push the out
into the declaration of MenuOption
-> data class MenuOption<out T : Actions> (...
• having val menuOptions: ArrayList<MenuOption<Actions>> = ArrayList()
being a global, mutable list. Global data is basically only ok, if it's immutable (not only the list, but also the elements it contains. MenuOption
seem immutable, but they contain Actions
which are not. Also your list type is ArrayList, which is a mutable List. Mutable data is fine(-ish), but try to wrap it in a class to make access to it safer
• MenuLoader.displayChoices
doesn't need a MutableList/ArrayList
to work, so it should only required an unmodifiable one: fun displayChoices(optionsToDisplay: ArrayList<MenuOption<Actions>>) {
• MenuLoader.selectedOptionCaller
needs a MutableList
but doesn't need to know that it's an ArrayList
-> fun selectedOptionCaller(choiceIndex: Int, optionsToDisplay: MutableList<MenuOption<Actions>>): Actions {
• maybe it would be a good idea if MenuLoader
was the owner of menuOptions
!? It could hold a private mutable version and only provide an unmodifiable List to the outside while still being able to mutate the list itself.
object MenuLoader {
private val menuOptions: MutableList<MenuOption<Actions>> =
ArrayList().apply {
add(MenuOption(OpenShopAction(), "Open Shop"))
add(MenuOption(ExitAction(), "Exit"))
}
fun displayChoices() {
for ((i, menuOption) in optionsToDisplay.withIndex()) {
println("$i: ${menuOption.dialogToScreen}");
}
}
fun selectedOptionCaller(choiceIndex: Int): Actions {
val newActionToCall = menuOptions.get(choiceIndex).action
if (newActionToCall.reducable == true) {
menuOptions.removeAt(choiceIndex); // as long as this is single-threaded it's fine, otherwise we need a ReadWriteLock around reading from/writing to menuOptions
}
return newActionToCall;
}
}
A game is a great way to learn programming. Keep at it! If you finish it, even if the game is simple, you'll have achieved something 👍HighTide
10/18/2023, 7:32 PMStephan Schröder
10/19/2023, 12:11 PM