Hi I have a piece of code here for a mouse to eat food on the maze and choose which direction.
Any1 have suggestion how can I modify the code, so the progrom runs faster and I want to simplify the code as much as possible.
An other action of the mouse I should add?
thanks
Code:
void evaltree(void)
{ if (pellets == 0 || steps > MAX_STEPS) return;
switch (*nodep++)
{ case LEFT: heading = (heading==NORTH? WEST : heading-1);
record_turn();
steps++;
return;
case RIGHT: heading = (heading+1) % 4;
record_turn();
steps++;
return;
case MOVE: switch (heading)
{ case NORTH: if (row == 0) row = MATRIX_SIZE;
row--;
break;
case EAST: col = (col+1) % MATRIX_SIZE;
break;
case SOUTH: row = (row+1) % MATRIX_SIZE;
break;
case WEST: if (col == 0) col = MATRIX_SIZE;
col--;
break;
}
if (trace)
{ //(AG) Old trace removed - add record move.
//trace_matrix[row][col] = 2;
//print_matrix();
//puts("Continue trace?");
//trace = (getch() == 'y');
fprintf(filep, "<step>\n");
fprintf(filep, "<automata_section>\n");
fprintf(filep, "<automata automaton_id=\"0\" shape=\"ant\" name=\"ANT\" >\n");
fprintf(filep, "<location loc_x=\"%d\" loc_y=\"%d\"/>\n", col, (31 - row));
fprintf(filep, "</automata>\n");
fprintf(filep, "</automata_section>\n");
fprintf(filep, "<cell_section>\n");
}
if (matrix[row][col])
{ matrix[row][col] = 0;
pellets--;
//(AG) Record cell eaten
if (trace)
{
fprintf(filep, "<cell state=\"eaten\" loc_x=\"%d\" loc_y=\"%d\"/>\n", col, (31 - row));
}
}
//(AG) Close cell section and step
if (trace)
{
fprintf(filep, "</cell_section>\n");
fprintf(filep, "</step>\n");
}
steps++;
return;
case IF_FOOD_AHEAD:
switch (heading)
{ case NORTH: next_row = (row == 0? MATRIX_SIZE-1: row-1);
next_col = col;
break;
case EAST: next_row = row;
next_col = (col+1) % MATRIX_SIZE;
break;
case SOUTH: next_row = (row+1) % MATRIX_SIZE;
next_col = col;
break;
case WEST: next_row = row;
next_col = (col == 0? MATRIX_SIZE-1: col-1);
break;
}
if (matrix[next_row][next_col])
{ evaltree();
skip_arg();
}
else
{ skip_arg();
evaltree();
}
return;
case PROGN2: evaltree(); evaltree(); return;
case PROGN3: evaltree(); evaltree(); evaltree(); return;
}
}
void test_fitness(int index)
{
// Evaluate fitness of the indexed individual
struct prog *progp = &population[index];
int count = progp->proglen;
// Initialise the trail matrix
memcpy(matrix, matrix_data, MATRIX_SIZE*MATRIX_SIZE);