Printing

Hello,

Now I want to print a chessboard to the console. The function takes a board as argument and should print a dot for empty square and a piece char for piece.
but I have some problems.
First the code does not compile so I don't know if it works or not. The programming should also be done in C.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <stdio.h>

struct position {
 int side;
 int board[64];
}; 

void printboard(struct position *p)
	
{
	int rank, file, sq, piece;
	
	char PceChar[] = ".PNBRQK";
	
	for (rank = 8; rank >=1; rank--) {
		
		for (file = 1; file <= 8; file++) {
			piece = p->board[sq];
			printf("%3c", PceChar[piece]);
		}
	
		printf("\n");
	}
}


int main()  

{
   printboard(board);
   return 0;
}
	



Last edited on
but I have some problems.

What problems?
First the code does not compile so I don't know if it works or not.

Thank you for not passing onto us the information that the compiler is giving you about why it won't compile. This is so much more fun when we have to play guessing games.
line 31: error C2065: 'board' : undeclared identifier


It's pretty obvious. You need to declare a variable board before you can pass it to printboard.
printboard(board);
Line 6: Where do you initialize the board?

Line 19: Where is sq initialized? Hint: It's garbage.

370 posts and you have not learned to use code tags?
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
I will not respond further until you apply code tags.



@AbstractionAnon: Sorry, I forgot the code tags. I'm afraid I have some problems that I haven't solved yet.

First, how should I call this print board function?

EDIT: I should initialize the board first? In this case I would prefer a function to do that.
Last edited on
I'm afraid I have some problems that I haven't solved yet.

¿What problems?

First, how should I call this print board function?

Let us take a look at the function prototype void printboard(struct position *p)
It is a void function so that usually implies it goes on a line by itself:
printboard();
It receives a pointer to a struct position variable. I am going to pretend we have one called bob for now.
printboard(&bob); //& gets the address of a variable (which is exactly what a pointer is)
And that is it. Done finished. Complete. With calling the function that is.

On to getting bob to come into existence...
struct position bob;
That is all there is to giving birth to bob.
bob has not learned anything yet in his life though. He is still just a random collection of leftover memory. We need to fill him up with some bits. So if you use a for loop to set every spot of bob.board to '.' you would be in business.
Last edited on
Hi there, I haven't done this stuff much but if I understand right it should be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include <stdio.h>

#define pawn 1
#define knight 2
#define bishop 3
#define rook 4
#define queen 5
#define king 6


struct position {
 int side;
 int board[64];
}; 

void initboard(struct position *p)
	
{
	int i;
	
	for (i = 0; i<64; ++i) {
	p->board[i] = 0;
	p->board[10] = knight;

    }
}
		
	
void printboard(struct position *p)
	
{
	int rank, file, sq, piece;
	sq = 0;
	
	char PceChar[] = ".PNBRQK";
	
	for (rank = 8; rank >=1; rank--) {
		
		for (file = 1; file <= 8; file++) {
			piece = p->board[sq];
			printf("%3c", PceChar[piece]);
		}
	
		printf("\n");
	}
}


int main()  

{
	struct position board;
	initboard(&board);
	printboard(&board);

      return 0;
}
	


Now I'm getting this output:


  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .


Which is obviously not what I want, concerning there is a knight standing on square 10...
Last edited on
Look at line 41. Each time though the loop you want to print a different piece but sq never gets updated, so you're always printing p->board[0]
1
2
line 41: piece = p->board[sq];
line 42: printf("%3c", PceChar[piece]);

The variable sq is always 0, so it always prints the dot.
Lol I tried to define square as a squareIndex = 8*rankIndex + fileIndex but does not work.

But thank you dhayden and Thomas1965, I will keep noting if I make any progress.
Last edited on
does not work

That doesn't tell us anything useful. What does that mean? Didn't compile? Crashed? Stole your girlfriend and burnt down your house?

Come on, you've been here long enough to know this. Give us information, and we can help you.
Last edited on
I tried to define square as a squareIndex = 8*rankIndex + fileIndex but does not work.

Did you do that in the innermost for loop? Or when the variable is declared?
Inside the for loop it would change and produce the index for every spot on the board since rankIndex and fileIndex would be changing.
If you did it when the variable was declared outside of the for loops, then that just happens once and it will never change.

What Thomas1965 was trying to get you to see is you need something to change sq inside the loop. I recommend simply incrementing sq inside the loop like ++sq;
I finally found the problem! My rank and file indexes were wrong. What does 8*8 +8 evaluate? Out of bounds ;)

Here is the code, now working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include <stdio.h>

#define pawn 1
#define knight 2
#define bishop 3
#define rook 4
#define queen 5
#define king 6


struct position {
 int side;
 int board[64];
}; 

void initboard(struct position *p)
	
{
	int i;
	
	for (i = 0; i<64; ++i) {
	p->board[i] = 0;
	p->board[10] = knight;

    }
}
		
	
void printboard(struct position *p)
	
{
	int rank, file, sq, piece;
	
	
	char PceChar[] = ".PNBRQK";
	
	for (rank = 7; rank >=0; rank--) {      
		
		for (file = 0; file <= 7; file++) {
			sq = 8*rank+file;
			piece = p->board[sq];
			printf("%3c", PceChar[piece]);
		}
	
		printf("\n");
	}
}


int main()  

{
	struct position board;
	initboard(&board);
	printboard(&board);
    return 0;
}


Output:


  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  .  N  .  .  .  .  .
  .  .  .  .  .  .  .  .
Last edited on
Topic archived. No new replies allowed.