An AI engine and multi-platform implementation for the board game Khet.
This project started as a Ruby gem and over time, new ways to interact with the core C code were built (see the Subprojects section below).
The core AI engine is written in C for performance and uses bit-packing, alpha-beta pruning, and Zobrist hashing. The code lives in the ext/khetai directory.
Example (via the Ruby gem):
require 'khetai'
# initial board setup:
# A = anubis, P = pyramid, S = scarab, X = pharaoh, L = sphinx
# capital letters = red, lowercase letters = silver
# numbers = 1 north, 2 east, 3 south, 4 west
board = ["L2", "--", "--", "--", "A2", "X2", "A2", "P1", "--", "--",
"--", "--", "P2", "--", "--", "--", "--", "--", "--", "--",
"--", "--", "--", "p3", "--", "--", "--", "--", "--", "--",
"P0", "--", "p2", "--", "S2", "S3", "--", "P1", "--", "p3",
"P1", "--", "p3", "--", "s1", "s0", "--", "P0", "--", "p2",
"--", "--", "--", "--", "--", "--", "P1", "--", "--", "--",
"--", "--", "--", "--", "--", "--", "--", "p0", "--", "--",
"--", "--", "p3", "a0", "x0", "a0", "--", "--", "--", "l0"]
whose_turn = 1 # silver = 0, red = 1
max_search_depth = 25 # between 2 and 25
max_search_time = 5 # in seconds
move = KhetAI.move(board, whose_turn, max_search_depth, max_search_time)
#=> [start_index, end_index, rotation]
# rotation = 0 none, 1 clockwise, -1 counterclockwiseUses SDL3 for its rendering and input processing. The laser is animated in real time using vector math and the pieces are all drawn with SDL_RenderGeometry calls. It also runs in the browser via WebAssembly, compiled with Emscripten.
Playable here: https://jkugs.github.io/
Located in the ext/khetai/dev/sdl-ui directory.
A C++ FLTK GUI that allows manual testing of the AI and gameplay. Supports live reloading of the AI during runtime using Shift + K.
Located in the ext/khetai/dev/fltk-ui directory.
$ gem install khetai
To build the gem from source, you'll need:
- Ruby >=
2.3.0 - GCC or compatible C compiler
- Development headers (usually provided by ruby-dev or ruby-devel package)
This project uses asdf as the version manager for Ruby. However, any Ruby version >= 2.3.0 should work to compile this gem.
$ gem install bundler
$ bundle install
$ bundle exec rake compile
$ bundle exec rake build
$ gem install pkg/khetai-<version>.gem
Once tested and verified, bump the version number in lib/khetai/version.rb and commit changes.
To release and push to rubygems.org:
$ bundle exec rake release
To push a pre-built gem manually:
$ gem push pkg/<gem>
To build and learn from... 🐢
