Games::Alak -- simple game-tree implementation of a gomoku-like game
% perl -MGames::Alak -e Games::Alak::play ...Or just run Alak.pm as if it were a program... ...Program responds with output, and a prompt: Lookahead set to 3. I am X, you are O. Enter h for help X moves from 1 to 5, yielding .xxxx..oooo alak> ...and now you enter the commands to play.
This module implements a simple game-tree system for the computer to play against the user in a game of Alak. You can just play the game for fun; or you can use this module as a starting point for understanding game trees (and implementing smarter strategy -- the module's current logic is fairly simple-minded), particularly after reading my Perl Journal #18 article on trees, which discusses this module's implementation of game trees as an example of general tree-shaped data structures.
Alak was invented by the mathematician A. K. Dewdney, and described in his 1984 book Planiverse. The rules of Alak are simple -- at least as I've (mis?)understood them and implemented them here:
* Alak is a two-player game played on a one-dimensional board with eleven slots on it. Each slot can hold at most one piece at a time. There's two kinds of pieces, which I represent here as "x" and "o" -- x's belong to one player (called X -- that's the computer), o's to the other (called O -- that's you).
* The initial configuration of the board is:
For sake of reference, the slots are numbered from 1 (on the left) to 11 (on the right), and X always has the first move.
* The players take turns moving. At each turn, each player can move only one piece, once. (This is unlike checkers, where you move one piece per move but get to keep moving it if you jump an your opponent's piece.) A player cannot pass up on his turn. A player can move any one of his pieces to the next unoccupied slot to its right or left, which may involve jumping over occupied slots. A player cannot move a piece off the side of the board.
* If a move creates a pattern where the opponent's pieces are surrounded, on both sides, by two pieces of the mover's color (with no intervening unoccupied blank slot), then those surrounded pieces are removed from the board.
* The goal of the game is to remove all of your opponent's pieces, at which point the game ends. Removing all-but-one ends the game as well, since the opponent can't surround you with one piece, and so will always lose within a few moves anyway.
A game between X (computer) and a particularly dim O (human):
xxxx___oooo ^ Move 1: X moves from 3 (shown with caret) to 5 (Note that any of X's pieces could move, but that the only place they could move to is 5.) xx_xx__oooo ^ Move 2: O moves from 9 to 7. xx_xx_oo_oo ^ Move 3: X moves from 4 to 6. xx__xxoo_oo ^ Move 4: O (stupidly) moves from 10 to 9. xx__xxooo_o ^ Move 5: X moves from 5 to 10, making the board "xx___xoooxo". The three o's that X just surrounded are removed. xx___x___xo O has only one piece, so has lost.
This module uses
Term::ReadLine to give you a prompt at which you can type commands.
Entering "h" for help at that prompt will give instructions on how to interact with the game.
When in doubt, consult the source -- it's made to be fairly clear.
Burke, Sean M. 2000. "Trees". (In submission: actual article title may differ.) Article in The Perl Journal #18.
http://www.tpj.com/ [Portions of this POD are excerpted from that article.]
Dewdney, A[lexander] K[eewatin]. 1984. Planiverse: Computer Contact with a Two-Dimensional World. Poseidon Press, New York.
Copyright (c) 2000-2006 Sean M. Burke. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Current maintainer Avi Finkel,
email@example.com; Original author Sean M. Burke,
Thanks to A. K. Dewdney (
http://www.dewdney.com/) for his encouragement in writing my (abovementioned) TPJ article, as well as for having written the enjoyable book where he briefly describes it.