Chess

Discussion in 'C Programming' started by Gregc., Apr 1, 2006.

  1. Gregc.

    Gregc. Guest

    G'day

    I am writing a chess program. Here is my code:

    #include <stdio.h>
    #include <stdlib.h>

    bool isInCheck (int krow, int kcol, int qrow, int qcol)
    {
    double check;
    double cal1;
    double cal2;
    cal1 = abs(krow-qrow);
    cal2 = abs(kcol-qcol);
    check=(krow=qrow)||(kcol=qcol);||((krow-qrow))=(kcol-qcol));
    return 0;

    }


    int main() {
    int score = 0;
    // Check row for check
    if (isInCheck(0,4,0,2) ) {
    printf("Correct horizontally\n");
    score++;
    }
    else {
    printf("Incorrect horizontally\n");
    }
    // Check column for check
    if (isInCheck(0,4,4,4) ) {
    printf("Correct diagonally\n");
    score++;
    }
    else {
    printf("Incorrect vertically\n");
    }
    // Check diagonal for check
    if (isInCheck (0,4,3,1) ) {
    printf("Correct diagonally\n");
    score++;
    }
    else {
    printf("Incorrect diagonally\n");
    }
    // Check that King is not in check
    if (!isInCheck (0,4,1,0) ) {
    printf("Correct for no check\n");
    score++;
    }
    else {
    printf("Incorrect for no check\n");
    }

    // Output Score
    printf("\n");
    printf("Score: %i%%\n", score*100/4);

    }

    My output is:

    Incorrect horizontally
    Incorrect vertically
    Incorrect diagonally
    Correct for no check

    Score: 25%

    I am trying to simply the isInCheck function. Would you guys have any
    suggestions.

    Thanks

    Greg
    Gregc., Apr 1, 2006
    #1
    1. Advertising

  2. Gregc.

    pete Guest

    Gregc. wrote:
    >
    > G'day
    >
    > I am writing a chess program. Here is my code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > bool isInCheck (int krow, int kcol, int qrow, int qcol)
    > {
    > double check;
    > double cal1;
    > double cal2;
    > cal1 = abs(krow-qrow);
    > cal2 = abs(kcol-qcol);
    > check=(krow=qrow)||(kcol=qcol);||((krow-qrow))=(kcol-qcol));
    > return 0;
    >
    > }
    >
    > int main() {
    > int score = 0;
    > // Check row for check
    > if (isInCheck(0,4,0,2) ) {
    > printf("Correct horizontally\n");
    > score++;
    > }
    > else {
    > printf("Incorrect horizontally\n");
    > }
    > // Check column for check
    > if (isInCheck(0,4,4,4) ) {
    > printf("Correct diagonally\n");
    > score++;
    > }
    > else {
    > printf("Incorrect vertically\n");
    > }
    > // Check diagonal for check
    > if (isInCheck (0,4,3,1) ) {
    > printf("Correct diagonally\n");
    > score++;
    > }
    > else {
    > printf("Incorrect diagonally\n");
    > }
    > // Check that King is not in check
    > if (!isInCheck (0,4,1,0) ) {
    > printf("Correct for no check\n");
    > score++;
    > }
    > else {
    > printf("Incorrect for no check\n");
    > }
    >
    > // Output Score
    > printf("\n");
    > printf("Score: %i%%\n", score*100/4);
    >
    > }
    >
    > My output is:
    >
    > Incorrect horizontally
    > Incorrect vertically
    > Incorrect diagonally
    > Correct for no check
    >
    > Score: 25%
    >
    > I am trying to simply the isInCheck function.
    > Would you guys have any
    > suggestions.


    /* BEGIN new.c */

    #include <stdio.h>

    int isInCheck (int krow, int kcol, int qrow, int qcol)
    {
    return
    krow == qrow
    || kcol == qcol
    || krow + kcol == qrow + qcol
    || krow - kcol == qrow - qcol;
    }

    int main(void)
    {
    int score = 0;

    if (isInCheck(0,4,0,2)) {
    puts("Correct horizontally");
    ++score;
    } else {
    puts("Incorrect horizontally");
    }
    if (isInCheck(0,4,4,4)) {
    puts("Correct diagonally");
    ++score;
    } else {
    puts("Incorrect vertically");
    }
    if (isInCheck (0,4,3,1)) {
    puts("Correct diagonally");
    ++score;
    } else {
    puts("Incorrect diagonally");
    }
    if (!isInCheck (0,4,1,0)) {
    puts("Correct for no check");
    ++score;
    } else {
    puts("Incorrect for no check");
    }
    printf("\nScore: %i%%\n", score * 100 / 4);
    return 0;
    }

    /* END new.c */


    --
    pete
    pete, Apr 1, 2006
    #2
    1. Advertising

  3. Gregc. wrote:
    > G'day
    >
    > I am writing a chess program. Here is my code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > bool isInCheck (int krow, int kcol, int qrow, int qcol)
    > {
    > double check;
    > double cal1;
    > double cal2;
    > cal1 = abs(krow-qrow);
    > cal2 = abs(kcol-qcol);
    > check=(krow=qrow)||(kcol=qcol);||((krow-qrow))=(kcol-qcol));
    > return 0;
    >
    > }



    There are several things wrong with this.

    You basically need to check that ((krow == qrow ) || (kcol == krow)),
    horizontal or verticle check,
    or ((krow + qrow) == (kcol + qcol))

    or ((kcol + krow) == (qcol + qrow)) the last two being diagonal check.


    I'm sure it's not beyond you to make an efficient condition statement
    out of these.

    > int main() {
    > int score = 0;
    > // Check row for check
    > if (isInCheck(0,4,0,2) ) {
    > printf("Correct horizontally\n");
    > score++;
    > }
    > else {
    > printf("Incorrect horizontally\n");
    > }
    > // Check column for check
    > if (isInCheck(0,4,4,4) ) {
    > printf("Correct diagonally\n");
    > score++;
    > }


    (0,4) and (4,4) are not diagonal. Try (0,4) and (4,0).
    Stephen Dedalus, Apr 1, 2006
    #3
  4. Stephen Dedalus wrote:
    > Gregc. wrote:
    > > G'day
    > >
    > > I am writing a chess program. Here is my code:
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > >
    > > bool isInCheck (int krow, int kcol, int qrow, int qcol)
    > > {
    > > double check;
    > > double cal1;
    > > double cal2;
    > > cal1 = abs(krow-qrow);
    > > cal2 = abs(kcol-qcol);
    > > check=(krow=qrow)||(kcol=qcol);||((krow-qrow))=(kcol-qcol));
    > > return 0;
    > >
    > > }

    >
    >
    > There are several things wrong with this.
    >
    > You basically need to check that ((krow == qrow ) || (kcol == krow)),


    I meant ((krow == qrow ) || (kcol == qcol)) here.

    > horizontal or verticle check,
    > or ((krow + qrow) == (kcol + qcol))
    >
    > or ((kcol + krow) == (qcol + qrow)) the last two being diagonal check.
    >
    >
    > I'm sure it's not beyond you to make an efficient condition statement
    > out of these.
    >
    > > int main() {
    > > int score = 0;
    > > // Check row for check
    > > if (isInCheck(0,4,0,2) ) {
    > > printf("Correct horizontally\n");
    > > score++;
    > > }
    > > else {
    > > printf("Incorrect horizontally\n");
    > > }
    > > // Check column for check
    > > if (isInCheck(0,4,4,4) ) {
    > > printf("Correct diagonally\n");
    > > score++;
    > > }

    >
    > (0,4) and (4,4) are not diagonal. Try (0,4) and (4,0).
    Stephen Dedalus, Apr 1, 2006
    #4
  5. Gregc.

    Eric Sosman Guest

    Gregc. wrote:
    > G'day
    >
    > I am writing a chess program. Here is my code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > bool isInCheck (int krow, int kcol, int qrow, int qcol)
    > {
    > double check;
    > double cal1;
    > double cal2;
    > cal1 = abs(krow-qrow);
    > cal2 = abs(kcol-qcol);
    > check=(krow=qrow)||(kcol=qcol);||((krow-qrow))=(kcol-qcol));
    > return 0;
    >
    > }


    You claim this code executes and produces incorrect
    results, but I do not believe you. I do not believe this
    code produces any result at all, correct or incorrect. If
    your compiler fails to produce at least one error message
    for this code, you should return it whence you got it and
    demand your money back; the compiler is broken beyond easy
    repair.

    Listen, Gregc, it is stupid Stupid STUPID to ask for
    help with one batch of code while showing us another.
    When you have a fever and the doctor takes your temperature,
    do you hide an ice cube in your mouth? When your car makes
    awful Ker-Chang!whizzzzz noises, do you take your neigbor's
    car to the mechanic for diagnosis? Ah, well -- at least
    this way you maintain your privacy, or something like that.

    I won't comment on the errors in your code, because what
    you've shown is obviously not your code. However, on the
    off-chance that it has some slight resemblance to your code,
    I'll ask two questions that you might want to think about:

    - Do you know the difference between the `=' and `=='
    operators? If so, how can you demonstrate your
    knowledge? If not, do you think it might be a good
    idea to learn?

    - If the White King is at a3 and a Black Queen is at
    a5, then the two are in the same column. Is the White
    King in check? Would your answer be influenced in any
    way if I were to tell you whether a4 is occupied, and
    by what?

    --
    Eric Sosman
    lid
    Eric Sosman, Apr 1, 2006
    #5
  6. Gregc.

    Gregc. Guest

    I didn't say it produces incorrect results, what I said is "I am trying
    to simply the isInCheck function. Would you guys have any
    suggestions."

    I suggest that you read all of what I said B4 you make your comments.
    I am learning this program (ie have been using this program for 5
    weeks), and I am open to any constructive criticism.

    Greg
    Gregc., Apr 1, 2006
    #6
  7. "Gregc." <> writes:
    > I didn't say it produces incorrect results, what I said is "I am trying
    > to simply the isInCheck function. Would you guys have any
    > suggestions."
    >
    > I suggest that you read all of what I said B4 you make your comments.
    > I am learning this program (ie have been using this program for 5
    > weeks), and I am open to any constructive criticism.


    Eric gave you constructive criticism. If it seemed a bit harsh, it's
    because we see entirely too many people making the same mistakes.

    You're trying to simplify the function, but the function you showed us
    won't even compile. It doesn't need simplification, it needs fixing.

    If at all possible, post code that actually compiles. If the problem
    is that you can't get it to compile, you need to say so, and you need
    to show us the error messages your compiler gave you. Don't post an
    approximation of your code; copy-and-paste the *exact* code you fed to
    your compiler.

    We're not telling you this because we enjoy ordering people around.
    We're telling you this because if you don't follow this advice, we
    just won't be able to help you.

    And please read <http://cfaj.freeshell.org/google/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 1, 2006
    #7
  8. Gregc.

    Gregc. Guest

    Sorry.
    Gregc., Apr 1, 2006
    #8
  9. "Gregc." <> writes:
    > Sorry.


    Really, read <http://cfaj.freeshell.org/google/>. Really.
    Not kidding.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 1, 2006
    #9
  10. Gregc.

    Gregc. Guest

    Keith Thompson wrote:
    > "Gregc." <> writes:
    > > Sorry.

    >
    > Really, read <http://cfaj.freeshell.org/google/>. Really.
    > Not kidding.

    Never knew you could do the edit bit.


    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.
    Gregc., Apr 1, 2006
    #10
  11. Gregc.

    osmium Guest

    "Eric Sosman" writes:

    > You claim this code executes and produces incorrect
    > results, but I do not believe you. I do not believe this
    > code produces any result at all, correct or incorrect. If
    > your compiler fails to produce at least one error message
    > for this code, you should return it whence you got it and
    > demand your money back; the compiler is broken beyond easy
    > repair.


    I'm glad someone finally pointed out that the king has no clothes. I was
    astonished to see the responses to his thread last night. AFAIK he was
    trying to [simplify] something that didn't work. My response was, Oh
    well....
    osmium, Apr 1, 2006
    #11
  12. Gregc.

    Guest

    On 31 Mar 2006 15:51:42 -0800, "Gregc." <>
    wrote:

    >G'day
    >
    >I am writing a chess program. Here is my code:
    >
    >
    >I am trying to simply the isInCheck function. Would you guys have any
    >suggestions.
    >
    >Thanks
    >
    >Greg


    I have a simple chess program, and for my incheck() I used the "super
    piece" idea.

    You imagine the king you're verifying, as a super piece, capable of
    every type of move (pawn, knight, bishop, rook, or king). The Queen
    you don't have to worry about because it's just a combo of the rook
    and the bishop.

    Now simulate the moves of the king, as these pieces would move. If the
    move results in the king capturing (landing on the same square as) an
    enemy's piece of the same type, then you're king is in check.

    For example, if I'm simulating the moves of a knight, and I capture an
    enemy knight, then that knight has me in check. If I capture an
    enemies pawn, while simulating the moves of the knight, then I'm OK,
    keep checking.

    Other techniques are used, certainly. One of them just simulates
    switching sides w/o making a move. Then, if the enemies pieces have a
    move in the move list that would capture your king, you know you're in
    check, and return.

    Another way is to look at the history of the moves as they're made. If
    an enemy piece didn't just move, it can't possibly be giving you check
    except by discovered check. So all you need to check are the piece
    that moved, and the possibility of a discovered check by pieces beyond
    the piece that moved.

    In my opinion, you'll write 3 chess programs before you write your
    real chess program:

    #1) Make it adhere to ALL the rules of chess! Not too easy.

    #2) Second program will give it the add on's: better looks and feel,
    an opening book, using endgame databases, etc., and fix #1! :)

    #3) You start really working on the engine to strengthen the play.


    Simplifying isn't a high priority, although clarity is GREATLY sought
    after.

    If you'd like to get on a forum with a lot of the best chess
    programmers on it, go to: www.talkchess.com and sign up. It's free and
    moderated, and has many of the top professional and amateur chess
    programmer's in the world on it. (and that doesn't include me, trust
    me!) Rybka, Shredder, Fruit, Hydra, Rebel, Zappa (the current world's
    champ, until Rybka (probably), out fights it, if possible), are
    members, and some (like prof. Hyatt, author of Crafty and co-author of
    Cray Blitz), are very active.


    Perhaps you could write up several functions like yours, one for each
    piece that needs to be checked, and then AFTER you got it to work, see
    how you could simplify those functions, into one function with minimal
    code used.


    Dave
    , May 4, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nick
    Replies:
    1
    Views:
    442
    bugbear
    Nov 19, 2004
  2. Tidds

    chess board

    Tidds, Nov 10, 2003, in forum: HTML
    Replies:
    18
    Views:
    8,595
    Greg Griffiths
    Nov 25, 2003
  3. Paul Franklin

    Who's for chess?

    Paul Franklin, Feb 16, 2004, in forum: C++
    Replies:
    1
    Views:
    398
    Thomas Matthews
    Feb 16, 2004
  4. Michael Loggel

    Who succeeded in compiling Crafty Chess source code?

    Michael Loggel, Dec 6, 2003, in forum: C Programming
    Replies:
    4
    Views:
    690
  5. Varun Hiremath
    Replies:
    1
    Views:
    459
    Will McGugan
    Sep 18, 2006
Loading...

Share This Page