Valid move checking in chess

This is a discussion on Valid move checking in chess within the Game Programming forums, part of the General Programming Boards category; I'm writing a chess game, to be played over a socket. In my eyes, a function that made a chess ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    Valid move checking in chess

    I'm writing a chess game, to be played over
    a socket. In my eyes, a function that made
    a chess move would have to do something
    about like this:


    int move(int old_r, int old_c, int new_r, int new_c)
    {
    if (checkMov(old_r, old_c, new_r, new_c)) {
    board[new_r][new_c] = board[old_r][old_c];
    board[old_r][old_c] = 0;
    }
    else printf("Invalid move.\n");

    return 0;
    }


    My question is, how would a function that
    examines if a move is valid work? I'm thinking that I could use a bunch of nested
    conditionals, but that would be kind of nasty.
    Does anyone else have a better way?

    Thanks in Advance,
    Transmitt

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    as a matter of fact, I do (: This might not be too clear to you as I explain this, but if it doesn't make sense, let me know and I'll clarify it...instead of having rows and columns, number the squares 1-64...now, you can use modulus (%) to help you out a little...say you are moving the rook towards the other side of the board...from square 1 to square 9.

    1%8=1
    9%8=1

    all you have to check is that the % is the same when moving the rook that direction. Otherwise, you just add or subtract from the square number, and make sure it is in the same row...which could be done with if statements, if you would like...or you can do that with % too, a little differently, if you can't figure it out, ask (: Now, to move something like a knight, you can do it like this...

    space - 16, space + 16, space + 18, space - 18, and 4 others, but you should get the point...then you check to make sure that it's in the proper row...it's actually kinda tough. Maybe you should ignore this post. Maybe I would think better earlier in the day...I'll think about your problem and post again later, probably (:
    Away.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    most likely your gonna end up with a lot of nested if's,
    especially with checking particular piece moves.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I just happened to stumble across this, and I remembered your post...anyway, the bitboard looks intruiging if you think you are up to it.

    http://www.aihorizon.com/essays/chessai/boardrep.htm
    Away.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    ...and about 10 seconds after posting that, I stumbled across this.

    http://www.aihorizon.com/essays/chessai/index.htm
    Away.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    Thanks

    Thanks for the help, Blackrat.
    I really appreciate it. =)

    - transmitt

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    well, it's an interesting topic, at least. I hope you get it working.
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe comp move
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2009, 09:36 AM
  2. Read .txt file into 2D array.
    By crazygopedder in forum C Programming
    Replies: 11
    Last Post: 10-21-2008, 09:42 PM
  3. Tic Tac Toe Comp Move help
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2008, 12:05 PM
  4. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  5. Replies: 5
    Last Post: 09-08-2001, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21