Tamed ZoG (Part 1: the wolf and the Goat)

Wolf and Goat previous article, I talked about the unique, in my opinion, the project Zillions of Games. As I promised, I'm starting a small series of educational articles on the description, a declarative language intended for the development of new (and the descriptions of already existing) in the framework of this project.

In order not to clutter the presentation of superfluous (at this stage) details, I chose to implement a very simple game. I played it in childhood. It's called "the Wolves and the Kid". The rules are as follows: Wolves (the black pieces) — go one square diagonally, only forward. Goat (white figure) — moves one space diagonally, but in any direction. The goal of the white — pass on any of the four cells of your color other. The goal of the black — white to deny the possibility of progress.

As on a regular 8x8 black is winning elementary, use for the game Board is 9x9 cells. This game is very simple (like children). With proper play, white will always win.

Language description rules (ZRF) is very similar to LISP. As in LISP-e, it is very important to monitor the balance of opening and closing brackets (otherwise, the game just won't load). As in LISP, e, comments begin with a semicolon and continue to the end of the row.

the
; *****************************************************************
; *** The wolf and the Goat
; *****************************************************************

(version "2.0")

Here, in the comments, we briefly describe the game and specify the version of Zillions of Games where she has to work. A younger version of "engine" the game will not start.

In the ZRF, and the use of macros. They are fairly simple but can use parameters. The following entry is a macro describing the only possible in our game move one square in the indicated direction:

the
(define checker-shift ($1 (verify empty?) add))

If you are using in your code write:

the
(checker-shift ???)

The macro expands as follows:

the
(??? (verify empty?) add)

The meaning of the passed parameter and this entry I will explain below. Yet, it is important to understand that macros save us from the big amount of paperwork and associated possibility of errors in the description.

The following describes the Board. Its description is also accepted to submit in the macro:

the
(define board-defs
(image "images\gluk\Board.bmp")
(grid
(start-rectangle 2 48 2 48)
(dimensions
("a/b/c/d/e/f/g/h/i" (48 0)) ; files
("9/8/7/6/5/4/3/2/1" (0 48)) ; ranks
)
(directions (nw -1 -1) (ne 1 -1) (se 1 1) (sw -1 1))
)
(symmetry Black (ne sw) (sw ne) (nw se) (se nw))
(zone (name goals) (players White) (positions b9 d9 f9 h9) )
)

The meaning of the image descriptions is clear — this is the name of a picture file are downloaded for display boards. The square Board (grid). It should be noted that the possibility of ZRF are not limited to the description of the rectangular boards. Using this language, we can describe triangular and hexagonal boards, it is possible to describe a multidimensional Board, up to 5 dimensions, you can "glue" the edges of the Board, specifying its topology, etc. We're not going to stop there. The details of such descriptions can be found in the. chm file that describes the language ZRF (supplied with the game), as well as a huge number of games sold on all sorts of boards.

For our game there are three important key phrase of the description grid: start-rectangle describes how the Board is superimposed on the uploaded image. The phrase dimensions — describes the measurements (we have two). Lines at the beginning of the description of each dimension is important — they describe the numbering of cells (defining, thus, the coordinate system). The following parameter determines how the "ruler" measurements superimposed on the Board. I should say that if you are using image boards, developed independently, difficulties may arise with the selection of these numerical values and the values specified in start-rectangle. May require many attempts in order to image the pieces fell in the right places on the picture Board.
Next, very important phrase (directions) sets the directions in which to move the shape. We define four directions, North-West (nw), northeast (ne), southwest (sw) and Southeast (se). The phrase symmetry, we define the rules by which these areas are converted to black player.

The last line (zone) — defines a set of cells, which we will use further in determining the conditions of victory for White.

the
(define game-defs
(board
(board-defs)
)
(board-setup
(White (WC e2) )
(Black (BC d9 b9 f9 h9) )
)
(win-condition (White) (absolute-config WC (goals)))
(loss-condition (White Black) stalemated)
)

In this macro, we define the rules of the game. Phrase board sets previously determined by us Board (the board macro-defs disclosed). In board-setup are determined by the initial position of the players, after which the phrase win-condition and a loss condition is determined by the conditions of victory and defeat for the players. Condition of victory for the White we define passing in previously defined us zone and defeat for both sides, the inability to make the next move.

It remains to determine the game:

the
(game
(title "the Wolves and the Kid")
(description "Spend a white chip to the last rank")
(players White Black)
(turn-order White Black)
(game-defs)
(piece
(WC name)
(image White "images\gluk\W. bmp")
(description "the Kid")
(help "Walks on 1 cell on a diagonal forward and back")
(moves
(checker-shift ne)
(checker-shift nw)
(checker-shift se)
(checker-shift sw)
)
)
(piece
(BC name)
(image Black "images\gluk\B. bmp")
(description "the Wolf")
(help "Walks on 1 cell diagonally forward")
(moves
(checker-shift ne)
(checker-shift nw)
)
)
)

Most of the definitions here intuitive. Worth staying on just a few points. The phrase describes most of those players players White and Black, which we already used earlier. Players can be more than two, and the puzzles can be defined by one player (there is even the possibility to define a player making random moves, but that's a topic for another discussion). The phrase turn-order determines the order of priority for players (it also can differ from simple alternation of moves of the two players).

Further, after the description of the game settings (game-defs), follows the description of the figures used in the game (piece). The descriptions are also clear. The section lists all possible moves for the figure moves. Here we use the macro checker-shift, which was mentioned in the beginning. It is easy to see, as a parameter, it is passed possible direction of movement of the figure. As a result of deploying a macro that turns something like the following:

the
( ne
(verify empty?)
add
)

We perform three steps — moving in specified direction, check whether the empty cage and put a figure (here is the difficult thing to understand. For myself, I believe that in the beginning of the turn figure is removed from the Board, and at the end of the back Board). Read more about the possibilities of describing the rules of moves I plan to tell in the following articles, for example, more complex games.

The new game is ready. Those interested can download it from the repository on GitHub.

Important note
Note: in order to run this code, you'll need some way to unlock the core Zillions of Games (i.e. by serial number)

Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Car navigation in detail

PostgreSQL: Analytics for DBA

Google has launched an online training course advanced search