Printing in C

Hello,

My simple C and chess learning continues as I try to print a chessboard on the console. I look forward for helpful replies, but not necessarily a complete solution. Thanks.

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

#include<stdio.h>


void printboard()
{
  
  int i,j;
  
  for( i = 8; i >= 1; --i)
  {
    for(j = 0;j <= 8;++j)
    {
      if( i <= 8)
        putchar('|');
      else  
        putchar('_');
     }
  putchar('\n');
  }
  
}

int main()
	
{
	printboard();
	
	return 0;

}
	
/* Modification needed, to print board as a table 

    A    B    C    D    E    F    G    H
  +----+----+----+----+----+----+----+----+
8 | 56 | 48 | 40 | 32 | 24 | 16 |  8 |  0 |  8th rank
  +----+----+----+----+----+----+----+----+
7 | 57 | 49 | 41 | 33 | 25 | 17 |  9 |  1 |  7th rank
  +----+----+----+----+----+----+----+----+
6 | 58 | 50 | 42 | 34 | 26 | 18 | 10 |  2 |  6th rank
  +----+----+----+----+----+----+----+----+
5 | 59 | 51 | 43 | 35 | 27 | 19 | 11 |  3 |  5th rank
  +----+----+----+----+----+----+----+----+
4 | 60 | 52 | 44 | 36 | 28 | 20 | 12 |  4 |  4th rank
  +----+----+----+----+----+----+----+----+
3 | 61 | 53 | 45 | 37 | 29 | 21 | 13 |  5 |  3rd rank
  +----+----+----+----+----+----+----+----+
2 | 62 | 54 | 46 | 38 | 30 | 22 | 14 |  6 |  2nd rank
  +----+----+----+----+----+----+----+----+
1 | 63 | 55 | 47 | 39 | 31 | 23 | 15 |  7 |  1st rank
  +----+----+----+----+----+----+----+----+
    A    B    C    D    E    F    G    H - file(s)

                                                */
 
Break the problem into smaller pieces.
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
#include <stdio.h>
#include <stdlib.h>

void printline ()
{   printf ("  +----+----+----+----+----+----+----+----+\n");
}

void printheader ()
{   printf ("    A    B    C    D    E    F    G    H    \n");
}

void print_row (int r)
{   int c, n;

    printf ("%d ", r);
    for (c=7; c>=0; c--)
    {   n = c * 8 + r;
        printf ("| %02d ",n);
    }
    printf ("|\n"); 
}
                  
void printboard()
{   int r;
    
    printheader();
    printline();      
    for (r=8; r>=1; --r)
        print_row (r);
    printline();       
}

int main()
{   printboard();
    system ("pause");
	return 0;
}
    A    B    C    D    E    F    G    H
  +----+----+----+----+----+----+----+----+
8 | 64 | 56 | 48 | 40 | 32 | 24 | 16 | 08 |
7 | 63 | 55 | 47 | 39 | 31 | 23 | 15 | 07 |
6 | 62 | 54 | 46 | 38 | 30 | 22 | 14 | 06 |
5 | 61 | 53 | 45 | 37 | 29 | 21 | 13 | 05 |
4 | 60 | 52 | 44 | 36 | 28 | 20 | 12 | 04 |
3 | 59 | 51 | 43 | 35 | 27 | 19 | 11 | 03 |
2 | 58 | 50 | 42 | 34 | 26 | 18 | 10 | 02 |
1 | 57 | 49 | 41 | 33 | 25 | 17 | 09 | 01 |
  +----+----+----+----+----+----+----+----+

The numbering is not what you had, but I'll let you work that out.
Last edited on
How about ranks then? I tried also to print the rank lines, here is what I came up with:

[/code]
#include <stdio.h>

void printline ()
{ printf (" +----+----+----+----+----+----+----+----+\n");
}

void printheader ()
{ printf (" A B C D E F G H \n");
}
void printboard()
{ int file, rank;

printheader();

for ( rank=8; rank>=1;rank-- ) {
printline();

for (file=1; file<=8;file++) {
printf("| ");
}
}
}

int main()
{
printboard();
return 0;
}
[/code]

Getting this output:
    A    B    C    D    E    F    G    H    
  +----+----+----+----+----+----+----+----+
| | | | | | | |   +----+----+----+----+----+----+----+----+
| | | | | | | |   +----+----+----+----+----+----+----+----+
| | | | | | | |   +----+----+----+----+----+----+----+----+
| | | | | | | |   +----+----+----+----+----+----+----+----+
| | | | | | | |   +----+----+----+----+----+----+----+----+
| | | | | | | |   +----+----+----+----+----+----+----+----+
| | | | | | | |   +----+----+----+----+----+----+----+----+



I don't think I'm confusing ranks with files anymore, but still the result is somewhat unexpected.
I gave you a working example.

If you want to add the rank text:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print_rank (int r)
{   printf ("%d", r);
    switch (r)
    {
    case 1: printf ("st");
            break;
    case 2: printf ("nd");
            break;
    case 3: printf ("rd");
            break;
    default: printf ("th");
    }
    printf (" rank\n");
}


BTW, your opening code tag should not have a \.

Hi, that's actually not what I meant.

In your example, there was only 2 ranklines, while I tried to write a loop to do it throughout the board. But it failed for some reason, so I wonder if you know how to do it (I'm only amateur with programming, and also limited skill).
Last edited on
Hi, that's actually not what I meant.

Then I don't know what you meant.

Adding the print_rank function give me the following output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    A    B    C    D    E    F    G    H
  +----+----+----+----+----+----+----+----+
8 | 64 | 56 | 48 | 40 | 32 | 24 | 16 | 08 | 8th rank
  +----+----+----+----+----+----+----+----+
7 | 63 | 55 | 47 | 39 | 31 | 23 | 15 | 07 | 7th rank
  +----+----+----+----+----+----+----+----+
6 | 62 | 54 | 46 | 38 | 30 | 22 | 14 | 06 | 6th rank
  +----+----+----+----+----+----+----+----+
5 | 61 | 53 | 45 | 37 | 29 | 21 | 13 | 05 | 5th rank
  +----+----+----+----+----+----+----+----+
4 | 60 | 52 | 44 | 36 | 28 | 20 | 12 | 04 | 4th rank
  +----+----+----+----+----+----+----+----+
3 | 59 | 51 | 43 | 35 | 27 | 19 | 11 | 03 | 3rd rank
  +----+----+----+----+----+----+----+----+
2 | 58 | 50 | 42 | 34 | 26 | 18 | 10 | 02 | 2nd rank
  +----+----+----+----+----+----+----+----+
1 | 57 | 49 | 41 | 33 | 25 | 17 | 09 | 01 | 1st rank
  +----+----+----+----+----+----+----+----+


In your example, there was only 2 ranklines,

I was very serious when I said break this up into small functions that do one thing.
Missing ranklines between rows? Simply add a call to printline where needed.
1
2
3
4
5
6
7
8
9
10
11
12
void print_row (int r)
{   int c, n;

    printf ("%d ", r);
    for (c=7; c>=0; c--)
    {   n = c * 8 + r;
        printf ("| %02d ",n);
    }
    printf ("| "); 
    print_rank (r);
    printline ();
}

Last edited on
Topic archived. No new replies allowed.