The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Games::Shogi - Base class describing the Shogi game and variants

SYNOPSIS

  use Games::Shogi;
  $Game = Games::Shogi->new;
  $tl_piece = $Game->board()->[0][0];
  print @{$Game->neighbor($tl_piece)};
  print $Game->japanese_name('g'); # kinsho

DESCRIPTION

initial_board()

Return a 2-D array of piece abbreviations in the initial board configuration.

neighbor($piece_abbr)

Return an array of directions that a given piece can travel. See below for descriptions of other restrictions that exist in the game, and the example source for how to use the directions in the course of an actual game.

board()

Return the board contents as a 2-D array reference.

english_name($piece_abbr)

Return the English name for the given piece.

japanese_name($piece_abbr)

Return the Japanese name in romaji for the given piece.

promote($piece_abbr)

Return what the piece promotes to, or undef if no promotion exists.

RULES SUMMARY

Much like Western chess, shogi is a two-player game on a square board. The most commonly-played variety is played on a 9-by-9 board, but subclasses implement games from Micro Shogi's 4 x 5 board to Taikyoku Shogi's 36 x 36 board, with more than 400 types of pieces.

In ASCII graphics, the initial layout looks like this:

   9 8 7 6 5 4 3 2 1
  +-----------------+
  |L N S G K G S N L|a
  |  R           B  |b
  |P P P P P P P P P|c
  |                 |d
  |                 |e
  |                 |f
  |p p p p p p p p p|g
  |  b           r  |h
  |l n s g k g s n l|i
  +-----------------+

The ranks read top to bottom, as in Western chess, but the files read right-to-left, following Japanese convention. Unlike Western chess, pawns have no special powers (I.E. they capture and move one square in front of them, no en passant captures, no two-square first move). Knights move like in Western chess, but only to the two squares in front of them. The rook and bishop move as in Western chess, but when they promote they gain the ability to move one square in any direction.

Pieces can promote by moving into or within the opponent's third rank. This is not mandatory, except for pieces that otherwise would be unable to move, such as a pawn or lance on the last rank, and knights in the last two ranks.

If a player captures an opponent's piece, it is not taken out of the game. Instead, it is said to be 'in hand.' The piece has any promotions taken away, but to compensate the player may elect to drop any piece they have in hand back onto the board instead of moving a piece on the board, subject to the following restrictions:

  • Only one unpromoted pawn per file

  • A pawn may not be dropped to give direct checkmate

  • Pieces may not be dropped where they have no legal move

  • A piece may not be promoted as it is dropped

Of course, checkmating the king is the object of the game. The king can escape check by moving out of check, interposing or dropping a piece to block the checking piece, or capturing the piece giving check.

PIECE DESCRIPTIONS

Since this module is basically an encapsulation of Shogi pieces for use in board games, the internal $piece hashref encodes a great deal of information about the pieces.

Pieces are indexed by a chosen (up to 4 letter) lower-case abbreviation that corresponds to what you see in the board description. Collisions are all too frequent, and in general the piece in the more common game wins. Following the pattern set up by the Gold and Silver generals, other generals such as the Tile General and Wood General also get their one-letter abbreviations when they don't collide with established abbreviations.

Some Shogi pieces do promote, and those are given a 'promote' key corresponding to the index of the piece they promote to. In some games it's possible for a piece to promote more than once, in which case it simply promotes to the next piece.

For some, not all, pieces the English pronunciation of their Japanese name is given, and eventually the Unicode equivalents will be added, if they actually exist.

Of course, this information would be useless if we didn't describe how the pieces move and capture. Where possible, we represent a piece's neighborhood of where it can move to on a 5x5 grid, which looks roughly like this internally:

  cm => {
    name => 'Center Master',
    neighborhood => [
      q(x x x),
      q( \|/ ),
      q(o3^3o),
      q( 3|3 ),
      q(o x o) ] },

I'm not sure of the piece's Japanese pronunciation, and to the best of my knowledge it doesn't promote, so those keys simply don't exist. The neighborhood is where this piece can move to, and is fairly complex. There are worse pieces, but those generally have to be dealt with on a case-by-case basis. We'll cover some of those later.

Working from the center of the diagram out, the '\', '|' and '/' represent directions in which the piece can slide until it reaches another piece. '/' represents the ability to slide to the northeast along a diagonal, '\' to the northwest, and so on.

The piece can also jump over intervening friendly pieces, and that ability is captured by the 'x' among the top row. This simply says that the Center Master can jump two squares to the north, northeast and northwest. Furthermore, the 'o3' and '3o' notations mean that the piece can slide 3 squares east, west, southeast and southwest.

