how can I simplify this chess square program in a more c++ way?

ok so in this simple program I'm checking the content of input against odd and even strings from sqname[64]. how can I simplify my code to check for odd or even elements in the sqname[] array without making such large else if statements? This is working code. on line 132 or 133 I gave my best guess how to do it, but I would really like to know a way to do it without raw loops, but I'll take what I can get thank you for your time.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <random>
#include <string> 

using namespace std;

string input = "";
int randomSquare();
bool black = 1, white = 0;
float y = 0, n = 0, t = 0;

// square names 
 string sqname[64] = {
	"a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8",
	"b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8",
	"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8",
	"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8",
	"e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8",
	"f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8",
	"g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8",
	"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"
}; 


// square values
bool sqval[64] = {
	1, 0, 1, 0, 1, 0, 1, 0 ,
	0, 1, 0, 1, 0, 1, 0, 1 ,
	1, 0, 1, 0, 1, 0, 1, 0 ,
	0, 1, 0, 1, 0, 1, 0, 1 ,
	1, 0, 1, 0, 1, 0, 1 ,0 ,
	0, 1, 0, 1, 0, 1, 0, 1 ,
	1, 0, 1, 0, 1, 0, 1, 0 ,
	0, 1, 0, 1, 0, 1, 0, 1
};

struct Square
{
	string sqr;
	bool val;
};

// 64 chess squares
Square sq[64];

int randomSquare()
{
	random_device rd; // obtain a random number from hardware
	mt19937 eng(rd()); // seed the generator
	uniform_int_distribution<> distr(0, 64); // define the range of chess squares 
	return distr(eng);
}

void quiz(int rs) // pass in the generator 
{
	// fill in the struct with the array values   
	for (int i = 0; i < 64; i++)
	{
		sq[i].sqr = sqname[i];
		sq[i].val = sqval[i];
	}

	cout << "is the chess square " << sq[rs].sqr << " white or black w : b" << endl;
	string guess = "";
	bool answer = 0;
	cin >> guess;
	cout << endl;
	if (guess == "w") answer = 0;
	else if (guess == "b") answer = 1;
	else cout << "guess is not valid\n" << endl;

	if (answer == sq[rs].val)
	{
		if (sq[rs].val == black)
		{
			cout << "that is correct " << sq[rs].sqr << " is " << "BLACK " << endl << endl;
			y++;
		}
		if (sq[rs].val == white)
		{
			cout << "that is correct " << sq[rs].sqr << " is " << "WHITE " << endl << endl;
			y++;
		}
	}
	else
	{
		if (sq[rs].val == black)
		{
			cout << "incorect " << sq[rs].sqr << " is " << " BLACK " << endl << endl;
			n++;
		}
		if (sq[rs].val == white)
		{
			cout << "incorect " << sq[rs].sqr << " is " << " WHITE " << endl << endl; ;
			n++;
		}
	}
}

int main()
{
	while (true)
	{
		cout << "please enter a chess square a1-h1 or\n";
		cout << "or enter r for random square quiz or q to quit: ";
		input == "";
		cin >> input;
		cout << endl;

		if (input == "q")
		{
			return 0;
		}

		else if (input == "r")
		{
			cout << "how many questions would you like? 1- 10 ";
			int loop;
			cin >> loop;
			for (int i = 0; i < loop; i++)
			{
				quiz(randomSquare());
			}

			t = (y / loop) * 100; // total 
			cout << "you got " << t << "% of the questions right " << endl << endl;
			t = 0; y = 0; n = 0;
		}

		
		
		/* 
		   -----------------------------------------------------------------
		   simplify below by seperating sqname[64] 
		   into two seperate arrays for odd and even based on element index 
		   itterate through array checking if it compares to the input  
		   -----------------------------------------------------------------
		*/ 
			
		
		// I wanted to do proper error checking on the input  
		else if (input == "a1" || input == "a3" || input == "a5" || input == "a7" ||
			input == "b2" || input == "b4" || input == "b6" || input == "b8" ||
			input == "c1" || input == "c3" || input == "c5" || input == "c7" ||
			input == "d2" || input == "d4" || input == "d6" || input == "d8" ||
			input == "e1" || input == "e3" || input == "e5" || input == "e7" ||
			input == "f2" || input == "f4" || input == "f6" || input == "f8" ||
			input == "g1" || input == "g3" || input == "g5" || input == "g7" ||
			input == "h2" || input == "h4" || input == "h6" || input == "h8")
		{
			cout << endl << "the square " << input << " is BLACK\n\n";
		}
		else if (input == "a2" || input == "a4" || input == "a6" || input == "a8" ||
			input == "b1" || input == "b3" || input == "b5" || input == "b7" ||
			input == "c2" || input == "c4" || input == "c6" || input == "c8" ||
			input == "d1" || input == "d3" || input == "d5" || input == "d7" ||
			input == "e2" || input == "e4" || input == "e6" || input == "e8" ||
			input == "f1" || input == "f3" || input == "f5" || input == "f7" ||
			input == "g2" || input == "g4" || input == "g6" || input == "g8" ||
			input == "h1" || input == "h3" || input == "h5" || input == "h7")
		{
			cout << endl << "the square " << input << " is WHITE\n\n";
		}
		else
		{
			cout << input << " is not a chess square! \n\n";
		}
	}
}  



Last edited on
If you have vector<string> sqname then use
http://www.cplusplus.com/reference/algorithm/find/
and
http://www.cplusplus.com/reference/iterator/distance/

The first will tell you whether it is a valid square or not; together with the second it will then tell you whether you are an odd or even distance from the start (sqname.begin()).

ya, I was thinking that could be another way after c++ 11. I need to go back over Kate Gregory's course on the algorithm library thanks for the quick reply!
Topic archived. No new replies allowed.