diff --git a/Exercises/NetworkedTicTacToe/.editorconfig b/Exercises/NetworkedTicTacToe/.editorconfig new file mode 100644 index 0000000..f6943b1 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/.editorconfig @@ -0,0 +1,18 @@ +# https://editorconfig.org +root = true + +[*] +indent_style = space +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.{java,scala,groovy,kt,kts}] +indent_size = 4 + +[*.gradle] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/Exercises/NetworkedTicTacToe/.gitattributes b/Exercises/NetworkedTicTacToe/.gitattributes new file mode 100644 index 0000000..05c96d8 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/.gitattributes @@ -0,0 +1,2 @@ +* text=auto eol=lf +*.bat text=auto eol=crlf diff --git a/Exercises/NetworkedTicTacToe/.gitignore b/Exercises/NetworkedTicTacToe/.gitignore new file mode 100644 index 0000000..0712bfc --- /dev/null +++ b/Exercises/NetworkedTicTacToe/.gitignore @@ -0,0 +1,203 @@ +## Gradle: +.gradle/ +gradle-app.setting +/build/ +/android/build/ +/core/build/ +/lwjgl2/build/ +/lwjgl3/build/ +/html/build/ +/teavm/build/ +/ios/build/ +/ios-moe/build/ +/headless/build/ +/server/build/ +/shared/build/ + +## Java: +*.class +*.war +*.ear +hs_err_pid* +.attach_pid* + +## Android: +/android/libs/armeabi-v7a/ +/android/libs/arm64-v8a/ +/android/libs/x86/ +/android/libs/x86_64/ +/android/gen/ +/android/out/ +local.properties +com_crashlytics_export_strings.xml + +## Robovm: +/ios/robovm-build/ + +## iOS: +/ios/xcode/*.xcodeproj/* +!/ios/xcode/*.xcodeproj/xcshareddata +!/ios/xcode/*.xcodeproj/project.pbxproj +/ios/xcode/native/ +/ios/IOSLauncher.app +/ios/IOSLauncher.app.dSYM + +## GWT: +/html/war/ +/html/gwt-unitCache/ +.apt_generated/ +/html/war/WEB-INF/deploy/ +/html/war/WEB-INF/classes/ +.gwt/ +gwt-unitCache/ +www-test/ +.gwt-tmp/ + +## TeaVM: +# Not sure yet... + +## IntelliJ, Android Studio: +.idea/ +*.ipr +*.iws +*.iml + +## Eclipse: +.classpath +.project +.metadata/ +/android/bin/ +/core/bin/ +/lwjgl2/bin/ +/lwjgl3/bin/ +/html/bin/ +/teavm/bin/ +/ios/bin/ +/ios-moe/bin/ +/headless/bin/ +/server/bin/ +/shared/bin/ +*.tmp +*.bak +*.swp +*~.nib +.settings/ +.loadpath +.externalToolBuilders/ +*.launch + + +## NetBeans: + +/nbproject/private/ +/android/nbproject/private/ +/core/nbproject/private/ +/lwjgl2/nbproject/private/ +/lwjgl3/nbproject/private/ +/html/nbproject/private/ +/teavm/nbproject/private/ +/ios/nbproject/private/ +/ios-moe/nbproject/private/ +/headless/nbproject/private/ +/server/nbproject/private/ +/shared/nbproject/private/ + +/nbbuild/ +/android/nbbuild/ +/core/nbbuild/ +/lwjgl2/nbbuild/ +/lwjgl3/nbbuild/ +/html/nbbuild/ +/teavm/nbbuild/ +/ios/nbbuild/ +/ios-moe/nbbuild/ +/headless/nbbuild/ +/server/nbbuild/ +/shared/nbbuild/ + +/dist/ +/android/dist/ +/core/dist/ +/lwjgl2/dist/ +/lwjgl3/dist/ +/html/dist/ +/teavm/dist/ +/ios/dist/ +/ios-moe/dist/ +/headless/dist/ +/server/dist/ +/shared/dist/ + +/nbdist/ +/android/nbdist/ +/core/nbdist/ +/lwjgl2/nbdist/ +/lwjgl3/nbdist/ +/html/nbdist/ +/teavm/nbdist/ +/ios/nbdist/ +/ios-moe/nbdist/ +/headless/nbdist/ +/server/nbdist/ +/shared/nbdist/ + +nbactions.xml +nb-configuration.xml + +## OS-Specific: +.DS_Store +Thumbs.db + +## Miscellaneous: +*~ +*.*# +*#*# +/.kotlin/ +/assets/assets.txt + +## Special cases: + +## There is a resource-config.json file generated by nativeimage.gradle if you use Graal Native Image. +## Some usage may need extra resource configuration in a different file with the same name. +## You could also add that configuration to the text in nativeimage.gradle . +## You should delete or comment out the next line if you have configuration in a different resource-config.json . +**/resource-config.json + +# Created by https://www.toptal.com/developers/gitignore/api/c++ +# Edit at https://www.toptal.com/developers/gitignore?templates=c++ + +### C++ ### +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app +GameServer/server +# End of https://www.toptal.com/developers/gitignore/api/c++ diff --git a/Exercises/NetworkedTicTacToe/GameServer/GameServer.cpp b/Exercises/NetworkedTicTacToe/GameServer/GameServer.cpp new file mode 100644 index 0000000..6f089eb --- /dev/null +++ b/Exercises/NetworkedTicTacToe/GameServer/GameServer.cpp @@ -0,0 +1,230 @@ +#include +#include +#include +#include +#include +#include +#include + +// TODO: move `GameServer` into its own files (h/cpp). +// Note: This is compiled with SFML 2.6.2 in mind. +// It would work similarly with slightly older versions of SFML. +// A thourough rework is necessary for SFML 3.0. + +enum GameMessageType : unsigned char { + JOIN_GAME = 0x01, PLACE_TOKEN = 0x02 +}; + +enum Token : unsigned char { + NOUGHTS = 0x01, CROSSES = 0x02 +}; + +class GameServer { +public: + GameServer(unsigned short tcp_port) : + m_tcp_port(tcp_port) {} + + // Binds to a port and then loops around. For every client that connects, + // we start a new thread receiving their messages. + void tcp_start() + { + // BINDING + sf::TcpListener listener; + sf::Socket::Status status = listener.listen(m_tcp_port, sf::IpAddress("A.B.C.D")); + if (status != sf::Socket::Status::Done) + { + std::cerr << "Error binding listener to port" << std::endl; + return; + } + + std::cout << "TCP Server is listening to port " + << m_tcp_port + << ", waiting for connections..." + << std::endl; + + while (true) + { + // ACCEPTING + if(m_player_count < 2) + { + sf::TcpSocket* client = new sf::TcpSocket; + status = listener.accept(*client); + if (status != sf::Socket::Status::Done) + { + delete client; + } else { + { + std::lock_guard lock(m_clients_mutex); + m_clients.push_back(client); + } + std::cout << "New client connected: " + << client->getRemoteAddress() + << std::endl; + + m_player_count++; + + std::thread(&GameServer::handle_client, this, client, m_player_count).detach(); + + if(m_player_count == 2) + { + // -------------------------------------------------------------- + // Slight pause to ensure the all threads have started + // -------------------------------------------------------------- + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + } + } + } + } + // No need to call close of the listener. + // The connection is closed automatically when the listener object is out of scope. + } + + +private: + unsigned short m_tcp_port; + unsigned short m_player_count { 0 }; + + std::vector m_clients; + std::mutex m_clients_mutex; + + // Loop around, receive messages from client and send them to all + // the other connected clients. + void handle_client(sf::TcpSocket* client, unsigned short player_num) + { + sf::Socket::Status status; + + if(player_num == 1) { + std::cout << "Player " << player_num << " is NOUGHTS" << std::endl; + char buffer[2] = { + GameMessageType::JOIN_GAME, + Token::NOUGHTS + }; + + status = client->send(buffer, message_size(GameMessageType::JOIN_GAME)); + + if (status != sf::Socket::Status::Done) + { + std::cerr << "Error sending JOIN_GAME to player 1" << std::endl; + return; + } + } + else if(player_num == 2) { + std::cout << "Player " << player_num << " is CROSSES" << std::endl; + char buffer[2] = { + GameMessageType::JOIN_GAME, + Token::CROSSES + }; + + status = client->send(buffer, message_size(GameMessageType::JOIN_GAME)); + + if (status != sf::Socket::Status::Done) + { + std::cerr << "Error sending JOIN_GAME to player 2" << std::endl; + return; + } + } + else { + return; // No more players please!!! + } + + + while (true) + { + // RECEIVING + char payload[1024]; + std::memset(payload, 0, 1024); + size_t received; + sf::Socket::Status status = client->receive(payload, 1024, received); + if (status != sf::Socket::Status::Done) + { + std::cerr << "Error receiving message from client" << std::endl; + break; + } else { + // Actually, there is no need to print the message if the message is not a string + debug_message(payload); + + broadcast_message(payload, client); + } + } + + // Everything that follows only makes sense if we have a graceful way to exiting the loop. + // Remove the client from the list when done + { + std::lock_guard lock(m_clients_mutex); + m_clients.erase(std::remove(m_clients.begin(), m_clients.end(), client), + m_clients.end()); + } + delete client; + } + + // Sends `message` from `sender` to all the other connected clients + bool broadcast_message(const char *buffer, sf::TcpSocket* sender) + { + size_t msgSize { message_size(buffer[0]) }; + + // You might want to validate the message before you send it. + // A few reasons for that: + // 1. Make sure the message makes sense in the game. + // 2. Make sure the sender is not cheating. + // 3. First need to synchronise the players inputs (usually done in Lockstep). + // 4. Compensate for latency and perform rollbacks (usually done in Ded Reckoning). + // 5. Delay the sending of messages to make the game fairer wrt high ping players. + // This is where you can write the authoritative part of the server. + std::lock_guard lock(m_clients_mutex); + for (auto& client : m_clients) + { + if (client != sender) + { + // SENDING + sf::Socket::Status status = client->send(buffer, msgSize) ; + if (status != sf::Socket::Status::Done) + { + std::cerr << "Error sending message to client" << std::endl; + + return false; + } + } + } + + return true; + } + + constexpr size_t message_size(const char messageType) + { + switch(messageType) { + case JOIN_GAME: return 2; + case PLACE_TOKEN: return sizeof(int) * 2 + 2; + default: return 0; + } + } + + void debug_message(const char *buf) + { + const unsigned char msgType = buf[0]; + + switch(msgType) { + case JOIN_GAME: { + std::cout << "Player Joined The Game" << std::endl; + break; + } + case PLACE_TOKEN: { + const unsigned char *row { (unsigned char* )buf + 1 }; + const unsigned char *col { (unsigned char* )buf + 1 + sizeof(int) }; + unsigned int rowI = be32toh(*((unsigned int *) row)); + unsigned int colI = be32toh(*((unsigned int *) col)); + std::cout << "Player Placed A Token: (" << rowI << ", " << colI << ")" << std::endl; + + break; + } + } + } +}; + +int main() +{ + GameServer server(4300); + + server.tcp_start(); + + return 0; +} diff --git a/Exercises/NetworkedTicTacToe/GameServer/Makefile b/Exercises/NetworkedTicTacToe/GameServer/Makefile new file mode 100644 index 0000000..3c99018 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/GameServer/Makefile @@ -0,0 +1,16 @@ +SFML_PATH=/usr/local/Cellar/sfml/2.6.1/ +CXXFLAGS= -std=c++14 -Wall -Wpedantic -I${SFML_PATH}include/ +LDFLAGS=-L${SFML_PATH}lib/ +CFLAGS=-g -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network -pthread +CPPFLAGS= +LDLIBS= +LIBS= +CPP=g++ + +all: server + +server: GameServer.o + $(CPP) $(CXXFLAGS) $(LDFLAGS) $(LIBS) $^ -o $@ $(CFLAGS) + +clean: + \rm -f *.o server diff --git a/Exercises/NetworkedTicTacToe/README.md b/Exercises/NetworkedTicTacToe/README.md new file mode 100644 index 0000000..ff39fb3 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/README.md @@ -0,0 +1,35 @@ +# NetworkedTicTacToe + +A [libGDX](https://libgdx.com/) project generated with [gdx-liftoff](https://github.com/libgdx/gdx-liftoff). + +This project was generated with a Kotlin project template that includes Kotlin application launchers and [KTX](https://libktx.github.io/) utilities. + +## Platforms + +- `core`: Main module with the application logic shared by all platforms. +- `lwjgl3`: Primary desktop platform using LWJGL3; was called 'desktop' in older docs. +- `android`: Android mobile platform. Needs Android SDK. + +## Gradle + +This project uses [Gradle](https://gradle.org/) to manage dependencies. +The Gradle wrapper was included, so you can run Gradle tasks using `gradlew.bat` or `./gradlew` commands. +Useful Gradle tasks and flags: + +- `--continue`: when using this flag, errors will not stop the tasks from running. +- `--daemon`: thanks to this flag, Gradle daemon will be used to run chosen tasks. +- `--offline`: when using this flag, cached dependency archives will be used. +- `--refresh-dependencies`: this flag forces validation of all dependencies. Useful for snapshot versions. +- `android:lint`: performs Android project validation. +- `build`: builds sources and archives of every project. +- `cleanEclipse`: removes Eclipse project data. +- `cleanIdea`: removes IntelliJ project data. +- `clean`: removes `build` folders, which store compiled classes and built archives. +- `eclipse`: generates Eclipse project data. +- `idea`: generates IntelliJ project data. +- `lwjgl3:jar`: builds application's runnable jar, which can be found at `lwjgl3/build/libs`. +- `lwjgl3:run`: starts the application. +- `test`: runs unit tests (if any). + +Note that most tasks that are not specific to a single project can be run with `name:` prefix, where the `name` should be replaced with the ID of a specific project. +For example, `core:clean` removes `build` folder only from the `core` project. diff --git a/Exercises/NetworkedTicTacToe/android/AndroidManifest.xml b/Exercises/NetworkedTicTacToe/android/AndroidManifest.xml new file mode 100644 index 0000000..6a2f0b2 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/AndroidManifest.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + diff --git a/Exercises/NetworkedTicTacToe/android/build.gradle b/Exercises/NetworkedTicTacToe/android/build.gradle new file mode 100644 index 0000000..24eacca --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/build.gradle @@ -0,0 +1,136 @@ + +buildscript { + repositories { + mavenCentral() + google() + } +} +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' + +android { + namespace = "u0012604.tictactoe" + compileSdk = 35 + sourceSets { + main { + manifest.srcFile 'AndroidManifest.xml' + java.setSrcDirs(['src/main/java', 'src/main/kotlin']) + aidl.setSrcDirs(['src/main/java', 'src/main/kotlin']) + renderscript.setSrcDirs(['src/main/java', 'src/main/kotlin']) + res.setSrcDirs(['res']) + assets.setSrcDirs(['../assets']) + jniLibs.setSrcDirs(['libs']) + } + } + packagingOptions { + resources { + excludes += ['META-INF/robovm/ios/robovm.xml', 'META-INF/DEPENDENCIES.txt', 'META-INF/DEPENDENCIES', + 'META-INF/dependencies.txt', '**/*.gwt.xml'] + pickFirsts += ['META-INF/LICENSE.txt', 'META-INF/LICENSE', 'META-INF/license.txt', 'META-INF/LGPL2.1', + 'META-INF/NOTICE.txt', 'META-INF/NOTICE', 'META-INF/notice.txt'] + } + } + defaultConfig { + applicationId 'u0012604.tictactoe' + minSdkVersion 21 + targetSdkVersion 35 + versionCode 1 + versionName "1.0" + } + compileOptions { + sourceCompatibility "8" + targetCompatibility "8" + coreLibraryDesugaringEnabled = true + } + buildTypes { + release { + minifyEnabled true + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + kotlin.compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8) + +} + +repositories { + // needed for AAPT2, may be needed for other tools + google() +} + +configurations { natives } + +dependencies { + coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5' + implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" + implementation project(':core') + + natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a" + natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a" + natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86" + natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64" + natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a" + natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" + natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" + natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64" + +} + +// Called every time gradle gets executed, takes the native dependencies of +// the natives configuration, and extracts them to the proper libs/ folders +// so they get packed with the APK. +tasks.register('copyAndroidNatives') { + doFirst { + file("libs/armeabi-v7a/").mkdirs() + file("libs/arm64-v8a/").mkdirs() + file("libs/x86_64/").mkdirs() + file("libs/x86/").mkdirs() + + configurations.natives.copy().files.each { jar -> + def outputDir = null + if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a") + if(jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a") + if(jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64") + if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86") + if(outputDir != null) { + copy { + from zipTree(jar) + into outputDir + include "*.so" + } + } + } + } +} + +tasks.matching { it.name.contains("merge") && it.name.contains("JniLibFolders") }.configureEach { packageTask -> + packageTask.dependsOn 'copyAndroidNatives' +} + +tasks.register('run', Exec) { + def path + def localProperties = project.file("../local.properties") + if (localProperties.exists()) { + Properties properties = new Properties() + localProperties.withInputStream { instr -> + properties.load(instr) + } + def sdkDir = properties.getProperty('sdk.dir') + if (sdkDir) { + path = sdkDir + } else { + path = "$System.env.ANDROID_SDK_ROOT" + } + } else { + path = "$System.env.ANDROID_SDK_ROOT" + } + + def adb = path + "/platform-tools/adb" + commandLine "$adb", 'shell', 'am', 'start', '-n', 'u0012604.tictactoe/u0012604.tictactoe.android.AndroidLauncher' +} + +eclipse.project.name = appName + "-android" diff --git a/Exercises/NetworkedTicTacToe/android/ic_launcher-web.png b/Exercises/NetworkedTicTacToe/android/ic_launcher-web.png new file mode 100644 index 0000000..67f2101 Binary files /dev/null and b/Exercises/NetworkedTicTacToe/android/ic_launcher-web.png differ diff --git a/Exercises/NetworkedTicTacToe/android/proguard-rules.pro b/Exercises/NetworkedTicTacToe/android/proguard-rules.pro new file mode 100644 index 0000000..bdc29bd --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/proguard-rules.pro @@ -0,0 +1,52 @@ +# To enable ProGuard in your project, edit project.properties +# to define the proguard.config property as described in that file. +# +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in ${sdk.dir}/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the ProGuard +# include property in project.properties. +# +# For more details, see +# https://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +-verbose + +-dontwarn android.support.** +-dontwarn com.badlogic.gdx.backends.android.AndroidFragmentApplication + +# Needed by the gdx-controllers official extension. +-keep class com.badlogic.gdx.controllers.android.AndroidControllers + +# Needed by the Box2D official extension. +-keepclassmembers class com.badlogic.gdx.physics.box2d.World { + boolean contactFilter(long, long); + boolean getUseDefaultContactFilter(); + void beginContact(long); + void endContact(long); + void preSolve(long, long); + void postSolve(long, long); + boolean reportFixture(long); + float reportRayFixture(long, float, float, float, float, float); +} + +# You will need the next three lines if you use scene2d for UI or gameplay. +# If you don't use scene2d at all, you can remove or comment out the next line: +-keep public class com.badlogic.gdx.scenes.scene2d.** { *; } +# You will need the next two lines if you use BitmapFont or any scene2d.ui text: +-keep public class com.badlogic.gdx.graphics.g2d.BitmapFont { *; } +# You will probably need this line in most cases: +-keep public class com.badlogic.gdx.graphics.Color { *; } + +# These two lines are used with mapping files; see https://developer.android.com/build/shrink-code#retracing +-keepattributes LineNumberTable,SourceFile +-renamesourcefileattribute SourceFile diff --git a/Exercises/NetworkedTicTacToe/android/project.properties b/Exercises/NetworkedTicTacToe/android/project.properties new file mode 100644 index 0000000..746df33 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/project.properties @@ -0,0 +1,14 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system edit +# "ant.properties", and override values to adapt the script to your +# project structure. +# +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-rules.pro + +# Project target. +target=android-21 diff --git a/Exercises/NetworkedTicTacToe/android/res/drawable-anydpi-v26/ic_launcher.xml b/Exercises/NetworkedTicTacToe/android/res/drawable-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..79ce5be --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/res/drawable-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/Exercises/NetworkedTicTacToe/android/res/drawable-anydpi-v26/ic_launcher_foreground.xml b/Exercises/NetworkedTicTacToe/android/res/drawable-anydpi-v26/ic_launcher_foreground.xml new file mode 100644 index 0000000..8e1c344 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/res/drawable-anydpi-v26/ic_launcher_foreground.xml @@ -0,0 +1,28 @@ + + + + + + + + + diff --git a/Exercises/NetworkedTicTacToe/android/res/drawable-hdpi/ic_launcher.png b/Exercises/NetworkedTicTacToe/android/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..36210f3 Binary files /dev/null and b/Exercises/NetworkedTicTacToe/android/res/drawable-hdpi/ic_launcher.png differ diff --git a/Exercises/NetworkedTicTacToe/android/res/drawable-mdpi/ic_launcher.png b/Exercises/NetworkedTicTacToe/android/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..a81fdd1 Binary files /dev/null and b/Exercises/NetworkedTicTacToe/android/res/drawable-mdpi/ic_launcher.png differ diff --git a/Exercises/NetworkedTicTacToe/android/res/drawable-xhdpi/ic_launcher.png b/Exercises/NetworkedTicTacToe/android/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..82a1b1d Binary files /dev/null and b/Exercises/NetworkedTicTacToe/android/res/drawable-xhdpi/ic_launcher.png differ diff --git a/Exercises/NetworkedTicTacToe/android/res/drawable-xxhdpi/ic_launcher.png b/Exercises/NetworkedTicTacToe/android/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..60f611c Binary files /dev/null and b/Exercises/NetworkedTicTacToe/android/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/Exercises/NetworkedTicTacToe/android/res/drawable-xxxhdpi/ic_launcher.png b/Exercises/NetworkedTicTacToe/android/res/drawable-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..6a87b2d Binary files /dev/null and b/Exercises/NetworkedTicTacToe/android/res/drawable-xxxhdpi/ic_launcher.png differ diff --git a/Exercises/NetworkedTicTacToe/android/res/values/color.xml b/Exercises/NetworkedTicTacToe/android/res/values/color.xml new file mode 100644 index 0000000..b3461fe --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/res/values/color.xml @@ -0,0 +1,5 @@ + + + #F5A623FF + #FFFFFFFF + diff --git a/Exercises/NetworkedTicTacToe/android/res/values/strings.xml b/Exercises/NetworkedTicTacToe/android/res/values/strings.xml new file mode 100644 index 0000000..09e7e0a --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/res/values/strings.xml @@ -0,0 +1,4 @@ + + + NetworkedTicTacToe + diff --git a/Exercises/NetworkedTicTacToe/android/res/values/styles.xml b/Exercises/NetworkedTicTacToe/android/res/values/styles.xml new file mode 100644 index 0000000..ef98b63 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/res/values/styles.xml @@ -0,0 +1,5 @@ + + + diff --git a/Exercises/NetworkedTicTacToe/android/src/main/kotlin/u0012604/tictactoe/android/AndroidLauncher.kt b/Exercises/NetworkedTicTacToe/android/src/main/kotlin/u0012604/tictactoe/android/AndroidLauncher.kt new file mode 100644 index 0000000..ff64350 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/android/src/main/kotlin/u0012604/tictactoe/android/AndroidLauncher.kt @@ -0,0 +1,18 @@ +package u0012604.tictactoe.android + +import android.os.Bundle + +import com.badlogic.gdx.backends.android.AndroidApplication +import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration +import u0012604.tictactoe.Main + +/** Launches the Android application. */ +class AndroidLauncher : AndroidApplication() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + initialize(Main(), AndroidApplicationConfiguration().apply { + // Configure your application here. + useImmersiveMode = true // Recommended, but not required. + }) + } +} diff --git a/Exercises/NetworkedTicTacToe/assets/logo.png b/Exercises/NetworkedTicTacToe/assets/logo.png new file mode 100644 index 0000000..e6d211e Binary files /dev/null and b/Exercises/NetworkedTicTacToe/assets/logo.png differ diff --git a/Exercises/NetworkedTicTacToe/build.gradle b/Exercises/NetworkedTicTacToe/build.gradle new file mode 100644 index 0000000..c03bf56 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/build.gradle @@ -0,0 +1,74 @@ +buildscript { + repositories { + mavenCentral() + gradlePluginPortal() + mavenLocal() + google() + maven { url = 'https://central.sonatype.com/repository/maven-snapshots/' } + } + dependencies { + classpath 'com.android.tools.build:gradle:8.13.2' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" + + } +} + +allprojects { + apply plugin: 'eclipse' + apply plugin: 'idea' + + // This allows you to "Build and run using IntelliJ IDEA", an option in IDEA's Settings. + idea { + module { + outputDir = file('build/classes/java/main') + testOutputDir = file('build/classes/java/test') + } + } +} + +configure(subprojects - project(':android')) { + apply plugin: 'java-library' + apply plugin: 'kotlin' + java.sourceCompatibility = 8 + + // From https://lyze.dev/2021/04/29/libGDX-Internal-Assets-List/ + // The article can be helpful when using assets.txt in your project. + tasks.register('generateAssetList') { + inputs.dir("${project.rootDir}/assets/") + // projectFolder/assets + File assetsFolder = new File("${project.rootDir}/assets/") + // projectFolder/assets/assets.txt + File assetsFile = new File(assetsFolder, "assets.txt") + // delete that file in case we've already created it + assetsFile.delete() + + // iterate through all files inside that folder + // convert it to a relative path + // and append it to the file assets.txt + fileTree(assetsFolder).collect { assetsFolder.relativePath(it) }.sort().each { + assetsFile.append(it + "\n") + } + } + processResources.dependsOn 'generateAssetList' + + compileJava { + options.incremental = true + } + compileKotlin.compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8) + compileTestKotlin.compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8) + +} + +subprojects { + version = "$projectVersion" + ext.appName = 'NetworkedTicTacToe' + repositories { + mavenCentral() + // You may want to remove the following line if you have errors downloading dependencies. + mavenLocal() + maven { url = 'https://central.sonatype.com/repository/maven-snapshots/' } + maven { url = 'https://jitpack.io' } + } +} + +eclipse.project.name = 'NetworkedTicTacToe' + '-parent' diff --git a/Exercises/NetworkedTicTacToe/core/build.gradle b/Exercises/NetworkedTicTacToe/core/build.gradle new file mode 100644 index 0000000..e7decbf --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/build.gradle @@ -0,0 +1,43 @@ +[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' +eclipse.project.name = appName + '-core' + +dependencies { + api "com.badlogicgames.ashley:ashley:$ashleyVersion" + api "com.badlogicgames.gdx:gdx-ai:$aiVersion" + api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" + api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" + api "com.badlogicgames.gdx:gdx:$gdxVersion" + api "com.kotcrab.vis:vis-ui:$visUiVersion" + api "io.github.libktx:ktx-actors:$ktxVersion" + api "io.github.libktx:ktx-ai:$ktxVersion" + api "io.github.libktx:ktx-app:$ktxVersion" + api "io.github.libktx:ktx-artemis:$ktxVersion" + api "io.github.libktx:ktx-ashley:$ktxVersion" + api "io.github.libktx:ktx-assets-async:$ktxVersion" + api "io.github.libktx:ktx-assets:$ktxVersion" + api "io.github.libktx:ktx-async:$ktxVersion" + api "io.github.libktx:ktx-box2d:$ktxVersion" + api "io.github.libktx:ktx-collections:$ktxVersion" + api "io.github.libktx:ktx-freetype-async:$ktxVersion" + api "io.github.libktx:ktx-freetype:$ktxVersion" + api "io.github.libktx:ktx-graphics:$ktxVersion" + api "io.github.libktx:ktx-i18n:$ktxVersion" + api "io.github.libktx:ktx-inject:$ktxVersion" + api "io.github.libktx:ktx-json:$ktxVersion" + api "io.github.libktx:ktx-log:$ktxVersion" + api "io.github.libktx:ktx-math:$ktxVersion" + api "io.github.libktx:ktx-preferences:$ktxVersion" + api "io.github.libktx:ktx-reflect:$ktxVersion" + api "io.github.libktx:ktx-scene2d:$ktxVersion" + api "io.github.libktx:ktx-style:$ktxVersion" + api "io.github.libktx:ktx-tiled:$ktxVersion" + api "io.github.libktx:ktx-vis-style:$ktxVersion" + api "io.github.libktx:ktx-vis:$ktxVersion" + api "net.onedaybeard.artemis:artemis-odb:$artemisOdbVersion" + api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" + api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion" + + if(enableGraalNative == 'true') { + implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion" + } +} diff --git a/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Board.kt b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Board.kt new file mode 100644 index 0000000..cc2641a --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Board.kt @@ -0,0 +1,110 @@ +package u0012604.tictactoe + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.Batch +import com.badlogic.gdx.graphics.glutils.ShapeRenderer +import com.badlogic.gdx.math.Vector2 +import com.badlogic.gdx.utils.Disposable +import com.badlogic.gdx.utils.viewport.Viewport +import ktx.assets.disposeSafely +import ktx.graphics.use +import u0012604.tictactoe.screens.FirstScreen.Companion.NOUGHT_RADIUS + +class Board(val viewport: Viewport) : Disposable { + private var thirdOfWidth = 0f + private var thirdOfHeight = 0f + + private var halfCellW = 0f + private var halfCellH = 0f + + private val shapeRenderer = ShapeRenderer() + + private var boardLines = emptyArray>() + + private val board = arrayOf( + arrayOf(Token.EMPTY, Token.EMPTY, Token.EMPTY), + arrayOf(Token.EMPTY, Token.EMPTY, Token.EMPTY), + arrayOf(Token.EMPTY, Token.EMPTY, Token.EMPTY) + ) + + fun placeToken(row: Int, col: Int, token: Token) = + if(row in (0..2) && col in (0 .. 2) && board[row][col] == Token.EMPTY) { + board[row][col] = token + true + } + else { + false + } + + fun draw(batch: Batch) { + shapeRenderer.use(ShapeRenderer.ShapeType.Filled, viewport.camera.combined) { sr -> + + sr.color = Color.RED + + boardLines.forEach { line -> +// Gdx.app.log(TAG, "p0:${line.first}, ${line.second}") + + sr.rectLine(line.first, line.second, 10f) + } + + board.forEachIndexed { rowIndex, row -> + row.forEachIndexed { colIndex, col -> + when(col) { + Token.NOUGHT -> drawNought(rowIndex, colIndex, sr) + + Token.CROSS -> drawCross(rowIndex, colIndex, sr) + + else -> {} + } + } + } + } + } + + fun resize(width: Int, height: Int) { + boardLines = emptyArray() + + thirdOfWidth = width / 3f + thirdOfHeight = height / 3f + + // Vertical lines + val x1 = thirdOfWidth + val x2 = Gdx.graphics.width / 1.5f + boardLines += Pair(Vector2(x1, 0f), Vector2(x1, height.toFloat())) + boardLines += Pair(Vector2(x2, 0f), Vector2(x2, height.toFloat())) + + // Horizontal lines + val y1 = thirdOfHeight + val v2 = Gdx.graphics.height.toFloat() / 1.5f + + boardLines += Pair(Vector2(0f, y1), Vector2(width.toFloat(), y1)) + boardLines += Pair(Vector2(0f, v2), Vector2(width.toFloat(), v2)) + + halfCellW = x1 / 2f + halfCellH = y1 / 2f + } + + private fun drawNought(row: Int, col: Int, sr: ShapeRenderer) { + val x = col * thirdOfWidth + halfCellW + val y = (2 - row) * thirdOfHeight + halfCellH + + sr.circle(x, y, NOUGHT_RADIUS) + } + + private fun drawCross(row: Int, col: Int, sr: ShapeRenderer) { + val flipRow = 2 - row + + val l1x1 = col * thirdOfWidth + 50f + val l1y1 = flipRow * thirdOfHeight + 50f + val l1x2 = col * thirdOfWidth - 50f + 2 * halfCellW + val l1y2 = flipRow * thirdOfHeight - 50f + 2 * halfCellH + + sr.rectLine(l1x1, l1y1, l1x2, l1y2, 10f) + sr.rectLine(l1x1, l1y2, l1x2, l1y1, 10f) + } + + override fun dispose() { + shapeRenderer.disposeSafely() + } +} diff --git a/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/GameMessageType.kt b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/GameMessageType.kt new file mode 100644 index 0000000..472df9e --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/GameMessageType.kt @@ -0,0 +1,65 @@ +package u0012604.tictactoe + +import java.nio.ByteBuffer + +interface Serializable { + fun serialize() : ByteArray +} + +interface Deserializable { + fun deserialize(bb: ByteBuffer) : GameMessage +} + +enum class GameMessageType(val id: Byte) { + JOIN_GAME(1), + PLACE_TOKEN(2); + + companion object { + fun fromByte(id: Byte) = entries.first { it.id == id } + } +} + +sealed class GameMessage(val type: GameMessageType) : Serializable { + + override fun serialize() = byteArrayOf(type.id) + + // ----------------------------------------------------------------------------------------------- + data class JoinGameMessage(val token: Token) : GameMessage(GameMessageType.JOIN_GAME) { + + override fun serialize(): ByteArray = with(ByteBuffer.allocate(2)) { + put(token.type) + super.serialize() + array() + } + + companion object : Deserializable { + + override fun deserialize(bb: ByteBuffer) = with(bb) { + JoinGameMessage(Token.fromByte(get())) + } + } + } + + // ----------------------------------------------------------------------------------------------- + data class PlaceTokenMessage(val row: Int, val col: Int, val token: Token) : GameMessage(GameMessageType.PLACE_TOKEN) { + + override fun serialize(): ByteArray = with(ByteBuffer.allocate(10)) { + putInt(row) + putInt(col) + put(token.type) + super.serialize() + array() + } + + companion object : Deserializable { + override fun deserialize(bb: ByteBuffer) = with(bb) { + + val row = getInt() + val col = getInt() + val token = Token.fromByte(get()) + + PlaceTokenMessage(row, col, token) + } + + } + + } +} diff --git a/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Main.kt b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Main.kt new file mode 100644 index 0000000..464406a --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Main.kt @@ -0,0 +1,40 @@ +package u0012604.tictactoe + + +import kotlinx.coroutines.channels.Channel +import ktx.app.KtxGame +import ktx.app.KtxScreen +import ktx.assets.disposeSafely +import ktx.async.KtxAsync +import u0012604.tictactoe.networking.NetworkHandler +import u0012604.tictactoe.screens.FirstScreen +import u0012604.tictactoe.screens.GameOverScreen + +class Main : KtxGame() { + + private val clientChannel = Channel(10) + private val serverChannel = Channel(10) + + private lateinit var networkHandler: NetworkHandler + + override fun create() { + KtxAsync.initiate() + + networkHandler = NetworkHandler("A.B.C.D", 4300, serverChannel, clientChannel) + + addScreen(FirstScreen(this, clientChannel, serverChannel)) + + addScreen(GameOverScreen()) + + setScreen() + + } + + override fun dispose() { + super.dispose() + serverChannel.close() + clientChannel.close() + networkHandler.disposeSafely() + } +} + diff --git a/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Token.kt b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Token.kt new file mode 100644 index 0000000..a94e1f9 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/Token.kt @@ -0,0 +1,9 @@ +package u0012604.tictactoe + +enum class Token(val type: Byte) { + EMPTY(0), NOUGHT(1), CROSS(2); + + companion object { + fun fromByte(type: Byte) = Token.entries.first { it.type == type } + } +} diff --git a/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/networking/NetworkHandler.kt b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/networking/NetworkHandler.kt new file mode 100644 index 0000000..32556b1 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/networking/NetworkHandler.kt @@ -0,0 +1,166 @@ +package u0012604.tictactoe.networking + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.utils.Disposable +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.channels.ReceiveChannel +import kotlinx.coroutines.channels.SendChannel +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import u0012604.tictactoe.GameMessage +import u0012604.tictactoe.GameMessageType +import java.net.ConnectException +import java.net.InetSocketAddress +import java.net.Socket +import java.net.SocketException +import java.net.SocketTimeoutException +import java.nio.ByteBuffer + +class NetworkHandler( + private val host: String, + private val port: Int, + private val toServerChannel: Channel, // Messages being received elsewhere in the client to send onwards to the client + private val fromServerChannel: SendChannel, // Messages being sent from the server to elsewhere within client + private val connectionTimeout: Int = 3000, + private val maxRetries: Int = 10 +) : Disposable +{ + fun ByteArray.processMessage() = decodeToString().trimEnd{it == Char(0)} // Removes trailing null character + + private val coroutineScope: CoroutineScope = + CoroutineScope(SupervisorJob() + Dispatchers.IO).apply { + launch { + var retries = 0 + + while(retries < maxRetries) { + if(startNetwork(host, port)) { + break; + } + delay(retries * 500L) + retries++ + } + if(retries == maxRetries) { + Gdx.app.error(TAG, "Maximum retries ($maxRetries) exceeded, giving up :(") + } + } + } + + private var socket: Socket? = null + + val isReady: Boolean + get() = socket?.isConnected ?: false + + + @OptIn(ExperimentalStdlibApi::class) + private fun startNetwork( + host: String, + port: Int + ) = try { + var assignedClientId: UShort = 0U + + // Create our socket + socket = Socket() + + // Connect with timeout set. + socket?.let { + val socketAddress = InetSocketAddress(host, port) + it.connect(socketAddress, connectionTimeout) + + // ------------------------------------------------------------- + // Coroutine to handle messages + // to be sent to the server + // ------------------------------------------------------------- + coroutineScope.launch { + try { + it.outputStream.apply { + while (true) { + // 1. Get the next message in the channel + // to send onward to the server. + val nextMessage = toServerChannel.receive() + + // 2. Write the message to the server + // via the socket's output stream + write(nextMessage.serialize()) + + flush() + + Gdx.app.log(TAG, "Sent the message $nextMessage") + + delay(10L) + } + } + } catch (ex: java.net.SocketException) { + Gdx.app.error(TAG, "[SEND] Socket Failed: ${ex.message}") + } + } + // ------------------------------------------------------------- + // Coroutine to handle messages + // being received from the server + // + // Messages received are then forwarded via the + // ------------------------------------------------------------- + coroutineScope.launch { + try { + it.inputStream.apply { + launch { + while (true) { + val byteArray = ByteArray(1024) + +// delay(250L) + + // 1. Read data from the socket's + // input stream. + val count = read(byteArray, 0, 1024) + + if (count == -1) { + Gdx.app.error(TAG, "Socket Read Error!") + break + } + + val gameMessage = buildGameMessage(byteArray) + + fromServerChannel.send(gameMessage) + } + } + } + } catch (ex: java.net.SocketException) { + Gdx.app.log(TAG, "[RECEIVE] Socket Failure::[${ex.message}") + } + } + + } + + true + } catch (ex: java.net.SocketTimeoutException) { + Gdx.app.error(TAG, "Timeout Exception: ${ex.message}") + false + } catch (ex: java.net.ConnectException) { + Gdx.app.error(TAG, "Connection Exception: ${ex.message}") + false + } catch (ex: java.net.SocketException) { + Gdx.app.error(TAG, "Exception Raised: ${ex.message}") + false + } + + fun buildGameMessage(ba: ByteArray) = with(ByteBuffer.wrap(ba)) { + val messageType = GameMessageType.fromByte(get()) + + when(messageType) { + GameMessageType.JOIN_GAME -> GameMessage.JoinGameMessage.deserialize(this) + + GameMessageType.PLACE_TOKEN -> GameMessage.PlaceTokenMessage.deserialize(this) + } + } + + override fun dispose() { + coroutineScope.cancel() + } + + companion object { + val TAG = NetworkHandler::class.simpleName!! + } +} diff --git a/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/screens/FirstScreen.kt b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/screens/FirstScreen.kt new file mode 100644 index 0000000..0e9bed3 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/screens/FirstScreen.kt @@ -0,0 +1,177 @@ +package u0012604.tictactoe.screens + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.InputProcessor +import com.badlogic.gdx.graphics.OrthographicCamera +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import com.badlogic.gdx.math.Vector2 +import com.badlogic.gdx.utils.viewport.FitViewport +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.channels.ReceiveChannel +import kotlinx.coroutines.channels.SendChannel +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import ktx.app.KtxGame +import ktx.app.KtxScreen +import ktx.app.clearScreen +import ktx.assets.disposeSafely +import ktx.graphics.use +import u0012604.tictactoe.Board +import u0012604.tictactoe.GameMessage +import u0012604.tictactoe.Token + +class FirstScreen( + val game: KtxGame, + private val receiveChannel: ReceiveChannel, + private val sendChannel: SendChannel +) : KtxScreen, InputProcessor { + + private val batch = SpriteBatch() + + + private val camera = + OrthographicCamera(WIDTH, HEIGHT).apply { + position.set(Gdx.graphics.width / 2f, Gdx.graphics.height / 2f, 0f) + }; + private val viewport = FitViewport(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat(), camera) + + private val board = Board(viewport) + + private var touchPosition = Vector2.Zero + + private var localPlayerToken: Token? = null + + private var localPlayerTurn = false + + private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + + init { + Gdx.input.inputProcessor = this + + initiateIncomingGameMessageHandling() + } + + override fun render(delta: Float) { + clearScreen(red = 0.7f, green = 0.7f, blue = 0.7f) + + camera.update() + + batch.use { board.draw(it) } + } + + override fun resize(width: Int, height: Int) { + viewport.update(width, height) + + board.resize(width, height) + } + + private fun initiateIncomingGameMessageHandling() { + coroutineScope.launch { + while(true) { + val gm = receiveChannel.receive() + + when(gm) { + is GameMessage.JoinGameMessage -> { + localPlayerToken = gm.token + + localPlayerTurn = localPlayerToken == Token.NOUGHT + } + + is GameMessage.PlaceTokenMessage -> { + Gdx.app.log(TAG, "The other player placed a token at: (${gm.row}, ${gm.col})") + + board.placeToken(gm.row, gm.col, gm.token) + + localPlayerTurn = true + } + + } + + Gdx.app.log(TAG, "THE MESSAGE RECEIVED IS: $gm") + + delay(10L) + } + } + } + + override fun dispose() { + batch.disposeSafely() + board.disposeSafely() + } + + override fun keyDown(keycode: Int) = true + + override fun keyUp(keycode: Int) = true + + override fun keyTyped(character: Char) = true + + override fun touchDown( + screenX: Int, + screenY: Int, + pointer: Int, + button: Int + ): Boolean { + + if(!localPlayerTurn) + return true; + + val col = ((screenX.toFloat() / Gdx.graphics.width) * 3).toInt() + val row = ((screenY.toFloat() / Gdx.graphics.height) * 3).toInt() + + localPlayerToken?.let { + if (board.placeToken(row, col, it)) { + + localPlayerTurn = false + + val gameMessage = GameMessage.PlaceTokenMessage(row, col, it) + + coroutineScope.launch { + sendChannel.send(gameMessage) + } + } + } + + Gdx.app.log(TAG, "THE ROW IS: ($row, $col)") + + touchPosition.set(screenX.toFloat(), screenY.toFloat()) + + viewport.unproject(touchPosition) + + return true + } + + override fun touchUp( + screenX: Int, + screenY: Int, + pointer: Int, + button: Int + ) = true + + override fun touchCancelled( + screenX: Int, + screenY: Int, + pointer: Int, + button: Int + ) = true + + override fun touchDragged( + screenX: Int, + screenY: Int, + pointer: Int + ) = true + + override fun mouseMoved(screenX: Int, screenY: Int) = true + + override fun scrolled(amountX: Float, amountY: Float) = true + + companion object { + val TAG = FirstScreen::class.simpleName!! + + const val WIDTH = 100f + const val HEIGHT = 16f * WIDTH / 9f + + const val NOUGHT_RADIUS = 100f + } +} diff --git a/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/screens/GameOverScreen.kt b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/screens/GameOverScreen.kt new file mode 100644 index 0000000..d9af4b4 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/core/src/main/kotlin/u0012604/tictactoe/screens/GameOverScreen.kt @@ -0,0 +1,21 @@ +package u0012604.tictactoe.screens + +import com.badlogic.gdx.graphics.g2d.BitmapFont +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import ktx.app.KtxScreen +import ktx.app.clearScreen +import ktx.graphics.use + +class GameOverScreen : KtxScreen { + + private val font = BitmapFont() + private val batch = SpriteBatch() + + override fun render(delta: Float) { + clearScreen(red = 0.7f, green = 0.7f, blue = 0.7f) + + batch.use { + font.draw(it, "Game Over!", 10f, 10f) + } + } +} diff --git a/Exercises/NetworkedTicTacToe/gradle.properties b/Exercises/NetworkedTicTacToe/gradle.properties new file mode 100644 index 0000000..b6affa9 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/gradle.properties @@ -0,0 +1,28 @@ +# This doesn't need to be false, and some projects may be able to take advantage of setting daemon to true. +# We set it to false by default in order to avoid too many daemons from being created and persisting; each needs RAM. +org.gradle.daemon=false +# Sets starting memory usage to 512MB, maximum memory usage to 1GB, and tries to set as much to use Unicode as we can. +org.gradle.jvmargs=-Xms512M -Xmx1G -Dfile.encoding=UTF-8 -Dconsole.encoding=UTF-8 +# "Configure on-demand" must be false because it breaks projects that have Android modules. The default is also false. +org.gradle.configureondemand=false +# The logging level determines which messages get shown about how Gradle itself is working, such as if build.gradle +# files are fully future-proof (which they never are, because Gradle constantly deprecates working APIs). +# You can change 'quiet' below to 'lifecycle' to use Gradle's default behavior, which shows some confusing messages. +# You could instead change 'quiet' below to 'info' to see info that's important mainly while debugging build files. +# Note that if you want to use Gradle Build Scans, you should set the below logging level to 'lifecycle', otherwise +# the link to the scan won't get shown at all. +# Documented at: https://docs.gradle.org/current/userguide/command_line_interface.html#sec:command_line_logging +org.gradle.logging.level=quiet +ktxVersion=1.13.1-rc1 +kotlinVersion=2.2.21 +kotlinxCoroutinesVersion=1.8.1 +aiVersion=1.8.2 +artemisOdbVersion=2.3.0 +ashleyVersion=1.7.4 +visUiVersion=1.5.7 +android.useAndroidX=true +android.enableR8.fullMode=false +enableGraalNative=false +graalHelperVersion=2.0.1 +gdxVersion=1.14.0 +projectVersion=1.0.0 diff --git a/Exercises/NetworkedTicTacToe/gradle/gradle-daemon-jvm.properties b/Exercises/NetworkedTicTacToe/gradle/gradle-daemon-jvm.properties new file mode 100644 index 0000000..368b932 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/gradle/gradle-daemon-jvm.properties @@ -0,0 +1,12 @@ +#This file is generated by updateDaemonJvm +toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/73c462e34475aeb6509ab7ba3eda218f/redirect +toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/54001d0b636ad500b432d20ef3d580d0/redirect +toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/73c462e34475aeb6509ab7ba3eda218f/redirect +toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/54001d0b636ad500b432d20ef3d580d0/redirect +toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/29c55e6bad8a0049163f0184625cecd9/redirect +toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/3ac7a5361c25c0b23d933f44bdb0abd9/redirect +toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/73c462e34475aeb6509ab7ba3eda218f/redirect +toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/54001d0b636ad500b432d20ef3d580d0/redirect +toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/28937bb8a7f83f57de92429a9a11c04e/redirect +toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/52fa104f4f641439587f75dd68b31bc2/redirect +toolchainVersion=17 diff --git a/Exercises/NetworkedTicTacToe/gradle/wrapper/gradle-wrapper.jar b/Exercises/NetworkedTicTacToe/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..d1fa32c Binary files /dev/null and b/Exercises/NetworkedTicTacToe/gradle/wrapper/gradle-wrapper.jar differ diff --git a/Exercises/NetworkedTicTacToe/gradle/wrapper/gradle-wrapper.properties b/Exercises/NetworkedTicTacToe/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..54bc13c --- /dev/null +++ b/Exercises/NetworkedTicTacToe/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.0-milestone-1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/Exercises/NetworkedTicTacToe/gradlew b/Exercises/NetworkedTicTacToe/gradlew new file mode 100644 index 0000000..770e0b3 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/Exercises/NetworkedTicTacToe/gradlew.bat b/Exercises/NetworkedTicTacToe/gradlew.bat new file mode 100644 index 0000000..57f73cc --- /dev/null +++ b/Exercises/NetworkedTicTacToe/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/build.gradle b/Exercises/NetworkedTicTacToe/lwjgl3/build.gradle new file mode 100644 index 0000000..c3f387c --- /dev/null +++ b/Exercises/NetworkedTicTacToe/lwjgl3/build.gradle @@ -0,0 +1,185 @@ + +buildscript { + repositories { + gradlePluginPortal() + } + dependencies { + classpath "io.github.fourlastor:construo:2.1.0" + if(enableGraalNative == 'true') { + classpath "org.graalvm.buildtools.native:org.graalvm.buildtools.native.gradle.plugin:0.9.28" + } + } +} +plugins { + id "application" +} +apply plugin: 'io.github.fourlastor.construo' +apply plugin: 'org.jetbrains.kotlin.jvm' + + +import io.github.fourlastor.construo.Target + +sourceSets.main.resources.srcDirs += [ rootProject.file('assets').path ] +application.mainClass = 'u0012604.tictactoe.lwjgl3.Lwjgl3Launcher' +eclipse.project.name = appName + '-lwjgl3' +java.sourceCompatibility = 8 +java.targetCompatibility = 8 +if (JavaVersion.current().isJava9Compatible()) { + compileJava.options.release.set(8) +} +kotlin.compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8) + +dependencies { + implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion" + implementation "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop" + implementation "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop" + implementation "com.badlogicgames.gdx:gdx-lwjgl3-angle:$gdxVersion" + implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" + implementation project(':core') + + if(enableGraalNative == 'true') { + implementation "io.github.berstanio:gdx-svmhelper-backend-lwjgl3:$graalHelperVersion" + implementation "io.github.berstanio:gdx-svmhelper-extension-freetype:$graalHelperVersion" + + } +} + +def os = System.properties['os.name'].toLowerCase(Locale.ROOT) + +run { + workingDir = rootProject.file('assets').path +// You can uncomment the next line if your IDE claims a build failure even when the app closed properly. + //setIgnoreExitValue(true) + + if (os.contains('mac')) jvmArgs += "-XstartOnFirstThread" +} + +jar { +// sets the name of the .jar file this produces to the name of the game or app, with the version after. + archiveFileName.set("${appName}-${projectVersion}.jar") +// the duplicatesStrategy matters starting in Gradle 7.0; this setting works. + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + dependsOn configurations.runtimeClasspath + from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } +// these "exclude" lines remove some unnecessary duplicate files in the output JAR. + exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA') + dependencies { + exclude('META-INF/INDEX.LIST', 'META-INF/maven/**') + } +// setting the manifest makes the JAR runnable. +// enabling native access helps avoid a warning when Java 24 or later runs the JAR. + manifest { + attributes 'Main-Class': application.mainClass, 'Enable-Native-Access': 'ALL-UNNAMED' + } +// this last step may help on some OSes that need extra instruction to make runnable JARs. + doLast { + file(archiveFile).setExecutable(true, false) + } +} + +// Builds a JAR that only includes the files needed to run on macOS, not Windows or Linux. +// The file size for a Mac-only JAR is about 7MB smaller than a cross-platform JAR. +tasks.register("jarMac") { + dependsOn("jar") + group("build") + jar.archiveFileName.set("${appName}-${projectVersion}-mac.jar") + jar.exclude("windows/x86/**", "windows/x64/**", "linux/arm32/**", "linux/arm64/**", "linux/x64/**", "**/*.dll", "**/*.so", + 'META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA') + dependencies { + jar.exclude("windows/x86/**", "windows/x64/**", "linux/arm32/**", "linux/arm64/**", "linux/x64/**", + 'META-INF/INDEX.LIST', 'META-INF/maven/**') + } +} + +// Builds a JAR that only includes the files needed to run on Linux, not Windows or macOS. +// The file size for a Linux-only JAR is about 5MB smaller than a cross-platform JAR. +tasks.register("jarLinux") { + dependsOn("jar") + group("build") + jar.archiveFileName.set("${appName}-${projectVersion}-linux.jar") + jar.exclude("windows/x86/**", "windows/x64/**", "macos/arm64/**", "macos/x64/**", "**/*.dll", "**/*.dylib", + 'META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA') + dependencies { + jar.exclude("windows/x86/**", "windows/x64/**", "macos/arm64/**", "macos/x64/**", + 'META-INF/INDEX.LIST', 'META-INF/maven/**') + } +} + +// Builds a JAR that only includes the files needed to run on Windows, not Linux or macOS. +// The file size for a Windows-only JAR is about 6MB smaller than a cross-platform JAR. +tasks.register("jarWin") { + dependsOn("jar") + group("build") + jar.archiveFileName.set("${appName}-${projectVersion}-win.jar") + jar.exclude("macos/arm64/**", "macos/x64/**", "linux/arm32/**", "linux/arm64/**", "linux/x64/**", "**/*.dylib", "**/*.so", + 'META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA') + dependencies { + jar.exclude("macos/arm64/**", "macos/x64/**", "linux/arm32/**", "linux/arm64/**", "linux/x64/**", + 'META-INF/INDEX.LIST', 'META-INF/maven/**') + } +} + +construo { + // name of the executable + name.set(appName) + // human-readable name, used for example in the `.app` name for macOS + humanName.set(appName) + + targets.configure { + register("linuxX64", Target.Linux) { + architecture.set(Target.Architecture.X86_64) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.15%2B6/OpenJDK17U-jdk_x64_linux_hotspot_17.0.15_6.tar.gz") + // Linux does not currently have a way to set the icon on the executable + } + register("macM1", Target.MacOs) { + architecture.set(Target.Architecture.AARCH64) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.15%2B6/OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.15_6.tar.gz") + // macOS needs an identifier + identifier.set("u0012604.tictactoe." + appName) + // Optional: icon for macOS, as an ICNS file + macIcon.set(project.file("icons/logo.icns")) + } + register("macX64", Target.MacOs) { + architecture.set(Target.Architecture.X86_64) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.15%2B6/OpenJDK17U-jdk_x64_mac_hotspot_17.0.15_6.tar.gz") + // macOS needs an identifier + identifier.set("u0012604.tictactoe." + appName) + // Optional: icon for macOS, as an ICNS file + macIcon.set(project.file("icons/logo.icns")) + } + register("winX64", Target.Windows) { + architecture.set(Target.Architecture.X86_64) + // Optional: icon for Windows, as a PNG + icon.set(project.file("icons/logo.png")) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.15%2B6/OpenJDK17U-jdk_x64_windows_hotspot_17.0.15_6.zip") + // Uncomment the next line to show a console when the game runs, to print messages. + //useConsole.set(true) + } + } +} + +// Equivalent to the jar task; here for compatibility with gdx-setup. +tasks.register('dist') { + dependsOn 'jar' +} + +distributions { + main { + contents { + into('libs') { + project.configurations.runtimeClasspath.files.findAll { file -> + file.getName() != project.tasks.jar.outputs.files.singleFile.name + }.each { file -> + exclude file.name + } + } + } + } +} + +startScripts.dependsOn(':lwjgl3:jar') +startScripts.classpath = project.tasks.jar.outputs.files + +if(enableGraalNative == 'true') { + apply from: file("nativeimage.gradle") +} diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.icns b/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.icns new file mode 100644 index 0000000..466410d Binary files /dev/null and b/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.icns differ diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.ico b/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.ico new file mode 100644 index 0000000..51b99d4 Binary files /dev/null and b/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.ico differ diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.png b/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.png new file mode 100644 index 0000000..9c84f59 Binary files /dev/null and b/Exercises/NetworkedTicTacToe/lwjgl3/icons/logo.png differ diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/nativeimage.gradle b/Exercises/NetworkedTicTacToe/lwjgl3/nativeimage.gradle new file mode 100644 index 0000000..4178f0e --- /dev/null +++ b/Exercises/NetworkedTicTacToe/lwjgl3/nativeimage.gradle @@ -0,0 +1,54 @@ + +project(":lwjgl3") { + apply plugin: "org.graalvm.buildtools.native" + + graalvmNative { + binaries { + main { + imageName = appName + mainClass = application.mainClass + requiredVersion = '23.0' + buildArgs.add("-march=compatibility") + jvmArgs.addAll("-Dfile.encoding=UTF8") + sharedLibrary = false + resources.autodetect() + } + } + } + + run { + doNotTrackState("Running the app should not be affected by Graal.") + } + + // Modified from https://lyze.dev/2021/04/29/libGDX-Internal-Assets-List/ ; thanks again, Lyze! + // This creates a resource-config.json file based on the contents of the assets folder (and the libGDX icons). + // This file is used by Graal Native to embed those specific files. + // This has to run before nativeCompile, so it runs at the start of an unrelated resource-handling command. + generateResourcesConfigFile.doFirst { + def assetsFolder = new File("${project.rootDir}/assets/") + def lwjgl3 = project(':lwjgl3') + def resFolder = new File("${lwjgl3.projectDir}/src/main/resources/META-INF/native-image/${lwjgl3.ext.appName}") + resFolder.mkdirs() + def resFile = new File(resFolder, "resource-config.json") + resFile.delete() + resFile.append( + """{ + "resources":{ + "includes":[ + { + "pattern": ".*(""") + // This adds every filename in the assets/ folder to a pattern that adds those files as resources. + fileTree(assetsFolder).each { + // The backslash-Q and backslash-E escape the start and end of a literal string, respectively. + resFile.append("\\\\Q${it.name}\\\\E|") + } + // We also match all of the window icon images this way and the font files that are part of libGDX. + resFile.append( + """libgdx.+\\\\.png|lsans.+)" + } + ]}, + "bundles":[] +}""" + ) + } +} diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/src/main/kotlin/u0012604/tictactoe/lwjgl3/Lwjgl3Launcher.kt b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/kotlin/u0012604/tictactoe/lwjgl3/Lwjgl3Launcher.kt new file mode 100644 index 0000000..366bde2 --- /dev/null +++ b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/kotlin/u0012604/tictactoe/lwjgl3/Lwjgl3Launcher.kt @@ -0,0 +1,40 @@ +@file:JvmName("Lwjgl3Launcher") + +package u0012604.tictactoe.lwjgl3 + +import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application +import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration +import u0012604.tictactoe.Main + +/** Launches the desktop (LWJGL3) application. */ +fun main() { + // This handles macOS support and helps on Windows. + if (StartupHelper.startNewJvmIfRequired()) + return + Lwjgl3Application(Main(), Lwjgl3ApplicationConfiguration().apply { + setTitle("NetworkedTicTacToe") + //// Vsync limits the frames per second to what your hardware can display, and helps eliminate + //// screen tearing. This setting doesn't always work on Linux, so the line after is a safeguard. + useVsync(true) + //// Limits FPS to the refresh rate of the currently active monitor, plus 1 to try to match fractional + //// refresh rates. The Vsync setting above should limit the actual FPS to match the monitor. + setForegroundFPS(Lwjgl3ApplicationConfiguration.getDisplayMode().refreshRate + 1) + //// If you remove the above line and set Vsync to false, you can get unlimited FPS, which can be + //// useful for testing performance, but can also be very stressful to some hardware. + //// You may also need to configure GPU drivers to fully disable Vsync; this can cause screen tearing. + + + setWindowedMode(640, 480) + //// You can change these files; they are in lwjgl3/src/main/resources/ . + //// They can also be loaded from the root of assets/ . + setWindowIcon(*(arrayOf(128, 64, 32, 16).map { "libgdx$it.png" }.toTypedArray())) + + //// This should improve compatibility with Windows machines with buggy OpenGL drivers, Macs + //// with Apple Silicon that have to emulate compatibility with OpenGL anyway, and more. + //// This uses the dependency `com.badlogicgames.gdx:gdx-lwjgl3-angle` to function. + //// You can choose to remove the following line and the mentioned dependency if you want; they + //// are not intended for games that use GL30 (which is compatibility with OpenGL ES 3.0). + setOpenGLEmulation(Lwjgl3ApplicationConfiguration.GLEmulation.ANGLE_GLES20, 0, 0) + + }) +} diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/src/main/kotlin/u0012604/tictactoe/lwjgl3/StartupHelper.kt b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/kotlin/u0012604/tictactoe/lwjgl3/StartupHelper.kt new file mode 100644 index 0000000..3d3cc2c --- /dev/null +++ b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/kotlin/u0012604/tictactoe/lwjgl3/StartupHelper.kt @@ -0,0 +1,174 @@ + +/* + * Copyright 2020 damios + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//Note, the above license and copyright applies to this file only. +package u0012604.tictactoe.lwjgl3 + +import com.badlogic.gdx.Version +import com.badlogic.gdx.backends.lwjgl3.Lwjgl3NativesLoader +import org.lwjgl.system.JNI +import org.lwjgl.system.macosx.LibC +import org.lwjgl.system.macosx.ObjCRuntime +import java.io.BufferedReader +import java.io.File +import java.io.InputStreamReader +import java.lang.management.ManagementFactory +import java.util.* + +/** + * Adds some utilities to ensure that the JVM was started with the + * `-XstartOnFirstThread` argument, which is required on macOS for LWJGL 3 + * to function. Also helps on Windows when users have names with characters from + * outside the Latin alphabet, a common cause of startup crashes. + * + * [Based on this java-gaming.org post by kappa](https://jvm-gaming.org/t/starting-jvm-on-mac-with-xstartonfirstthread-programmatically/57547) + * @author damios + */ +class StartupHelper private constructor() { + init { + throw UnsupportedOperationException() + } + + companion object { + private const val JVM_RESTARTED_ARG = "jvmIsRestarted" + /** + * Starts a new JVM if the application was started on macOS without the + * `-XstartOnFirstThread` argument. This also includes some code for + * Windows, for the case where the user's home directory includes certain + * non-Latin-alphabet characters (without this code, most LWJGL3 apps fail + * immediately for those users). Returns whether a new JVM was started and + * thus no code should be executed. + * + * **Usage:** + * + * ``` + * fun main() { + * if (StartupHelper.startNewJvmIfRequired(true)) return // This handles macOS support and helps on Windows. + * // after this is the actual main method code + * } + * ``` + * + * @param redirectOutput + * whether the output of the new JVM should be rerouted to the + * old JVM, so it can be accessed in the same place; keeps the + * old JVM running if enabled + * @return whether a new JVM was started and thus no code should be executed + * in this one + */ + @JvmOverloads + fun startNewJvmIfRequired(redirectOutput: Boolean = true): Boolean { + val osName = System.getProperty("os.name").lowercase(Locale.ROOT) + if (!osName.contains("mac")) { + if (osName.contains("windows")) { + // Here, we are trying to work around an issue with how LWJGL3 loads its extracted .dll files. + // By default, LWJGL3 extracts to the directory specified by "java.io.tmpdir", which is usually the user's home. + // If the user's name has non-ASCII (or some non-alphanumeric) characters in it, that would fail. + // By extracting to the relevant "ProgramData" folder, which is usually "C:\ProgramData", we avoid this. + // We also temporarily change the "user.name" property to one without any chars that would be invalid. + // We revert our changes immediately after loading LWJGL3 natives. + val programData = System.getenv("ProgramData") ?: "C:\\Temp\\" + val prevTmpDir = System.getProperty("java.io.tmpdir", programData) + val prevUser = System.getProperty("user.name", "libGDX_User") + System.setProperty("java.io.tmpdir", "$programData/libGDX-temp") + System.setProperty("user.name", "User_${prevUser.hashCode()}_GDX${Version.VERSION}".replace('.', '_')) + Lwjgl3NativesLoader.load() + System.setProperty("java.io.tmpdir", prevTmpDir) + System.setProperty("user.name", prevUser) + } + return false + } + + // There is no need for -XstartOnFirstThread on Graal native image + if (System.getProperty("org.graalvm.nativeimage.imagecode", "").isNotEmpty()) { + return false + } + + // Checks if we are already on the main thread, such as from running via Construo. + val objcMsgSend = ObjCRuntime.getLibrary().getFunctionAddress("objc_msgSend") + val nsThread = ObjCRuntime.objc_getClass("NSThread") + val currentThread = JNI.invokePPP(nsThread, ObjCRuntime.sel_getUid("currentThread"), objcMsgSend) + val isMainThread = JNI.invokePPZ(currentThread, ObjCRuntime.sel_getUid("isMainThread"), objcMsgSend) + if (isMainThread) return false + + val pid = LibC.getpid() + + // check whether -XstartOnFirstThread is enabled + if ("1" == System.getenv("JAVA_STARTED_ON_FIRST_THREAD_$pid")) { + return false + } + + // check whether the JVM was previously restarted + // avoids looping, but most certainly leads to a crash + if ("true" == System.getProperty(JVM_RESTARTED_ARG)) { + System.err.println( + "There was a problem evaluating whether the JVM was started with the -XstartOnFirstThread argument." + ) + return false + } + + // Restart the JVM with -XstartOnFirstThread + val jvmArgs = ArrayList() + val separator = System.getProperty("file.separator", "/") + // The following line is used assuming you target Java 8, the minimum for LWJGL3. + val javaExecPath = System.getProperty("java.home") + separator + "bin" + separator + "java" + // If targeting Java 9 or higher, you could use the following instead of the above line: + //String javaExecPath = ProcessHandle.current().info().command().orElseThrow(); + if (!File(javaExecPath).exists()) { + System.err.println( + "A Java installation could not be found. If you are distributing this app with a bundled JRE, be sure to set the -XstartOnFirstThread argument manually!" + ) + return false + } + jvmArgs.add(javaExecPath) + jvmArgs.add("-XstartOnFirstThread") + jvmArgs.add("-D$JVM_RESTARTED_ARG=true") + jvmArgs.addAll(ManagementFactory.getRuntimeMXBean().inputArguments) + jvmArgs.add("-cp") + jvmArgs.add(System.getProperty("java.class.path")) + var mainClass = System.getenv("JAVA_MAIN_CLASS_$pid") + if (mainClass == null) { + val trace = Thread.currentThread().stackTrace + mainClass = if (trace.isNotEmpty()) { + trace[trace.size - 1].className + } else { + System.err.println("The main class could not be determined.") + return false + } + } + jvmArgs.add(mainClass) + try { + if (!redirectOutput) { + val processBuilder = ProcessBuilder(jvmArgs) + processBuilder.start() + } else { + val process = ProcessBuilder(jvmArgs) + .redirectErrorStream(true).start() + val processOutput = BufferedReader( + InputStreamReader(process.inputStream) + ) + var line: String? + while (processOutput.readLine().also { line = it } != null) { + println(line) + } + process.waitFor() + } + } catch (e: Exception) { + System.err.println("There was a problem restarting the JVM") + e.printStackTrace() + } + return true + } + } +} diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx128.png b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx128.png new file mode 100644 index 0000000..9c84f59 Binary files /dev/null and b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx128.png differ diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx16.png b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx16.png new file mode 100644 index 0000000..41b4465 Binary files /dev/null and b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx16.png differ diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx32.png b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx32.png new file mode 100644 index 0000000..5139d3b Binary files /dev/null and b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx32.png differ diff --git a/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx64.png b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx64.png new file mode 100644 index 0000000..6c207cb Binary files /dev/null and b/Exercises/NetworkedTicTacToe/lwjgl3/src/main/resources/libgdx64.png differ diff --git a/Exercises/NetworkedTicTacToe/settings.gradle b/Exercises/NetworkedTicTacToe/settings.gradle new file mode 100644 index 0000000..4fbd49b --- /dev/null +++ b/Exercises/NetworkedTicTacToe/settings.gradle @@ -0,0 +1,8 @@ +plugins { + // Applies the foojay-resolver plugin to allow automatic download of JDKs. + id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0" +} +// A list of which subprojects to load as part of the same larger project. +// You can remove Strings from the list and reload the Gradle project +// if you want to temporarily disable a subproject. +include 'android', 'core', 'lwjgl3'