Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Chess

Reply
Thread Tools

Chess

 
 
Gregc.
Guest
Posts: n/a
 
      03-31-2006
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

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      04-01-2006
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
 
Reply With Quote
 
 
 
 
Stephen Dedalus
Guest
Posts: n/a
 
      04-01-2006

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

 
Reply With Quote
 
Stephen Dedalus
Guest
Posts: n/a
 
      04-01-2006

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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-01-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Gregc.
Guest
Posts: n/a
 
      04-01-2006
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-01-2006
"Gregc." <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Gregc.
Guest
Posts: n/a
 
      04-01-2006
Sorry.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-01-2006
"Gregc." <(E-Mail Removed)> writes:
> Sorry.


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

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Gregc.
Guest
Posts: n/a
 
      04-01-2006

Keith Thompson wrote:
> "Gregc." <(E-Mail Removed)> 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) (E-Mail Removed) <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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interfacing my chess client with GNU chess using python Varun Hiremath Python 1 09-18-2006 05:24 PM
battle chess...needed.. Zippo Computer Support 17 01-10-2005 03:25 AM
use Java to write Chiness Chess? nick Java 1 11-19-2004 10:50 AM
Chess lovers NBN Computer Support 1 09-23-2004 04:00 AM
chess board Tidds HTML 18 11-25-2003 11:48 PM



Advertisments