📜 ⬆️ ⬇️

Tame ZoG (Part 1: The Wolves and the Goat)

Wolves and Kid In the previous article I talked about the unique, in my opinion, project Zillions of Games . As I promised, I begin a small cycle of educational articles on the description of the possibilities of a declarative language intended for the development of new (and description of already existing) games within this project.

In order not to overload the presentation with unnecessary (at this stage) details, I chose to implement a very simple game. I played it in my childhood. It is called "Wolves and the Goat." The rules are as follows: Wolves (black pieces) - walk one cell diagonally, only forward. The goatling (white figure) - also walks on one cell diagonally, but in any direction. White's task is to pass to any of the four cells of his color of the last horizontal. The task of black is to deprive whites of the opportunity to move.

Since on the standard 8x8 chess board, Black wins over elementary, we use a 9x9 board for playing. This game is very simple (and like children). With the right game, whites always win.

The rules description language (ZRF) is very similar to LISP . As in LISP, it is very important to keep track of the balance of opening and closing brackets (otherwise, the game just won't load). As in LISP, comments begin with a semicolon and continue to the end of the line.
')
; ***************************************************************** ; ***    ; ***************************************************************** (version "2.0") 

Here, in the commentary, we briefly describe the game and indicate the version of Zillions of Games on which it should work. On the younger version of the "engine" the game will not start.

In ZRF, macros are widely used. They are pretty simple, but they can use parameters. The following entry is a macro describing the only possible move in our game - one cell in the indicated direction:

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

When used in write code:

 (checker-shift ???) 

The macro is revealed as follows:

 (??? (verify empty?) add) 

The meaning of the transmitted parameter and the whole of this record, I will explain below. So far, it is important for us to understand that macros save us from a large amount of scribbling and the associated possibility of errors in the description.

The following describes the board. Its description is also taken to make a macro:

 (define board-defs (image "images\gluk\Board.bmp") (grid (start-rectangle 2 2 48 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 description of the image is clear - this is the name of the image file that is loaded to display the board. The following is a description of the square board (grid). It should be noted that the possibilities of ZRF are not limited to the description of rectangular boards. Using this language, one can describe triangular and hexagonal boards, one can describe multidimensional boards, up to 5 dimensions, one can “glue” the edges of the board, defining its topology, etc. We will not stop here now. Details of such descriptions can be found in the chm-file that describes the language ZRF (supplied with the game), as well as in a huge number of already implemented games on all kinds of boards.

Three key phrases of the grid description are important for our game: the start-rectangle describes how the board “overlaps” the loaded picture. The phrase dimensions - describes the measurements (we have two). The lines at the beginning of the description of each dimension are important - they describe the order of cell numbering (thus determining the coordinate system). The following parameter determines how the “ruler” of the measurement is superimposed on the board. It should be said that when using an image of a board designed on its own, difficulties may arise with the selection of these numerical values, as well as the values ​​indicated in the start-rectangle. It may take many attempts to get the images of the figures in the right places on the image of the board.

The next, very important phrase (directions) defines the directions in which figures can move. We define four directions — northwest (nw), northeast (ne), southwest (sw), and southeast (se). By the phrase symmetry, we define the rules by which these directions are converted for the black player.

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

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

In this macro, we define the conditions of the game. The board phrase defines the board we previously defined (the board-defs macro expands). In the board-setup, the initial positions of the players are determined, after which, the win-condition and loss-condition phrases define the victory and defeat conditions for the players. For Whites, we define the victory condition in the zone that we previously defined, and the defeat condition, for both sides, is the inability to make another move.

It remains to determine the game:

 (game (title "  ") (description "     ") (players White Black) (turn-order White Black) (game-defs) (piece (name WC) (image White "images\gluk\W.bmp") (description "") (help "  1      ") (moves (checker-shift ne) (checker-shift nw) (checker-shift se) (checker-shift sw) ) ) (piece (name BC) (image Black "images\gluk\B.bmp") (description "") (help "  1     ") (moves (checker-shift ne) (checker-shift nw) ) ) ) 

Most of the definitions here are intuitive. It is worth staying at just a few points. The phrase players describes the very players of White and Black, which we have already used earlier. There can be more than two players, and one player can be identified in the puzzles (it is even possible to determine the player who makes random moves, but this is a topic for a separate conversation). The turn-order phrase defines the order of turn for players (it may also differ from the simple alternation of two players).

Next, after the description of the game settings (game-defs), follows the description of the figures used in the game (piece). Most descriptions are also understandable. The moves section lists all possible moves for the figure. It is here that we use the checker-shift macro, which we spoke about at the very beginning. As it is easy to see, as a parameter, the possible direction of movement of the figure is transferred to it. As a result of the macro deployment, it turns out something like the following:

 ( ne (verify empty?) add ) 

We perform three steps - we move in the indicated direction, check whether the cell is empty and place the figure (there is a difficult time to understand. For myself, I believe that at the beginning of the turn the figure is removed from the board, and at the end of the turn it returns to the board). In more detail, I plan to talk about the possibilities of describing the rules of moves in the following articles, using more complex games as an example.

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

Important remark
Warning: in order to run this code, you will need to unlock the Zillions of Games kernel in some way (for example, buy a serial number)

Source: https://habr.com/ru/post/211100/


All Articles