Originally Posted by
laserlight
I have a better solution for you: break up your functions into smaller functions that each does one thing and does it well. You will then never have such long functions (except in special cases, e.g., where they just define data).
here is the whole function
Code:
int calculate_moves(int old_x, int old_y, int new_x, int new_y)
{
int piece_index, value_to_return = -1;
piece_index = find_piece(old_x, old_y);
if (piece_index >= 0)
{
if (whos_turn == false) //whites turn
{
switch (old_x - new_x)
{
case 0:// taking 2 pieces
if (new_y - old_y == 4)
{
//printf("take 2 pieces");
if(take_piece(old_x, old_y, new_x, new_y) == 0)//check this
{
value_to_return = 0;
break;
}
}
case 1: case -1:
if (new_y - old_y == 1)
{
//printf("move piece\n");
if(move_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 2: case -2:
if (new_y - old_y == 2) // taking 1 piece
{
//printf("1 piece taken");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
else if (new_y - old_y == 6) // taking three pieces
{
//printf("three pieces taken\n");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 4: case -4://take 2 pieces in a straight line
if (new_y - old_y == 4)
{
//printf("2 pieces taken in a straight line");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 6: case -6: // take 3 pieces in a straight line
if (new_y - old_y == 6)
{
//printf("3 pieces taken in a straight line");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
default:
//printf("Illegal move");
value_to_return = -1;
}
}
else
{
switch (old_x - new_x)
{
case 0:// taking 2 pieces
if (old_y - new_y == 4)
{
//printf("take 2 pieces");
if(take_piece(old_x, old_y, new_x, new_y) == 0)//check this
{
value_to_return = 0;
break;
}
}
case 1: case -1:
if (old_y - new_y == 1)
{
//printf("move piece\n");
if(move_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 2: case -2:
if (old_y - new_y == 2) // taking 1 piece
{
//printf("1 piece taken");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
else if (old_y - new_y == 6) // taking three pieces
{
//printf("three pieces taken\n");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 4: case -4://take 2 pieces in a straight line
if (old_y - new_y == 4)
{
//printf("2 pieces taken in a straight line");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 6: case -6: // take 3 pieces in a straight line
if (old_y - new_y == 6)
{
//printf("3 pieces taken in a straight line");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
default:
//printf("Illegal move");
value_to_return = -1;
}
}
}
else
{
value_to_return = -1;
}
return value_to_return;
}
the bit that makes it so long is the switch case statements but if i use if else if and else statements it then runs out of space to the right without having to scroll
for example if i replaced the case -1: case 1 with an else if it would be
Code:
else if (((old_x - new_x == 0) || (old_x - new_x == 1)) && (new_y - old_y == 1)))
with out the new_y part of the evaluation i just replace 1 line of code with another
so i cant see how to make it any shorter.
coop