Tamed ZoG (Part 3: Football Kuma valley)

But we say, here in this cave on the edge of the world, dwarves and trolls have made peace, so to go hand in hand under the hand of Death.

And we say: our enemy is not a Troll and not a Dwarf, and malice, slander, cowardice, vessels disgust, those who do evil under the guise of good. That's who we fought today, but the stubbornness of fools forever and say... that's a lie

sir Terry Pratchett


In the previous article, I talked about original table the game, designed by Trevor Turana, based on the works of famous English writer Terry Pratchett. In 2005, Truran developed a new the game using the same set of pieces on the same Board. The name of this game — "Koom Valley Thud" and, today, I will try to implement it, in passing telling about the possibilities of language ZRF, which are not told earlier.

Let me remind you that ZRF is the language of the description of the game rules (like Lisp) used by Zillions of Games. Despite the fact that there are some difficult moments, overall, it is quite simple and can be mastered by anyone, even very far from programming. The main advantage of the game kernel ZoG is its versatility. Describing the rules, we almost immediately received a new game. Although AI ZoG inferior to specialized game engines, he plays a surprisingly strong. Unfortunately, the free demo version of the application allows you to run only a limited set of games and allows you to load the ZRF-describe own development.

As usual, I'll start with the rules of the game. Board for Koom Valley Thud is the only difference from the original Board to Thud Central field is available for the moves of the pieces. "Scala", located on it in the beginning of the game, can move across the field. The goal of the dwarves to drag the rock to the last rank of the Board. Trolls, in order to win, you need to surround the Rock at least three pieces, and to do this they need to on your course.

The moves of the pieces have undergone more serious changes. Trolls, in this game, you can move one, two or three squares in any direction (vertical, horizontal, or diagonal). The capture of opponent's pieces is carried out according to the rules of Draughts — Troll jumps over the gnome (located on an adjacent cell), and removes his figure from the Board, stopping at the next cell. As in Checkers, the chain takes can be continued, but, unlike them, the taking is at the request of the player. As you move, take can be in any direction. Through their shapes and the Rock trolls can not jump.

Dwarves, as before, can go to any number of squares vertically, horizontally, or diagonal (like the Queen in Chess). In order to withdraw the figure of the Troll, the dwarves must "surround" it with two sides (in this respect, the game resembles a TEFL, with the difference that "surround" you and diagonally). For move, from the Board can be removed a few trolls (if they were "surrounded" by the dwarves). Instead of running figure, dwarves can move the Rock one space in any direction. A prerequisite for this movement is the door to the figure of the dwarf in both primary and end point displacement (neighbor does not have to be the same dwarf). Gnomes can't jump over other pieces.

Comprehended rules, you can proceed with the description of the ZRF. I will not give here the macros descriptions of the boards and the game. Differences them from Thud is minimal, in addition, a full description of the game you can always watch on GitHub. Focus on less obvious things. From the trolls, we need to describe the features of two fundamentally different moves (with and without capture). Thus, if you are taking shape, the next move can only be taking. This is done as follows:
Initially described the usual course of a Troll
( define kv-troll 
( $1 
(verify empty?) 
add
$1
(verify empty?) 
add
$1
(verify empty?) 
add
)
)


It says here that the figure can walk on empty cells in one, two or three cells in the specified direction.

Next, describe the progress with the capture
( define kv-troll-capturing
( $1
(verify (piece? Dwarf) )
capture
$1
(verify empty?)
(add-partial capturing-type)
)
)


We are moving in that direction and, if the cell is a dwarf, remove it from the Board. Moving on, if the next cell is blank, and complete the course of the new policy add-partial. In contrast to add, it allows you to perform a partial stroke. As a parameter, the Directive add-partial is passed the type of stroke, which must be continued. The types of moves are defined in the description of the shape:

Types of moves
 (piece
(name Troll)
(image Trolls "images\gluk\T. bmp")
(description "Troll")
(moves
(move-type capturing-type)
(kv-troll-capturing n) (kv-troll-capturing ne)
(kv-troll-capturing e) (kv-troll-capturing nw)
(kv-troll-capturing s) (kv-troll-capturing se)
(kv-troll-capturing w) (kv-troll-capturing sw)
(move-type non-capturing-type)
(kv-troll n) (kv-troll ne)
(kv-troll e) (kv-troll nw)
(kv-troll s) (kv-troll se)
(kv-troll w) (kv-troll sw)
)
)


