Game writing tutorial

To write a new game you need to create two files: an xml file describing the layout of the client (what it looks like) and a java file that implements the rules of the game. Each game consists of a number of different collections of cards, like a stack or a list of cards. These are defined in the xml file. The java file enables transactions of cards between those collections according to the rules. You can also use other user interface elements like buttons and labels.

The xml file

The root element of the xml file is called "gameLayout", it can have three types of children. The first type is the "player" element, it holds all the card collections and other user interface elements that belong to one of the players. The player element has an "index" attribute. This attribute is the position of the player at the table. The local player always has index 0 and the player to the local player's left has index 1, and so on.

The second child element is called "common". This holds all the user interface elements that are common to all the players.

Finnally there are the "invisible" elements. These are card collections that aren't shown in the user interface but are referred to in the rules. Typically the main stack of cards (from wich the cards are dealt) is one of the invisible elements. You can't use other user interface elements in the invisible section.

<gameLayout>
   <player index="0">
      ...
   </player>
   <player index="1">
      ...
   </player>
   <common>
      ...
   </common>
   <invisible>
       ...
   </invisible>
</gameLayout>

There are 5 types of card collections: "cardList", "sortedCardList", "stack", "trick" and "trickStack" and 4 kinds of other user interface elements: "label", "suitDisplay", "button" and "numberInput". They all have a "name" attribute that has to be unique and that can be used to access them in the java file.

All user interface elements are positioned in the GUI using a "layout" child element, this element follows the GridBagConstraints positioning rules and can have all the attributes that are available for these constraints. Ofcourse the layout element has no use for invisible collections.

<layout gridx="1" gridy="3" top="5" left="5" bottom="5" right="5" />

Card collections

The "cardList" is a list of cards that are displayed from left to right partially overlapping each other. The "sortedCardList" looks the same but the cards are displayed in a sorted order that is detirmined according to a Comparator passed to the collection in the java file. They both have 3 additional attributes. The attribute "maxCards" gives an indication on how wide the list needs to be in the GUI, although they can contain more cards during the game. The "rotate" attribute can have the values "0", "90", "180" and "270". These are the degrees that the list is rotated in the GUI. Finally the "faceVisible" attribute sets to whom the face of the cards is visible. The values can be "none", "owner" or "all".

<sortedCardList name="hand" maxCards="13" rotate="0" faceVisible="owner">
   <layout ... />
</sortedCardList>

A "cardList" and a "sortedCardList" can also have a "selectButton" attached to it. This button is needed when the java file sends a SelectCardsAction to the player (see later). The button needs to appear in the same section ("player" or "common") as the card list. The "selectButton" has two attributes: the "cardList" and a "label". The "cardList" reffers to the name of the linked card list and the label gets put on the button in the GUI.

<sortedCardList name="hand" maxCards="13" rotate="0" faceVisible="owner">
   <layout ... />
</sortedCardList>
<selectButton cardList="hand" label="Play Cards">
   <layout ... />
</selectButton>

The "stack" represents a stack of cards and has only one extra attribute: "faceVisible". This can have the same values as with a "cardList".

<stack name="playerStack" faceVisible="none">
   <layout ... />
</stack>

A "trick" represents the trick in trick taking games. It has an extra attribute "nbCards", this is the number of cards in a trick and usually equals the number of players.

<trick name="trick" nbCards="4">
   <layout ... />
</trick>

A "trickStack" represents the tricks that are collected (taken) by a player during the game. It has a "maxTricks" and "rotate" attribute. The "maxTricks" is again used to calculate the size in the GUI. The "rotate" attribute works the same as in the "cardList".

<trickStack name="trickStack" maxTricks="13" rotate="180">
   <layout ... />
</trickStack>

Other user interface elements

For other user interface elements you can use: "label", "suitDisplay", "button" and "numberInput".

A label puts some text on the GUI screen. It has two attributes: a "name" and an optional "value". The name is used by the java file and the value gets put on the screen. There are two special labels with the names "playerName" and "score". They always have to be put in the player section of the document and they don't need a value. The first gets replaced with the name of the player that sits on that position and the second with that player's score.

<label name="statusLabel" value="Start of the game">
   <layout ... />
</label>

A "suitDisplay" is used to show one of the four suits. It can for example be used to show the thrump suit. It only has a "name" attribute.

<suitDisplay name="thrump">
   <layout ... />
</suitDisplay>

A "button" has two attributes: a "name" and a "label". The "name" is used in the java file to reffer to it and the "label" gets shown in the GUI.

<button name="pass" label="Pass">
   <layout ... />
</button>

A "numberInput" is for example used for the betting in a poker game. Like the "button" it has a "name" and a "label" attribute that both work in the same way as with the "button".

<numberInput name="betRaise" label="Bet">
   <layout ... />
</numberInput>

Finally you can also use "panel" elements to group user interface elements and create a more refined layout. A panel always has a layout element as child and can also have card collections, other user interface elements and other panels as children.

<panel>
   <layout ... />
   ... (other children)
</panel>

A panel can also have an optional "name" attribute. With this you can group user interface elements that are defined in different sections ("player" or "common"). The second occurance of a named panel doesn't need a layout element.

<player index="0">
   ...
   <panel name="buttonPanel">
      <layout ... />
      <selectButton cardList="hand" label="Play Cards">
         <layout ... />
      </selectButton>
   </panel>
   ...
</player>
<common>
   ...
   <panel name="buttonPanel">
      <button name="pass" label="Pass">
         <layout ... />
      </button>
   </panel>
   ...
</common>
previous - next