Edited with liberal comments.
Code:
/* This function takes two characters which tell it
** whether to add or subtract a grid cell from the
** two coordinate variables x and y. x and y are
** assumed to be global in this example.
*/
void change_location ( const char modex, const char modey )
{
/* Place a space character at the
** current location to clear the
** grid cell.
*/
grid[x][y] = CLEAR;
/* Test modex, which will determine
** how the x coordinate is to be
** changed. '+' means add, '-' means
** subtract, and any other value
** means to do nothing with x.
*/
if ( modex == '+' ) x++;
else if ( modex == '-' ) x--;
/* Test modey which effects the x
** coordinate in the same manner as
** the x coordinate.
*/
if ( modey == '+' ) y++;
else if ( modey == '-' ) y--;
/* The coordinates are set for the new
** location, place the character that
** was to be moved at the new location.
*/
grid[x][y] = FILL;
}
.
.
.
/* Switch with the move variable. Each direction
** is denoted by a named constant for ease of
** reading.
*/
switch ( move ) {
/* Each call to change_location effects either
** x or y, the first argument to change_location
** effects x and the second effects y. A '+' means
** to add, a '-' means to subtract, and in this
** case, 0 means to do nothing though any value
** other than '+' and '-' could be used.
*/
case UP: change_location ( 0, '-' ); break;
case DOWN: change_location ( 0, '+' ); break;
case LEFT: change_location ( '-', 0 ); break;
case RIGHT: change_location ( '+', 0 ); break;
default:
perror ( "Invalid input" );
}
-Prelude