At the level of the description of the game (game), the Directive move-priorities allows you to set priorities for the types of moves, making the capture binding (as in Checkers), but we will not do this, allowing a choice to the player. Move the gnome described simply:

gnome
( define check-troll
( if (enemy? $1)
mark
$1
( if (piece? Dwarf $1) 
capture
)
back
)
)

( define kv-dwarf
( $1 
( while empty? 
( if on-board?
(check-troll n) (check-troll nw)
(check troll s) (check-troll sw)
(check-troll w) (check-troll ne)
(check-troll e) (check-troll se)
add
)
$1 
)
)
)


Concluding the course, we check out all areas, for the presence of the Troll, "surrounded" a dwarf from the opposite side and removed from the Board found figure. You should pay attention to the fact that the predicate a piece? may accept two parameters — the type of shape and direction (that allows to check not moving to the next cell). It remains to describe the move "Rocks":

Move "the Rock"
( define rock
( (verify (or (piece? Dwarf n) (piece? Dwarf nw) 
(piece? Dwarf s) (piece? Dwarf sw) 
(piece? Dwarf w) (piece? Dwarf ne) 
(piece? Dwarf e) (piece? Dwarf se) ) )
$1
(verify empty?)
(verify (or (piece? Dwarf n) (piece? Dwarf nw) 
(piece? Dwarf s) (piece? Dwarf sw) 
(piece? Dwarf w) (piece? Dwarf ne) 
(piece? Dwarf e) (piece? Dwarf se) ) )
add
)
)


The main difficulty, as usual, connected with the counting. We need to determine the condition of a victory of trolls as the next "Cliff" with three of their figures. This could be achieved by using the Directive relative-config, similar to how it is done in the implementation of the TIC TAC toe, but we would need to describe all the mutual arrangement of four adjacent figures (which in itself is quite interesting combinatorial problem) and, most importantly, such a condition would work as the course the trolls and the dwarves (which is contrary to the terms of the game).

There is another way
( define check-proximity
( if (on-board? $1)
( if (friend? $1)
( if (flag? is-second)
change-owner
else
( set-flag is-second true )
)
)
)
)

( define check-rock-direction
( set-flag is-second false )
( if (on-board? $1)
$1
( if (and enemy? (piece? Rock) ) 
( check-proximity $2 )
( check-proximity $3 )
( check-proximity $4 )
( check-proximity $5 )
( check-proximity $6 )
( check-proximity $7 )
( check-proximity $8 )
)
$5
)
)

( define check-rock
( check-rock-direction n ne e se s sw w nw)
( check-rock-direction ne e se s sw w nw n)
( check-rock-direction e se s sw w nw n ne)
( check-rock-direction se s sw w nw n ne e)
( check-rock-direction s sw w nw n ne e se)
( check-rock-direction sw w nw n ne e se s)
( check-rock-direction w nw n ne e se s sw)
( check-rock-direction nw n ne e se s sw w)
)


This macro allows you, after completing the course a Troll, to check, whether is not adjacent to the endpoint of the stroke, the Rock adjacent also with two other trolls. It is used as follows:
Modified moves trolls
( define kv-troll 
( $1 
(verify empty?) 
(check-rock)
add
$1
(verify empty?) 
(check-rock)
add
$1
(verify empty?) 
(check-rock)
add
)
)

( define kv-troll-capturing
( $1
(verify (piece? Dwarf) )
capture
$1
(verify empty?)
(check-rock)
(add-partial capturing-type)
)
)


If the condition of the neighborhood with the three trolls is performed, we change the owner of "Cliff" change owner. Now we can easily formulate the condition for the defeat of the dwarves, as the loss of all figures of type "Rock":

the
(loss-condition (Dwarfs) (pieces-remaining 0 Rock))

In the implementation of the macro should also pay attention to the flag teams flag? and set-flag. Flags allow you to store state in the generation process of the move. In addition to the global flags, there is the possibility to associate Boolean values with the positions of the Board (with the command set-position-flag), and also with the shapes (command set-attribute). Notice that any attempt to use the flags to store global state, like a this will fail.

