..by this I mean a logic handler for a chess game. Basically validating a move and checking if somebody has won.

Now ignore the complexity of the game(if you can..) I'd like some sort of psuedo code on how it would look using callbacks in javascript.

For arguments sake imagine once a move is completed in the browser it sends a JSON object of the piece that has been moved and to which space it has been moved to. On the server we have access to a JSON object which contains data for the complete board. With this information I expect to pass it in to my engine and determine from the output if the move is valid..and if so is it a winning move?

I'm not looking for any specific logic on how to determine a move in chess -> just a kind of psuedo example/template of the flow of the game using the above paragraph as a guide to what must be done.

The reason for this is I would like to understand how callbacks can be used to produce something of this nature as opposed to synchronous code.

  • I'm not sure how this really differs from any other request/response chain that can be dealt with using node (and there are a whole whack of examples and tutorials on that). Commented Sep 8, 2011 at 22:48
  • Checking that a move is valid isn't all that complex. Commented Jan 20, 2014 at 17:58

1 Answer 1


Have I interpreted it correctly that you have a server that records all of the game board layouts, and each game has two clients that can see the board and move the pieces?

So there's a server program and a client program:

Client player moves piece to destination square:

If moving piece from piece.square to destination is an invalid move then
    Alert "You cannot move that piece there."
    Send (game, piece, destination) to server
    While within 30 second timeout
        Listen for response
    If no response received then
        Alert "Cannot contact server."
        If response is failure then
            Alert "You cannot move that piece there."
            Move response.piece to response.destination
            If response.check is check then
                Alert "You put them in check."
            ElseIf response.check is checkmate then
                Alert "You win!."
                End game
            Wait for the server to say it's your turn

Server receives a move:

Retrieve board for game
If moving piece from piece.square to destination is an invalid move then
    Send (failure) to client
    Move piece to destination
    Save game board
    Check = check if player is safe, in check or in checkmate
    Send (piece, destination, check) to client
    Send (your move, check) to other client

Client waits for their turn:

Until a response is received
    Listen for response
If response.check is check then
    Alert "You are in check."
If response.check is checkmate then
    Alert "You lose!"
    End game
Let player make their move.

The program double-checks that the move is valid. It checks at the client end to prevent sending an obviously invalid move to the server and wasting the player's time. It checks at the server end to ensure a player isn't sending hacked packets to make illegal moves. The server returns the move to the client because that what the server has acted upon, and accurately represents the state of the game.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.