An implementation of Gröbner Nim in Kotlin/JS.
Gröbner Nim is a game that involves the production of sticks. It is named for Gröbner bases, and by sheer coincidence simulates the computation of a Gröbner basis over the modulo-2 field. A full description appears in a paper I wrote with Haley Dozier. You can find the game in action here, along with a summary of how to play, along with a couple of variants.
The game is pedagogical in nature, and I wrote it for a summer program with high school students. However, I have had some fun trying to beat the computer.
This is a work-in-progress and is provided free-of-charge, so while it is in current use, it isn't guaranteed to be suitable for any needs except my own, and even then I encounter bugs on occasion.
-
First build it in Idea, obtaining the files
Groebner_Nim.js
,Groebner_Nim.js.map
in thebuild/distributions
folder. -
Copy these files to the folder where your HTML source code resides.
-
In the HTML file, create a canvas element. The example linked above uses dimensions of 600×600.
- For now, it is essential to give the ID
game_canvas
to the canvas element; that is,<canvas id="game_canvas" ...>
.
- For now, it is essential to give the ID
-
At the beginning of the HTML file, preferably in the header, add the following line:
<script type="text/javascript" src="path/to/kotlin.js"></script> <script type="text/javascript" src="path/to/Groebner_Nim.js"></script>path/to/
is the path to wherever you've placedGroebner_Nim.js
. IfGroebner_Nim.js
is in the same folder as the HTML file, you can simply writesrc=Groebner_Nim.js
and ignore the path. You can probably place this line anywhere in the file, just so long as it appears before the line stated below.After that, you should be able to access the following functions from JavaScript:
Groebner_Nim.random_game()
,Groebner_Nim.level_zero_game()
,Groebner_Nim.level_one_game()
,Groebner_Nim.replay()
. The example page above invokes these when the user clicks a button; for instance,<input type="button" value="Random" onclick="Groebner_Nim.random_game()">
...but there are other ways to do it.
The source code is in Kotlin and compiles to JavaScript. Once Kotlin compiles to WebAssembly, I intend to bypass JavaScript entirely.
simple.kt
handles the game control. The unfortunate name is due to Idea's defaults, and I plan to change it once I get around to figuring out how.Games.kt
defines a game ofGroebner_Solitaire
, along with aJS_Game
data class, a front-end for JavaScript.Grids.kt
defines the abstractGrid
class and theJS_Grid
data class, a front-end for JavaScript.Orderings.kt
defines several ways to evaluate the head of a stick.- An abstract
Ordering
class. - Three objects that implement and instantiate it:
GrevLex_Ordering
,Lex_Ordering
, andWeighted_GrevLex
.
- An abstract
Points.kt
defines aPoint
data class.Sticks.kt
defines both aStick
data class and several functions related to determining when the game finishes.
There is also an example HTML page, index.html
.
Already linked, but here it is again.
Gröbner Nim by John Perry is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.