Primarily, this is due to the fact that the flags and positions are reset at the beginning of each turn, but the main reason is that they can be used only in the process of calculating moves, and the order of invocation of macros, at this stage, is not defined (which leads to undesirable side effects). In addition, the developers ZoG is recommended not to abuse positioning flags, because their use has a negative impact on performance. In principle, for storing state between moves, you can use the attributes of shapes, but this method is very time-consuming.

It remains to describe the game. As we add new game option to the existing ZRF-description, use the keyword variant:

Koom Valley Thud
(variant
(title "Koom Valley Thud")
(description "...")
(history "...")
(strategy "a") 
(players Dwarfs Trolls)
(turn-order Dwarfs Trolls)
(kv-game-defs)
(piece
(name of Rock)
(image Dwarfs "images\gluk\R. bmp"
Trolls "images\gluk\R. bmp" )
(description "Rock")
(moves
(w rock) (rock sw)
(e rock) (rock se)
(n rock) (rock nw)
(rock's) (rock ne)
)
)
(piece
(Dwarf name)
(image Dwarfs "images\gluk\d.bmp")
(description "Dwarf")
(moves
(kv-dwarf ne) (kv-dwarf n)
(kv-dwarf nw) (kv-dwarf's)
(kv-dwarf se) (kv-dwarf e)
(kv-dwarf sw) (kv-dwarf w)
)
)
(piece
(name Troll)
(image Trolls "images\gluk\T. bmp")
(description "Troll")
(moves
(move-type capturing-type)
(kv-troll-capturing n) (kv-troll-capturing ne)
(kv-troll-capturing e) (kv-troll-capturing nw)
(kv-troll-capturing s) (kv-troll-capturing se)
(kv-troll-capturing w) (kv-troll-capturing sw)
(move-type non-capturing-type)
(kv-troll n) (kv-troll ne)
(kv-troll e) (kv-troll nw)
(kv-troll s) (kv-troll se)
(kv-troll w) (kv-troll sw)
)
)
)


We can add in the options menu separators using the following syntax:

the
(variant
(title"")
)

Running the game on a run, you notice that the dwarves are busy, mainly running from the trolls (as in the previous game, the trolls will be too strong), and time to move "Rocks" they have no choice. As I and old Marcoin favour of peace between dwarves and trolls, I used the flexibility of ZoG and made rules minor changes:

I banned all capture of enemy pieces
( define  shift  
( $1 
( while empty? 
add 
$1
)
)
)

( define kvb-troll 
( $1 
(verify empty?) 
(verify (on-board? $1) ) 
(check-rock)
add
$1
(verify empty?) 
(verify (on-board? $1) ) 
(check-rock)
add
$1
(verify empty?) 
(verify (on-board? $1) ) 
(check-rock)
add
)
)


And was allowed to move the ball (let it be a ball) more than one cell
( define ball
( ( verify (or (piece? Dwarf n) (piece? Dwarf nw) 
(piece? Dwarf s) (piece? Dwarf sw) 
(piece? Dwarf w) (piece? Dwarf ne) 
(piece? Dwarf e) (piece? Dwarf se) ) 
)
$1
( while empty?
( if (or (piece? Dwarf n) (piece? Dwarf nw) 
(piece? Dwarf s) (piece? Dwarf sw) 
(piece? Dwarf w) (piece? Dwarf ne) 
(piece? Dwarf e) (piece? Dwarf se) )
add
)
$1
)
)
)


In addition, as you can see, I banned the trolls to come to the edge of the Board. If this is not done, the trolls win-win strategy appears to fill the last rank his pieces, arranging the remaining trolls (just three) hunting for the ball. In Koom Valley Thud it is also possible, but there are trolls you can eat (by the way, the computer, under the control of ZoG, to such tactics never guessed).

The result is, in my opinion, good. The game remained fairly complex and positional, becoming more dynamic. Dwarves (under computer control) is now easily cope with the task (which suggests that I too have facilitated their life) and, after a dizzying number of passes across the field, roll the ball into the goal. Those interested can see how it looks on the video:


Article based on information from habrahabr.ru

Комментарии

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

Automatically create Liquibase migrations for PostgreSQL

Vkontakte sync with address book for iPhone. How it was done

What part of the archived web