Friday, January 11, 2013

Versi Tutorial

A new tutorial was added to Lark's Python Guide: Versi Tutorial -- a clone of the Reversi game.

Some of the highlights:

A rather neat approach is used to capture enemy pieces -- a 'ray' is cast from the starting location (ray creates the list of all pieces in line, horizontal, vertical or diagonal, to the end of board), then I use the itertools.groupby() function to group tiles into player's, enemy's and blanks; finally a custom nextgroup() function is used two times to get two next groups.

The nextgroup(groupby_iterator, default=None) returns a convenient wrapper object with key and group attributes; or a default value if there is no next group.

This makes it very easy to create the piece-capture logic: to be successful, first group needs to be enemy tiles and the second, player's; on success, first group returned to be captured.

Turns handling is a little tricky in this game, and, I think, nicely handled here: a player may have no turns available, then the enemy takes turns until the player can make a move, the game only ends when either player can make no turn (which may happen when some blank tiles are still left on the board).

The full list of tutorials is here:  Lark's Guide.

No comments:

Post a Comment