Drawing a chess table

Hi. I want to make a program which draws a chess table.
This table should not contain white and black fields. The fields should be made of spaces and simbols.
The number of spaces and simbols is in total 64, so 32 spaces and 32 simbols.
By this, I donĀ“t mean just 32 spaces and 32 simbols.
The input format looks like this:
2 #
0
In this case, my chess field should contain spaces and "$"'s grouped in groups of two.
(The program finishes with exit code 0.)
The output should look like this:
|----------------|
| ## ## ## ##|
| ## ## ## ##|
|## ## ## ## |
|## ## ## ## |
| ## ## ## ##|
| ## ## ## ##|
|## ## ## ## |
|## ## ## ## |
| ## ## ## ##|
| ## ## ## ##|
|## ## ## ## |
|## ## ## ## |
| ## ## ## ##|
| ## ## ## ##|
|## ## ## ## |
|## ## ## ## |
|----------------|
I am intending to do the borders at the end of the construction of my program.
My problem is, that my code does not truely work.
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
#include <iostream>
using namespace std;

int main(){
    int groups;
    char simbol;
    
    bool black=false;
    bool white=true;
    
    while(1){
        
    std::cin>>groups;
    
    if(groups==0){break;}else{
        
    std::cin>>simbol;
    
    for(int a=0;a<32;a++){
        //whitefirst
        if(white=true){
        for(int v=0;v<groups;v++){
            cout<<" ";
        }
        for(int r=0;r<groups;r++){
            cout<<simbol;
        }
        }
        //blackfirst
        if(black=true){
            for(int r=0;r<groups;r++){
            cout<<simbol;
            for(int v=0;v<groups;v++){
            cout<<" ";
        }
        }
        }
        //linebreak and beginning change
        if(a%4==0){
            cout<<"\n";
            if(white=true){
                white=false;
                black=true;
            }
            if(black=true){
                black=false;    
                white=true;
            }
        }
    }
    }
    }
    return 0;
}

Someaone knows which mistake I made?
Thanks
Last edited on
The mistake was to try to code it without planning.

Get yourself a piece of graph paper and a pencil (or a fixed-width text editor) and draw the final result. (Congratulations, you have already done that!)

Next, look at it line by line and figure out how to produce each line.
The first line, for example, is:

1
2
3
4
5
  //  first line
  cout << '|';
  for (int n = 0; n < CELL_WIDTH * 8; n++)
    cout << '-';
  cout << "|\n";

Conveniently, that is also the last line.

There are several hints in that snippet about drawing each successive line. I would personally use a modulo/remainder trick to decide whether to draw spaces or symbols in a given cell, but you could just make a loop over four groups of two vertical cells each...

You have to count and think about the loops to produce a cell, a line, and a pair of lines.

Forget the black and white variables. They are adding complexity that is messing you up.

[edit] Once you can produce the whole thing, then look at the problem again and see if you can reduce it using some loops.

Good luck!
Last edited on
Hi yabi2943993,

It will be helpful to follow the good suggestions by Duthomhas.

A few changes that I see with code are:

Change assignments to comparitors:
1
2
if (white = true) //assigns true to white
if (white == true) //compares value in white variable to true 


The WhiteFirst loop is not nested while the BlackFirst loop is nested:
1
2
3
4
5
6
7
8
9
//whitefirst - not nested
        if(white == true){
        for(int v=0;v<groups;v++){
            cout<<" ";
        }
        for(int r=0;r<groups;r++){
            cout<<simbol;
        }
        }


1
2
3
4
5
6
7
8
9
//whitefirst - nested
if (white == true) {
   for (int v = 0; v < groups; v++) {
      cout << " ";
      for (int r = 0; r < groups; r++) {
         cout << simbol;
      }
   }
}


Here is what I have so far, and have run out of time - its not working yet, and is a good start:

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
#include <iostream>
using namespace std;

