Help to create the right condition

Need to make rook's possible moves, but don't know how to make the right condition.

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
  
	char x;
	int y;
	bool menu = true;

	cin >> x >> y;

	x -= 97;


	for (int row = 0;row <= 8; row++)
	{

		if (row == 0)
		{
			for (int col = 0;col < 8; col++)
			{
				cout << '|' << (char)(col + 97) << '|';
			}
			cout << endl;
		}
		else
		{
			for (int col = 0;col <= 8; col++)
			{
				cout << '|';

				if (col == 8)
				{
					cout << row;
				}
				else if (col == x && row == y) 
				{
					cout << 'H';

				}
				else if (x == row   && y == col) // the first condition
				{
					cout << "*";

				}
				else if (x == row  && y == col)
				{
					cout << "*";

				}
				else if (x == row   && y == col)
				{
					cout << "*";

				}
				else if (x == row  && y == col) // the last condition
				{
					cout << "*";

				}



				else
					cout << " ";
				cout << '|';
			}
			cout << endl;
		}
	}
Last edited on
Here is a function I use to check the move of a castle.

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
bool Castle::Valid_Move(const size_t& x, const size_t& y, const char& ch, const std::string Board_Array[8][8])const
{
	//Checks for valid movement
	{
		if (pos[1] == x && pos[0] != y);
		else if (pos[0] == y && pos[1] != x);
		else
			return false;
	}
	//Checks for obstruction
	{
		if (pos[1] == x)
		{
			if (pos[0] < y)
			{
				for (size_t i = pos[0] + 1; i <= y; i++)
				{
					if (i == y && Board_Array[i][x][0] == 'R' && ch == 'W');
					else if (i == y && Board_Array[i][x][0] == 'W' && ch == 'B');
					else if (Board_Array[i][x][0] == 'W' || Board_Array[i][x][0] == 'R')
						return false;
				}
			}
			else
			{
				for (size_t i = pos[0] - 1; i > y; i--)
				{
					if (Board_Array[i][x][0] == 'W' || Board_Array[i][x][0] == 'R')
						return false;
				}
				if (Board_Array[y][x][0] == 'R' && ch == 'W');
				else if (Board_Array[y][x][0] == 'W' && ch == 'B');
				else if (Board_Array[y][x][0] == 'W' || Board_Array[y][x][0] == 'R')
					return false;
			}
		}
		else
		{
			if (pos[1] < x)
			{
				for (size_t i = pos[1] + 1; i <= x; i++)
				{
					if (i == x && Board_Array[y][i][0] == 'R' && ch == 'W');
					else if (i == x && Board_Array[y][i][0] == 'W' && ch == 'B');
					else if (Board_Array[y][i][0] == 'W' || Board_Array[y][i][0] == 'R')
						return false;
				}
			}
			else
			{
				for (size_t i = pos[1] - 1; i > x; i--)
				{
					if (Board_Array[y][i][0] == 'W' || Board_Array[y][i][0] == 'R')
						return false;
				}
				if (Board_Array[y][x][0] == 'R' && ch == 'W');
				else if (Board_Array[y][x][0] == 'W' && ch == 'B');
				else if (Board_Array[y][x][0] == 'W' || Board_Array[y][x][0] == 'R')
					return false;
			}
		}
	}
	return true;
}
Since a rook can move only up or down vertically and either left or right horizontally if the fields are empty or occupied by an enemy piece.
For example:
if the rook is on row 0 and col 0 then it can move to row 1 col 0 when the field is empty or an enemy piece is there. Same principle is for horizontal move.
Topic archived. No new replies allowed.