my understanding of for loops is it sets the variable to 10 say then counts up/down to the next instruction. for example
Code:
for (x = 10; x > 0; x--)
{
printf("%d ", x);
}
will print out 10 9 8 7 6 5 4 3 2 1 where as if i set x >= 0 in the above loop it would print a zero as well.
why then does the last for loop in the below code count down to 0 when x is >= 0 but if tell it to run till x > 0 its an infinite loop.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int board[8][8], start_x_coordinate, start_y_coordinate, finish_x_coordinate, finish_y_coordinate, temp_x, temp_y;
int route[20][20][1], route_count = 0, move = 1, found_branch = 0;
int a, x, y;
int player = 0, multiplyer_y = player ? -1 : 1;
int count_loop = 0;
start_x_coordinate = 2;
start_y_coordinate = 0;
finish_x_coordinate = 4;
finish_y_coordinate = 6;
temp_x = start_x_coordinate;
temp_y = start_y_coordinate;
// inistalize board
for (y = 7; y >= 0; y--)
{
for (x = 0; x < 8; x++)
{
if ((y % 2 == 0 && x % 2 == 0) || (y % 2 != 0 && x % 2 != 0))
{
board[x][y] = 0; //good square
printf("%d ", board[x][y]);
}
else
{
board[x][y] = 5; //invalid square
printf("%d ", board[x][y]);
}
}
printf("\n");
}
// work out routes
a = 1;
while (a)
{
count_loop++;
//check the diagonal squares are still on the board
if (((temp_x - 2 >= 0 && temp_x - 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7)) &&
((temp_x + 2 >= 0 && temp_x + 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7)))
{
//record branch
route[route_count][move][0] = 2;
move += 1;
//jump left
route[route_count][move][0] = 0;
temp_x -= 2;
temp_y += 2 * multiplyer_y;
move += 1;
}
else if((temp_x - 2 >= 0 && temp_x - 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7))
{
// jump left from whites point of view
route[route_count][move][0] = 0;
temp_x -= 2;
temp_y += 2 * multiplyer_y;
move += 1;
}
else if ((temp_x + 2 >= 0 && temp_x - 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7))
{
//jump right
route[route_count][move][0] = 1;
temp_x += 2;
temp_y += 2 * multiplyer_y;
move += 1;
}
else
{
route[route_count][move][0] = -2;
repeat:
if (move >= 0)
{
for (x = move; x > 0; x--)
{
printf("array at %d = %d\n", x, route[route_count][x][0]);
if (route[route_count][x][0] == 2)
{
//last branch in the route found
found_branch = x;
break;
}
}
if (found_branch != 0)
{
temp_x = start_x_coordinate;
temp_y = start_y_coordinate;
move = 0;
for (x = 0; x <= found_branch; x++)
{
if (route[route_count][move][0] == 0)
{
temp_x -= 2;
temp_y += 2 * multiplyer_y;
move++;
}
else if (route[route_count][move][0] == 1)
{
temp_x += 2;
temp_y += 2 * multiplyer_y;
move++;
}
else
{
// just increment move to preserve branch record
move++;
}
}
// move is pointing at the move after the last branch so decrement move by 1 then jump right
route[route_count][move - 1][0] = 1;
temp_x += 2;
temp_y += 2 * multiplyer_y;
move += 1;
}
else
{
printf("no more branches\n");
break;
}
}
else
{
printf("move was less than 1\n");
break;
}
}
if (temp_x == finish_x_coordinate && temp_y == finish_y_coordinate)
{
printf("route found\n");
//copy old route into new route
for (x = move; x >= 0; x--) //<---- this one here
{
route[route_count + 1][x][0] = route[route_count][x][0];
}
found_branch = 0;
route_count++;
goto repeat;
}
}
printf("took %d goes to find a route\n", count_loop);
return 0;
}
coop