Skip to content

Latest commit

 

History

History
175 lines (141 loc) · 4.86 KB

README.md

File metadata and controls

175 lines (141 loc) · 4.86 KB

JVM gradle scripts

Quickstart

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.

Build all the games:

 cd buildJvm
 ./gradlew -q clean 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

Using an IDE to work on JVM games

You can open the entire Basic Computer Games project in an IDE, with any IDE capable of importing from a gradle project.

IntelliJ / Android Studio

  1. (Optional) If you want to make changes, or contribute a new kotlin or java version of one of the games, use github "fork" to create your own editable fork of the project.
  2. Check out the code using File -> New -> Project from Version Control
    1. Enter the URL of the project. For the main project this will be https://github.com/coding-horror/basic-computer-games.git, for your own fork this will be https://github.com/YOURNAMEHERE/basic-computer-games.git
    2. Choose a directory for the clone to live in
  3. Click Clone

The project will open, and eventually you will get a little alert box in the bottom right corner saying "Gradle build script found".

Click the "Load" link in the alert box, to load the gradle project.

You should see all the games appear on the left side of the screen. If you have loaded your own fork, you can modify, commit and push your changes to github.

If you are using the main coding-horror branch, you can still make and run your own changes. If your git skills are up to the task, you might even fork the project and change your local clone to point to your new forked project.


Adding a new game

These are build scripts for all JVM games contributed so far. New games can be added:

  • Create a new build_NUMBER_NAME_[java/kotlin] directory
  • Add a build.gradle file to that directory. All build.gradle files under build_NUMBER_* are identical.
  • Add 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.
  • Add an entry in settings.gradle

The build.gradle file should be identical to all the other build.gradle files in all the other subprojects:

 sourceSets {
     main {
         java {
             srcDirs "../../$gameSource"
         }
     }
 }
 application {
     mainClass = gameMain
 }

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.

The settings.gradle must be maintained as a list of all subprojects. Add your new project to the list.

include ":build_91_Train_java"

Adding a game with tests

You can add tests for JVM games with a build.gradle looking a little different. Use the build files from 03_Animal as a template to add tests:

sourceSets {
    main {
        java {
            srcDirs "../../$gameSource"
        }
    }
    test {
        java {
            srcDirs "../../$gameTest"
        }
    }
}

application {
    mainClass = gameMain
}

dependencies {
    testImplementation(project(":build_00_utilities").sourceSets.test.output)
}

The gradle.properties needs an additional directory name for the tests, as gameTest :

gameSource=03_Animal/java/src
gameTest=03_Animal/java/test
gameMain=Animal

Each project should have its own test, and shouldn't share test source directories with other projects, even if they are for the same game.

Tests are constructed by subclassing ConsoleTest. This allows you to use the assertConversation function to check for correct interactive conversations.

import com.pcholt.console.testutils.ConsoleTest
import org.junit.Test

class AnimalJavaTest : ConsoleTest() {
    @Test
    fun `should have a simple conversation`() {
       assertConversation(
          """
            WHAT'S YOUR NAME? {PAUL}
            YOUR NAME IS PAUL? {YES}
            THANKS FOR PLAYING
            """
       ) {
          // The game's Main method
          main()
       }
    }
}

Curly brackets are the expected user input. Note - this is actually just a way of defining the expected input as "PAUL" and "YES" and not that the input happens at the exact prompt position. Thus this is equivalent:

"""
{PAUL} {YES} WHAT'S YOUR NAME? 
YOUR NAME IS PAUL?
THANKS FOR PLAYING
"""

Amounts of whitespace are not counted, but whitespace is significant: You will get a failure if your game emits "NAME?" when it expects "NAME ?".

Run all the tests from within the buildJvm project directory:

cd buildJvm
./gradlew test