mouse movement

This is a discussion on mouse movement within the C++ Programming forums, part of the General Programming Boards category; Hi I have a piece of code here for a mouse to eat food on the maze and choose which ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    85

    mouse movement

    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);

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Run a profiler if there are any speed issues. It will find bottlenecks.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    How about can i simply the code, but carry out the same result?
    thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's what the suggestion Desolation made was for: find out what's taking the time, then look to simplify that, instead of just trying to work on random pieces of the code.

    I don't see much inefficiency, and while this may be slow I don't know that it's going to get better, because I see lots of file i/o, and that is approximately 75346.82 times slower than anything else. The only possible weird thing is the recursion in evaltree() calling itself 2 or three times as though this probably wasn't already in some big loop. Edit to add: although since there don't appear to be any local variables, or any parameters, it should be really easy to push the stack, so that won't help (or hurt).
    Last edited by tabstop; 04-13-2008 at 11:02 PM.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Thanks
    So what can i modify for the moves?
    I really want to simplify the code, any1 can help me please
    thanks

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Who knows what you can modify? You're the one with all the code. The snippet you posted seems fine. Did you make sure your optimizations were turned up all the way (-O3 or similar)?

    But rather than posting random snippets of your code onto random message boards, time your code. Are you using GNU compilers? If so, look at
    http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html
    (The manual seems old, but it's probably still all right.)

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    I am sorry
    I know i asked some question before, but I have solve the random maze on my own.
    I was looking to simplify the code into 3-4 line shorter.

    I am very sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulating mouse movement in other windows
    By Wiretron in forum Windows Programming
    Replies: 11
    Last Post: 10-13-2007, 09:11 AM
  2. simple mouse movement program
    By skankles in forum Windows Programming
    Replies: 3
    Last Post: 05-27-2007, 08:54 PM
  3. Recognizing mouse movement???
    By jrkid in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2004, 12:49 PM
  4. Controlling Mouse movement?
    By raum in forum Windows Programming
    Replies: 6
    Last Post: 07-02-2004, 09:46 AM
  5. Need to read mouse movement in program
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2002, 03:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21