int main()
{
    int groups;
    char simbol;

    bool black = false;
    bool white = true;

    while (1) {
        cin >> groups;

        if (groups == 0) { break; }
        else {
            std::cin >> simbol;

            for (int a = 1; a < 33; a++) {
                //whitefirst
                if (white == true) {
                    for (int v = 0; v < groups; v++) {
                        cout << " ";
                        for (int r = 0; r < groups; r++) {
                            cout << simbol;
                        }
                    }
                }
                //blackfirst
                if (black == true) {
                    for (int r = 0; r < groups; r++) {
                        cout << simbol;
                        for (int v = 0; v < groups; v++) {
                            cout << " ";
                        }
                    }
                }
                //linebreak and beginning change
                if (a % 2 == 0) {
                    
                    cout << "\n";
                    if (white == true) {
                        white = false;
                        black = true;
                    }
                    if (black == true) {
                        black = false;
                        white = true;
                    }
                }
            }
        }
    }
    return 0;
}


The output is not correct yet, and is getting closer.
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##
  ##  ##  ##  ##


This is a start to some possible changes to the code.

Edit: Changed loop from 0 to 32 to 1 to 33.
Last edited on
@yabi2943993

Here's my way to create the chess board, using string.
Don't know if it'll help you or not, but, ....

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
60
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int groups;
	char simbol;

	bool white = false;
	
	while (1) 
	{
		cout << "How many characters wide, are each square ? " << endl;
		cin >> groups;

		if (groups == 0) 
		{ break; }
		else {
			cout << "What character do you want used for the black squares?" << endl;
			cin >> simbol;
			string board_space(groups, ' ');
			string board_line(groups, simbol);
			string white_first = board_space+board_line+board_space+board_line+board_space+board_line+board_space+board_line;
			string black_first = board_line+board_space+board_line+board_space+board_line+board_space+board_line+board_space;
			cout << "|";
			for (int a=0;a<groups*8;a++)
				cout << "-";
			cout << "|" << endl;
			for (int a = 0; a < 8; a++)
			{
				//whitefirst
				if (white) // is true
				{
					for(int x=0;x<groups;x++)
					{
						cout << "|" << white_first << "|" << endl;
					}
					//cout << endl;
				}
				//blackfirst
				else // otherwise
				{
					for(int x=0;x<groups;x++)
					{
						cout << "|" << black_first << "|" << endl;
					}
					//cout << endl;
				}
				white = !white; // Change white from true to false to true...
			}
		}
		cout << "|";
		for (int a=0;a<groups*8;a++)
			cout << "-";
		cout << "|"<< endl;
	}
	cout << endl;
}
Last edited on
You can work out the details of adding borders and what happens if display is from black POV but the 'ancient' idea of preparing the data first, followed by displaying the data cuts out a lot of crap.

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
60
#include <iostream>

int main()
{
    char symbol{'#'}; // DEFAULT FOR TESTING
    int no_symbols{4};
    
    std::cout << "        Please  enter symbol: ";
    std::cin >> symbol;
    
    std::cout << "Please  enter no. of symbols: ";
    std::cin >> no_symbols;
    
    int size = 8 * no_symbols;
    
    // SETUP BOARD ARRAY
    char **chessboard = new char* [size];
    for(int i = 0; i < size; ++i)
    {
        chessboard[i] = new char[size];
    }
    
    // SETUP CELLS
    int corner_x{0}, corner_y{0};
    char print_symbol{' '};
    
    for(int row = 0; row < size; row ++)
    {
        corner_x = row/no_symbols;
        for(int col = 0; col < size; col++)
        {
            corner_y = col/no_symbols;
            
            if((corner_x + corner_y) % 2 == 0)
                print_symbol = ' ';
            else
                print_symbol = symbol;
            
            chessboard[row][col] = print_symbol;
        }
    }
    
    // DISPLAY BOARD
    for(int row = 0; row < size; row++)
    {
        for(int col = 0; col < size; col++)
        {
            std::cout << chessboard[row][col];
        }
        std::cout << '\n';
    }
    
    // CLEANUP
    for(int i = 0; i < size; ++i) {
        delete [] chessboard[i];
    }
    delete [] chessboard;
    
    return 0;
}


 Please  enter symbol: #
Please  enter no. of symbols: 2
  ##  ##  ##  ##
  ##  ##  ##  ##
