By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Need help with the simple chess program in C

P: n/a
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 help. i
would appreciate if somebody help me out of this to debug the program
below. The chess board suppose to be printed with "*" and space " " as
you can see below as an example. Again I would appreciate for your help
in advance.

sample print or output:

***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
#include <iostream>

using namespace std;

int main()
{
for (int row = 1; row < 9; row++)// Draw big row 8 times
{
for (int row1 = 1; row1 < 4; row1++) //Draw a little row 3 times
{
for (int col = 1; col < 9; col++) //Draw a big column 8 times
{
for (int col1 = 1; col1 < 6; col1++) //Draw a character 5
times
{
if (col % 2 == 0 && row % 2 == 0)
{
cout <<' ';
}
else
{
cout << '*';
}
if(col1%2 == 0 && row1 % 2 ==0)
{
cout <<' ';
}
else
{
cout << '*';
}
}

}
}
}

return 0;
} // function main

Nov 17 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
as*****@hotmail.com wrote:
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 help.
This is not google groups. It's NNTP. Google offers an interface
to NNTP newsgroups along with the interface to Google Groups. I
think they actually tell you that this is not Google Groups.

This is also comp.lang.c, if you don't have questions related to
the C language try a different group. So, the code you provide
should be standard ISO C.
i
would appreciate if somebody help me out of this to debug the program
below. The chess board suppose to be printed with "*" and space " " as
you can see below as an example. Again I would appreciate for your help
in advance.

sample print or output:

***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
#include <iostream>
This is not a standard header in C.
>
using namespace std;
This fails to compile.
>
int main()
int main(void) in C.
{
for (int row = 1; row < 9; row++)// Draw big row 8 times
You can't declare row like this in C. There are some compilers
that would allow you to do this as an extension (gcc does, I
think) but it's not standard C.
{
for (int row1 = 1; row1 < 4; row1++) //Draw a little row 3 times
{
for (int col = 1; col < 9; col++) //Draw a big column 8 times
{
for (int col1 = 1; col1 < 6; col1++) //Draw a character 5
times
{
if (col % 2 == 0 && row % 2 == 0)
{
cout <<' ';
}
else
{
cout << '*';
}
if(col1%2 == 0 && row1 % 2 ==0)
{
cout <<' ';
}
else
{
cout << '*';
}
}

}
}
}

return 0;
} // function main
I think there's a mistake every two lines but I'll stop before I
get to be too annoying :-). Your code is not C, it's C++. For C++
code try comp.lang.c++, or, if you want to solve that problem in
C, write C code and come back here for help.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Nov 17 '06 #2

P: n/a
as*****@hotmail.com wrote:
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 help. i
would appreciate if somebody help me out of this to debug the program
below.
<snip C++ code>

This group, as you can see by it's name, deals with the C language.
Your program is in C++. Ergo, post to comp.lang.c++ or some other group
that deals with C++.

Nov 17 '06 #3

