Q:

C++ program to determine the color of chess square

0

chess board is equally divided into 64 identical squares that are black and white alternately. Each square on the chessboard can be identified by the coordinates as 'A' to 'H' on the horizontal axis and '1' to '8' on the vertical axis as shown in the figure.

Each square can be identified using the coordinate system specified above. For example, the square with coordinates G5 is colored BlackA6 is colored White and so on...

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Determine the color of a chess square board in C++

#include<iostream>
#include<cctype>
using namespace std;

int main()
{
	char string[10], x;
	cout << "Enter the coordinates of the square, \
	\nthe first coordinate A to H and second coordinate 1 to 8: ";
	cin.getline(string, 10);
	x = string[0];
	x = tolower(x);
	string[0] = x;
	if (string[0] == 'a' || string[0] == 'c' || string[0] == 'e' || string[0] == 'g')
	{
		if (string[1] == '1' || string[1] == '3' || string[1] == '5' || string[1] == '7')
			cout << "Black square";
		else
			cout << "White square";
	}
	else
	{
		if (string[1] == '1' || string[1] == '3' || string[1] == '5' || string[1] == '7')
			cout << "white square";
		else
			cout << "Black square";
	}

	return 0;
}

Output

 
First run:
Enter the coordinates of the square,
the first coordinate A to H and second coordinate 1 to 8: C5
Black square

Seccond run:
Enter the coordinates of the square,
the first coordinate A to H and second coordinate 1 to 8: F3
white square

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now