Need help with the simple chess program in C

A

asif929

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
 
N

Nelu

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

santosh

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++.
 
J

Jack Klein

(e-mail address removed) 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.
 
C

CBFalconer

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

Nelu

Jack said:
(e-mail address removed) 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.
 
R

Richard Heathfield

(e-mail address removed) 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.
 
B

Ben Bacarisse

I have been trying to create this chess program from many hours and
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.
 
P

patrik.kahari

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
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address removed) 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.
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield <[email protected]> writes:

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

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

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

asif929

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top