Another special class of piece is the Hook Mover.

  pc => {
    name => 'Peacock',
    promote => 'lng',
    neighborhood => [
      q(X   X),
      q( \ / ),
      q(  ^  ),
      q( o o ),
      q(o   o) ] },

The 'X' here represents a Hook Mover piece as opposed to a jumping piece. The Peacock is a diagonal hook mover, other pieces (with the hook represented by '+' move orthogonally. The Peacock can slide any distance on the forward diagonals, turn 90 degrees, and keep moving.

Orthogonal hook movers (those with '+' at the end of a '|' or '-') slide any distance on the orthogonal, then turn 90 degrees and keep moving.

Of course, some pieces can jump where others merely slide. Take, for an extreme example, the Great General in Tenjiku Shogi and larger:

  \ | /
   \|/
  --^--
   /|\
  / | \

The doubled dashes, diagonals &c indicate that this piece can jump over any intervening pieces in any direction. Unlike the Fire Demon, however, the Great General can only capture the piece it lands on.

Pieces below are pretty much too complex to code reasonably, and have to have special exceptions made in the move logic.

Lion
  22222
  21112
  21^12
  21112
  22222

Lions can slide to any square marked 1, or jump to a square marked 2. It can also slide to any square marked 1, then slide back to its starting square, effectively passing.

When capturing, Lions use what's known as an igui power. This is to say that they can capture on any square numbered 1, and either capture another piece on an adjacent square marked 1 or capture on an adjacent square marked 2.

If you capture your opponent's Lion with a Lion, you can only do so if your opponent's Lion is either undefended and on a square marked 2, or if you use your igui power to capture a piece that is not a Pawn or Go-Between on a square marked 1.

Otherwise, capturing your opponent's Lion with a piece that is not a Lion is subject to the restriction that your opponent can't capture your Lion on her turn unless it's with another Lion.

Horned Falcon
    2
   \1/
   -^-
   /|\

The Horned Falcon can capture a piece on the square marked 1 and then either return to its starting square or capture a piece on the square marked 2, using its igui power.

Soaring Eagle
  2   2
   1|1
   -^-
   /|\

The Soaring Eagle also has igui power.

Fire Demon
  \   |   /
   ooooooo
   ooooooo
   oo!!!oo
  -oo!^!oo-
   oo!!!oo
   ooooooo
   ooooooo
  /   |   \

Tenjiku Shogi introduces the Fire Demon, with yet another complicated move. This piece can slide to any piece within three squares and capture there. It can also slide like the Western queen, with the added benefit that when it stops, it captures any enemy pieces on the squares marked '!'.

Vice General
  \   |   /
   ooooooo
   ooooooo
   ooooooo
  -ooo^ooo-
   ooooooo
   ooooooo
   ooooooo
  /   |   \

The Vice General is nearly the equivalent of the Fire Demon, except that it can't burn pieces one square away like the Fire Demon can.

Lion Hawk
  \  |  /
   22222
   21112
  -21^12-
   21112
   22222
  /  |  \

The Lion Hawk is essentially the Lion with the ability to range. Thankfully it can't use both the ranging and the igui power in the same move.

Heavenly Tetrarchs
   \   /
    !!!
  ox!^!xo
    !!!
   /   \

Like the Fire Demon, it can burn any piece in a one-square radius when it stops sliding. Unlike the Fire Demon, it can't move to any square just one away. It either has to follow the diagonals or jump to the 'x' or 'o' squares.

Buddhist Spirit

This moves as a Lion Hawk, essentially.

Furious Fiend

This moves as either the Lion Hawk or Lion Dog.

Emperor

The Emperor can move to any open square on the board, and capture any piece that isn't being defended by another piece. It has to be captured to win the game, so it seems that the only way to win is capture any pieces defending the emperor, then go in for the kill.

OTHER VARIANTS

Many other variants of Shogi exist and are still played today. The subclasses Games::Shogi::Tenjiku, Games::Shogi::Taikyoku etc. describe variants of Shogi that have a wide variety of pieces and powers. Pieces with special powers are detailed in the individual subclasses, but a few pieces deserve special notice.

For instance, the Fire Demon (in Tenjiku Shogi, among others) can move three squares in any direction, slide diagonally, *and* captures ("burns") any enemy piece in a one-square radius. The Lion (also in Tenjiku Shogi) can move one square any direction, capture, and choose to move one more square in any direction.

EXPORT

None

SEE ALSO

perl

AUTHOR

Jeffrey Goff, <jgoff@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2004 by Jeffrey Goff

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 366:

=over should be: '=over' or '=over positive_number'