/Home or play chess at
......... Google App Engine ........... .... ....

Chess software for fun

otrdiena, 2009. gada 9. jūnijs

Chessforeva for Web developments

This Chessforeva code is a free source for web programming, blogging or all the ideas around browser javascript/DHTML based solutions. Tested already on mobile phones, different browsers and implemented as opensocal online chess site on http://chessforeva.appspot.com
Some help is possible:
grozny0 [at] gmail. com

A good demo site is

You may also
play 3D chess with great java chess engine from Lokasoft or
3D chess integration with Jester - the strongest java chess engine from Ludosoft.
Or try Silly chess for beginners. Other chess engines connected to these libraries:
Js Toledo, Js p4wn, Js Micro-Max, java of Mediocre.
In fact, there are various new engines: silverlight, Unity3D based, new javascripts, etc... See it all on Chessforeva.

The advantages of this code:

  • No installations needed (no plugins, no programs)
  • browsers IE/Firefox/Chrome/Safari/Opera compatible
  • use on your sites, blogs, programs, etc.
  • chess logic for board table is correct
  • 3D chess, good visibility, animated
  • mouse, cursor for legal moves
  • easy to use
If you have a site for chess educational, diagram, or other purposes, just take it and use as You wish for Your projects.

Instructions and usage:

Put this code in Your html in body section in the beginning:

Add c0_chess.js main javascript code by copy/paste this:

<script src="http://chessforeva.appspot.com/c0_chess.js" type="text/javascript"></script>

Add c0_pgn.js functions only for FEN or PGN level (described later):
<script src="http://chessforeva.appspot.com/c0_pgn.js" type="text/javascript"></script>

That includes chess javascript library, prepares workspace, declares necessary variables.
Now You may access it and create the board.
Put this code in Your html where it should be:

<script type="text/javascript">

This created variables and prepared some elements.

The chess board can be re sized before it appears in the document. Just adjust relative size.
Or set c0_relative_size_procents=0 for automatic adjustment.
var c0_at_top=120;
var c0_at_left=20;
The chess board can be re positioned before it appears in the document. Just adjust left top corner (points px).

Which side is closest to us. 1 for white, -1 for black. Default setting is 1

That variable means "do nothing" for state false (default setting). If true is adjusted then the page viewer is able to make a move by moving piece by mouse. Then additional programming is needed. It isn't so simple and some code is at Chessforeva.

Which side moves. 1 for white, -1 for black. Default setting is 1

There is art style (for true) implemented also in the code

You may also disable zooming by setting

Otherwise users may press zoom in/out buttons that are visible by default.

You may set the initial top-view mode
If zooming allowed, users can press button to switch modes. Art style then is ignored.


This option removes letters A-H, 1-8 from the sides of the chess board. Use before initial drawings.

This draws empty board in Your page. The first drawing.


For better visibility 2D chess board in corner should be displayed. Or just don't use this function if that feature is no needed.

c0_set_start_position( "" );
The pieces should be displayed using this function. If the parameter omitted ("") then the state is the beginning of game. The variable c0_moveslist is set to empty.
The syntax for moves list: e2e4e7e5g1f3........e1g1[0]....a7a8[Q]....h2h1[R]....
e1g1[0] is 0-0
e1c1[0] is 0-0-0
a7a8[Q] is white Queen promoted on a8
h2h1[R] is black Rook promoted on h1

So, You can compose first moves and use it in parameter.
c0_moveslist always contains the list after.

That's all for simple publishing purposes.

There are also other variables that can be used for advanced programming:
Contains current chess position on board.
For example: wpa2;wpb2;wKg1;bKh8;bQg7

These variables are flags-indicators. For example c0_w00=true means that the white side has done castling before. Rock is Rook, "c" looks like incomplete "o" :D

This contains number (1-8) of pawn ready for "en passant" killing.

For very advanced programmers there is possible to create DIVs that can be moved by user. Define them in


There can be additional features developed in these DIVs like chess clock, position viewers, etc...

