Modifying game options

From Jocly Wiki
Jump to: navigation, search

You may want to run an existing game but with some options modified, like for instance the sound disabled by default.

You can do so like this:

<html>
    <head>
        ...
        <script>
            $(document).ready(function() {
    		var applet = $("#applet").jocly();
    		applet.jocly("defineGame","alquerque/alquerque",{
    		    view: {
    		        defaultOptions: {
                            sounds: false,
                        },
                    },
                });
                applet.jocly("localPlay","alquerque");
            });
        </script>
    <head>
    <body>
        ...
        <div id="applet"></div>
        ...
    </body>
</html>

What did we do here ?

We started with initializing the Jocly applet:

    		var applet = $("#applet").jocly();

Then we called the defineGame method:

    		applet.jocly("defineGame","alquerque/alquerque",{
    		    view: {
    		        defaultOptions: {
                            sounds: false,
                        },
                    },
                });

Finally, we launched a game of modified alquerque against the computer:

                applet.jocly("localPlay","alquerque");

The defineGame method was called with 2 parameters:

  • alquerque/alquerque: we want to start from the alquerque (the first one) configuration, the resulting game name will be alquerque (second one),
  • a modifier: this javascript object modifies the original object (the game configuration) the same way the jQuery extend function works (with the deep flag set). There is one difference with jQuery.extend: when an array is specified (with [...]), the entire array is replaced (with jQuery.extend, elements are appended).

Of course, if you want to modify several parameters, there is no need to call defineGame several times, just use a modifier with all the options you want.

The modifier higher field is view since we wanted to change a view related parameter (it could have been model for modifying the rules), then defaultOptions which specifies that we want to change the default view parameters, then sounds which represents the audio parameter. The available view options at this level are:

  • sounds: whether we want sounds to be heard by default
  • moves: whether we want to possible moves to be highlighted on the board
  • notation': whether we want notation on the board
  • autocomplete: whether we want to minimize the number of clicks from the player

Note that some of those options may not be available for the game being modified. For instance, autocomplete is currently only used in Chess and Checkers modules. The fact a parameter is being used (and appears in the user interface so the player can modify it) is declared at view level:

    {
        view: {
            useNotation: true,
            useShowMoves: true,
            useAutoComplete: true,
            defaultOptions: {
                ...
            }
        },
    }