Chess

G

Gregc.

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
 
P

pete

Gregc. said:
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 */
 
S

Stephen Dedalus

Gregc. said:
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).
 
S

Stephen Dedalus

Stephen said:
There are several things wrong with this.

You basically need to check that ((krow == qrow ) || (kcol == krow)),

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

Eric Sosman

Gregc. said:
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?
 
G

Gregc.

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
 
K

Keith Thompson

Gregc. said:
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/>.
 
O

osmium

Eric Sosman said:
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....
 
A

adak_dave

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top