www.webdeveloper.com
Results 1 to 12 of 12

Thread: chess rules script (and chess rules only)

  1. #1
    Join Date
    May 2011
    Posts
    7

    chess rules script (and chess rules only)

    Hi all,

    Newbie here, trying my first chess interface using jQuery and jQuery UI. All going well with the interface (click-click, drag-drop), but I am stuck with implementing the chess rules.
    What I need is some JavaScript code that returnes a TRUE / FALSE if a move is possible or not.

    Ideally, I would need everything wrapped up in a single function, something like this:
    function(idfrom, piece, idto) {}

    I know there are quite a few around, the most impressive I found is the pretty javascript-chess-with-cpu-oppo.html (I simply love it, tiny as it is), the only drawback is I don't need the AI behind it, but only the checking for legal moves. Does anybody knows how to extract that from the script?

    Or else, better (and more human readable) alternatives?

    Thanks in advance,
    Adrian

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Assuming you already have a 2-d array to represent the chessboard, define a "directions" array of 16 elements, each containing an x and y (rank and file) pair like 1,0 0,1 -1,0 0,-1 / 1,1 1,-1 -1,-1 -1,1 / 1,2 2,1 -1,2 -2,1 1,-2 2,-1, -1,-2 -2,-1 . The slashes separate the orthogonal moves, diagonal moves, and those by a knight. Determined by what piece is moving, a range of subscripts (e.g, 4-7 for bishops) would determine what constants would be added to the subscripts of where the piece stood at the beginning of the move. If the piece moved on lines (QRB) and you moved to an empty square, add those same constants again. White pawns and Black pawns would each need their own routine for both moving and capturing although one routine would probably work for castling. Of course you would need to see if a piece was moving off the board or into a friendly piece.

    One more thing to check (pun not intended) is that after the move is "made", the player's king may be hanging. A move like Nf3-e5 might be legal most of the time, but not if it left the player's king in check. Attempt to move every enemy piece (and pawn in a capturing motion) into the square occupied by the king. If you can't the move is legal.

    Blitz or Cray Blitz used a linear array of 120 elements for their board; more details are HyattCray.pdf.

    "A Forrest Gump of Chess"

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    your check function only needs two arguments, given a start square, the piece is known...

    legal-move arrays are one way to do it, but i find them cumbersome after a while.

    1. is there actually a square there?
    2. is it a square my current piece can move to?
    3. is there another piece in the way to the square? (except knight)
    4. is there another piece on the square (pawn only)



    i started on formula-based move defs, but i have to go to a meeting so i'll leave the knight def and debugging up to you:


    Code:
    function isLegal(strPiece, r1,c1, r2,c2){
    return {
     pawn: (c1==c2 && c2>c1  && (r2-r1) < 3) || (c2==c1+1||c2==c1-1)&&r2==r1+1,
     rook: r1==r2 || c1==c2,
     bishop: r1!=r2 && c1!=c2 && Math.abs(r2-r1)==Math.abs(c2-c1),
     king: [0,1,1][Math.abs(c2-c1)+Math.abs(r2-r1)],
     queen: (r1!=r2 && c1!=c2 && Math.abs(r2-r1)==Math.abs(c2-c1)) || ( r1==r2 || c1==c2)
    
    }[strPiece];
    
    }
    note: these only check move combos, not square occupancy or cliff edges!!!
    Last edited by rnd me; 05-16-2011 at 02:43 PM.

  4. #4
    Join Date
    May 2011
    Posts
    7
    Thanks very much, wbport and rnd_me.

    I am also away from my desk for a few days, but will return shortly to check the above code.
    The Cray document is very informative, and the mathematical rules in the code above is exactly what I need. Further messages will certainly follow, upon testing and debugging.

    On equick question though: do these formulae check for en-passant and castle?

    Best wishes,
    Adrian

  5. #5
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    You're welcome. Being both a chessplayer and programmer when Blitz was being developed in this state, I got to see if from the beginning. My program to proof read/play-over a game only got the arrival square--for Nf3 I had to search all eight squares the knight could have come from. Since you know where the piece came from your procedure won't have to be as complex except to determine if a king is in check.

    You will need to add something for castling, kicked off when the king moves two squares or you find O-O or O-O-O. Check for empty squares between K & R, and is the king safe on e1, f1, and g1 (or e,d, & c for Q-side or 8 for Black).

    For en passent, save the location of any square a pawn moves over when it starts by moving two squares from its initial position to see if it is the target for a pawn capture on the opponent's next move.

    A Forrest Gump of Chess.
    Last edited by wbport; 05-17-2011 at 10:04 AM.

  6. #6
    Join Date
    May 2011
    Posts
    7
    Well, indeed this would be the obvious way to "code" things...
    I just realised, however, that I did not provide the complete link to the already built javascript chess program, here:
    http://javascript.internet.com/games...-cpu-oppo.html

    This little javascript gem not only succesfully verifies the legality of the chess moves, but also proposes chess moves (a little AI). Now, since the problems I am trying to solve is already solved, the obvious springs to my mind: how could I extract the relevant part of the code from that link...?

    Best,
    Adrian

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by dusadrian View Post
    , the obvious springs to my mind: how could I extract the relevant part of the code from that link...?
    javascript is always delivered as text, just view the source and you'll have working source code.

    depending on the code of course, i find it often easier to create than integrate.

  8. #8
    Join Date
    May 2011
    Posts
    7
    Oh, but I already have the code... with the only "little" drawback that is minified (sigh!)
    I am unable to distinguish between the AI part of the code and the part responsible with testing the legality of the moves.

    I was hoping that someone on this forum, familiar with the project, could provide the original source code, or give a hint on how to "unminify" this code. I find it strange to believe that an open-source code, like this JS, is actually unusable simply because it's obfuscated through minification...

    Thanks again, anyways!
    Last edited by dusadrian; 05-23-2011 at 06:27 PM. Reason: spelling error

  9. #9
    Join Date
    May 2011
    Posts
    7
    Oh, but I already have the code... with the only "little" drawback that is minified (sigh!)
    I am unable to distinguish between the AI part of the code and the part responsible with testing the legality of the moves.

    I was hoping that someone on this forum, familiar with the project, could provide the original source code, or give a hint on how to "unminify" this code. I find it strange to believe that an open-source code, like this JS, is actually unusable simply because it's obfuscated through minification...

    Thanks again, anyways!

  10. #10
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    i think the code was for a 5k content back in the day.
    there's a semi-unminified version:http://www.chesskids.com/ckbook/kaspyl10.htm

    pieces are represented by numbers:
    Code:
    white={
    "65":"pawn",
    "66":"knight",
    "67":"bishop",
    "68":"rook",
    "69":"queen",
    "70":"king"
    }
    other variables and functions:
    Code:
    Z: "move", //a function to move  piece to a square
    b: "board", //b is  the board, a 64-length array (local but consistent)
    ki, pa, kn, bi, ro //piece functions
    t     // move calculator: checks for empty destination and clear paths
    i just quickly skimmed the source, i don't fully guarantee the accuracy of any of these...

    copy the code to a new file and just start doing global find/replaces; renaming the variables (case-sensitive EXACT replacements, whole words only), until more and more of it makes sense.

    it's like a crossword puzzle, once you get going, the remainder becomes easier to fill-out.

  11. #11
    Join Date
    May 2011
    Posts
    7
    Ah-haa...!
    I had previously figured that 'pb'/'pw' stands for player black/white, and as you say, the more I figure the less it remains to fill out.
    I also tried to email the original author Neil Pierce, but did not get an answer, unfortunately.
    Will keep hiting the code until I manage to crack it...

  12. #12
    Join Date
    May 2011
    Posts
    7
    Oh, I believe to have found a better code here:
    http://p4wn.sourceforge.net/

    Douglas Bagnall (the author) wrote it for the same 5k contest and even created a sourceforge project for it, releasing the code in the public domain. The main branch (version with "Better play, more readable code...") is simply perfect with readable functions and suitable explanations.

    EDIT: On the same webpage, there's a link to Ron Winter's reversed engineerd version, which is even more self-explanatory:
    http://p4wn.cvs.sourceforge.net/p4wn/chess/misc/ron/

    I am going to use this one.

    Thanks very much everyone,
    Adrian
    Last edited by dusadrian; 05-25-2011 at 02:51 AM. Reason: spelling error

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles