jTicTacToe – Display a Tic Tac Toe Game State/Animation (jGames)

Posted on by Kenny Cason
tags = [ jgames, jquery, jtictactoe, tic-tac-toe ]

jTicTacToe is one module within the jGames suite used to display Tic Tac Toe game states, as well as animations.

Display Static Tic Tac Toe State First include the following lines to your webpage

1
2
    <script type="text/javascript" src="js/jgames/jquery.jgames.js"></script>
    <link href="js/jgames/css/style.css" rel="stylesheet" type="text/css" />

Create an empty div tag and give it an ID, i.e. “tictactoe”. This is where the tic tac toe board will be rendered to.

1
<div id="tictactoe"></div>

Next, create the state of the tic tac toe board using Javascript. The below state represents every piece in the Tic Tac Toe game and renders the tic tac toe above left tic tac toe board.

1
2
3
4
5
6
        var board_tictactoe = [
            ["o", "o", "x"],
            ["o", "x", "x"],
            [" ", "o", " "]
        ];
       $("#tictactoe").tictactoe(board_tictactoe);

Creating an Animation Creating an animation is very easy. You simply pass an array of states, and the time interval between states (in milliseconds) to the tictactoeAnimator() function. Below is the code to render the above right Tic Tac Toe animation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        var board_tictactoe_anim =
        [
            [
                [" ", " ", " "],
                [" ", " ", " "],
                [" ", " ", " "]
            ],
            [
                [" ", " ", "o"],
                [" ", " ", " "],
                [" ", " ", " "]
            ],
            [
                [" ", " ", "o"],
                [" ", "x", " "],
                [" ", " ", " "]
            ],
            [
                [" ", " ", "o"],
                [" ", "x", " "],
                [" ", " ", "o"]
            ],
            [
                [" ", " ", "o"],
                [" ", "x", "x"],
                [" ", " ", "o"]
            ],
            [
                [" ", " ", "o"],
                [" ", "x", "x"],
                [" ", "o", "o"]
            ],
            [
                [" ", " ", "o"],
                ["x", "x", "x"],
                [" ", "o", "o"]
            ]
        ];

        $("#tictactoe_anim").tictactoeAnimator(board_tictactoe_anim, 1000);
comments powered by Disqus