##  ##  ##  ##  
##  ##  ##  ##  
  ##  ##  ##  ##
  ##  ##  ##  ##
##  ##  ##  ##  
##  ##  ##  ##  
  ##  ##  ##  ##
  ##  ##  ##  ##
##  ##  ##  ##  
##  ##  ##  ##  
  ##  ##  ##  ##
  ##  ##  ##  ##
##  ##  ##  ##  
##  ##  ##  ##  
Program ended with exit code: 0
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
#include <iostream>
#include <string>

int main()
{
    char symbol{'#'};
    int no_symbols{4};
    
    std::cout << "        Please  enter symbol: ";
    std::cin >> symbol;
    
    std::cout << "Please  enter no. of symbols: ";
    std::cin >> no_symbols;
    
    int size = 8 * no_symbols;
    
    // SETUP BOARD ARRAY
    char **chessboard = new char* [size];
    
    for(int i = 0; i < size; ++i)
        chessboard[i] = new char[size];
    
    // DISPLAY BOARD
    int corner_x{0}, corner_y{0};
    char line{'-'};
    std::string border{ '+' + std::string(size + 2, line) + '+'};
    
    std::cout << border << '\n';
    for(int row = 0; row < size; row ++)
    {
        corner_x = row/no_symbols;
        
        std::cout << "| ";
        for(int col = 0; col < size; col++)
        {
            corner_y = col/no_symbols;
            
            if( (corner_x + corner_y) % 2 == 0 )
                std::cout << ' ';
            else
                std::cout << symbol;
        }
        std::cout << " |\n";
    }
    std::cout << border << '\n';
    
    // CLEANUP
    for(int i = 0; i < size; ++i)
        delete [] chessboard[i];
    delete [] chessboard;
    
    return 0;
}

you had a few issues. 1 being if a=0 a%4 evaluates to 0 making the check true the first time through. also between 0 and 4 is actually 5 so i had to change the a count to 1 to >33

second you had a nested for loop in the if black==true statement which it shouldn't have been nested. was resulting in symbol space*groups symbol. etc.

also the bool switching statement would swap the bools but then enter the 2nd if statement bc that evaluated as true once the swap was done and then they were just changed right back to what they originally were....

but the output looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Chess Board Maker
Enter # Of Groups...
4
Enter Your Symbol...
%

    %%%%    %%%%    %%%%    %%%%
%%%%    %%%%    %%%%    %%%%
    %%%%    %%%%    %%%%    %%%%
%%%%    %%%%    %%%%    %%%%
    %%%%    %%%%    %%%%    %%%%
%%%%    %%%%    %%%%    %%%%
    %%%%    %%%%    %%%%    %%%%
%%%%    %%%%    %%%%    %%%%



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
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

int main()
{
    int groups;
    char simbol;

    bool black = false;
    bool white = true;
    std::cout << "Chess Board Maker" << endl;
    while (1) {
        cout << "Enter # Of Groups..."<<endl;
        std::cin >> groups;

        if (groups == 0) { break; }
        else {
            std::cout << "Enter Your Symbol..."<<endl;
            std::cin >> simbol;
            cout << endl;

            for (int a = 1; a < 33; a++) {
                //whitefirst
                if (white == true) {
                    for (int v = 0; v < groups; v++) {
                        cout << " ";
                    }
                    for (int r = 0; r < groups; r++) {
                        cout << simbol;
                    }
                }
                //blackfirst
                if (black == true) {
                    for (int r = 0; r < groups; r++) {
                        cout << simbol;
                    }
                        for (int v = 0; v < groups; v++) {
                            cout << " ";
                        }
                   
                }
                //linebreak and beginning change


                if (a % 4 == 0 && a!=0) {
                    cout << "\n";
                    bool justchanged=false;
                    if (white == true) {
                        white = false;
                        black = true;
                        justchanged = true;
                    }
                    if (black==true && justchanged==false){
                        black = false;
                        white = true;
                    }
                    justchanged = false;
                }
            }
        }
    }
  
    return 0;
}

Last edited on
Thanks to all for your help!
Topic archived. No new replies allowed.