P: n/a
On Thu, 16 Nov 2006 22:54:42 -0500, Nelu <sp*******@gmail.comwrote
in comp.lang.c:
as*****@hotmail.com wrote:
[snip]
{
for (int row = 1; row < 9; row++)// Draw big row 8 times

You can't declare row like this in C. There are some compilers
that would allow you to do this as an extension (gcc does, I
think) but it's not standard C.
Actually it is. And has been since October 1999, more than 7 years
now.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 17 '06 #4

P: n/a
as*****@hotmail.com wrote:
>
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 help. i
would appreciate if somebody help me out of this to debug the program
below. The chess board suppose to be printed with "*" and space " " as
you can see below as an example. Again I would appreciate for your help
in advance.

sample print or output:

***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****

#include <iostream>
comp.lang.c++ is thataway --->

You have not come to google groups, you have come to the usenet
news system, to which google is only an extremely poor interface.
If you try, you can get some use out of it, but you would be better
advised to get a real newsreader.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Nov 17 '06 #5

P: n/a
Jack Klein wrote:
On Thu, 16 Nov 2006 22:54:42 -0500, Nelu <sp*******@gmail.comwrote
in comp.lang.c:
>as*****@hotmail.com wrote:

[snip]
>>{
for (int row = 1; row < 9; row++)// Draw big row 8 times
You can't declare row like this in C. There are some compilers
that would allow you to do this as an extension (gcc does, I
think) but it's not standard C.

Actually it is. And has been since October 1999, more than 7 years
now.
I take that back. You're right. I actually read about it on the
gcc mailing list a while (few years) back, but I forgot.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Nov 17 '06 #6

P: n/a
as*****@hotmail.com said:

[Subject: Need help with the simple chess program in C]
I have been trying to create this chess program from many hours and
still couldn't figure out how to complete it.
That's probably because you were using the wrong language.
After consume all these
efforts i come to this google groups for the first time for help.
No, you came to Usenet. You came /through/ Google Groups. There are plenty
of doors into this place, and Google Groups is just one of them.
i
would appreciate if somebody help me out of this to debug the program
below. The chess board suppose to be printed with "*" and space " " as
you can see below as an example. Again I would appreciate for your help
in advance.

sample print or output:

***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
Okay, your first debugging step is to throw away all your code, because it
is not written in the language you require.

Step 2 is to write a function that displays a row of N characters, all the
same:

#include <stdio.h>

int print_repeated_char(FILE *fp, int c, int n)
{
int i;
for(i = 0; i < n; i++)
{
putc(c, fp);
}
return ferror(fp);
}

Next, you want to be able to print a complete line of the chessboard:

int print_chess_row(FILE *fp,
int sqh, /* how many lines per square (3 in your eg) */
int sqw, /* how many columns per square (5 in eg) */
int black,/* character to use for black */
int white,/* character to use for white */
int rowlen, /* 8 for normal chess! */
int startwithwhite /* 0 = start with black */
)
{
int r = 0; /* row */
int t = 0; /* temp for colour */
int c = 0; /* column */

for(r = 0; r < sqh; r++)
{
t = startwithwhite ? white : black;
for(c = 0; c < rowlen; c++)
{
print_repeated_char(fp, t, sqw);
t = (t == white) ? black : white;
}
putc('\n', fp);
}
return ferror(fp);
}

Now it's time to write a quick test driver.

int main(void)
{
int i;
for(i = 0; i < 8; i++)
{
print_chess_row(stdout, 3, 5, '*', '_', 8, i % 2);
}
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 17 '06 #7

P: n/a
as*****@hotmail.com writes:
I have been trying to create this chess program from many hours and
<snip>
sample print or output:

***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
Another way to look at it is that one is mapping a {0,1} valued
function over a 2D array of pixels:

#include <stdio.h>

int main(void)
{
const int lines_per_row = 3;
const int chars_per_col = 5;
int l, c;

for (l = 0; l < 8 * lines_per_row; l++) {
for (c = 0; c < 8 * chars_per_col; c++)
putchar(((l / lines_per_row) + (c / chars_per_col)) % 2 ? ' ' : '*');
putchar('\n');
}

return 0;
}

I don't think this has any advantages other being open interesting
variations.

--
Ben.
Nov 17 '06 #9

P: n/a
Yes, this is the wrong group but i have a suggestion for you... If you
are going to write a chess program that does anything more than display
a board, you probably want to read up on minmax tree algorithms for the
computer AI. They dont have to be that hard to code and still be
effective. I have been playing chess for a while but my simple chess
program still beats me. Im not sure if thats a good or bad thing. Good
luck with your program.

Regars Patrik

Nov 17 '06 #10

P: n/a
Richard Heathfield <in*****@invalid.invalidwrites:
as*****@hotmail.com said:

[Subject: Need help with the simple chess program in C]
>I have been trying to create this chess program from many hours and
still couldn't figure out how to complete it.

That's probably because you were using the wrong language.
>After consume all these
efforts i come to this google groups for the first time for help.

No, you came to Usenet. You came /through/ Google Groups. There are plenty
of doors into this place, and Google Groups is just one of them.
[...]
Okay, your first debugging step is to throw away all your code, because it
is not written in the language you require.
Either that, or you need to ask about it in a different newsgroup.
Nobody will object if you choose to implement it in C++; we just can't
help you with C++ here.

--
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.
Nov 17 '06 #11

P: n/a
Keith Thompson said:
Richard Heathfield <in*****@invalid.invalidwrites:
<snip>
>
>Okay, your first debugging step is to throw away all your code, because
it is not written in the language you require.

Either that, or you need to ask about it in a different newsgroup.
Nobody will object if you choose to implement it in C++; we just can't
help you with C++ here.
He specifically wants to write it in C. It says so in the Subject line.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 17 '06 #12

P: n/a
Richard Heathfield <in*****@invalid.invalidwrites:
Keith Thompson said:
>Richard Heathfield <in*****@invalid.invalidwrites:
<snip>
>>
>>Okay, your first debugging step is to throw away all your code, because
it is not written in the language you require.

Either that, or you need to ask about it in a different newsgroup.
Nobody will object if you choose to implement it in C++; we just can't
help you with C++ here.

He specifically wants to write it in C. It says so in the Subject line.
He specifically wants to write it in C++. It says so in the code
fragment he posted. We know they can't both be right, but we can't
tell which.

--
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.
Nov 17 '06 #13

P: n/a
Thanks for your response and for correction that the program is not C.
Its a silly of me for posting the question in a wrong group. But, i
still appreciate for the prompt response.

Nelu wrote:
as*****@hotmail.com wrote:
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 help.

This is not google groups. It's NNTP. Google offers an interface
to NNTP newsgroups along with the interface to Google Groups. I
think they actually tell you that this is not Google Groups.

This is also comp.lang.c, if you don't have questions related to
the C language try a different group. So, the code you provide
should be standard ISO C.
i
would appreciate if somebody help me out of this to debug the program
below. The chess board suppose to be printed with "*" and space " " as
you can see below as an example. Again I would appreciate for your help
in advance.

sample print or output:

***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
#include <iostream>

This is not a standard header in C.

using namespace std;

This fails to compile.

int main()

int main(void) in C.
{
for (int row = 1; row < 9; row++)// Draw big row 8 times

You can't declare row like this in C. There are some compilers
that would allow you to do this as an extension (gcc does, I
think) but it's not standard C.
{
for (int row1 = 1; row1 < 4; row1++) //Draw a little row 3 times
{
for (int col = 1; col < 9; col++) //Draw a big column 8 times
{
for (int col1 = 1; col1 < 6; col1++) //Draw a character 5
times
{
if (col % 2 == 0 && row % 2 == 0)
{
cout <<' ';
}
else
{
cout << '*';
}
if(col1%2 == 0 && row1 % 2 ==0)
{
cout <<' ';
}
else
{
cout << '*';
}
}

}
}
}

return 0;
} // function main

I think there's a mistake every two lines but I'll stop before I
get to be too annoying :-). Your code is not C, it's C++. For C++
code try comp.lang.c++, or, if you want to solve that problem in
C, write C code and come back here for help.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Nov 17 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.