Writing Your Own Zillions Game
The rules for a game are stored in a Zillions Rules File. These files end with a ".zrf". Zillions loads the ZRF file, and uses it to find out how to run the game. ZRF files have 4 main parts: the board, the pieces, the goals of the game, and additional information like help and strategy.
You can use any common text editor (like Windows Notepad) to create your new game. Just remember to save your --game with a ".zrf" ending, and make sure you save as text or ASCII.
Here is a framework for a game:
(game
;...players, help and extra information goes here
;...board stuff goes here
;...piece stuff goes here
;...goals of game go here
)
There are some important things to note about the overall format. Firstly, you will want to pay careful attention to match parentheses when writing Zillions Rules Files. The parentheses are used to group sections together, so use a ")" for every "(" you type.
Notice that some lines begin with a semi-colon (";") character. This indicates that the rest of this line is a comment and will be ignored when Zillions loads this ZRF. It is a good idea to use lots of comments in your ZRF to make it easier to understand.
Consistent indentation also makes your program easier to read. Zillions, on the other hand, doesn稚 care. It treats all "white space" equally, from a single space to many tabs and carriage returns. The exception is in a string, delimited with quotation marks, which we値l get to shortly.
OK, let's add a little more to our game:
(game
(title "Tic-Tac-Toe")
(description "One side takes X's and the other side takes O's.
Players alternate placing their marks on open spots.
The object is to get three of your marks in a row horizontally,
vertically, or diagonally. If neither side accomplishes this,
it's a cat's game (a draw).")
(history "Tic-Tac-Toe was an old adaptation of Three Men's Morris to
situations where there were no available pieces. You can draw or
carve marks and they are never moved. It is played all over the
world under various names, such as 'Noughts and Crosses' in
England.")
(strategy "With perfect play, Tic-Tac-Toe is a draw. Against less
than perfect opponents it's an advantage to go first, as having an
extra mark on the board never hurts your position. The center is
the key square as 4 possible wins go through it. The corners are
next best as 3 wins go through each of them. The remaining
squares are least valuable, as only 2 wins go through them.
Try to get in positions where you can `trap` your opponent by
threatening two 3-in-a-rows simultaneously with a single move. To
be a good player, you must not only know how to draw as the second
player, you must also be able to takes advantage of bad play.")
)
You can see we have added several sections which describe the game. The "title" section gives our game a name. All ZRF games need names. A single ZRF can contain multiple games and variants, so don稚 forget to add a title. In this case, we are programming the game "Tic-Tac-Toe".
Next, you will see sections on "description", "history" and "strategy". This is extra information for the user, to tell them the object of the game, the history of the game, and suggested strategy to improve his play.
OK, now we need to add some more parts:
(game
(title "Tic-Tac-Toe")
; description, history and strategy omitted to save space.
(players X O)
(turn-order X O)
(board
(image "images\TicTacToe\TTTbrd.bmp")
(grid
(start-rectangle 16 16 112 112) ; top-left position
(dimensions ;3x3
("top-/middle-/bottom-" (0 112)) ; rows
("left/middle/right" (112 0))) ; columns
(directions (n -1 0) (e 0 1) (nw -1 -1) (ne -1 1))
)
)
(piece
(name man)
(help "Man: drops on any empty square")
(image X "images\TicTacToe\TTTX.bmp"
O "images\TicTacToe\TTTO.bmp")
(drops ((verify empty?) add))
)
(board-setup
(X (man off 5))
(O (man off 5))
)
(draw-condition (X O) stalemated)
(win-condition (X O)
(or (relative-config man n man n man)
(relative-config man e man e man)
(relative-config man ne man ne man)
(relative-config man nw man nw man)
)
)
)
The above sample is a complete game. You could cut it out and paste it into a ZRF file, and play it. Let's look at each of the new sections:
(players X O)
This line tells Zillions the names of the players. In this case, there are two players named "X" and "O". That was easy!
(turn-order X O)
This line tells Zillions the turn-order that players move. In this case, "X" will move first, and then "O" will move. After that, the turn order will repeat, so "X" will move again, and so on.
OK, we have told Zillions about the players. Let痴 tell the program about the board:
(board
(image "images\TicTacToe\TTTbrd.bmp")
(grid
(start-rectangle 16 16 112 112) ; top-left position
(dimensions ;3x3
("top-/middle-/bottom-" (0 112)) ; rows
("left/middle/right" (112 0))) ; columns
(directions (n -1 0) (e 0 1) (nw -1 -1) (ne -1 1))
)
)
Use the "board" statement to tell Zillions about the board. The "image" statement tells Zillions what bitmap file (*.BMP) to use to display the board. In this case, the file in "images\TicTacToe\TTTbrd.bmp" will be used. You can create new board images using a graphics editor, like Windows Paint.
The "grid" statement makes it easy for you to specify a regularly spaced set of positions. The "start-rectangle" tells Zillions rectangle of the upper left screen position. In this case, this is from the point 16,16 to the point 112, 112. Points are measured over and down from the upper left corner of the window.
The "dimensions" section has information about the placement and name of the positions. The line "("top-/middle-/bottom-" (0 112)) " tells Zillions the rows will be named "top-", "middle-" and "bottom-". To progress downward to the next row, the Y coordinate will have 112 added to it (the X coordinate will remain unchanged, since there is a 0 in that position). The next line has "("left/middle/right" (112 0)))". This specifies the column names and offsets. The "directions" statement indicates the directions linking each position ("n" for north, "e" for "east" and so on). The numbers after these names indicated which way to step for that direction. Use 0 for no change, or "1" or "-1" to got forward or backward. When Zillions reads this "grid" statement, then it will combine all this information to make a three by three grid, with names of positions like "top-left" and "bottom-right".
OK, we have a board now, what do we use to specify a piece? Why, the "piece" statement, of course:
(piece
(name man)
(help "Man: drops on any empty square")
(image X "images\TicTacToe\TTTX.bmp"
O "images\TicTacToe\TTTO.bmp")
(drops ((verify empty?) add))
)
The "name" section gives this piece a name: "man". The "help" section gives the text which Zillions will automatically display in the Status bar when the user points to the piece. You should put information about how the piece moves in this section. The "image" section gives the bitmap names for Zillions to use for each piece and for each player. You can make new piece bitmaps using a graphics editor like Windows Paint. Note that any fully green part of the bitmap will appear as "transparent". The "drops" section tells Zillions that this piece is dropped onto the board when it moves. The "(verify empty?)" section tells Zillions to make sure a position is empty before "add"ing it to the board.
Next, we tell Zillions how many pieces there are, and where they are located at the start of the game:
(board-setup
(X (man off 5))
(O (man off 5))
)
The "board-setup" section tells Zillions that there are 5 men for each player off the board at the start of the game.
OK, just one more section. How do we tell Zillions the goal(s) of the game? Easy, use "-condition" statements:
(draw-condition (X O) stalemated)
(win-condition (X O)
(or (relative-config man n man n man)
(relative-config man e man e man)
(relative-config man ne man ne man)
(relative-config man nw man nw man)
)
)
The "draw-condition" statement tells us that if any side is "stalemated" (has no legal moves left), then the game is a draw. The "win-condition" statement is a little more complex, so let's go over it:
After "(win-condition" we see "(X O)", the names of the players which this condition applies to. Next comes an "(or". This tells Zillions that any of the following conditions indicates a win. In Tic-Tac-Toe, you can win by placing three of your men in a row. They can be vertical (n direction), horizontal (e direction), or along any of the two diagonals (ne and nw directions). The "relative-config" statements tell Zillions that any position where a "man" piece is north of another "man" piece which is north of a third "man" piece indicates a win. This line is repeated for the other three directions.
That痴 it. Zillions supplies lots more commands and ways to generate moves, add sound effects and music to your games, and detect more complex winning goals, but you have the basics now. Look through the keywords section to learn more about these and other Zillions commands. And remember, there are a host of examples to look at included with your copy of
Zillions of Games.
Zillions of Games lets you create game variants with very little work. Let痴 say you wanted to create a variant of Tic-Tac-Toe where the first player to get three-in-a-row loses. The only changes you would need are in the game title, description and the goal of the game. Just add these lines to the sample ZRF file:
(variant
(title "Losing Tic-Tac-Toe")
(description "This is the same as normal TicTacToe, except that the
object is NOT to get 3-in-a-row.\\
One side takes X's and the other side takes O's. Players alternate
placing their marks on open spots. The object is to avoid getting three of
your marks in a row horizontally, vertically, or diagonally. If there are
no 3-in-a-rows, it's a cat's game (a draw).")
(loss-condition (X O)
(or (relative-config man n man n man)
(relative-config man e man e man)
(relative-config man ne man ne man)
(relative-config man nw man nw man)
)
)
)
You can see that we have changed the title and description to better describe the new variant, plus the win-condition has been changed to a loss-condition.
Zillions will use the other sections from the main game description (like the board, pieces and so on) and just substitute the changed sections (title, description and loss-condition in this case). Simple variants can be created in a few minutes.
最終更新:2013年05月05日 20:36