Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

..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.

share|improve this question
    
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). –  Demian Brecht Sep 8 '11 at 22:48
    
Checking that a move is valid isn't all that complex. –  whatsisname Jan 20 at 17:58
add comment

1 Answer

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."
Else
    Send (game, piece, destination) to server
    While within 30 second timeout
        Listen for response
    If no response received then
        Alert "Cannot contact server."
    Else
        If response is failure then
            Alert "You cannot move that piece there."
        Else
            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
Else
    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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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