jChess - Display a Chess Game State/Animation (jGames)

javascript jquery

jChess is one module within the jGames suite used to display Chess game states, as well as animations.

Display Static Chess State First include the following lines to your webpage

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

Create an empty div tag and give it an ID, i.e. "chess". This is where the chess board will be rendered to.

<div id="chess"></div>

Next, create the state of the chess board using Javascript. The below state represents every piece in the Chess game and renders the chess above left chess board.

var board_chess = [
      ["br", "bn", "bb", "bk", "bq", "bb", "bn", "br"],
      ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
      [" ", " ", " ", " ", " ", " ", " ", " "],
      [" ", " ", " ", " ", " ", " ", " ", " "],
      [" ", " ", " ", " ", " ", " ", " ", " "],
      [" ", " ", " ", " ", " ", " ", " ", " "],
      ["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
      ["wr", "wn", "wb", "wk", "wq", "wb", "wn", "wr"]];
$("#chess").chess(board_chess);

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 chessAnimator() function. Below is the code to render the above right Chess animation.

var chess_anim
[
    [
        ["br", "bn", "bb", "bk", "bq", "bb", "bn", "br"],
        ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        ["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
        ["wr", "wn", "wb", "wk", "wq", "wb", "wn", "wr"]
    ],
    [
        ["br", "bn", "bb", "bk", "bq", "bb", "bn", "br"],
        ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", "wp", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        ["wp", "wp", "wp", " ", "wp", "wp", "wp", "wp"],
        ["wr", "wn", "wb", "wk", "wq", "wb", "wn", "wr"]
    ],
    [   
        ["br", "bn", "bb", "bk", "bq", "bb", "bn", "br"],
        ["bp", "bp", "bp", "bp", " ", "bp", "bp", "bp"],
        [" ", " ", " ", " ", "bp", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", "wp", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        ["wp", "wp", "wp", " ", "wp", "wp", "wp", "wp"],
        ["wr", "wn", "wb", "wk", "wq", "wb", "wn", "wr"]
    ],
    [   
        ["br", "bn", "bb", "bk", "bq", "bb", "bn", "br"],
        ["bp", "bp", "bp", "bp", " ", "bp", "bp", "bp"],
        [" ", " ", " ", " ", "bp", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", "wp", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", "wn", " ", " "],
        ["wp", "wp", "wp", " ", "wp", "wp", "wp", "wp"],
        ["wr", "wn", "wb", "wk", "wq", "wb", " ", "wr"]
    ],
    [   
        ["br", "bn", "bb", "bk", " ", "bb", "bn", "br"],
        ["bp", "bp", "bp", "bp", "bq", "bp", "bp", "bp"],
        [" ", " ", " ", " ", "bp", " ", " ", " "],
        [" ", " ", " ", " ", " ", " ", " ", " "],
        [" ", " ", " ", "wp", " ", " ", " ", " "],
        [" ", " ", " ", " ", " ", "wn", " ", " "],
        ["wp", "wp", "wp", " ", "wp", "wp", "wp", "wp"],
        ["wr", "wn", "wb", "wk", "wq", "wb", " ", "wr"]
    ]
];
$("#chess_anim").chessAnimator(chess_anim, 1000);