473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Chess

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

Apr 1 '06 #1
11 2544
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
Apr 1 '06 #2

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

Apr 1 '06 #3

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


Apr 1 '06 #4
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
es*****@acm-dot-org.invalid
Apr 1 '06 #5
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

Apr 1 '06 #6
"Gregc." <gr*********@bigpond.com> 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) ks***@mib.org <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.
Apr 1 '06 #7
Sorry.

Apr 1 '06 #8
"Gregc." <gr*********@bigpond.com> writes:
Sorry.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Apr 1 '06 #9

Keith Thompson wrote:
"Gregc." <gr*********@bigpond.com> 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) ks***@mib.org <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.


Apr 1 '06 #10
"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....
Apr 1 '06 #11
On 31 Mar 2006 15:51:42 -0800, "Gregc." <gr*********@bigpond.com>
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 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Will McGugan | last post by:
Hi folks, I've written a Python chess module that does the following. * Reads / Writes PGN files * Write FEN files * Validates moves * Lists legal moves * Detects check / mate / stalemate /...
5
by: derian | last post by:
Do you guys know if there is a program that can generate chess moves? I recently wrote a chess game but its only 2 player... I deceided it was too difficult to come up with the logic for the AI...
5
by: Paolo Pantaleo | last post by:
Well Python is not a good language for writing a chess engine (even if a chess engine exists: http://www.kolumbus.fi/jyrki.alakuijala/pychess.html), but it could be grat for chess interfaces, for...
1
by: Varun Hiremath | last post by:
Hello, I have written a chess client using python which is a graphic interface to play chess. It is at present a two player version, players move their peices by clicking two squares on the board...
13
by: asif929 | last post by:
I have been trying to create this chess program from many hours and still couldn't figure out how to complete it. After consume all these efforts i come to this google groups for the first time for...
63
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html...
2
by: CoreyWhite | last post by:
When playing games, perhaps the most simple is tic-tac-toe. The game has two simple strategies, one is defensive and the other offensive. It is not hard at first to learn how to tie games when...
10
by: sam_cit | last post by:
Hi Everyone, I'm working on developing a chess game and i decided to use c++ for its object oriented approach. I have a bass class unit and is inherited to distinct number of units (like king,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.