diff --git a/53_King/kotlin/King.kt b/53_King/kotlin/King.kt index 71e6d505..169cd6a0 100644 --- a/53_King/kotlin/King.kt +++ b/53_King/kotlin/King.kt @@ -1,3 +1,5 @@ +package king53 + import kotlin.math.abs import kotlin.random.Random import kotlin.system.exitProcess @@ -22,6 +24,7 @@ fun main() { with(gameState) { do { + recalculateLandCost() displayStatus() inputLandSale() @@ -84,6 +87,7 @@ fun loadOldGame(): GameState = GameState().apply { println(" COME ON, YOUR TERM IN OFFICE IS ONLY $yearsRequired YEARS.") retry = true } + } while (retry) print("HOW MUCH DID YOU HAVE IN THE TREASURY? ") @@ -115,6 +119,10 @@ fun loadOldGame(): GameState = GameState().apply { */ sealed class YearOutcome { + /** + * Display output for the end of the year, for each different possible + * year outcome. + */ open fun displayConsequences() { // Default display nothing } @@ -132,7 +140,7 @@ sealed class YearOutcome { object ContinueNextYear : YearOutcome() - class Win(val yearsRequired: Int) : YearOutcome() { + class Win(private val yearsRequired: Int) : YearOutcome() { override fun displayConsequences() { // The misspelling of "successfully" is in the original code. println( @@ -229,12 +237,18 @@ sealed class YearOutcome { * Record data, allow data input, and process the simulation for the game. */ class GameState(val yearsRequired: Int = 8) { + /** * The current year. Years start with zero, but we never * output the current year. */ var currentYear = 0 + /** + * Keep track of each year's crop loss, so we can report increases. + */ + private var lastYearsCropLoss: Int = 0 + /** * Number of countrymen who have died of either pollution * or starvation this year. @@ -535,11 +549,12 @@ class GameState(val yearsRequired: Int = 8) { the population, but does not affect crop losses. */ var cropLoss = ((2000 - landArea) * (rnd + 1.5) / 2.0).toInt() - val cropLossWorse = false if (foreignWorkers > 0) print("OF $plantingArea SQ. MILES PLANTED,") if (plantingArea <= cropLoss) cropLoss = plantingArea + val cropLossWorse = cropLoss > lastYearsCropLoss + lastYearsCropLoss = cropLoss println(" YOU HARVESTED ${plantingArea - cropLoss} SQ. MILES OF CROPS.") if (cropLoss > 0) { diff --git a/buildJvm/README.md b/buildJvm/README.md new file mode 100644 index 00000000..f0046ab3 --- /dev/null +++ b/buildJvm/README.md @@ -0,0 +1,63 @@ +# JVM gradle scripts + +## Quickstart + +Build all the games: + + cd buildJvm + ./gradlew -q assemble installDist distributeBin distributeLib + +Then, run a game + +### Mac or linux: + + build/distrib/bin/build_53_King_kotlin + +### Windows +[not tested yet] + + build\distrib\bin\build_53_King_kotlin.bat + +You will need to install openjdk 17, because some games use advanced Java features. +We should be using version 17 anyway, because anything less than 17 is deprecated. + +--- +## Adding a new game + +These are build scripts for all JVM games contributed so far. +New games can be added by: +- Creating a `build_NUMBER_NAME_[java/kotlin]` directory +- Adding a `build.gradle` file to that directory. +All `build.gradle` files under `build_NUMBER_*` should be nearly identical, unless +there is some special requirement. +- Adding a `gradle.properties` file to that directory, defining the source +directory for the java or kotlin file, and the class that contains the `main` method. + +The `build.gradle` file will normally be identical to this: + + plugins { + id 'application' + } + + sourceSets { + main { + java { + srcDirs "../../$gameSource" + } + } + } + + repositories { + mavenCentral() + } + + application { + mainClass = gameMain + } + +And the `gradle.properties` file should look like this: + + gameSource=91_Train/java/src + gameMain=Train + +where `gameSource` is the root of the source code directory, and `gameMain` is the main class.