possible to simplify code?

It's a chess program and this bit is doing the rook moves. Can I get all this in a single loop rather than one for each direction?

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
 
	for (char i= pos.first + 1; i <= 7;i++ )  
	{
		co_ord pos_i (i,pos.second);
		Squarestate pos_state = possibleSquare (pos_i, piece_colour);

		if (pos_state == Samecolour || pos_state == Diffcolourking)
			break;

		possible_moves.push_back(pos_i);
			
		if (pos_state==Diffcolour)
			break;
	}

	for (char i= pos.second + 1; i <= 7;i++ )
	{
		co_ord pos_i (pos.first,i);	
		Squarestate pos_state = possibleSquare (pos_i,piece_colour);

		if (pos_state == Samecolour || pos_state == Diffcolourking)
			break;
	
		possible_moves.push_back(pos_i);

		if (pos_state== Diffcolour)
				break;
	}

	for (char i= pos.first - 1; i >= 0;i-- )
	{
		co_ord pos_i (i,pos.second);
		Squarestate pos_state = possibleSquare (pos_i,piece_colour);

		if (pos_state== Samecolour || pos_state == Diffcolourking)
			break;
	
		possible_moves.push_back(pos_i);

		if (pos_state==Diffcolour)
				break;
	}

	for (char i= pos.second - 1; i >= 0;i-- )
	{
		co_ord pos_i (pos.first,i);
		Squarestate pos_state = possibleSquare(pos_i,piece_colour);

		if (pos_state == Samecolour || pos_state == Diffcolourking)
			break;
	
		possible_moves.push_back(pos_i);

		if (pos_state==Diffcolour)
				break;
	}
Last edited on
Topic archived. No new replies allowed.