Samples of more complex usage should be obtained from the source code of chessforeva sites listed before.

Move pieces by using
c0_move_to( "e2", "e4" );

Don't forget to set up and clear after each move these variables
These variables manage information about which piece should be promoted (if needed) and last piece promoted on board.

Movement without changes on screen:
c0_move_to( c0_moveto(c0_convH888("e2"), c0_convH888("e4"), false);
If something more internal needed.


This function uses c0_moveslist variable to return one move before.

The programming is possible by using timer/mouse/keyboard events and javascript callback function which is called after each move of any piece on board, defined in

For connecting purposes, use Ajax calls on events to web-servers. Ok, it's slow, but possible.
If the code is needed then just download it from the original site.

There are some internal functions that can be used. Of course, the chess logic may be corrupted. It's better to use function c0_set_start_position
This places white pawn on e4. Use K,Q,R,B,N for other pieces.
Remove piece if there is on e4. These functions should be connected with variables that describe current position, moves, etc.

There are functions related to chess logic available.
You can set any position on board and detect status after each move.
Parameters: 1.list of pieces ("wKe1;bKe3;wNe2"), 2.-5. status for white (king has moved flag, left rook has moved, right rook has moved, there was castling before), 6.-9. (the same for black, just left is on A8), 10. en passant  (0-none, 1-A,..,8-H vertical),  11.previous moves, if important, 12. which side moves 1/-1 (white/black);
Status detection functions:
If piece is on a1 and wish to move h8 then this function returns true if b2, c3, d4, e5, f6, g7 are empty. For h8 and a1 use other detection functions. Returns true/false.


For white king use "w" parameter to detect it's status, "b" for black.

Test for a possible move can be performed any time. Returns true/false.

c0_D_is_enemy("e5", "w")

If a white pawn stays on f4 then this function detects enemy on e5 and returns true.


Function returns what's on a4. For example: "wK" (white King), "bp" (black pawn), or "" (empty).


The same as "take the tail" of variable c0_moveslist to get last move.


This function returns next possible moves for current position as a list in string variable. This is much faster than looking for pieces on board at A1-A8-...-H8. You may set, if needed, two validator-functions (written by You) which should be called in loops.
c0_f_evals1() - when just found new move in variable c0_foundmove;
c0_f_evals2() - make this move internally (bit slower), and than evaluate;
Find moves possible and analyze them for best. ;)
This function can't be used recursively. The Silly chess is based on it, as example.

The sample for detection and various advanced functions is here.
Use browser's source viewer to get the code.

c0_pgn.js functions

Some of these functions may be useful.

c0_set_FEN( FEN string );

To set up position according to the chess standards of saved position (

To get position as a FEN string.
You should set (or ignore) after the variable c0_moveslist. This standard doesn't care about previous moves. Anyway, it may be important internally.

Chess standard of portable game notation (PGN) is supported.

c0_get_from_PGN( PGN string );

Returns a whole list of moves as a string, if moves are possible and PGN is valid. On error variable c0_errflag is set to true.

Internal array c0_PGN_header contains all the header items.

Internal variable c0_start_FEN remembers the startup pusition, if FEN tag is used in PGN header. Clear it by c0_start_FEN="" for other game.


Returns a generated PGN as a string from variable moves.
Predefined array c0_PGN_header is used to generate header. On error variable c0_errflag is set to true.
In case of c0_put_to_PGN("") variable c0_moveslist is used.

For startup positions (FEN as header item) all drawings should be performed.
Standard cases do not require chess board on screen.

The demo for PGN functions is here.

Fischerrandom is supported from June 2010.
Castling works well on replay modes for now.
There are 2 interal variables containing all data:

10x8/other specific chess rules are NOT supported in standard c0_chess.js, c0_pgn.js libraries. Classical chess only! Or advance this code as You like.


And, of course, close the javascript section at the end of chess code:


Demo for interactive Chessforeva code in frame
(use mouse for activity)

Nav komentāru:

Ierakstīt komentāru