示例#1
0
void update_snake_head(snake * cur_snake, board * cur_board, int growth_per_food)
/*! Attempt to move the head of the given snake one cell (by appending to the snake)
    according to the snake's heading (north is up).  The head can move in the given
    direction if the destination cell is either open or food.  If the cell is food,
    add the given growth per food to the snake's growth counter.  If the destination
    cell is a snake or wall, do nothing.
*/
{

/* do nothing if it has no pieces */
   if(cur_snake->head == NULL)
	{
	}
   else
   {
       int col = cur_snake->head->col;
       int row = cur_snake->head->row;


       switch(cur_snake->heading)
       {
        case NORTH:
	    row--;
	    break;
	case SOUTH:
 	    row++;
	    break;
	case EAST:
	    col++;
	    break;
	case WEST:
	    col--;
	    break;
        }


      if( *(board_cell(cur_board, row, col)) == CELL_OPEN || *(board_cell(cur_board, row, col)) == CELL_FOOD )
      {
          if( *(board_cell(cur_board, row, col)) == CELL_FOOD )
	          cur_snake->growth += growth_per_food;

          append_snake_head(cur_snake, cur_board, row, col);
      }
    }

}
示例#2
0
void update_snake_head(snake * cur_snake, board * cur_board, int growth_per_food)
/*! Attempt to move the head of the given snake one cell (by appending to the snake)
    according to the snake's heading (north is up).  The head can move in the given
    direction if the destination cell is either open or food.  If the cell is food,
    add the given growth per food to the snake's growth counter.  If the destination
    cell is a snake or wall, do nothing.
*/

{
    int temp_row = cur_snake->head->row;
    int temp_col = cur_snake->head->col;
    cell * pCell;
    switch (cur_snake->heading){
        
        case NORTH:
            temp_row--;
        break;
        case SOUTH:
            temp_row++;
        break;
        case EAST:
            temp_col++;
        break;
        case WEST:
            temp_col--;
        break;
    }
//append the new head
    pCell = board_cell(cur_board, temp_row, temp_col);
        if(*pCell == CELL_OPEN || *pCell == CELL_FOOD){
            if(*pCell == CELL_FOOD){
                cur_snake->growth += growth_per_food;

                cur_snake->food += 1;
            }
        append_snake_head(cur_snake, cur_board, temp_row, temp_col);
        }
 
}