<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.doc.boardgamearena.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sean-the-sheep</id>
	<title>Board Game Arena - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.doc.boardgamearena.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sean-the-sheep"/>
	<link rel="alternate" type="text/html" href="https://en.doc.boardgamearena.com/Special:Contributions/Sean-the-sheep"/>
	<updated>2026-09-18T08:10:32Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.0</generator>
	<entry>
		<id>https://en.doc.boardgamearena.com/index.php?title=BGA_Type_Safe_Template:_Reversi&amp;diff=24271</id>
		<title>BGA Type Safe Template: Reversi</title>
		<link rel="alternate" type="text/html" href="https://en.doc.boardgamearena.com/index.php?title=BGA_Type_Safe_Template:_Reversi&amp;diff=24271"/>
		<updated>2025-03-03T13:51:49Z</updated>

		<summary type="html">&lt;p&gt;Sean-the-sheep: /* Step 9 - Player Actions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Important note: BGA Type Safe Template is a project created by a developer that is not part of the BGA team. This is currently not officially supported by BGA.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;This page was generated from [https://github.com/NevinAF/bga-ts-template/blob/main/docs/tutorials/reversi.md BGA TS Template: Reversi.md]. It is recommended to view this document from the source file. All links will redirect to source documentation, and images will only be included via link.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;ts-template-reversi-tutorial&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= TS Template: Reversi Tutorial =&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NevinAF/bga-ts-template?tab=readme-ov-file#board-game-arena-type-safe-template &#039;&#039;BGA Type Safe Template&#039;&#039;]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Project Files:&#039;&#039;&#039; [[BGA-TS-Template-Reversi]]&lt;br /&gt;
&lt;br /&gt;
This page is a beginner friendly tutorial which mimics the official [https://en.doc.boardgamearena.com/Tutorial_reversi Tutorial Reversi] page, but is more approachable and uses the BGA Type Safe Template. If you are not a new Board Game Arena developer, you may want to follow along with the [https://github.com/NevinAF/bga-ts-template?tab=readme-ov-file#getting-started BGA Type Safe Template: Getting Started] guide for a more concise overview of the template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;All steps are recorded as a git branch so you can track the exact changes that were made, and view the project files from any point in the tutorial.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/reversi_steps_branches.gif&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;steps-0-1-2-3&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Steps 0, 1, 2, 3 ==&lt;br /&gt;
&lt;br /&gt;
Go to [https://github.com/NevinAF/bga-ts-template/blob/main/docs/tutorials/index.md TS Template: Tutorial] for the first four steps of the tutorial. The first four steps for any project are the same. To avoid redundancy, they are not repeated here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-3.5---from-here-on-out&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 3.5 - From here on out ==&lt;br /&gt;
&lt;br /&gt;
At this point, your are ready to start developing your game. You can choose to follow any of the tutorials or guides on the [https://en.doc.boardgamearena.com/Main_Page BGA Studio Documentation] to continue.&lt;br /&gt;
&lt;br /&gt;
From this point in the tutorial and on:&lt;br /&gt;
&lt;br /&gt;
* It will be assumed that files are built and synced before running the game in the BGA studio.&lt;br /&gt;
* It will be assumed that you do a hard refresh of the client page after making changes.&lt;br /&gt;
* It will be assumed that you are using all of the default settings for initializing the typescript template. Adjustments can be made as needed and all sections have links to original file documentation for reference.&lt;br /&gt;
* Most steps will have specific instructions, but you can always adjust at your own discretion.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Make sure the following game information is set for Reversi:&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;json&amp;quot;&amp;gt;// gameinfos.jsonc&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;players&amp;quot;: [ 2 ],&lt;br /&gt;
    &amp;quot;bgg_id&amp;quot;: 2389,&lt;br /&gt;
    &amp;quot;player_colors&amp;quot;: [ &amp;quot;cbcbcb&amp;quot;, &amp;quot;363636&amp;quot; ],&lt;br /&gt;
    &amp;quot;favorite_colors_support&amp;quot;: false&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-4---game-board&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 4 - Game Board ==&lt;br /&gt;
&lt;br /&gt;
# Download the following board image for Reversi, and place it in the &amp;lt;code&amp;gt;img&amp;lt;/code&amp;gt; folder of your project. It should be named &amp;lt;code&amp;gt;board.jpg&amp;lt;/code&amp;gt; to match the SCSS background-image in the next step.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/assets/board.jpg&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;ol start=&amp;quot;2&amp;quot; style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the board to the DOM by adding the following Smarty template code to the &amp;lt;code&amp;gt;yourgamename_yourgamename.tpl&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;html&amp;quot;&amp;gt;&amp;lt;div id=&amp;quot;board&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;!-- BEGIN square --&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;square_{X}_{Y}&amp;quot; class=&amp;quot;square&amp;quot; style=&amp;quot;left: {LEFT}px; top: {TOP}px;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;!-- END square --&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;The &amp;lt;code&amp;gt;&amp;amp;lt;!-- BEGIN &amp;amp;lt;block&amp;amp;gt; --&amp;amp;gt;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;&amp;amp;lt;!-- END &amp;amp;lt;block&amp;amp;gt; --&amp;amp;gt;&amp;lt;/code&amp;gt; are Smarty template tags that let you programmatically generate HTML as you will use in a following step. See [https://en.doc.boardgamearena.com/Game_layout:_view_and_template:_yourgamename.view.php_and_yourgamename_yourgamename.tpl X_X.tpl] for more information about the template file for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following SCSS to the &amp;lt;code&amp;gt;yourgamename.scss&amp;lt;/code&amp;gt; file to style the board:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;scss&amp;quot;&amp;gt;#board {&lt;br /&gt;
    // Setup Image&lt;br /&gt;
    width: 536px;&lt;br /&gt;
    height: 528px;&lt;br /&gt;
    background-image: url(&#039;img/board.jpg&#039;);&lt;br /&gt;
    // Center the board&lt;br /&gt;
    margin: auto;&lt;br /&gt;
    left: 0; top: 0; right: 0; bottom: 0;&lt;br /&gt;
    // Tokens and squares are absolutely placed relative to the board&lt;br /&gt;
    position: relative;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
.square {&lt;br /&gt;
    // Size of a square&lt;br /&gt;
    width: 62px;&lt;br /&gt;
    height: 62px;&lt;br /&gt;
    // Position relative to the board, not affected by other elements.&lt;br /&gt;
    position: absolute;&lt;br /&gt;
    // Placeholder for validating the positions.&lt;br /&gt;
    background-color: red;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;See [https://en.doc.boardgamearena.com/Game_interface_stylesheet:_yourgamename.css X.css] for more information about the style sheet for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step4-3.png&amp;lt;br&amp;gt; &amp;lt;i&amp;gt;Reloading the game (with a hard refresh).&amp;lt;/i&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following code to the &amp;lt;code&amp;gt;yourgamename.view.php&amp;lt;/code&amp;gt; file.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;// function build_page( $viewArgs ) {&lt;br /&gt;
// ...&lt;br /&gt;
/*********** Place your code below:  ************/&lt;br /&gt;
&lt;br /&gt;
// States that we should start inserting at the &#039;square&#039; block&lt;br /&gt;
// &amp;lt;yourgamename&amp;gt;_&amp;lt;yourgamename&amp;gt; should be replaced with your game name: tstemplatereversi_tstemplatereversi&lt;br /&gt;
$this-&amp;gt;page-&amp;gt;begin_block( &amp;quot;&amp;lt;yourgamename&amp;gt;_&amp;lt;yourgamename&amp;gt;&amp;quot;, &amp;quot;square&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
$hor_scale = 64.8; // Constant for square width&lt;br /&gt;
$ver_scale = 64.4; // Constant for square height&lt;br /&gt;
for( $x=1; $x&amp;lt;=8; $x++ ) // Loop the 8 columns..&lt;br /&gt;
{&lt;br /&gt;
    for( $y=1; $y&amp;lt;=8; $y++ ) // Loop the 8 rows..&lt;br /&gt;
    {&lt;br /&gt;
        // Inserts the code found at the square block based on the variables.&lt;br /&gt;
        $this-&amp;gt;page-&amp;gt;insert_block( &amp;quot;square&amp;quot;, array(&lt;br /&gt;
            &#039;X&#039; =&amp;gt; $x,&lt;br /&gt;
            &#039;Y&#039; =&amp;gt; $y,&lt;br /&gt;
            &#039;LEFT&#039; =&amp;gt; round( ($x-1)*$hor_scale+10 ),&lt;br /&gt;
            &#039;TOP&#039; =&amp;gt; round( ($y-1)*$ver_scale+7 )&lt;br /&gt;
        ) );&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;See [https://en.doc.boardgamearena.com/Game_layout:_view_and_template:_yourgamename.view.php_and_yourgamename_yourgamename.tpl X.view.php] for more information about the view file for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step4-4.png&amp;lt;br&amp;gt; &amp;lt;i&amp;gt;Reloading the game (with a hard refresh).&amp;lt;/i&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-5---the-tokens&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 5 - The Tokens ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following token image to the &amp;lt;code&amp;gt;img&amp;lt;/code&amp;gt; folder of your project. It should be named &amp;lt;code&amp;gt;tokens.png&amp;lt;/code&amp;gt; to match the SCSS background-image in the next step.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/assets/tokens.png&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add a template variable to the &amp;lt;code&amp;gt;yourgamename_yourgamename.tpl&amp;lt;/code&amp;gt; file to represent the tokens:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;html&amp;quot;&amp;gt;&amp;lt;div id=&amp;quot;board&amp;quot;&amp;gt; ... &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var jstpl_token=&#039;&amp;lt;div class=&amp;quot;token tokencolor_${color}&amp;quot; id=&amp;quot;token_${x_y}&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&#039;;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;Note that this does not have any information that needs to be populated by the server, so this could be directly defined in the TypeScript file.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following SCSS to the &amp;lt;code&amp;gt;yourgamename.scss&amp;lt;/code&amp;gt; file to style the tokens:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;scss&amp;quot;&amp;gt;// Same as square, but matching the token size/image.&lt;br /&gt;
.token {&lt;br /&gt;
    width: 56px;&lt;br /&gt;
    height: 56px;&lt;br /&gt;
    position: absolute;&lt;br /&gt;
    background-image: url(&#039;img/tokens.png&#039;);&lt;br /&gt;
}&lt;br /&gt;
// For clarity, but doesn&#039;t actually do anything.&lt;br /&gt;
.tokencolor_cbcbcb { background-position: 0px 0px; }&lt;br /&gt;
// Translate the background the exact size of the white token, so the black one shows instead.&lt;br /&gt;
.tokencolor_363636 { background-position: -56px 0px; }&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add a TypeScript function to the &amp;lt;code&amp;gt;yourgamename.ts&amp;lt;/code&amp;gt; file to place the tokens on the board. You can hover over any function/property in the TypeScript file to see the documentation and usage.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;///////////////////////////////////////////////////&lt;br /&gt;
//// Utility methods&lt;br /&gt;
&lt;br /&gt;
/** Adds a token matching the given player to the board at the specified location. */&lt;br /&gt;
addTokenOnBoard( x: number, y: number, player_id: number )&lt;br /&gt;
{&lt;br /&gt;
    let player = this.gamedatas.players[ player_id ];&lt;br /&gt;
    if (!player)&lt;br /&gt;
        throw new Error( &#039;Unknown player id: &#039; + player_id );&lt;br /&gt;
&lt;br /&gt;
    dojo.place( this.format_block( &#039;jstpl_token&#039;, {&lt;br /&gt;
        x_y: `${x}_${y}`,&lt;br /&gt;
        color: player.color&lt;br /&gt;
    } ) , &#039;board&#039; );&lt;br /&gt;
&lt;br /&gt;
    this.placeOnObject( `token_${x}_${y}`, `overall_player_board_${player_id}` );&lt;br /&gt;
    this.slideToObject( `token_${x}_${y}`, `square_${x}_${y}` ).play();&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;See [https://en.doc.boardgamearena.com/Game_interface_logic:_yourgamename.js X.js] for more information about the script file for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;For a placeholder, test your tokens by manually placing some in the setup function. you can also remove the boilerplate code.:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;///////////////////////////////////////////////////&lt;br /&gt;
//// Game setup&lt;br /&gt;
&lt;br /&gt;
setup(gamedatas: Gamedatas): void&lt;br /&gt;
{&lt;br /&gt;
    console.log( &amp;quot;Starting game setup&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
    this.addTokenOnBoard( 2, 2, this.player_id );&lt;br /&gt;
    this.addTokenOnBoard( 6, 3, this.player_id );&lt;br /&gt;
&lt;br /&gt;
    this.setupNotifications(); // &amp;lt;-- Keep this line&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step5-4.png&amp;lt;br&amp;gt; &amp;lt;i&amp;gt;Reloading the game (with a hard refresh).&amp;lt;/i&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-6---the-database-and-initial-setup&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 6 - The Database and Initial Setup ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Create a database for your board in the &amp;lt;code&amp;gt;dbmodel.sql&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;sql&amp;quot;&amp;gt;CREATE TABLE IF NOT EXISTS `board` (&lt;br /&gt;
    `board_x` smallint(5) unsigned NOT NULL,&lt;br /&gt;
    `board_y` smallint(5) unsigned NOT NULL,&lt;br /&gt;
    `board_player` int(10) unsigned DEFAULT NULL,&lt;br /&gt;
    PRIMARY KEY (`board_x`,`board_y`)&lt;br /&gt;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Pay special attention to the backtick &amp;lt;code&amp;gt;`&amp;lt;/code&amp;gt; character vs. the single quote &amp;lt;code&amp;gt;&#039;&amp;lt;/code&amp;gt; when working with SQL. You can quit and restart your game to see if the file is correct.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;See [https://en.doc.boardgamearena.com/Game_database_model:_dbmodel.sql dbmodel.sql] for more information about the database file for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Setup the board in your &amp;lt;code&amp;gt;yourgamename.game.php&amp;lt;/code&amp;gt; file. This is initializing the data in the database and setting the four discs in the center of the board:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;protected function setupNewGame( $players, $options = array() )&lt;br /&gt;
{&lt;br /&gt;
    //...&lt;br /&gt;
    $sql .= implode( &#039;,&#039;, $values );&lt;br /&gt;
    self::DbQuery( $sql );&lt;br /&gt;
    // self::reattributeColorsBasedOnPreferences( $players, $gameinfos[&#039;player_colors&#039;] );&lt;br /&gt;
    self::reloadPlayersBasicInfos();&lt;br /&gt;
&lt;br /&gt;
    /************ Start the game initialization *****/&lt;br /&gt;
    $sql = &amp;quot;INSERT INTO board (board_x,board_y,board_player) VALUES &amp;quot;;&lt;br /&gt;
    $sql_values = array();&lt;br /&gt;
    list( $whiteplayer_id, $blackplayer_id ) = array_keys( $players );&lt;br /&gt;
    for( $x=1; $x&amp;lt;=8; $x++ )&lt;br /&gt;
    {&lt;br /&gt;
        for( $y=1; $y&amp;lt;=8; $y++ )&lt;br /&gt;
        {&lt;br /&gt;
            // Initial positions of white player&lt;br /&gt;
            if( ($x==4 &amp;amp;&amp;amp; $y==4) || ($x==5 &amp;amp;&amp;amp; $y==5) )&lt;br /&gt;
                $token_value = &amp;quot;&#039;$whiteplayer_id&#039;&amp;quot;;&lt;br /&gt;
            // Initial positions of black player&lt;br /&gt;
            else if( ($x==4 &amp;amp;&amp;amp; $y==5) || ($x==5 &amp;amp;&amp;amp; $y==4) )&lt;br /&gt;
                $token_value = &amp;quot;&#039;$blackplayer_id&#039;&amp;quot;;&lt;br /&gt;
            // Not a starting position&lt;br /&gt;
            else&lt;br /&gt;
                $token_value = &amp;quot;NULL&amp;quot;;&lt;br /&gt;
            $sql_values[] = &amp;quot;(&#039;$x&#039;,&#039;$y&#039;,$token_value)&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    $sql .= implode( &#039;,&#039;, $sql_values );&lt;br /&gt;
    self::DbQuery( $sql );&lt;br /&gt;
    //...&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Now we need to send the board information to the client whenever the page is reloaded. In your &amp;lt;code&amp;gt;yourgamename.game.php&amp;lt;/code&amp;gt; file, add the following code to the &amp;lt;code&amp;gt;getAllDatas&amp;lt;/code&amp;gt; function:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;protected function getAllDatas()&lt;br /&gt;
{&lt;br /&gt;
    //...&lt;br /&gt;
    $sql = &amp;quot;SELECT board_x x, board_y y, board_player player&lt;br /&gt;
            FROM board WHERE board_player IS NOT NULL&amp;quot;;&lt;br /&gt;
    $result[&#039;board&#039;] = self::getObjectListFromDB( $sql );&lt;br /&gt;
    //...&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the board data to the &amp;lt;code&amp;gt;Gamedatas&amp;lt;/code&amp;gt; interface in the &amp;lt;code&amp;gt;yourgamename.d.ts&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;interface Gamedatas {&lt;br /&gt;
    board: { x: number, y: number, player: number }[];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following to the &amp;lt;code&amp;gt;yourgamename.ts&amp;lt;/code&amp;gt; file place the discs at the start of a page load:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;setup(gamedatas: Gamedatas): void&lt;br /&gt;
{&lt;br /&gt;
    console.log( &amp;quot;Starting game setup&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
    // Place the tokens on the board&lt;br /&gt;
    for( let i in gamedatas.board )&lt;br /&gt;
    {&lt;br /&gt;
        let square = gamedatas.board[i];&lt;br /&gt;
&lt;br /&gt;
        if( square?.player ) // If square is defined and has a player&lt;br /&gt;
            this.addTokenOnBoard( square.x, square.y, square.player );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    this.setupNotifications(); // &amp;lt;-- Keep this line&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step6-5.png&amp;lt;br&amp;gt; &amp;lt;i&amp;gt;Reloading the game (with a hard refresh).&amp;lt;/i&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-7---game-states&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 7 - Game States ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Replace the gamestates.jsonc file with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;json&amp;quot;&amp;gt;{&lt;br /&gt;
    &amp;quot;$schema&amp;quot;: &amp;quot;../../node_modules/bga-ts-template/schema/gamestates.schema.json&amp;quot;,&lt;br /&gt;
&lt;br /&gt;
    // The initial state. Please do not modify.&lt;br /&gt;
    &amp;quot;1&amp;quot;: {&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;gameSetup&amp;quot;,&lt;br /&gt;
        &amp;quot;description&amp;quot;: &amp;quot;&amp;quot;,&lt;br /&gt;
        &amp;quot;type&amp;quot;: &amp;quot;manager&amp;quot;,&lt;br /&gt;
        &amp;quot;action&amp;quot;: &amp;quot;stGameSetup&amp;quot;,&lt;br /&gt;
        &amp;quot;transitions&amp;quot;: { &amp;quot;&amp;quot;: 10 }&lt;br /&gt;
    },&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;10&amp;quot;: {&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;playerTurn&amp;quot;,&lt;br /&gt;
        &amp;quot;description&amp;quot;: &amp;quot;${actplayer} must play a disc&amp;quot;,&lt;br /&gt;
        &amp;quot;descriptionmyturn&amp;quot;: &amp;quot;${you} must play a disc&amp;quot;,&lt;br /&gt;
        &amp;quot;type&amp;quot;: &amp;quot;activeplayer&amp;quot;,&lt;br /&gt;
        &amp;quot;args&amp;quot;: &amp;quot;argPlayerTurn&amp;quot;,&lt;br /&gt;
        &amp;quot;argsType&amp;quot;: {&lt;br /&gt;
            &amp;quot;possibleMoves&amp;quot;: &amp;quot;boolean[][]&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;possibleactions&amp;quot;: {&lt;br /&gt;
            &amp;quot;playDisc&amp;quot;: [&lt;br /&gt;
                { &amp;quot;name&amp;quot;: &amp;quot;x&amp;quot;, &amp;quot;type&amp;quot;: &amp;quot;AT_int&amp;quot; },&lt;br /&gt;
                { &amp;quot;name&amp;quot;: &amp;quot;y&amp;quot;, &amp;quot;type&amp;quot;: &amp;quot;AT_int&amp;quot; }&lt;br /&gt;
            ]&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;transitions&amp;quot;: {&lt;br /&gt;
            &amp;quot;playDisc&amp;quot;: 11,&lt;br /&gt;
            &amp;quot;zombiePass&amp;quot;: 11&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;11&amp;quot;: {&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;nextPlayer&amp;quot;,&lt;br /&gt;
        &amp;quot;type&amp;quot;: &amp;quot;game&amp;quot;,&lt;br /&gt;
        &amp;quot;action&amp;quot;: &amp;quot;stNextPlayer&amp;quot;,&lt;br /&gt;
        &amp;quot;updateGameProgression&amp;quot;: true,&lt;br /&gt;
        &amp;quot;transitions&amp;quot;: {&lt;br /&gt;
            &amp;quot;nextTurn&amp;quot;: 10,&lt;br /&gt;
            &amp;quot;cantPlay&amp;quot;: 11,&lt;br /&gt;
            &amp;quot;endGame&amp;quot;: 99&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
&lt;br /&gt;
    // Final state.&lt;br /&gt;
    // Please do not modify (and do not overload action/args methods}.&lt;br /&gt;
    &amp;quot;99&amp;quot;: {&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;gameEnd&amp;quot;,&lt;br /&gt;
        &amp;quot;description&amp;quot;: &amp;quot;End of game&amp;quot;,&lt;br /&gt;
        &amp;quot;type&amp;quot;: &amp;quot;manager&amp;quot;,&lt;br /&gt;
        &amp;quot;action&amp;quot;: &amp;quot;stGameEnd&amp;quot;,&lt;br /&gt;
        &amp;quot;args&amp;quot;: &amp;quot;argGameEnd&amp;quot;,&lt;br /&gt;
        &amp;quot;argsType&amp;quot;: &amp;quot;object&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;There is a lot of information in this file. Most information can be found in the hover-over tooltips, but the [https://en.doc.boardgamearena.com/Your_game_state_machine:_states.inc.php Game States] page has more information about the concepts.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;This will automatically be converted to &amp;lt;code&amp;gt;gamestates.inc.php&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;yourgamename.action.php&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;build/gamestates.d.ts&amp;lt;/code&amp;gt; when you run &amp;lt;code&amp;gt;npm run build&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add placeholder functions to remove &amp;lt;code&amp;gt;Undefined method&amp;lt;/code&amp;gt; errors. Add the following to your &amp;lt;code&amp;gt;yourgamename.action.php&amp;lt;/code&amp;gt; file. Note that this replaces the large section of comments in the file describing the player actions, state arguments, and state actions.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;// Player actions&lt;br /&gt;
function playDisc( int $x, int $y ) {&lt;br /&gt;
    /* TODO */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Game state arguments&lt;br /&gt;
function argPlayerTurn() {&lt;br /&gt;
    /* TODO */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Game state actions&lt;br /&gt;
function stNextPlayer() {&lt;br /&gt;
    /* TODO */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Fix the &amp;lt;code&amp;gt;dummmy&amp;lt;/code&amp;gt; state errors in your &amp;lt;code&amp;gt;yourgamename.ts&amp;lt;/code&amp;gt; file. These are added as a placeholder and now cause issues because there is no gamestate with the name &amp;lt;code&amp;gt;dummmy&amp;lt;/code&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;onEnteringState(stateName: GameStateName, args: CurrentStateArgs): void&lt;br /&gt;
{&lt;br /&gt;
    console.log( &#039;Entering state: &#039; + stateName );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
onLeavingState(stateName: GameStateName): void&lt;br /&gt;
{&lt;br /&gt;
    console.log( &#039;Leaving state: &#039; + stateName );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
onUpdateActionButtons(stateName: GameStateName, args: AnyGameStateArgs | null): void&lt;br /&gt;
{&lt;br /&gt;
    console.log( &#039;onUpdateActionButtons: &#039; + stateName, args );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;You should no longer have any type issues in any of your files.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
When you reload your game (this will cause current games to fail), your should see the banner change to &amp;lt;code&amp;gt;{actplayer} must play a disc&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;You must play a disc&amp;lt;/code&amp;gt;. This will be the only visible change in the game at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step7-3.png&amp;lt;br&amp;gt; &amp;lt;i&amp;gt;Reloading the game (with a hard refresh).&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-8---game-rules-and-possible-moves&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 8 - Game Rules and Possible Moves ==&lt;br /&gt;
&lt;br /&gt;
There are a couple of variations on the rules for Reversi, but the most common is with &amp;lt;code&amp;gt;outflanking rules&amp;lt;/code&amp;gt;. You can see the official rules at [https://www.worldothello.org/about/about-othello/othello-rules/official-rules/ World Othello]. There rules have been implemented with the following utility functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Copy these functions into your &amp;lt;code&amp;gt;yourgamename.game.php&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;// Get the complete board with a double associative array&lt;br /&gt;
function getBoard()&lt;br /&gt;
{&lt;br /&gt;
    $sql = &amp;quot;SELECT board_x x, board_y y, board_player player FROM board&amp;quot;;&lt;br /&gt;
    return self::getDoubleKeyCollectionFromDB( $sql, true );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Get the list of possible moves (x =&amp;gt; y =&amp;gt; true)&lt;br /&gt;
function getPossibleMoves( $player_id )&lt;br /&gt;
{&lt;br /&gt;
    $result = array();&lt;br /&gt;
&lt;br /&gt;
    $board = self::getBoard();&lt;br /&gt;
&lt;br /&gt;
    for( $x=1; $x&amp;lt;=8; $x++ )&lt;br /&gt;
    {&lt;br /&gt;
        for( $y=1; $y&amp;lt;=8; $y++ )&lt;br /&gt;
        {&lt;br /&gt;
            $returned = self::getTurnedOverDiscs( $x, $y, $player_id, $board );&lt;br /&gt;
            if( count( $returned ) == 0 )&lt;br /&gt;
            {&lt;br /&gt;
                // No discs returned =&amp;gt; not a possible move&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // Okay =&amp;gt; set this coordinate to &amp;quot;true&amp;quot;&lt;br /&gt;
                if( ! isset( $result[$x] ) )&lt;br /&gt;
                    $result[$x] = array();&lt;br /&gt;
&lt;br /&gt;
                $result[$x][$y] = true;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $result;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Get the list of returned disc when &amp;quot;player&amp;quot; we play at this place (&amp;quot;x&amp;quot;, &amp;quot;y&amp;quot;),&lt;br /&gt;
//  or a void array if no disc is returned (invalid move)&lt;br /&gt;
function getTurnedOverDiscs( $x, $y, $player, $board )&lt;br /&gt;
{&lt;br /&gt;
    $turnedOverDiscs = array();&lt;br /&gt;
&lt;br /&gt;
    if( $board[ $x ][ $y ] === null ) // If there is already a disc on this place, this can&#039;t be a valid move&lt;br /&gt;
    {&lt;br /&gt;
        // For each directions...&lt;br /&gt;
        $directions = array(&lt;br /&gt;
            array( -1,-1 ), array( -1,0 ), array( -1, 1 ), array( 0, -1),&lt;br /&gt;
            array( 0,1 ), array( 1,-1), array( 1,0 ), array( 1, 1 )&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        foreach( $directions as $direction )&lt;br /&gt;
        {&lt;br /&gt;
            // Starting from the square we want to place a disc...&lt;br /&gt;
            $current_x = $x;&lt;br /&gt;
            $current_y = $y;&lt;br /&gt;
            $bContinue = true;&lt;br /&gt;
            $mayBeTurnedOver = array();&lt;br /&gt;
&lt;br /&gt;
            while( $bContinue )&lt;br /&gt;
            {&lt;br /&gt;
                // Go to the next square in this direction&lt;br /&gt;
                $current_x += $direction[0];&lt;br /&gt;
                $current_y += $direction[1];&lt;br /&gt;
&lt;br /&gt;
                if( $current_x&amp;lt;1 || $current_x&amp;gt;8 || $current_y&amp;lt;1 || $current_y&amp;gt;8 )&lt;br /&gt;
                    $bContinue = false; // Out of the board =&amp;gt; stop here for this direction&lt;br /&gt;
                else if( $board[ $current_x ][ $current_y ] === null )&lt;br /&gt;
                    $bContinue = false; // An empty square =&amp;gt; stop here for this direction&lt;br /&gt;
                else if( $board[ $current_x ][ $current_y ] != $player )&lt;br /&gt;
                {&lt;br /&gt;
                    // There is a disc from our opponent on this square&lt;br /&gt;
                    // =&amp;gt; add it to the list of the &amp;quot;may be turned over&amp;quot;, and continue on this direction&lt;br /&gt;
                    $mayBeTurnedOver[] = array( &#039;x&#039; =&amp;gt; $current_x, &#039;y&#039; =&amp;gt; $current_y );&lt;br /&gt;
                }&lt;br /&gt;
                else if( $board[ $current_x ][ $current_y ] == $player )&lt;br /&gt;
                {&lt;br /&gt;
                    // This is one of our disc&lt;br /&gt;
&lt;br /&gt;
                    if( count( $mayBeTurnedOver ) == 0 )&lt;br /&gt;
                    {&lt;br /&gt;
                        // There is no disc to be turned over between our 2 discs =&amp;gt; stop here for this direction&lt;br /&gt;
                        $bContinue = false;&lt;br /&gt;
                    }&lt;br /&gt;
                    else&lt;br /&gt;
                    {&lt;br /&gt;
                        // We found some disc to be turned over between our 2 discs&lt;br /&gt;
                        // =&amp;gt; add them to the result and stop here for this direction&lt;br /&gt;
                        $turnedOverDiscs = array_merge( $turnedOverDiscs, $mayBeTurnedOver );&lt;br /&gt;
                        $bContinue = false;&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $turnedOverDiscs;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the list of possible moves to the &amp;lt;code&amp;gt;argPlayerTurn&amp;lt;/code&amp;gt; function in your &amp;lt;code&amp;gt;yourgamename.action.php&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;function argPlayerTurn()&lt;br /&gt;
{&lt;br /&gt;
    $possibleMoves = self::getPossibleMoves( self::getActivePlayerId() );&lt;br /&gt;
    return array(&lt;br /&gt;
        &#039;possibleMoves&#039; =&amp;gt; $possibleMoves&lt;br /&gt;
    );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add a css class to represent the possible moves in your &amp;lt;code&amp;gt;yourgamename.scss&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;scss&amp;quot;&amp;gt;.possibleMove {&lt;br /&gt;
    background-color: rgba(255, 255, 255, 0.15);&lt;br /&gt;
    cursor: pointer;&lt;br /&gt;
    transition: 100ms;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
.possibleMove:hover {&lt;br /&gt;
    background-color: rgba(255, 255, 255, 0.3);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the possible moves to the board when entering a new state by adding the following to your &amp;lt;code&amp;gt;yourgamename.ts&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;// Utility methods&lt;br /&gt;
&lt;br /&gt;
/** Removes the &#039;possibleMove&#039; class from all elements. */&lt;br /&gt;
clearPossibleMoves() {&lt;br /&gt;
    document.querySelectorAll(&#039;.possibleMove&#039;).forEach(element =&amp;gt; {&lt;br /&gt;
        element.classList.remove(&#039;possibleMove&#039;);&lt;br /&gt;
    });&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/** Updates the squares on the board matching the possible moves. */&lt;br /&gt;
updatePossibleMoves( possibleMoves: boolean[][] )&lt;br /&gt;
{&lt;br /&gt;
    this.clearPossibleMoves();&lt;br /&gt;
&lt;br /&gt;
    for( var x in possibleMoves )&lt;br /&gt;
    {&lt;br /&gt;
        for( var y in possibleMoves[ x ] )&lt;br /&gt;
        {&lt;br /&gt;
            let square = $(`square_${x}_${y}`);&lt;br /&gt;
            if( !square )&lt;br /&gt;
                throw new Error( `Unknown square element: ${x}_${y}. Make sure the board grid was set up correctly in the tpl file.` );&lt;br /&gt;
            square.classList.add(&#039;possibleMove&#039;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    this.addTooltipToClass( &#039;possibleMove&#039;, &#039;&#039;, _(&#039;Place a disc here&#039;) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Game &amp;amp; client states&lt;br /&gt;
&lt;br /&gt;
onEnteringState(stateName: GameStateName, args: CurrentStateArgs): void&lt;br /&gt;
{&lt;br /&gt;
    console.log( &#039;Entering state: &#039;+stateName );&lt;br /&gt;
&lt;br /&gt;
    switch( stateName )&lt;br /&gt;
    {&lt;br /&gt;
        case &#039;playerTurn&#039;:&lt;br /&gt;
            this.updatePossibleMoves( args.args!.possibleMoves );&lt;br /&gt;
            break;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step8-4.png&amp;lt;br&amp;gt; &amp;lt;i&amp;gt;Reloading the game (with a hard refresh).&amp;lt;/i&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The game is still not playable, but you can now see the possible moves for the first turn.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-9---player-actions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 9 - Player Actions ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Fill in the &amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; function in your &amp;lt;code&amp;gt;yourgamename.action.php&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;function playDisc( int $x, int $y )&lt;br /&gt;
{&lt;br /&gt;
    // Check that this player is active and that this action is possible at this moment&lt;br /&gt;
    self::checkAction( &#039;playDisc&#039; );&lt;br /&gt;
&lt;br /&gt;
    // Now, check if this is a possible move&lt;br /&gt;
    $board = self::getBoard();&lt;br /&gt;
    $player_id = self::getActivePlayerId();&lt;br /&gt;
    $turnedOverDiscs = self::getTurnedOverDiscs( $x, $y, $player_id, $board );&lt;br /&gt;
&lt;br /&gt;
    if( count( $turnedOverDiscs ) &amp;lt;= 0 )&lt;br /&gt;
        throw new BgaSystemException( &amp;quot;Impossible move&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
    // This move is possible!&lt;br /&gt;
&lt;br /&gt;
    // Let&#039;s place a disc at x,y and return all &amp;quot;$returned&amp;quot; discs to the active player&lt;br /&gt;
    $sql = &amp;quot;UPDATE board SET board_player=&#039;$player_id&#039;&lt;br /&gt;
            WHERE ( board_x, board_y) IN ( &amp;quot;;&lt;br /&gt;
    foreach( $turnedOverDiscs as $turnedOver )&lt;br /&gt;
        $sql .= &amp;quot;(&#039;&amp;quot;.$turnedOver[&#039;x&#039;].&amp;quot;&#039;,&#039;&amp;quot;.$turnedOver[&#039;y&#039;].&amp;quot;&#039;),&amp;quot;;&lt;br /&gt;
    $sql .= &amp;quot;(&#039;$x&#039;,&#039;$y&#039;) ) &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    self::DbQuery( $sql );&lt;br /&gt;
&lt;br /&gt;
    // Update scores according to the number of disc on board&lt;br /&gt;
    $sql = &amp;quot;UPDATE player&lt;br /&gt;
            SET player_score = (&lt;br /&gt;
            SELECT COUNT( board_x ) FROM board WHERE board_player=player_id&lt;br /&gt;
            )&amp;quot;;&lt;br /&gt;
    self::DbQuery( $sql );&lt;br /&gt;
&lt;br /&gt;
    // Then, go to the next state&lt;br /&gt;
    $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;playDisc&#039; );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Fill in the &amp;lt;code&amp;gt;stNextPlayer&amp;lt;/code&amp;gt; function in your &amp;lt;code&amp;gt;yourgamename.action.php&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;function stNextPlayer()&lt;br /&gt;
{&lt;br /&gt;
    // Active next player&lt;br /&gt;
    $player_id = self::activeNextPlayer();&lt;br /&gt;
&lt;br /&gt;
    // Check if both player has at least 1 discs, and if there are free squares to play&lt;br /&gt;
    $sql = &amp;quot;SELECT board_player, COUNT( board_x ) FROM board&lt;br /&gt;
            GROUP BY board_player&amp;quot;;&lt;br /&gt;
    $player_to_discs = self::getCollectionFromDb( $sql, true );&lt;br /&gt;
&lt;br /&gt;
    if( ! isset( $player_to_discs[ null ] ) )&lt;br /&gt;
    {&lt;br /&gt;
        // Index 0 has not been set =&amp;gt; there&#039;s no more free place on the board !&lt;br /&gt;
        // =&amp;gt; end of the game&lt;br /&gt;
        $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;endGame&#039; );&lt;br /&gt;
        return ;&lt;br /&gt;
    }&lt;br /&gt;
    else if( ! isset( $player_to_discs[ $player_id ] ) )&lt;br /&gt;
    {&lt;br /&gt;
        // Active player has no more disc on the board =&amp;gt; he looses immediately&lt;br /&gt;
        $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;endGame&#039; );&lt;br /&gt;
        return ;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Can this player play?&lt;br /&gt;
&lt;br /&gt;
    $possibleMoves = self::getPossibleMoves( $player_id );&lt;br /&gt;
    if( count( $possibleMoves ) == 0 )&lt;br /&gt;
    {&lt;br /&gt;
        // This player can&#039;t play&lt;br /&gt;
        // Can his opponent play ?&lt;br /&gt;
        $opponent_id = self::getUniqueValueFromDb( &amp;quot;SELECT player_id FROM player WHERE player_id!=&#039;$player_id&#039; &amp;quot; );&lt;br /&gt;
        if( count( self::getPossibleMoves( $opponent_id ) ) == 0 )&lt;br /&gt;
        {&lt;br /&gt;
            // Nobody can move =&amp;gt; end of the game&lt;br /&gt;
            $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;endGame&#039; );&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {            &lt;br /&gt;
            // =&amp;gt; pass his turn&lt;br /&gt;
            $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;cantPlay&#039; );&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    else&lt;br /&gt;
    {&lt;br /&gt;
        // This player can play. Give him some extra time&lt;br /&gt;
        self::giveExtraTime( $player_id );&lt;br /&gt;
        $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;nextTurn&#039; );&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Connect an &amp;lt;code&amp;gt;onclick&amp;lt;/code&amp;gt; function to all of the squares on the board:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;setup(gamedatas: Gamedatas): void { // … dojo.query( ‘.square’ ).connect( ‘onclick’, this, ‘onPlayDisc’ ); // … }&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the &amp;lt;code&amp;gt;onPlayDisc&amp;lt;/code&amp;gt; function to your &amp;lt;code&amp;gt;yourgamename.ts&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;// Player&#039;s action&lt;br /&gt;
&lt;br /&gt;
/** Called when a square is clicked, check if it is a possible move and send the action to the server. */&lt;br /&gt;
onPlayDisc( evt: Event )&lt;br /&gt;
{&lt;br /&gt;
    // Stop this event propagation&lt;br /&gt;
    evt.preventDefault();&lt;br /&gt;
&lt;br /&gt;
    if (!(evt.currentTarget instanceof HTMLElement))&lt;br /&gt;
        throw new Error(&#039;evt.currentTarget is null! Make sure that this function is being connected to a DOM HTMLElement.&#039;);&lt;br /&gt;
&lt;br /&gt;
    // Check if this is a possible move&lt;br /&gt;
    if( !evt.currentTarget.classList.contains(&#039;possibleMove&#039;) )&lt;br /&gt;
        return;&lt;br /&gt;
&lt;br /&gt;
    // Check that this action is possible at this moment (shows error dialog if not possible)&lt;br /&gt;
    if( !this.checkAction( &#039;playDisc&#039; ) )&lt;br /&gt;
        return;&lt;br /&gt;
&lt;br /&gt;
    // Get the clicked square x and y&lt;br /&gt;
    // Note: square id format is &amp;quot;square_X_Y&amp;quot;&lt;br /&gt;
    let [_square_, x, y] = evt.currentTarget.id.split(&#039;_&#039;);&lt;br /&gt;
&lt;br /&gt;
    this.ajaxcall( `/${this.game_name}/${this.game_name}/playDisc.html`, {&lt;br /&gt;
        x, y, lock: true&lt;br /&gt;
    }, this, function() {} );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you should be able to play the game! However, you will need to reload the game after every move to force the board to update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;step-10---notifications&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Step 10 - Notifications ==&lt;br /&gt;
&lt;br /&gt;
Notifications are used to inform players when something happens in the game. In our case, we only need to send one notification stating when the player has played a disc; however, it is often much more useful to send the notifications as a sequence of events so they can be automatically sequenced in the client. In this case, we will send three notifications:=, all of which can be sent from the player action function: - When a player plays a disc - When discs are converted because of a move - When the scores&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following to the &amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; function in your &amp;lt;code&amp;gt;yourgamename.action.php&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;function playDisc( int $x, int $y )&lt;br /&gt;
{&lt;br /&gt;
    // ...&lt;br /&gt;
&lt;br /&gt;
    // Notify&lt;br /&gt;
    self::notifyAllPlayers( &amp;quot;playDisc&amp;quot;, clienttranslate( &#039;${player_name} plays a disc and turns over ${returned_nbr} disc(s)&#039; ), array(&lt;br /&gt;
        &#039;player_id&#039; =&amp;gt; $player_id,&lt;br /&gt;
        &#039;player_name&#039; =&amp;gt; self::getActivePlayerName(),&lt;br /&gt;
        &#039;returned_nbr&#039; =&amp;gt; count( $turnedOverDiscs ),&lt;br /&gt;
        &#039;x&#039; =&amp;gt; $x,&lt;br /&gt;
        &#039;y&#039; =&amp;gt; $y&lt;br /&gt;
    ) );&lt;br /&gt;
&lt;br /&gt;
    self::notifyAllPlayers( &amp;quot;turnOverDiscs&amp;quot;, &#039;&#039;, array(&lt;br /&gt;
        &#039;player_id&#039; =&amp;gt; $player_id,&lt;br /&gt;
        &#039;turnedOver&#039; =&amp;gt; $turnedOverDiscs&lt;br /&gt;
    ) );&lt;br /&gt;
&lt;br /&gt;
    $newScores = self::getCollectionFromDb( &amp;quot;SELECT player_id, player_score FROM player&amp;quot;, true );&lt;br /&gt;
    self::notifyAllPlayers( &amp;quot;newScores&amp;quot;, &amp;quot;&amp;quot;, array(&lt;br /&gt;
        &amp;quot;scores&amp;quot; =&amp;gt; $newScores&lt;br /&gt;
    ) );&lt;br /&gt;
&lt;br /&gt;
    // Then, go to the next state&lt;br /&gt;
    $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;playDisc&#039; );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the notifications to your &amp;lt;code&amp;gt;NotifTypes&amp;lt;/code&amp;gt; interface in the &amp;lt;code&amp;gt;yourgamename.d.ts&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;interface NotifTypes {&lt;br /&gt;
    &#039;playDisc&#039;: { x: number, y: number, player_id: number };&lt;br /&gt;
    &#039;turnOverDiscs&#039;: {&lt;br /&gt;
        player_id: number,&lt;br /&gt;
        turnedOver: { x: number, y: number }[]&lt;br /&gt;
    },&lt;br /&gt;
    &#039;newScores&#039;: { scores: Record&amp;lt;number, number&amp;gt; };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following to your &amp;lt;code&amp;gt;yourgamename.ts&amp;lt;/code&amp;gt; file to listen and handle the notifications:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;typescript&amp;quot;&amp;gt;setupNotifications()&lt;br /&gt;
{&lt;br /&gt;
    console.log( &#039;notifications subscriptions setup&#039; );&lt;br /&gt;
&lt;br /&gt;
    dojo.subscribe( &#039;playDisc&#039;, this, &amp;quot;notif_playDisc&amp;quot; );&lt;br /&gt;
    this.notifqueue.setSynchronous( &#039;playDisc&#039;, 500 );&lt;br /&gt;
    dojo.subscribe( &#039;turnOverDiscs&#039;, this, &amp;quot;notif_turnOverDiscs&amp;quot; );&lt;br /&gt;
    this.notifqueue.setSynchronous( &#039;turnOverDiscs&#039;, 1000 );&lt;br /&gt;
    dojo.subscribe( &#039;newScores&#039;, this, &amp;quot;notif_newScores&amp;quot; );&lt;br /&gt;
    this.notifqueue.setSynchronous( &#039;newScores&#039;, 500 );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
notif_playDisc( notif: NotifAs&amp;lt;&#039;playDisc&#039;&amp;gt; )&lt;br /&gt;
{&lt;br /&gt;
    this.clearPossibleMoves();&lt;br /&gt;
    this.addTokenOnBoard( notif.args.x, notif.args.y, notif.args.player_id );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
notif_turnOverDiscs( notif: NotifAs&amp;lt;&#039;turnOverDiscs&#039;&amp;gt; )&lt;br /&gt;
{&lt;br /&gt;
    // Change the color of the turned over discs&lt;br /&gt;
    for( var i in notif.args.turnedOver )&lt;br /&gt;
    {&lt;br /&gt;
        let token_data = notif.args.turnedOver[ i ]!;&lt;br /&gt;
        let token = $&amp;lt;HTMLElement&amp;gt;( `token_${token_data.x}_${token_data.y}` );&lt;br /&gt;
&lt;br /&gt;
        if (!token)&lt;br /&gt;
            throw new Error( `Unknown token element: ${token_data.x}_${token_data.y}. Make sure the board grid was set up correctly in the tpl file.` );&lt;br /&gt;
&lt;br /&gt;
        token.classList.toggle(&#039;tokencolor_cbcbcb&#039;);&lt;br /&gt;
        token.classList.toggle(&#039;tokencolor_363636&#039;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
notif_newScores( notif: NotifAs&amp;lt;&#039;newScores&#039;&amp;gt; )&lt;br /&gt;
{&lt;br /&gt;
    for( var player_id in notif.args.scores )&lt;br /&gt;
    {&lt;br /&gt;
        let counter = this.scoreCtrl[ player_id ];&lt;br /&gt;
        let newScore = notif.args.scores[ player_id ];&lt;br /&gt;
        if (counter &amp;amp;&amp;amp; newScore)&lt;br /&gt;
            counter.toValue( newScore );&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;(optional) Add a flipping animation to the token when the classes:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&#039;&#039;yourgamename_yourgamename.tpl&#039;&#039; &amp;lt;code&amp;gt;html  // Updated token template  var jstpl_token = &#039;&amp;amp;lt;div class=&amp;amp;quot;token-container tokencolor_${color}&amp;amp;quot; id=&amp;amp;quot;token_${x_y}&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;div class=&amp;amp;quot;token-flip&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;div class=&amp;amp;quot;token-white&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;/div&amp;amp;gt;&amp;amp;lt;div class=&amp;amp;quot;token-black&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;/div&amp;amp;gt;&amp;amp;lt;/div&amp;amp;gt;&amp;amp;lt;/div&amp;amp;gt;&#039;;&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&#039;&#039;yourgamename.scss&#039;&#039; ```scss .token-container { background-color: transparent; width: 56px; height: 56px; perspective: 250px; position: absolute; }&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;.token-flip { position: relative; width: 100%; height: 100%; text-align: center; transition: transform 1.2s; transform-style: preserve-3d; border-radius:50%; box-shadow: 0 4px 8px 0 rgba(0,0,0,0.25); }&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;.tokencolor_363636 &amp;amp;gt; .token-flip { transform: rotateY(180deg); }&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;.token-white, .token-black { position: absolute; width: 100%; height: 100%; -webkit-backface-visibility: hidden; backface-visibility: hidden; background-image: url(“img/tokens.png”); }&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;.token-white { background-position: 0px 0px; }&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;.token-black { background-position: -56px 0px; transform: rotateY(180deg); } ```&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step10-4.gif&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At this point, you now have a fully functional game that can be played on BGA. The following steps are to add flurishes and additional features to the game.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;optional-step-11---statistics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== (optional) Step 11 - Statistics ==&lt;br /&gt;
&lt;br /&gt;
Statistics are mostly optional and are a fun way to compare player strategies or how much they have played the game.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Replace the contents of &amp;lt;code&amp;gt;shared/stats.jsonc&amp;lt;/code&amp;gt; with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;json&amp;quot;&amp;gt;{&lt;br /&gt;
    &amp;quot;$schema&amp;quot;: &amp;quot;../../node_modules/bga-ts-template/schema/stats.schema.json&amp;quot;,&lt;br /&gt;
    // All of these stats are tracked per player&lt;br /&gt;
    &amp;quot;player&amp;quot;: {&lt;br /&gt;
        &amp;quot;discPlayedOnCorner&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 10,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Discs played on a corner&amp;quot;,&lt;br /&gt;
            &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;discPlayedOnBorder&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 11,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Discs played on a border&amp;quot;,&lt;br /&gt;
            &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;discPlayedOnCenter&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 12,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Discs played on board center part&amp;quot;,&lt;br /&gt;
            &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;turnedOver&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 13,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Number of discs turned over&amp;quot;,&lt;br /&gt;
            &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;See [https://en.doc.boardgamearena.com/Game_statistics:_stats.json stats.json] for more information about the stats file for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following to the &amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; function in your &amp;lt;code&amp;gt;yourgamename.action.php&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;function playDisc( int $x, int $y )&lt;br /&gt;
{&lt;br /&gt;
    //...&lt;br /&gt;
    // Update scores...&lt;br /&gt;
&lt;br /&gt;
    // Statistics&lt;br /&gt;
    self::incStat( count( $turnedOverDiscs ), &amp;quot;turnedOver&amp;quot;, $player_id );&lt;br /&gt;
    if( ($x==1 &amp;amp;&amp;amp; $y==1) || ($x==8 &amp;amp;&amp;amp; $y==1) || ($x==1 &amp;amp;&amp;amp; $y==8) || ($x==8 &amp;amp;&amp;amp; $y==8) )&lt;br /&gt;
        self::incStat( 1, &#039;discPlayedOnCorner&#039;, $player_id );&lt;br /&gt;
    else if( $x==1 || $x==8 || $y==1 || $y==8 )&lt;br /&gt;
        self::incStat( 1, &#039;discPlayedOnBorder&#039;, $player_id );&lt;br /&gt;
    else if( $x&amp;gt;=3 &amp;amp;&amp;amp; $x&amp;lt;=6 &amp;amp;&amp;amp; $y&amp;gt;=3 &amp;amp;&amp;amp; $y&amp;lt;=6 )&lt;br /&gt;
        self::incStat( 1, &#039;discPlayedOnCenter&#039;, $player_id );&lt;br /&gt;
&lt;br /&gt;
    // Notify...&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Reload the statistics configuration on the manage game page. See [https://github.com/NevinAF/bga-ts-template/blob/main/docs/tutorials/index.md#step-3---game-information--metadata Step 3.3] for more information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/game_configuration.png&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Play a game and verify that your new statistics are visible at the end of the game:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step11-3.png&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;optional-step-12---game-options&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== (Optional) Step 12 - Game Options ==&lt;br /&gt;
&lt;br /&gt;
Game options are a way for users to change game behavior or rules. As an example, we will be adding another rule variant called “Reversi Bombs” that flips all 8 adjacent discs when a disc is played.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/reversi_bombs_rules.png&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Replace your gameoptions with the following in the &amp;lt;code&amp;gt;shared/gameoptions.jsonc&amp;lt;/code&amp;gt; file:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;json&amp;quot;&amp;gt;{&lt;br /&gt;
    &amp;quot;$schema&amp;quot;: &amp;quot;../../node_modules/bga-ts-template/schema/gameoptions.schema.json&amp;quot;,&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;100&amp;quot;: {&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;Variant&amp;quot;,&lt;br /&gt;
        &amp;quot;values&amp;quot;: {&lt;br /&gt;
            // This is the current rule set we have implemented&lt;br /&gt;
            &amp;quot;1&amp;quot;: {&lt;br /&gt;
                &amp;quot;name&amp;quot;: &amp;quot;Standard&amp;quot;,&lt;br /&gt;
                &amp;quot;description&amp;quot;: &amp;quot;Flip all outflanked discs.&amp;quot;&lt;br /&gt;
            },&lt;br /&gt;
            &amp;quot;2&amp;quot;: {&lt;br /&gt;
                &amp;quot;name&amp;quot;: &amp;quot;Reversi Bombs&amp;quot;,&lt;br /&gt;
                &amp;quot;description&amp;quot;: &amp;quot;Flip all 8 adjacent discs.&amp;quot;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;See [https://en.doc.boardgamearena.com/Options_and_preferences:_gameoptions.json,_gamepreferences.json gameoptions.json] for more information about the options file for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Modify the &amp;lt;code&amp;gt;getTurnedOverDiscs&amp;lt;/code&amp;gt; function in your &amp;lt;code&amp;gt;yourgamename.game.php&amp;lt;/code&amp;gt; file to handle the new game option:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;function getTurnedOverDiscs( $x, $y, $player, $board )&lt;br /&gt;
{&lt;br /&gt;
    //...&lt;br /&gt;
&lt;br /&gt;
    foreach( $directions as $direction )&lt;br /&gt;
    {&lt;br /&gt;
        // If game option &#039;100&#039; is set to 2 (reversi bombs)&lt;br /&gt;
        if ($this-&amp;gt;gamestate-&amp;gt;table_globals[100] == 2)&lt;br /&gt;
        {&lt;br /&gt;
            $current_x = $x + $direction[0];&lt;br /&gt;
            $current_y = $y + $direction[1];&lt;br /&gt;
            if( $current_x&amp;lt;1 || $current_x&amp;gt;8 || $current_y&amp;lt;1 || $current_y&amp;gt;8 )&lt;br /&gt;
                continue; // Out of the board =&amp;gt; stop here for this direction&lt;br /&gt;
&lt;br /&gt;
            if ($board[ $current_x ][ $current_y ] !== null)&lt;br /&gt;
                // push the disc to be turned over&lt;br /&gt;
                $turnedOverDiscs[] = array( &#039;x&#039; =&amp;gt; $current_x, &#039;y&#039; =&amp;gt; $current_y );&lt;br /&gt;
&lt;br /&gt;
            continue; // Don&#039;t do standard game logic&lt;br /&gt;
        }&lt;br /&gt;
        // Standard...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    //...&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Fix the &amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; function to flip all turned disc instead of setting them to the active player:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;php&amp;quot;&amp;gt;// Let&#039;s place a disc at x,y and return all &amp;quot;$returned&amp;quot; discs to the active player&lt;br /&gt;
$other_id = self::getUniqueValueFromDb( &amp;quot;SELECT player_id FROM player WHERE player_id!=&#039;$player_id&#039;&amp;quot; );&lt;br /&gt;
$sql = &amp;quot;UPDATE board SET board_player = CASE WHEN board_player = &#039;$player_id&#039; THEN &#039;$other_id&#039; ELSE &#039;$player_id&#039; END WHERE (board_x, board_y) IN (&amp;quot;;&lt;br /&gt;
foreach( $turnedOverDiscs as $turnedOver )&lt;br /&gt;
    //...&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Reload the game options from within the game manager page. See [https://github.com/NevinAF/bga-ts-template/blob/main/docs/tutorials/index.md#step-3---game-information--metadata Step 3.3] for more information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/game_configuration.png&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step12-4.gif&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;user-preferences&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== User Preferences ==&lt;br /&gt;
&lt;br /&gt;
User preferences is something cosmetic about the game interface which you want users to be able to controll. Reversi has a classic athstetic, but in this example, we will add a user preference to change the color of the board. This is not a great use case for user preferences, but it is a simple example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Replace the contents of &amp;lt;code&amp;gt;shared/gamepreferences.jsonc&amp;lt;/code&amp;gt; with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;json&amp;quot;&amp;gt;{&lt;br /&gt;
    &amp;quot;$schema&amp;quot;: &amp;quot;../../node_modules/bga-ts-template/schema/gamepreferences.schema.json&amp;quot;,&lt;br /&gt;
    &amp;quot;101&amp;quot;: {&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;Game Style&amp;quot;,&lt;br /&gt;
        &amp;quot;needReload&amp;quot;: true,&lt;br /&gt;
        &amp;quot;values&amp;quot;: {&lt;br /&gt;
            &amp;quot;1&amp;quot;: {&lt;br /&gt;
                &amp;quot;name&amp;quot;: &amp;quot;Classic&amp;quot;,&lt;br /&gt;
                &amp;quot;cssPref&amp;quot;: &amp;quot;game_style_classic&amp;quot;&lt;br /&gt;
            },&lt;br /&gt;
            &amp;quot;2&amp;quot;: {&lt;br /&gt;
                &amp;quot;name&amp;quot;: &amp;quot;Wooden&amp;quot;,&lt;br /&gt;
                // Then this is added to the page, we will override the image urls in the css.&lt;br /&gt;
                &amp;quot;cssPref&amp;quot;: &amp;quot;game_style_wooden&amp;quot;&lt;br /&gt;
            }&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;default&amp;quot;: 1&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;p&amp;gt;See [https://en.doc.boardgamearena.com/Options_and_preferences:_gameoptions.json,_gamepreferences.json gamepreferences.json] for more information about the options file for BGA games.&amp;lt;/p&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Download the following images and add them to your &amp;lt;code&amp;gt;img&amp;lt;/code&amp;gt; folder. They should be named ‘wooden_board.jpg’ and ‘wooden_tokens.png’.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;table align=&amp;quot;center&amp;quot; width=&amp;quot;50%&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/assets/wooden_board.jpg&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/assets/wooden_tokens.png&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Add the following to your &amp;lt;code&amp;gt;yourgamename.scss&amp;lt;/code&amp;gt; file to change the board and token images:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;scss&amp;quot;&amp;gt;// This overrides the #board + .token- images when the .game_style_wooden is automatically added to the page (by the user preference).&lt;br /&gt;
.game_style_wooden {&lt;br /&gt;
    #board {&lt;br /&gt;
        background-image: url(&#039;img/wooden_board.jpg&#039;);&lt;br /&gt;
    }&lt;br /&gt;
    .token-white, .token-black {&lt;br /&gt;
        background-image: url(&#039;img/wooden_tokens.png&#039;);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Refresh the game options in the game manager page. See [https://github.com/NevinAF/bga-ts-template/blob/main/docs/tutorials/index.md#step-3---game-information--metadata Step 3.3] for more information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/game_configuration.png&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Reload the game and change the user preference to see the new board and token images:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;https://github.com/NevinAF/bga-ts-template/tree/main/docs/tutorials/img/reversi/step13-5.gif&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sean-the-sheep</name></author>
	</entry>
	<entry>
		<id>https://en.doc.boardgamearena.com/index.php?title=Tutorial_reversi&amp;diff=24270</id>
		<title>Tutorial reversi</title>
		<link rel="alternate" type="text/html" href="https://en.doc.boardgamearena.com/index.php?title=Tutorial_reversi&amp;diff=24270"/>
		<updated>2025-03-03T13:47:47Z</updated>

		<summary type="html">&lt;p&gt;Sean-the-sheep: Remove reference to dojo method because the tutorial now uses `document.querySelectorAll` instead.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;If you want to use TypeScript, you can &#039;&#039;&#039;check out an updated version&#039;&#039;&#039; of the [[BGA Type Safe Template: Reversi|Reversi Tutorial]] which uses the [[BGA Type Safe Template]]. &#039;&#039;&#039;Important note: BGA Type Safe Template is a project created by a developer that is not part of the BGA team. This is currently not officially supported by BGA.&#039;&#039;&#039; {{Studio_Framework_Navigation}}&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Using this tutorial, you can build a complete working game on the BGA environment: Reversi.&lt;br /&gt;
&lt;br /&gt;
Before you read this tutorial, you must:&lt;br /&gt;
* Read the overall presentations of the BGA Framework ([[Studio|see here]]).&lt;br /&gt;
* Know the [http://en.wikipedia.org/wiki/Reversi#Rules rules of Reversi].&lt;br /&gt;
* Know the languages used on BGA: PHP, SQL, HTML, CSS, Javascript&lt;br /&gt;
* &#039;&#039;&#039;Setup your development environment&#039;&#039;&#039; [http://en.doc.boardgamearena.com/First_steps_with_BGA_Studio First Steps with BGA Studio]&lt;br /&gt;
&lt;br /&gt;
== Create your first game ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Note: you should already have created a project following instructions in [[First_steps_with_BGA_Studio#Create_a_new_game_project|Create a new game project]]. While you will find a &#039;&#039;&#039;reversi&#039;&#039;&#039; directory in your SFTP folder, do not use it for this tutorial. Instead, use the project you have created as an (empty) starting point.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With the initial skeleton of code provided in your project, you can already start a game from the BGA Studio:&lt;br /&gt;
* Go to your [https://studio.boardgamearena.com/controlpanel studio Control panel], then Manage games and select your initial project. &#039;&#039;Note: there are warnings displayed about a missing BGG_ID and presentation text. You can ignore that for now.&#039;&#039;&lt;br /&gt;
* Click the Play link next to your project name. This will open the Play page and offer to create a new table for your project. &#039;&#039;Optional: click the Heart icon to add your project to your favorite games list.&#039;&#039;&lt;br /&gt;
* On the Play page, on the top of the page, make sure that your settings are &amp;quot;Simple game&amp;quot;, &amp;quot;Real time&amp;quot; and &amp;quot;Manual&amp;quot;.&lt;br /&gt;
* Click &amp;quot;Create table&amp;quot; to create a table of your project. &lt;br /&gt;
* For now, we are going to work with one player only, so use the (-) button to set the number of players to 1. Most of the time it is simpler to proceed with only one player during the early phase of development of your game, as it&#039;s easy and fast to start/stop games. If you choose to start with 2 players, you should see two names on the right: testdude0 and testdude1. To switch between them, press the red arrow button near their names; it will open another tab. This way you don&#039;t need to login and logout from multiple accounts.) &lt;br /&gt;
* Reminder: Always use the &amp;quot;Express Start&amp;quot; button to start the game.&lt;br /&gt;
&lt;br /&gt;
Thus, you can start a &amp;quot;Reversi&amp;quot; game, and arrive on a void, empty game. Yeah.&lt;br /&gt;
&lt;br /&gt;
End the game by clicking on the game options icon on the top right, and then on &amp;quot;Express Stop&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Editing the game information (Optional) ==&lt;br /&gt;
&lt;br /&gt;
This step is optional and will fix the warnings on the project page (missing BGG_ID and presentation).&lt;br /&gt;
&lt;br /&gt;
==== Edits to fix Errors ====&lt;br /&gt;
* Edit your local copy of the &amp;lt;code&amp;gt;gameinfos.inc.php&amp;lt;/code&amp;gt; file:&lt;br /&gt;
** Change the &amp;lt;code&amp;gt;bgg_id&amp;lt;/code&amp;gt; value from &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;2389&amp;lt;/code&amp;gt; - that&#039;s around line 26.&lt;br /&gt;
** Add &amp;lt;code&amp;gt;1,&amp;lt;/code&amp;gt; to the players array (so you can start 1-player games while testing) - that&#039;s around line 29.&lt;br /&gt;
* Upload the &amp;lt;code&amp;gt;gameinfos.inc.php&amp;lt;/code&amp;gt; file to the SFTP server (see [[First_steps_with_BGA_Studio#Connect_to_your_SFTP_folder|Connect to your SFTP folder]]).&lt;br /&gt;
&lt;br /&gt;
==== Test your Edits ====&lt;br /&gt;
* Go back to your project page, and in the &amp;quot;Game Information&amp;quot; section, click &amp;quot;Reload game informations&amp;quot;.&lt;br /&gt;
* Finally, refresh the project page in your browser (usually CTRL-F5).&lt;br /&gt;
&lt;br /&gt;
===== Not working? =====&lt;br /&gt;
Some changes will require bypassing the cache. It is often worth doing a hard refresh to make sure the&lt;br /&gt;
&lt;br /&gt;
Sometimes the cache will keep your changes from showing. Since this is a possibility, it will be useful to know how to bypass the cache. To do so you may manually clear cache or use a shortcut to refresh and ignore the cached version of the page. Here&#039;s how&lt;br /&gt;
&lt;br /&gt;
====== Windows ======&lt;br /&gt;
Chrome, Firefox, or Edge: Press &amp;lt;code&amp;gt;Ctrl+F5&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Shift+F5&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Ctrl+Shift+R&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====== Mac ======&lt;br /&gt;
Chrome or Firefox: Press &amp;lt;code&amp;gt;Shift+Command+R&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Safari for Mac: Press &amp;lt;code&amp;gt;Command+Option+E&amp;lt;/code&amp;gt; to empty the cache, then hold down Shift and click Reload in the toolbar&lt;br /&gt;
&lt;br /&gt;
== Make it look like Reversi ==&lt;br /&gt;
&lt;br /&gt;
Let&#039;s start with the board. This will give you a good idea of how things will look and where tokens should go.&lt;br /&gt;
&lt;br /&gt;
Be careful designing the layout of your game: you must always keep in mind that players with a 1024px screen width must be able to play. Usually, it means that the width of the play area can be 750px (in the worst case).&lt;br /&gt;
&lt;br /&gt;
For Reversi, it&#039;s useless to have a 750x750px board - much too big, so we chose this one which fit perfectly (536x528):&lt;br /&gt;
&lt;br /&gt;
[[File:Board.jpg]]&lt;br /&gt;
&lt;br /&gt;
Note that we are using a jpg file. Jpg files are lighter than png, so they are faster to load. Later, we are going to use PNGs for tokens because they allow for transparency.&lt;br /&gt;
&lt;br /&gt;
==== Add the board ====&lt;br /&gt;
use lowercase file names&lt;br /&gt;
* upload &amp;lt;code&amp;gt;board.jpg&amp;lt;/code&amp;gt; in your &amp;lt;code&amp;gt;img/&amp;lt;/code&amp;gt; directory. &lt;br /&gt;
* edit &amp;lt;code&amp;gt;reversi.js&amp;lt;/code&amp;gt; to add the &amp;lt;code&amp;gt;div&amp;lt;/code&amp;gt; for your board at the beginning of the setup function.&lt;br /&gt;
&lt;br /&gt;
Note: If you are building this game by following the tutorial, you will have a different project name than &amp;lt;code&amp;gt;reversi&amp;lt;/code&amp;gt; (i.e. &amp;lt;code&amp;gt;mygame.js&amp;lt;/code&amp;gt;). The file names in your project will be different than shown in this tutorial, replacing &amp;lt;code&amp;gt;reversi&amp;lt;/code&amp;gt; with your project name. Be sure that any code (other than comments) that references &amp;lt;code&amp;gt;reversi&amp;lt;/code&amp;gt; is changed to your actual project name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
this.getGameAreaElement().insertAdjacentHTML(&#039;beforeend&#039;, `&lt;br /&gt;
  &amp;lt;div id=&amp;quot;board&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
`);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* edit your &amp;lt;code&amp;gt;reversi.css&amp;lt;/code&amp;gt; file to transform it into a visible board:&lt;br /&gt;
&lt;br /&gt;
 #board {&lt;br /&gt;
    width: 536px;&lt;br /&gt;
    height: 528px;&lt;br /&gt;
    background-image: url(&#039;img/board.jpg&#039;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important: refresh your page.&#039;&#039;&#039; Here&#039;s your board:[[File:reversi1.jpg]]&lt;br /&gt;
&lt;br /&gt;
If the board does not appear, refresh the page (always do this when you update the CSS file), and check the image filename. Remember file names are case sensitive!&lt;br /&gt;
&lt;br /&gt;
==== Code the Grid ====&lt;br /&gt;
Now, we need to create some invisible HTML elements where squares are. These elements will be used as position references for the white and black tokens. &lt;br /&gt;
&lt;br /&gt;
===== Build the grid of squares =====&lt;br /&gt;
The board is 8 squares by 8 squares. This means we need 64 squares. To avoid writing 64 individual &amp;lt;code&amp;gt;div&amp;lt;/code&amp;gt; elements on our template, we are going to generate the squares on JS &amp;lt;code&amp;gt;setup&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We&#039;ll do this in our Javascript &amp;lt;code&amp;gt;setup&amp;lt;/code&amp;gt; method under &amp;lt;code&amp;gt;// TODO: Set up your game interface here, according to &amp;quot;gamedatas&amp;quot;&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const board = document.getElementById(&#039;board&#039;);&lt;br /&gt;
const hor_scale = 64.8;&lt;br /&gt;
const ver_scale = 64.4;&lt;br /&gt;
for (let x=1; x&amp;lt;=8; x++) {&lt;br /&gt;
    for (let y=1; y&amp;lt;=8; y++) {&lt;br /&gt;
        const left = Math.round((x - 1) * hor_scale + 10);&lt;br /&gt;
        const top = Math.round((y - 1) * ver_scale + 7);&lt;br /&gt;
        // we use afterbegin to make sure squares are placed before discs&lt;br /&gt;
        board.insertAdjacentHTML(`afterbegin`, `&amp;lt;div id=&amp;quot;square_${x}_${y}&amp;quot; class=&amp;quot;square&amp;quot; style=&amp;quot;left: ${left}px; top: ${top}px;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;`);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: as you can see, squares in our &amp;lt;code&amp;gt;board.jpg&amp;lt;/code&amp;gt; files do not have an exact width/height in pixels, and that&#039;s the reason we are using floating point numbers here.&lt;br /&gt;
&lt;br /&gt;
===== Style Those Squares =====&lt;br /&gt;
Now, to finish our work and check if everything works fine, we are going to style our square a little bit in our CSS stylesheet:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#board {&lt;br /&gt;
    width: 536px;&lt;br /&gt;
    height: 528px;&lt;br /&gt;
    background-image: url(&#039;img/board.jpg&#039;);&lt;br /&gt;
    position: relative;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
.square {&lt;br /&gt;
    width: 62px;&lt;br /&gt;
    height: 62px;&lt;br /&gt;
    position: absolute;&lt;br /&gt;
    background-color: red;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Explanations:&lt;br /&gt;
* With &amp;quot;&amp;lt;code&amp;gt;position: relative&amp;lt;/code&amp;gt;&amp;quot; on board, we ensure square elements are positioned relatively to board.&lt;br /&gt;
* &amp;lt;code&amp;gt;background-color: red;&amp;lt;/code&amp;gt; is used for testing. This allows us to see the invisible elements. (You could instead do something like &amp;lt;code&amp;gt;outline: 2px solid orange;&amp;lt;/code&amp;gt; have fun and be creative)&lt;br /&gt;
&lt;br /&gt;
Let&#039;s refresh and check our (beautiful) squares:&lt;br /&gt;
&lt;br /&gt;
[[File:reversi2.jpg]]&lt;br /&gt;
&lt;br /&gt;
Now that you know the squares are there, you can remove the test line &amp;lt;code&amp;gt;background-color: red;&amp;lt;/code&amp;gt;  from your &amp;lt;code&amp;gt;.square&amp;lt;/code&amp;gt; class in the CSS stylesheet.&lt;br /&gt;
&lt;br /&gt;
===== Not Working? =====&lt;br /&gt;
If the styled squares do not appear, inspect and check your css (Chrome DevTools: Application &amp;gt; Frames &amp;gt; top &amp;gt; Stylesheets &amp;gt; reversi.css). &lt;br /&gt;
&lt;br /&gt;
== The Tokens ==&lt;br /&gt;
&lt;br /&gt;
Now, our board is ready for some tokens!&lt;br /&gt;
&lt;br /&gt;
[Note: Throughout this tutorial, sometimes &amp;quot;tokens&amp;quot; is used, and sometimes &amp;quot;discs&amp;quot; is used. They are often swapped if you&#039;re looking at code in the reversi example project.]&lt;br /&gt;
&lt;br /&gt;
=== Build the Token ===&lt;br /&gt;
There are quite a few steps before the tokens will appear. You may be used to testing after every change, but that won&#039;t work well here. The token will &#039;&#039;&#039;not&#039;&#039;&#039; show until you have add styles to the css, js template to the tpl, utility method in the js,  adjusted the php file, and added the token to the board in the js file.&lt;br /&gt;
&lt;br /&gt;
==== HTML in the .js file ====&lt;br /&gt;
At first, we introduce a new &#039;div&#039; element as a child of &amp;quot;board&amp;quot; to host all these tokens (in the template we started at the beginning of the setup function of the JS file):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;board&amp;quot;&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    &amp;lt;div id=&amp;quot;discs&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: &amp;lt;code&amp;gt;discs&amp;lt;/code&amp;gt; is plural. This div will be used to hold the token divs. Shortly, we will use javascript to add individual tokens to the board.&lt;br /&gt;
&lt;br /&gt;
==== Add Token to img directory ====&lt;br /&gt;
Here&#039;s a new piece of art with the tokens. We need transparency here so we are using a png file:&lt;br /&gt;
&lt;br /&gt;
[[File:tokens.png]]&lt;br /&gt;
&lt;br /&gt;
Upload this image file &amp;lt;code&amp;gt;tokens.png&amp;lt;/code&amp;gt; in your &amp;lt;code&amp;gt;img/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
Important Fun Fact: we are using ONE file for both tokens. It is really important to use a minimum number of graphic files for your game. This is called the &amp;quot;CSS sprite&amp;quot; technique, because it makes the game load faster and more reliable. [http://www.w3schools.com/css/css_image_sprites.asp Read more about CSS sprites].&lt;br /&gt;
&lt;br /&gt;
==== Style the Tokens in .css file ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
.disc {&lt;br /&gt;
    width: 56px;&lt;br /&gt;
    height: 56px;&lt;br /&gt;
    position: absolute;&lt;br /&gt;
    background-image: url(&#039;img/tokens.png&#039;);&lt;br /&gt;
    background-size: auto 100%;&lt;br /&gt;
}&lt;br /&gt;
.disc[data-color=&amp;quot;ffffff&amp;quot;] { background-position-x: 0%; }&lt;br /&gt;
.disc[data-color=&amp;quot;000000&amp;quot;] { background-position-x: 100%; }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this CSS code, we can set and change the token color by changing the &amp;lt;code&amp;gt;data-color&amp;lt;/code&amp;gt; attribute. Using data instead of a class ensures it can be only one of them (the disc cannot be black and white at the same time).&lt;br /&gt;
&lt;br /&gt;
Note the &amp;quot;&amp;lt;code&amp;gt;position: absolute&amp;lt;/code&amp;gt;&amp;quot; which allows us to position tokens on the board and make them &amp;quot;slide&amp;quot; to their positions.&lt;br /&gt;
&lt;br /&gt;
==== Add Token Utility Method in .js file ====&lt;br /&gt;
Now, let&#039;s make the first token appear on our board. Tokens are not visible at the beginning of the game: they appear dynamically during the game. For this reason, we are going to make them appear from our Javascript code, using a template string&lt;br /&gt;
&lt;br /&gt;
Let&#039;s create a method in our Javascript code (in the &amp;lt;code&amp;gt;reversi.js&amp;lt;/code&amp;gt; file) that will make a token appear on the board, using this template. Add under the section &amp;lt;code&amp;gt;//// Utility methods&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
addDiscOnBoard: async function( x, y, player )&lt;br /&gt;
        {&lt;br /&gt;
            const color = this.gamedatas.players[ player ].color;&lt;br /&gt;
            &lt;br /&gt;
            document.getElementById(&#039;discs&#039;).insertAdjacentHTML(&#039;beforeend&#039;, `&amp;lt;div class=&amp;quot;disc&amp;quot; data-color=&amp;quot;${color}&amp;quot; id=&amp;quot;disc_${x}${y}&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;`);&lt;br /&gt;
            &lt;br /&gt;
            this.placeOnObject( `disc_${x}${y}`, &#039;overall_player_board_&#039;+player );&lt;br /&gt;
            &lt;br /&gt;
            const anim = this.slideToObject( `disc_${x}${y}`, &#039;square_&#039;+x+&#039;_&#039;+y );&lt;br /&gt;
            await this.bgaPlayDojoAnimation(anim);&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Utility Method Explanation =====&lt;br /&gt;
* with &amp;lt;code&amp;gt;element.insertAdjacentHTML&amp;lt;/code&amp;gt; method, we create a HTML piece of code and insert it as a new child of &amp;lt;code&amp;gt;tokens&amp;lt;/code&amp;gt; div element.&lt;br /&gt;
* with &amp;lt;code&amp;gt;this.placeOnObject&amp;lt;/code&amp;gt; BGA method, we place this element over the panel of some player. &lt;br /&gt;
* Immediately after, using &amp;lt;code&amp;gt;this.slideToObject&amp;lt;/code&amp;gt; BGA method, we make the token slide to the &amp;lt;code&amp;gt;square&amp;lt;/code&amp;gt; element, its final destination.&lt;br /&gt;
* &amp;lt;code&amp;gt;&#039;overall_player_board_&#039;+player&amp;lt;/code&amp;gt; refers to the div element that contains each player&#039;s information and avatar. By initially placing the token here, it gives the effect that the player&#039;s avatar is throwing the token onto the board.&lt;br /&gt;
&lt;br /&gt;
Note: don&#039;t forget to call the &amp;lt;code&amp;gt;bgaPlayDojoAnimation()&amp;lt;/code&amp;gt;, otherwise the token will remain at its original location.&lt;br /&gt;
&lt;br /&gt;
Note: during this process, the parent of the new token HTML element will stay &amp;lt;code&amp;gt;tokens&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;placeOnObject&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;slideToObject&amp;lt;/code&amp;gt; methods are &#039;&#039;only&#039;&#039; moving the position of elements on screen, and they are &#039;&#039;not&#039;&#039; modifying the HTML tree.&lt;br /&gt;
&lt;br /&gt;
==== Set Token Colors in setupNewGame in modules/php/Game.php file ====&lt;br /&gt;
Before we can show a token, we need to set the player colors in the &amp;lt;code&amp;gt;setupNewGame&amp;lt;/code&amp;gt; function in  &amp;lt;code&amp;gt;modules/php/Game.php&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Replace &amp;lt;code&amp;gt;$default_colors = $gameinfos[&#039;player_colors&#039;];&amp;lt;/code&amp;gt; with the following line:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$default_colors = array( &amp;quot;ffffff&amp;quot;, &amp;quot;000000&amp;quot; );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: A few lines below, you may have to remove the line &amp;lt;code&amp;gt;this-&amp;gt;reattributeColorsBasedOnPreferences( $players, $gameinfos[&#039;player_colors&#039;] );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test the Token ===&lt;br /&gt;
Now, to test if everything works fine&lt;br /&gt;
&lt;br /&gt;
==== &amp;lt;code&amp;gt;addTokenOnBoard()&amp;lt;/code&amp;gt; in .js file to Test ====&lt;br /&gt;
In &amp;lt;code&amp;gt;reversi.js&amp;lt;/code&amp;gt;, in &amp;lt;code&amp;gt;setup: function&amp;lt;/code&amp;gt;, under the code we added to generate the squares. &lt;br /&gt;
 this.addDiscOnBoard( 2, 2, this.player_id );&lt;br /&gt;
Now restart the game.&lt;br /&gt;
&lt;br /&gt;
A token should appear and slide immediately to its position, like this:&lt;br /&gt;
&lt;br /&gt;
[[File:reversi3.jpg]]&lt;br /&gt;
&lt;br /&gt;
== The database ==&lt;br /&gt;
&lt;br /&gt;
We did most of the client-side programming, so let&#039;s have a look on the other side now. To design the database model of our game, you will need to access the database. You won&#039;t need to do anything in database UI, yet.&lt;br /&gt;
&lt;br /&gt;
=== Accessing the Database ===&lt;br /&gt;
To access the database, start a game, then click &amp;quot;Go to game database&amp;quot; link at the bottom of our game, to access the database directly with a PhpMyAdmin instance.&lt;br /&gt;
&lt;br /&gt;
After the first time you&#039;ve access the database, you could skip opening a game and instead, go to https://studio.boardgamearena.com/db/ . Your PhpMyAdmin username/password is in your welcome email.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: do not remove existing tables&lt;br /&gt;
&lt;br /&gt;
=== Create Table in .sql file ===&lt;br /&gt;
Now, you are able to create the table(s) you need for your game, and report every SQL command used in your &amp;lt;code&amp;gt;dbmodel.sql&amp;lt;/code&amp;gt; file. &lt;br /&gt;
&lt;br /&gt;
The database model of Reversi is short: just one table with the squares of the board. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CREATE TABLE IF NOT EXISTS `board` (&lt;br /&gt;
  `board_x` smallint(5) unsigned NOT NULL,&lt;br /&gt;
  `board_y` smallint(5) unsigned NOT NULL,&lt;br /&gt;
  `board_player` int(10) unsigned DEFAULT NULL,&lt;br /&gt;
  PRIMARY KEY (`board_x`,`board_y`)&lt;br /&gt;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add the above SQL to &amp;lt;code&amp;gt;dbmodel.sql&amp;lt;/code&amp;gt;. Pay special attention to the backtick &amp;lt;code&amp;gt;`&amp;lt;/code&amp;gt; character vs. the single quote &amp;lt;code&amp;gt;&#039;&amp;lt;/code&amp;gt; when working with SQL. &lt;br /&gt;
&lt;br /&gt;
=== Test the Table ===&lt;br /&gt;
Now, a new database with a &amp;lt;code&amp;gt;board&amp;lt;/code&amp;gt; table will be created each time we start a Reversi game. This is why after modifying our &amp;lt;code&amp;gt;dbmodel.sql&amp;lt;/code&amp;gt; it&#039;s a good time to stop your current game &amp;amp; start a new game.&lt;br /&gt;
&lt;br /&gt;
Start a new game and verify a table is created.&lt;br /&gt;
&lt;br /&gt;
== Setup the initial game position ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: From now on, you must launch the game with &#039;&#039;&#039;two players&#039;&#039;&#039; to get two &amp;lt;code&amp;gt;player_id&amp;lt;/code&amp;gt;s within the database. Otherwise, the game will crash.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;setupNewGame&amp;lt;/code&amp;gt; method of our &amp;lt;code&amp;gt;Game.php&amp;lt;/code&amp;gt; is called during initial setup. This initializes our data and places the starting tokens on the board. At the beginning of the game, there should be 4 tokens on the board.&lt;br /&gt;
&lt;br /&gt;
=== Initialize the Board in modules/php/Game.php file ===&lt;br /&gt;
Under &amp;lt;code&amp;gt;// TODO: setup the initial game situation here&amp;lt;/code&amp;gt;, initialize the board&amp;lt;pre&amp;gt;&lt;br /&gt;
// Init the board&lt;br /&gt;
$sql = &amp;quot;INSERT INTO board (board_x,board_y,board_player) VALUES &amp;quot;;&lt;br /&gt;
$sql_values = array();&lt;br /&gt;
list( $blackplayer_id, $whiteplayer_id ) = array_keys( $players );&lt;br /&gt;
for( $x=1; $x&amp;lt;=8; $x++ )&lt;br /&gt;
{&lt;br /&gt;
	for( $y=1; $y&amp;lt;=8; $y++ )&lt;br /&gt;
	{&lt;br /&gt;
	$token_value = &amp;quot;NULL&amp;quot;;&lt;br /&gt;
	if( ($x==4 &amp;amp;&amp;amp; $y==4) || ($x==5 &amp;amp;&amp;amp; $y==5) )  // Initial positions of white player&lt;br /&gt;
	$token_value = &amp;quot;&#039;$whiteplayer_id&#039;&amp;quot;;&lt;br /&gt;
	else if( ($x==4 &amp;amp;&amp;amp; $y==5) || ($x==5 &amp;amp;&amp;amp; $y==4) )  // Initial positions of black player&lt;br /&gt;
	$token_value = &amp;quot;&#039;$blackplayer_id&#039;&amp;quot;;&lt;br /&gt;
                    &lt;br /&gt;
	$sql_values[] = &amp;quot;(&#039;$x&#039;,&#039;$y&#039;,$token_value)&amp;quot;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
$sql .= implode( &#039;,&#039;, $sql_values );&lt;br /&gt;
$this-&amp;gt;DbQuery( $sql );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Board Initialization Explanation ====&lt;br /&gt;
&lt;br /&gt;
* We create one table entry for each square, with a &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; value which means &amp;quot;empty square&amp;quot;&lt;br /&gt;
* On 4 of the squares, we place an initial token.&lt;br /&gt;
&lt;br /&gt;
After this, we set &amp;lt;code&amp;gt;activeNextPlayer&amp;lt;/code&amp;gt; to make the first player active at the beginning of the game (this line is already present in the default code template).&lt;br /&gt;
&lt;br /&gt;
If you didn&#039;t do it earlier, you need to remove the call to &amp;lt;code&amp;gt;this-&amp;gt;reattributeColorsBasedOnPreferences()&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;SetupNewGame()&amp;lt;/code&amp;gt;. If you don&#039;t, player color preferences will try (and fail) to override the two colors supported here.&lt;br /&gt;
&lt;br /&gt;
=== Show the Initial Token Setup ===&lt;br /&gt;
Now, we need to make these tokens appear on the client side. The first step is to return the token positions with our &amp;lt;code&amp;gt;getAllDatas()&amp;lt;/code&amp;gt; PHP method. &amp;lt;code&amp;gt;getAllDatas()&amp;lt;/code&amp;gt; is called during each page reload.&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;code&amp;gt;getAllDatas()&amp;lt;/code&amp;gt; method, after &amp;lt;code&amp;gt;// TODO: Gather all information about current game situation (visible by player $current_player_id)&amp;lt;/code&amp;gt;, add the following lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Get reversi board token&lt;br /&gt;
$result[&#039;board&#039;] = self::getObjectListFromDB( &amp;quot;SELECT board_x x, board_y y, board_player player&lt;br /&gt;
	FROM board&lt;br /&gt;
	WHERE board_player IS NOT NULL&amp;quot; );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, you may need to modify the query that gets player information to also get the player&#039;s colors. This is in the variable &amp;lt;code&amp;gt;$sql&amp;lt;/code&amp;gt;, above the lines you just inserted in &amp;lt;code&amp;gt;getAllDatas()&amp;lt;/code&amp;gt;. Below is what the line will look like. Notice how we&#039;ve added &amp;lt;code&amp;gt;player_color color&amp;lt;/code&amp;gt; to the sql query.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$sql = &amp;quot;SELECT player_id id, player_score score, player_color color FROM player &amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We are using the BGA framework&#039;s &amp;lt;code&amp;gt;getObjectListFromDB()&amp;lt;/code&amp;gt; that formats the result of this SQL query in a PHP array with x, y and player attributes. We add it to the result associative array with the key &amp;lt;code&amp;gt;board&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Last, we process this array client side. Let&#039;s place a token on the board for each array item. We&#039;ll do this in our Javascript &amp;lt;code&amp;gt;setup&amp;lt;/code&amp;gt; method under the code we added to generate the squares. &lt;br /&gt;
&lt;br /&gt;
This will result in a removal or edit of the previously added line &amp;lt;code&amp;gt;this.addDiscOnBoard(2, 2, this.player_id);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for( var i in gamedatas.board )&lt;br /&gt;
{&lt;br /&gt;
    var square = gamedatas.board[i];&lt;br /&gt;
    &lt;br /&gt;
    if( square.player !== null )&lt;br /&gt;
    {&lt;br /&gt;
        this.addDiscOnBoard( square.x, square.y, square.player );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;board&amp;lt;/code&amp;gt; entry created in &amp;lt;code&amp;gt;getAllDatas()&amp;lt;/code&amp;gt; is used here as &amp;lt;code&amp;gt;gamedatas.board&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test the Game Start ===&lt;br /&gt;
Reload... and here we are:&lt;br /&gt;
&lt;br /&gt;
[[File:reversi5.jpg]]&lt;br /&gt;
&lt;br /&gt;
It starts to feel like Reversi here...&lt;br /&gt;
&lt;br /&gt;
== The game state machine ==&lt;br /&gt;
&lt;br /&gt;
Stop your game, again. You&#039;re about to start the core game logic.&lt;br /&gt;
&lt;br /&gt;
You already read [http://www.slideshare.net/boardgamearena/bga-studio-focus-on-bga-game-state-machine Focus on BGA game state machine], so you know that this is the heart of your game logic. For reversi, it&#039;s relatively simple. Here&#039;s a diagram of our game state machine:&lt;br /&gt;
&lt;br /&gt;
[[File:reversi6.jpg]]&lt;br /&gt;
&lt;br /&gt;
=== Build your States ===&lt;br /&gt;
And here&#039;s our &amp;lt;code&amp;gt;states.inc.php&amp;lt;/code&amp;gt;, according to this diagram:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$machinestates = array(&lt;br /&gt;
&lt;br /&gt;
    ST_BGA_GAME_SETUP =&amp;gt; array(&lt;br /&gt;
        &amp;quot;name&amp;quot; =&amp;gt; &amp;quot;gameSetup&amp;quot;,&lt;br /&gt;
        &amp;quot;description&amp;quot; =&amp;gt; clienttranslate(&amp;quot;Game setup&amp;quot;),&lt;br /&gt;
        &amp;quot;type&amp;quot; =&amp;gt; &amp;quot;manager&amp;quot;,&lt;br /&gt;
        &amp;quot;action&amp;quot; =&amp;gt; &amp;quot;stGameSetup&amp;quot;,&lt;br /&gt;
        &amp;quot;transitions&amp;quot; =&amp;gt; array( &amp;quot;&amp;quot; =&amp;gt; ST_PLAYER_PLAY_DISC )&lt;br /&gt;
    ),&lt;br /&gt;
    &lt;br /&gt;
    ST_PLAYER_PLAY_DISC =&amp;gt; array(&lt;br /&gt;
        &amp;quot;name&amp;quot; =&amp;gt; &amp;quot;playerTurn&amp;quot;,&lt;br /&gt;
		&amp;quot;description&amp;quot; =&amp;gt; clienttranslate(&#039;${actplayer} must play a disc&#039;),&lt;br /&gt;
		&amp;quot;descriptionmyturn&amp;quot; =&amp;gt; clienttranslate(&#039;${you} must play a disc&#039;),&lt;br /&gt;
        &amp;quot;type&amp;quot; =&amp;gt; &amp;quot;activeplayer&amp;quot;,&lt;br /&gt;
        &amp;quot;args&amp;quot; =&amp;gt; &amp;quot;argPlayerTurn&amp;quot;,&lt;br /&gt;
        &amp;quot;possibleactions&amp;quot; =&amp;gt; array( &#039;actPlayDisc&#039; ),&lt;br /&gt;
        &amp;quot;transitions&amp;quot; =&amp;gt; array( &amp;quot;playDisc&amp;quot; =&amp;gt; ST_NEXT_PLAYER, &amp;quot;zombiePass&amp;quot; =&amp;gt; ST_NEXT_PLAYER )&lt;br /&gt;
    ),&lt;br /&gt;
    &lt;br /&gt;
    ST_NEXT_PLAYER =&amp;gt; array(&lt;br /&gt;
        &amp;quot;name&amp;quot; =&amp;gt; &amp;quot;nextPlayer&amp;quot;,&lt;br /&gt;
        &amp;quot;type&amp;quot; =&amp;gt; &amp;quot;game&amp;quot;,&lt;br /&gt;
        &amp;quot;action&amp;quot; =&amp;gt; &amp;quot;stNextPlayer&amp;quot;,&lt;br /&gt;
        &amp;quot;updateGameProgression&amp;quot; =&amp;gt; true,        &lt;br /&gt;
        &amp;quot;transitions&amp;quot; =&amp;gt; array( &amp;quot;nextTurn&amp;quot; =&amp;gt; ST_PLAYER_PLAY_DISC, &amp;quot;cantPlay&amp;quot; =&amp;gt; ST_NEXT_PLAYER, &amp;quot;endGame&amp;quot; =&amp;gt; ST_END_GAME )&lt;br /&gt;
    ),&lt;br /&gt;
   &lt;br /&gt;
    ST_END_GAME =&amp;gt; array(&lt;br /&gt;
        &amp;quot;name&amp;quot; =&amp;gt; &amp;quot;gameEnd&amp;quot;,&lt;br /&gt;
        &amp;quot;description&amp;quot; =&amp;gt; clienttranslate(&amp;quot;End of game&amp;quot;),&lt;br /&gt;
        &amp;quot;type&amp;quot; =&amp;gt; &amp;quot;manager&amp;quot;,&lt;br /&gt;
        &amp;quot;action&amp;quot; =&amp;gt; &amp;quot;stGameEnd&amp;quot;,&lt;br /&gt;
        &amp;quot;args&amp;quot; =&amp;gt; &amp;quot;argGameEnd&amp;quot;&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We used constants to give an index to the different states. This is to avoid mistakes when we reuse these indexes on the transitions array.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s create a file you&#039;ll put in &amp;lt;code&amp;gt;modules/php/constants.inc.php&amp;lt;/code&amp;gt; :&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * State constants&lt;br /&gt;
 */&lt;br /&gt;
const ST_BGA_GAME_SETUP = 1;&lt;br /&gt;
&lt;br /&gt;
const ST_PLAYER_PLAY_DISC = 10;&lt;br /&gt;
const ST_NEXT_PLAYER = 11;&lt;br /&gt;
&lt;br /&gt;
const ST_END_GAME = 99;&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will also need to add &amp;lt;code&amp;gt;require_once(&amp;quot;modules/php/constants.inc.php&amp;quot;);&amp;lt;/code&amp;gt; at the beginning of the state.inc.php to load these constants.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now, in &amp;lt;code&amp;gt;modules/php/Game.php&amp;lt;/code&amp;gt; let&#039;s create the methods that are declared in this game states description file:&lt;br /&gt;
* &amp;lt;code&amp;gt;argPlayerTurn&amp;lt;/code&amp;gt;: referenced in the &amp;lt;code&amp;gt;args&amp;lt;/code&amp;gt; property of the &amp;lt;code&amp;gt;playerTurn&amp;lt;/code&amp;gt; state; this is the name of the method to call to retrieve arguments for this gamestate. Arguments are sent to the client side to be used on &amp;lt;code&amp;gt;onEnteringState&amp;lt;/code&amp;gt; or to set arguments in the gamestate description.&lt;br /&gt;
* &amp;lt;code&amp;gt;stNextPlayer&amp;lt;/code&amp;gt;: referenced in the &amp;lt;code&amp;gt;action&amp;lt;/code&amp;gt; property of the &amp;lt;code&amp;gt;nextPlayer&amp;lt;/code&amp;gt; state; this is the name of the method to call when this game state become the current game state.&lt;br /&gt;
&lt;br /&gt;
=== Test Your States ===&lt;br /&gt;
... and start a new Reversi game.&lt;br /&gt;
&lt;br /&gt;
As you can see on the screen capture below, the BGA framework makes the game jump to our first game state &amp;lt;code&amp;gt;playerTurn&amp;lt;/code&amp;gt; right after the initial setup. That&#039;s why the status bar contains the description of &amp;lt;code&amp;gt;playerTurn&amp;lt;/code&amp;gt; state (&amp;quot;XXXX must play a disc&amp;quot;):&lt;br /&gt;
&lt;br /&gt;
[[File:reversi7.jpg]]&lt;br /&gt;
&lt;br /&gt;
== The rules ==&lt;br /&gt;
&lt;br /&gt;
We will use the &amp;lt;code&amp;gt;getPossibleMoves&amp;lt;/code&amp;gt; PHP method to:&lt;br /&gt;
* Indicate to the current player where she is allowed to play by returning a list of coordinates&lt;br /&gt;
* Check if the player has the right to play in the spot they choose&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Example of &amp;lt;code&amp;gt;getPossibleMoves&amp;lt;/code&amp;gt; here https://gist.github.com/leocaseiro/a8bc2851bd0caddd06685b5035937d15&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is pure PHP programming here, and there are no special things from the BGA framework that can be used. This is why we won&#039;t go into details here. The overall idea is:&lt;br /&gt;
* Create a &amp;lt;code&amp;gt;getTurnedOverDiscs(x,y)&amp;lt;/code&amp;gt; method that returns coordinates of discs that would be turned over if a token would be played at &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;,&amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Loop through all free squares of the board and call the &amp;lt;code&amp;gt;getTurnedOverDiscs&amp;lt;/code&amp;gt; method on each of them. If at least 1 token is turned over, this is a valid move.&lt;br /&gt;
&lt;br /&gt;
IMPORTANT: Making a database query is slow! Please don&#039;t load the entire game board with a SQL query multiple times. In our implementation, we load the entire board once at the beginning of &amp;lt;code&amp;gt;getPossibleMoves&amp;lt;/code&amp;gt;, and then pass the board as an argument to all methods.&lt;br /&gt;
&lt;br /&gt;
If you want to look into details, please look at the &amp;quot;utility method&amp;quot; sections of &amp;lt;code&amp;gt;Game.php&amp;lt;/code&amp;gt;. If building the tutorial yourself, copy the functions under &amp;quot;Utility functions&amp;quot; comment from the Reversi tutorial.&lt;br /&gt;
&lt;br /&gt;
== Display allowed moves ==&lt;br /&gt;
&lt;br /&gt;
Now we want to highlight the squares where the player can place a disc.&lt;br /&gt;
&lt;br /&gt;
To do this, we add a &amp;lt;code&amp;gt;argPlayerTurn&amp;lt;/code&amp;gt; method in &amp;lt;code&amp;gt;modules/php/Game.php&amp;lt;/code&amp;gt;. This method is called on the server each time we enter into &amp;lt;code&amp;gt;playerTurn&amp;lt;/code&amp;gt; game state, and its result is transferred automatically to the client-side:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function argPlayerTurn(): array&lt;br /&gt;
{&lt;br /&gt;
    return [&lt;br /&gt;
        &#039;possibleMoves&#039; =&amp;gt; $this-&amp;gt;getPossibleMoves( intval($this-&amp;gt;getActivePlayerId()) )&lt;br /&gt;
    ];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We use the &amp;lt;code&amp;gt;getPossibleMoves&amp;lt;/code&amp;gt; method we just developed.&lt;br /&gt;
&lt;br /&gt;
Each time we enter into a new game state, we use the &amp;lt;code&amp;gt;onEnteringState&amp;lt;/code&amp;gt; Javascript method (in the &amp;lt;code&amp;gt;reversi.js&amp;lt;/code&amp;gt; file, under &amp;quot;Game &amp;amp; client states&amp;quot;). This lets us use the data returned by the method above on the client side.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        onEnteringState: function( stateName, args )&lt;br /&gt;
        {&lt;br /&gt;
           console.log( &#039;Entering state: &#039;+stateName );&lt;br /&gt;
            &lt;br /&gt;
            switch( stateName )&lt;br /&gt;
            {&lt;br /&gt;
            case &#039;playerTurn&#039;:&lt;br /&gt;
                this.updatePossibleMoves( args.args.possibleMoves );&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
        },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, when we enter into &amp;lt;code&amp;gt;playerTurn&amp;lt;/code&amp;gt; game state, we call our &amp;lt;code&amp;gt;updatePossibleMoves&amp;lt;/code&amp;gt; method (under the &amp;quot;Utility methods&amp;quot; section). This method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
updatePossibleMoves: function( possibleMoves )&lt;br /&gt;
{&lt;br /&gt;
    // Remove current possible moves&lt;br /&gt;
    document.querySelectorAll(&#039;.possibleMove&#039;).forEach(div =&amp;gt; div.classList.remove(&#039;possibleMove&#039;));&lt;br /&gt;
&lt;br /&gt;
    for( var x in possibleMoves )&lt;br /&gt;
    {&lt;br /&gt;
        for( var y in possibleMoves[ x ] )&lt;br /&gt;
        {&lt;br /&gt;
            // x,y is a possible move&lt;br /&gt;
            document.getElementById(`square_${x}_${y}`).classList.add(&#039;possibleMove&#039;);&lt;br /&gt;
        }            &lt;br /&gt;
    }&lt;br /&gt;
                &lt;br /&gt;
    this.addTooltipToClass( &#039;possibleMove&#039;, &#039;&#039;, _(&#039;Place a disc here&#039;) );&lt;br /&gt;
},&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here&#039;s what this does. At first, it removes all &amp;lt;code&amp;gt;possibleMove&amp;lt;/code&amp;gt; classes currently applied with the very useful &amp;lt;code&amp;gt;document.querySelectorAll&amp;lt;/code&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
Then it loops through all possible moves our PHP &amp;lt;code&amp;gt;updatePossibleMoves&amp;lt;/code&amp;gt; function created for us, and adds the &amp;lt;code&amp;gt;possibleMove&amp;lt;/code&amp;gt; class to each corresponding square.&lt;br /&gt;
&lt;br /&gt;
Finally, it uses the BGA framework &amp;lt;code&amp;gt;addTooltipToClass&amp;lt;/code&amp;gt; method to associate a tooltip to all those highlighted squares so that players can understand their meaning.&lt;br /&gt;
&lt;br /&gt;
To see the possible moves we need to create a CSS class (&amp;lt;code&amp;gt;possibleMove&amp;lt;/code&amp;gt;) that can be applied to a &amp;lt;code&amp;gt;square&amp;lt;/code&amp;gt; element to highlight it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
.possibleMove {&lt;br /&gt;
    background-color: white;&lt;br /&gt;
    opacity: 0.2; &lt;br /&gt;
    cursor: pointer;  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And here we are:&lt;br /&gt;
&lt;br /&gt;
[[File:reversi8.jpg.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Let&#039;s play ==&lt;br /&gt;
&lt;br /&gt;
From now, it&#039;s better to restart a game with 2 players, because we are going to implement a complete Reversi turn. The summary of what we are going to do is:&lt;br /&gt;
* When we click on a &amp;lt;code&amp;gt;possibleMove&amp;lt;/code&amp;gt; square, send the move to the server.&lt;br /&gt;
* Server side, check the move is correct, apply Reversi rules and jump to next player.&lt;br /&gt;
* Client side, change the token position to reflect the move.&lt;br /&gt;
&lt;br /&gt;
First we associate each click on a square to one of our methods using our Javascript &amp;lt;code&amp;gt;setup&amp;lt;/code&amp;gt; method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
document.querySelectorAll(&#039;.square&#039;).forEach(square =&amp;gt; square.addEventListener(&#039;click&#039;, e =&amp;gt; this.onPlayDisc(e)));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, our &amp;quot;onPlayDisc&amp;quot; method is called each time someone clicks on a square.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s our &amp;quot;onPlayDisc&amp;quot; method below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
onPlayDisc: function( evt )&lt;br /&gt;
{&lt;br /&gt;
    // Stop this event propagation&lt;br /&gt;
    evt.preventDefault();&lt;br /&gt;
    evt.stopPropagation();&lt;br /&gt;
&lt;br /&gt;
    // Get the cliqued square x and y&lt;br /&gt;
    // Note: square id format is &amp;quot;square_X_Y&amp;quot;&lt;br /&gt;
    var coords = evt.currentTarget.id.split(&#039;_&#039;);&lt;br /&gt;
    var x = coords[1];&lt;br /&gt;
    var y = coords[2];&lt;br /&gt;
&lt;br /&gt;
    if(!document.getElementById(`square_${x}_${y}`).classList.contains(&#039;possibleMove&#039;)) {&lt;br /&gt;
        // This is not a possible move =&amp;gt; the click does nothing&lt;br /&gt;
        return ;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    this.bgaPerformAction(&amp;quot;actPlayDisc&amp;quot;, {&lt;br /&gt;
        x:x,&lt;br /&gt;
        y:y&lt;br /&gt;
    });&lt;br /&gt;
},&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What we do here is:&lt;br /&gt;
* We stop the propagation of the Javascript &amp;lt;code&amp;gt;onclick&amp;lt;/code&amp;gt; event. Otherwise, it can lead to random behavior so it&#039;s always a good idea.&lt;br /&gt;
* We get the x/y coordinates of the square by using &amp;lt;code&amp;gt;evt.currentTarget.id&amp;lt;/code&amp;gt;&lt;br /&gt;
* We check that clicked square has the &amp;lt;code&amp;gt;possibleMove&amp;lt;/code&amp;gt; class, otherwise we know for sure that we can&#039;t play there.&lt;br /&gt;
* Finally, we make a call to the server using BGA &amp;lt;code&amp;gt;bgaPerformAction&amp;lt;/code&amp;gt; method with argument x and y. This call will check that &amp;lt;code&amp;gt;actPlayDisc&amp;lt;/code&amp;gt; action is possible, according to current game state (see &amp;lt;code&amp;gt;possibleactions&amp;lt;/code&amp;gt; entry in our &amp;lt;code&amp;gt;playerTurn&amp;lt;/code&amp;gt; game state defined above). This check is important to avoid issues if a player double clicks on a square.&lt;br /&gt;
&lt;br /&gt;
Now, we have to manage this &amp;lt;code&amp;gt;actPlayDisc&amp;lt;/code&amp;gt; action on the server side. Add a corresponding &amp;lt;code&amp;gt;actPlayDisc&amp;lt;/code&amp;gt; method in our game logic (&amp;lt;code&amp;gt;Game.php&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function actPlayDisc( int $x, int $y )&lt;br /&gt;
{&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
(The function will be called when the front-side action is triggered using the Autowire mechanism, if you want to see how it works in details check [[Main game logic: Game.php|https://en.doc.boardgamearena.com/Main_game_logic:_Game.php#Actions_%28autowired%29]] )&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $player_id = intval($this-&amp;gt;getActivePlayerId()); &lt;br /&gt;
        &lt;br /&gt;
        // Now, check if this is a possible move&lt;br /&gt;
        $board = $this-&amp;gt;getBoard();&lt;br /&gt;
        $turnedOverDiscs = $this-&amp;gt;getTurnedOverDiscs( $x, $y, $player_id, $board );&lt;br /&gt;
        &lt;br /&gt;
        if( count( $turnedOverDiscs ) &amp;gt; 0 )&lt;br /&gt;
        {&lt;br /&gt;
            // This move is possible!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
...now, we are using the &amp;lt;code&amp;gt;getTurnedOverDiscs&amp;lt;/code&amp;gt; method again to check that this move is possible.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            // Let&#039;s place a disc at x,y and return all &amp;quot;$returned&amp;quot; discs to the active player&lt;br /&gt;
            &lt;br /&gt;
            $sql = &amp;quot;UPDATE board SET board_player=&#039;$player_id&#039;&lt;br /&gt;
                    WHERE ( board_x, board_y) IN ( &amp;quot;;&lt;br /&gt;
            &lt;br /&gt;
            foreach( $turnedOverDiscs as $turnedOver )&lt;br /&gt;
            {&lt;br /&gt;
                $sql .= &amp;quot;(&#039;&amp;quot;.$turnedOver[&#039;x&#039;].&amp;quot;&#039;,&#039;&amp;quot;.$turnedOver[&#039;y&#039;].&amp;quot;&#039;),&amp;quot;;&lt;br /&gt;
            }&lt;br /&gt;
            $sql .= &amp;quot;(&#039;$x&#039;,&#039;$y&#039;) ) &amp;quot;;&lt;br /&gt;
                       &lt;br /&gt;
            $this-&amp;gt;DbQuery( $sql );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
... we update the database to change the color of all turned over disc + the disc we just placed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            // Update scores according to the number of disc on board&lt;br /&gt;
            $sql = &amp;quot;UPDATE player&lt;br /&gt;
                    SET player_score = (&lt;br /&gt;
                    SELECT COUNT( board_x ) FROM board WHERE board_player=player_id&lt;br /&gt;
                    )&amp;quot;;&lt;br /&gt;
            $this-&amp;gt;DbQuery( $sql );&lt;br /&gt;
            &lt;br /&gt;
            // Statistics&lt;br /&gt;
            $this-&amp;gt;incStat( count( $turnedOverDiscs ), &amp;quot;turnedOver&amp;quot;, $player_id );&lt;br /&gt;
            if( ($x==1 &amp;amp;&amp;amp; $y==1) || ($x==8 &amp;amp;&amp;amp; $y==1) || ($x==1 &amp;amp;&amp;amp; $y==8) || ($x==8 &amp;amp;&amp;amp; $y==8) )&lt;br /&gt;
                $this-&amp;gt;incStat( 1, &#039;discPlayedOnCorner&#039;, $player_id );&lt;br /&gt;
            else if( $x==1 || $x==8 || $y==1 || $y==8 )&lt;br /&gt;
                $this-&amp;gt;incStat( 1, &#039;discPlayedOnBorder&#039;, $player_id );&lt;br /&gt;
            else if( $x&amp;gt;=3 &amp;amp;&amp;amp; $x&amp;lt;=6 &amp;amp;&amp;amp; $y&amp;gt;=3 &amp;amp;&amp;amp; $y&amp;lt;=6 )&lt;br /&gt;
                $this-&amp;gt;incStat( 1, &#039;discPlayedOnCenter&#039;, $player_id );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
... now, we update both player score by counting all disc, and we manage game statistics.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            // Notify&lt;br /&gt;
            $this-&amp;gt;notify-&amp;gt;all( &amp;quot;playDisc&amp;quot;, clienttranslate( &#039;${player_name} plays a disc and turns over ${returned_nbr} disc(s)&#039; ), array(&lt;br /&gt;
                &#039;player_id&#039; =&amp;gt; $player_id,&lt;br /&gt;
                &#039;player_name&#039; =&amp;gt; $this-&amp;gt;getActivePlayerName(),&lt;br /&gt;
                &#039;returned_nbr&#039; =&amp;gt; count( $turnedOverDiscs ),&lt;br /&gt;
                &#039;x&#039; =&amp;gt; $x,&lt;br /&gt;
                &#039;y&#039; =&amp;gt; $y&lt;br /&gt;
            ) );&lt;br /&gt;
&lt;br /&gt;
            $this-&amp;gt;notify-&amp;gt;all( &amp;quot;turnOverDiscs&amp;quot;, &#039;&#039;, array(&lt;br /&gt;
                &#039;player_id&#039; =&amp;gt; $player_id,&lt;br /&gt;
                &#039;turnedOver&#039; =&amp;gt; $turnedOverDiscs&lt;br /&gt;
            ) );&lt;br /&gt;
            &lt;br /&gt;
            $newScores = $this-&amp;gt;getCollectionFromDb( &amp;quot;SELECT player_id, player_score FROM player&amp;quot;, true );&lt;br /&gt;
            $this-&amp;gt;notify-&amp;gt;all( &amp;quot;newScores&amp;quot;, &amp;quot;&amp;quot;, array(&lt;br /&gt;
                &amp;quot;scores&amp;quot; =&amp;gt; $newScores&lt;br /&gt;
            ) );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
... then we notify about all these changes. We are using for that 3 notifications (&amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;turnOverDiscs&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;newScores&amp;lt;/code&amp;gt; that we are going to implement on client side later). Note that the description of the &amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; notification will be logged in the game log.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            // Then, go to the next state&lt;br /&gt;
            $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;playDisc&#039; );&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
            throw new \BgaSystemException( &amp;quot;Impossible move&amp;quot; );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
... finally, we jump to the next game state if everything goes fine (&amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; is the name of a transition in the &amp;lt;code&amp;gt;playerTurn&amp;lt;/code&amp;gt; game state description above which leads to state 11 which is &amp;lt;code&amp;gt;nextPlayer&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
To make the statistics work, we have to initialize them in &amp;lt;code&amp;gt;stats.json&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;player&amp;quot;: {&lt;br /&gt;
    &amp;quot;discPlayedOnCorner&amp;quot;: {&lt;br /&gt;
      &amp;quot;id&amp;quot;: 10,&lt;br /&gt;
      &amp;quot;name&amp;quot;: &amp;quot;Discs played on a corner&amp;quot;,&lt;br /&gt;
      &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    &amp;quot;discPlayedOnBorder&amp;quot;: {&lt;br /&gt;
      &amp;quot;id&amp;quot;: 11,&lt;br /&gt;
      &amp;quot;name&amp;quot;: &amp;quot;Discs played on a border&amp;quot;,&lt;br /&gt;
      &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    &amp;quot;discPlayedOnCenter&amp;quot;: {&lt;br /&gt;
      &amp;quot;id&amp;quot;: 12,&lt;br /&gt;
      &amp;quot;name&amp;quot;: &amp;quot;Discs played on board center part&amp;quot;,&lt;br /&gt;
      &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    &amp;quot;turnedOver&amp;quot;: {&lt;br /&gt;
      &amp;quot;id&amp;quot;: 13,&lt;br /&gt;
      &amp;quot;name&amp;quot;: &amp;quot;Number of discs turned over&amp;quot;,&lt;br /&gt;
      &amp;quot;type&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A last thing to do on the server side is to activate the next player when we enter the &amp;lt;code&amp;gt;nextPlayer&amp;lt;/code&amp;gt; game state (in the &amp;lt;code&amp;gt;modules/php/Game.php&amp;lt;/code&amp;gt; file, under &amp;quot;Game state reactions&amp;quot;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    function stNextPlayer(): void&lt;br /&gt;
    {&lt;br /&gt;
        // Active next player&lt;br /&gt;
        $player_id = intval($this-&amp;gt;activeNextPlayer());&lt;br /&gt;
&lt;br /&gt;
        // Check if both player has at least 1 discs, and if there are free squares to play&lt;br /&gt;
        $player_to_discs = $this-&amp;gt;getCollectionFromDb( &amp;quot;SELECT board_player, COUNT( board_x )&lt;br /&gt;
                                                       FROM board&lt;br /&gt;
                                                       GROUP BY board_player&amp;quot;, true );&lt;br /&gt;
&lt;br /&gt;
        if( ! isset( $player_to_discs[ null ] ) )&lt;br /&gt;
        {&lt;br /&gt;
            // Index 0 has not been set =&amp;gt; there&#039;s no more free place on the board !&lt;br /&gt;
            // =&amp;gt; end of the game&lt;br /&gt;
            $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;endGame&#039; );&lt;br /&gt;
            return ;&lt;br /&gt;
        }&lt;br /&gt;
        else if( ! isset( $player_to_discs[ $player_id ] ) )&lt;br /&gt;
        {&lt;br /&gt;
            // Active player has no more disc on the board =&amp;gt; he looses immediately&lt;br /&gt;
            $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;endGame&#039; );&lt;br /&gt;
            return ;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Can this player play?&lt;br /&gt;
&lt;br /&gt;
        $possibleMoves = $this-&amp;gt;getPossibleMoves( $player_id );&lt;br /&gt;
        if( count( $possibleMoves ) == 0 )&lt;br /&gt;
        {&lt;br /&gt;
&lt;br /&gt;
            // This player can&#039;t play&lt;br /&gt;
            // Can his opponent play ?&lt;br /&gt;
            $opponent_id = (int)$this-&amp;gt;getUniqueValueFromDb( &amp;quot;SELECT player_id FROM player WHERE player_id!=&#039;$player_id&#039; &amp;quot; );&lt;br /&gt;
            if( count( $this-&amp;gt;getPossibleMoves( $opponent_id ) ) == 0 )&lt;br /&gt;
            {&lt;br /&gt;
                // Nobody can move =&amp;gt; end of the game&lt;br /&gt;
                $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;endGame&#039; );&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {            &lt;br /&gt;
                // =&amp;gt; pass his turn&lt;br /&gt;
                $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;cantPlay&#039; );&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
            // This player can play. Give him some extra time&lt;br /&gt;
            $this-&amp;gt;giveExtraTime( $player_id );&lt;br /&gt;
            $this-&amp;gt;gamestate-&amp;gt;nextState( &#039;nextTurn&#039; );&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we play a token, the rules are checked and the token appears in the database.&lt;br /&gt;
&lt;br /&gt;
[[File:reversi9.jpg]]&lt;br /&gt;
&lt;br /&gt;
Of course, as we don&#039;t manage notifications on client side, we need to press F5 after each move to see the changes on the board.&lt;br /&gt;
&lt;br /&gt;
== Make the move appear automatically ==&lt;br /&gt;
&lt;br /&gt;
Now, what we have to do is process the notifications sent by the server and make the move appear on the interface.&lt;br /&gt;
&lt;br /&gt;
In our &amp;lt;code&amp;gt;setupNotifications&amp;lt;/code&amp;gt; method in &amp;lt;code&amp;gt;reversi.js&amp;lt;/code&amp;gt;, we tell BGA to listen to notifications we will add later (&amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;turnOverDiscs&amp;lt;/code&amp;gt;) using promise-based notifications:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
setupNotifications: function()&lt;br /&gt;
        {&lt;br /&gt;
            console.log( &#039;notifications subscriptions setup&#039; );&lt;br /&gt;
&lt;br /&gt;
            // automatically listen to the notifications, based on the `notif_xxx` function on this class.&lt;br /&gt;
            this.bgaSetupPromiseNotifications();&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will associate each of our 3 notifications with a method prefixed with &amp;lt;code&amp;gt;notif_&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Let&#039;s have a look now on the &amp;lt;code&amp;gt;playDisc&amp;lt;/code&amp;gt; notification handler method:&amp;lt;pre&amp;gt;&lt;br /&gt;
notif_playDisc: async function( args )&lt;br /&gt;
        {&lt;br /&gt;
            // Remove current possible moves (makes the board more clear)&lt;br /&gt;
            document.querySelectorAll(&#039;.possibleMove&#039;).forEach(div =&amp;gt; div.classList.remove(&#039;possibleMove&#039;));&lt;br /&gt;
        &lt;br /&gt;
            await this.addDiscOnBoard( args.x, args.y, args.player_id );&lt;br /&gt;
        },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No surprise here, we re-used some existing stuff to:&lt;br /&gt;
* Remove the highlighted squares.&lt;br /&gt;
* Add a new disc on board, coming from player panel. That&#039;s where the async await part of &amp;lt;code&amp;gt;addDiscOnBoard&amp;lt;/code&amp;gt; becomes useful.&lt;br /&gt;
&lt;br /&gt;
Now, here&#039;s the method that handles the &amp;lt;code&amp;gt;turnOverDiscs&amp;lt;/code&amp;gt; notification:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
notif_turnOverDiscs: async function( args )&lt;br /&gt;
        {&lt;br /&gt;
            // Get the color of the player who is returning the discs&lt;br /&gt;
            const targetColor = this.gamedatas.players[ args.player_id ].color;&lt;br /&gt;
&lt;br /&gt;
            // wait for the animations of all turned discs to be over before considering the notif done&lt;br /&gt;
            await Promise.all(&lt;br /&gt;
                args.turnedOver.map(disc =&amp;gt; &lt;br /&gt;
                    this.animateTurnOverDisc(disc, targetColor)&lt;br /&gt;
                )&lt;br /&gt;
            );&lt;br /&gt;
        },&lt;br /&gt;
&lt;br /&gt;
        animateTurnOverDisc: async function(disc, targetColor) {&lt;br /&gt;
            const discDiv = document.getElementById(`disc_${disc.x}${disc.y}`);&lt;br /&gt;
            if (!this.bgaAnimationsActive()) {&lt;br /&gt;
                // do not play animations if the animations aren&#039;t activated (fast replay mode)&lt;br /&gt;
                discDiv.dataset.color = targetColor;&lt;br /&gt;
                return Promise.resolve();&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            // Make the disc blink 2 times&lt;br /&gt;
            const anim = dojo.fx.chain( [&lt;br /&gt;
                dojo.fadeOut( { node: discDiv } ),&lt;br /&gt;
                dojo.fadeIn( { node: discDiv } ),&lt;br /&gt;
                dojo.fadeOut( { &lt;br /&gt;
                                node: discDiv,&lt;br /&gt;
                                onEnd: () =&amp;gt; discDiv.dataset.color = targetColor,&lt;br /&gt;
                            } ),&lt;br /&gt;
                dojo.fadeIn( { node: discDiv } )&lt;br /&gt;
            ] ); // end of dojo.fx.chain&lt;br /&gt;
&lt;br /&gt;
            await this.bgaPlayDojoAnimation(anim);&lt;br /&gt;
        },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The list of the discs to be turned over has been made available by our server side code in &amp;lt;code&amp;gt;args.turnedOver&amp;lt;/code&amp;gt; (see previous paragraph). We loop through all these discs, and trigger a single disc animation using &amp;lt;code&amp;gt;animateTurnOverDisc&amp;lt;/code&amp;gt;. This function creates a complex animation using &amp;lt;code&amp;gt;dojo.Animation&amp;lt;/code&amp;gt; for the disc in parameter. The complete documentation on dojo animations [http://dojotoolkit.org/documentation/tutorials/1.6/animation/ can be found here].&lt;br /&gt;
&lt;br /&gt;
In few words: we create a chain of 4 animations to make the disc fade out, fade in, fade out again, and fade in again. At the end of the second fade out, we change the color of the disc. Finally, we launch the animation with &amp;lt;code&amp;gt;bgaPlayDojoAnimation()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Also the notification to update the scores:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
notif_newScores: async function( args )&lt;br /&gt;
        {&lt;br /&gt;
            for( var player_id in args.scores )&lt;br /&gt;
            {&lt;br /&gt;
                var newScore = args.scores[ player_id ];&lt;br /&gt;
                this.scoreCtrl[ player_id ].toValue( newScore );&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Studio]]&lt;/div&gt;</summary>
		<author><name>Sean-the-sheep</name></author>
	</entry>
</feed>