Vulpes

mmouse

Apr 21st, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 42.59 KB | None | 0 0
  1. #include <iostream>
  2. #include "Blake_Math.h"
  3.  
  4.  
  5. typedef struct BFS_node BFS_node;
  6. struct BFS_node {
  7.     int * cell;
  8.     BFS_node * next_node;
  9. };
  10. typedef struct move_node move_node;
  11. typedef struct move_list move_list;
  12. struct move_node {
  13.     move_node * next;
  14.     unsigned short int direction_went;
  15. };
  16. struct move_list {
  17.     unsigned int moves_gone;
  18.     move_node * head;
  19. };
  20. struct mouse_location {
  21.     int x;
  22.     int y;
  23. };
  24.  
  25.  
  26. move_node * global_current_node;
  27. move_list the_global_move_list;
  28.  
  29.  
  30. class Maze_Class {
  31. private:
  32. protected:
  33. public:
  34.     void initialize_maze();
  35.     /*
  36.      *  ###########  
  37.      *  # + + + + #
  38.      *  #+@+@+@+@+#
  39.      *  # + + + + #
  40.      *  #+@+@+@+@+#
  41.      *  # + + + + #
  42.      *  #+@+@+@+@+#
  43.      *  # + + + + #
  44.      *  #+@+@+@+@+#
  45.      *  # + + + + #
  46.      *  ###########  
  47.      */
  48.     /*
  49.      *  BORDER WALL =         0XFF
  50.      *  INNER WALL DNE =      0X10
  51.      *  INNER WALL EXISTS =   0X11
  52.      *  INNER WALL UNKNOWN =  0X12
  53.      *  WALL CONNECTION =     0X20  
  54.      *  STEPS:
  55.      *  UNEXPLORED =          0X00
  56.      *  EXPLORED =            0X01
  57.      *  FINISH =              0X02
  58.      *  SHORTEST ROUTE =      0X03
  59.      *  MOUSE =               0X04
  60.      *  
  61.      *  
  62.      */
  63.     int * maze_map;//our map of the maze
  64.     int size_x;
  65.     int size_y;
  66.    
  67.     Maze_Class(int xside, int yside);
  68.     ~Maze_Class();
  69.     void * convert_ll_to_mat(BFS_node * linklist_head, int * write_to_mat_size);
  70.     void destroy_ll(BFS_node * head_to_destroy);
  71.     void * bfsearch(int from_where_absol, int * mat_size);
  72.     BFS_node * make_link_list(int * ending_spot);
  73.     int convert_to_absolute(mouse_location from_m_l);
  74.     mouse_location convert_from_absolute(int from_abs);
  75.     int maze_write_wall(int type_seen, int location);
  76.     void maze_write_cell(int location);
  77.     void clear_out_80s();
  78.     void disp_maze(int properties, mouse_location * mouse, mouse_location * cheese);
  79. };
  80.  
  81. class Simulator : public Maze_Class {
  82. private:
  83. protected:
  84. public:
  85.     //Maze_Class absolute_maze(5,5);
  86.     Simulator(char**John_maze);
  87.     ~Simulator();
  88.    
  89.     int mouse_sees_wall(mouse_location location_of_wall);
  90.     void mouse_went_step(unsigned short int direction);
  91.     //void user_clicks_wall_on_off();
  92.     //void temp_function_to_make_maze_walls();
  93.    
  94. };
  95.  
  96. //user interaction functions
  97. //mouse_location write_starting_mouse_location();
  98. //struct mouse_location write_cheese_location();
  99. //void write_walls_to_maze(Maze_Class*, char**);
  100. //end user interaction functions
  101. int direction(int orientation, int size_x);
  102. class Mouse_Class : public Maze_Class {
  103. private:
  104. protected:
  105. public:
  106.     mouse_location mouse_start;
  107.     mouse_location mouse;
  108.     mouse_location cheese;
  109.     Mouse_Class(char**);
  110.     ~Mouse_Class();
  111.     void init_mouse(char**);
  112.     void set_cheese(char**);
  113.     int confirm_place(struct mouse_location cheese_or_mouse);
  114.     int go_number_of_spaces(int spaces_and_orientation, Simulator * sim);
  115.     int go_mouse(int orientation, Simulator * sim);
  116.    
  117.     /*
  118.      *  Mouse logic
  119.      *  mouse travels down way, marks territory
  120.      *  mouse knows paths traveled & paths not traveled
  121.      *  mouse knows how many steps from path untraveled to get back onto untraveled path (from dead end or something)
  122.      *  mouse travels DFS until it runs into dead end or path already traveled
  123.      *  then execute BFS to find shortest path to unexplored route !!! provided it is within maze reach (submaze which is made after a route to finish is found)
  124.      *  mouse finds all available paths to finish from start point.
  125.      *  Execute BFS to find shortest route from start to finish
  126.      *  send mouse to start, send mouse through route found to finish. terminate program.
  127.      */
  128.     /*
  129.      *  I don't need to keep track of how many spaces the mouse has gone because I'm just looking around at the walls & stuff;
  130.      *  track of spaces wil occur when I perform BFS's & DFS's
  131.      */
  132.     /*
  133.      *  ###########  
  134.      *  # + + + + #
  135.      *  #+@+@+@+@+#
  136.      *  # + + + + #
  137.      *  #+@+@+@+@+#
  138.      *  # + + + + #
  139.      *  #+@+@+@+@+#
  140.      *  # + + + + #
  141.      *  #+@+@+@+@+#
  142.      *  # + + + + #
  143.      *  ###########  
  144.      */
  145.     int which_way_unexplored();
  146.     int look_down_maze_to_see_where_stop(int orientation);
  147.     unsigned short int get_mouse_back_to_unexplored(Simulator * sim);
  148.     void mark_path_with_green();
  149.     void get_back_to_start(Simulator * sim);
  150.     //void go_straight_to_finish(Simulator * sim); 
  151.     void main_mouse_function(Simulator * sim);
  152. };
  153.  
  154. //mouse_maze.h
  155.  
  156. //mouse_maze.cpp
  157. void Maze_Class::initialize_maze(){
  158.     //debug//printf("void Maze_Class::initialize_maze()\n");//debug
  159.     for (int i1=0; i1<size_x; i1++) {
  160.         maze_map[i1                  ]=0XFF;
  161.         maze_map[i1+(size_y-1)*size_x]=0XFF;
  162.     }
  163.     for (int i1=1; i1<(size_y-1); i1++) {
  164.         maze_map[ i1*size_x     ]=0XFF;
  165.         maze_map[(i1+1)*size_x-1]=0XFF;
  166.     }
  167.     for (int i1=0; i1<((int)(size_y-3)/2); i1++) {
  168.         for (int i2=0; i2<((int)(size_x-3)/2); i2++) {
  169.             maze_map[(2+2*i1)*size_x+(2+2*i2)]=0X20;
  170.         }
  171.     }
  172.     for (int i1=0; i1<((int)(size_y-1)/2); i1++) {
  173.         for (int i2=0; i2<((int)(size_x-3)/2); i2++) {
  174.             maze_map[(1+2*i1)*size_x+(2+2*i2)]=0X12;
  175.         }
  176.     }
  177.     for (int i1=0; i1<((int)(size_y-3)/2); i1++) {
  178.         for (int i2=0; i2<((int)(size_x-1)/2); i2++) {
  179.             maze_map[(2+2*i1)*size_x+(1+2*i2)]=0X12;
  180.         }
  181.     }
  182.     for (int i1=0; i1<((int)(size_y-1)/2); i1++) {
  183.         for (int i2=0; i2<((int)(size_x-1)/2); i2++) {
  184.             maze_map[2*(i2+i1*size_x)+size_x+1]=0X00;
  185.             //size_x+1+i1*size_x*2+i2*2
  186.         }
  187.     }
  188.    
  189.    
  190.     //disp_maze(1,0,0);
  191. }
  192. /*
  193.  *  ###########  
  194.  *  # + + + + #
  195.  *  #+@+@+@+@+#
  196.  *  # + + + + #
  197.  *  #+@+@+@+@+#
  198.  *  # + + + + #
  199.  *  #+@+@+@+@+#
  200.  *  # + + + + #
  201.  *  #+@+@+@+@+#
  202.  *  # + + + + #
  203.  *  ###########  
  204.  */
  205. /*
  206.  *  BORDER WALL =         0XFF
  207.  *  INNER WALL DNE =      0X10
  208.  *  INNER WALL EXISTS =   0X11
  209.  *  INNER WALL UNKNOWN =  0X12
  210.  *  WALL CONNECTION =     0X20  
  211.  *  STEPS:
  212.  *  UNEXPLORED =          0X00
  213.  *  EXPLORED =            0X01
  214.  *  FINISH =              0X02
  215.  *  SHORTEST ROUTE =      0X03
  216.  *  MOUSE =               0X04
  217.  *  
  218.  *  BFS =                 0X80+
  219.  *  
  220.  *  
  221.  */
  222. Maze_Class::Maze_Class(int xside, int yside){
  223.     //debug//printf("Maze_Class::Maze_Class(int %d, int %d)\n",xside,yside);//debug
  224.     size_x=xside*2+1;
  225.     size_y=yside*2+1;
  226.     maze_map=(int*)malloc(sizeof(int)*(size_x)*(size_y));
  227.     initialize_maze();
  228. }
  229. Maze_Class::~Maze_Class(){
  230.     //debug//printf("Maze_Class::~Maze_Class()\n");//debug
  231.     free(maze_map);
  232. }
  233. int Maze_Class::convert_to_absolute(mouse_location from_m_l){
  234.     //debug//printf("int Maze_Class::convert_to_absolute(mouse_location %dx %dy)\n",from_m_l.x,from_m_l.y);//debug
  235.     return from_m_l.y*size_x+from_m_l.x;
  236. }
  237. mouse_location Maze_Class::convert_from_absolute(int from_abs){
  238.     //debug//printf("mouse_location Maze_Class::convert_from_absolute(int %d)\n",from_abs);//debug
  239.     using namespace b_math;
  240.     mouse_location to_m_l;
  241.     to_m_l.y=fix((double)from_abs/((double)size_x));
  242.     to_m_l.x=(from_abs%size_x);
  243.     return to_m_l;
  244. }
  245. int Maze_Class::maze_write_wall(int type_seen, int location){
  246.     //debug//printf("int Maze_Class::maze_write_wall(int %X, int %d)",type_seen,location);
  247.     int * wall_replaced;
  248.    
  249.     wall_replaced=&(maze_map[location]);
  250.     //*wall_replaced=type_seen;
  251.     //uncomment this when undoing the auto-maze write because this is a very important  if/else line to have
  252.     using namespace std;
  253.     if (*wall_replaced==0X12) {
  254.         *wall_replaced=type_seen;
  255.     }
  256.     else {
  257.         if (maze_map[location]!=type_seen) {
  258.             //debug//cout << "Error with maze_write_wall, your maze appears to be warping" << endl;
  259.             //debug//printf("maze_write_wall(type_seen==%X, location==%d) maze_map[location]==%X\n",type_seen,location,maze_map[location]);//debug
  260.         }
  261.         else {
  262.             //everything is good. we've seen that wall before
  263.         }  
  264.     }
  265.    
  266.     return * wall_replaced;
  267.    
  268. }
  269. void Maze_Class::maze_write_cell(int location){
  270.     //debug//printf("void Maze_Class::maze_write_cell(int %d)\n",location);//debug
  271.     using namespace std;
  272.     if (maze_map[location]==0X00) {
  273.         maze_map[location]=0X01;//I'm just marking my territory
  274.     }
  275.     else if (maze_map[location]==0X02) {
  276.         //yay I win now I can mark shortest path from here. perform BFS & then get your a** back to start to do the speedrun
  277.         //wait, I take this back, there might be a shorter way
  278.     }
  279.     else {
  280.         //else it is either: explored or shortest route, in either case, I am indifferent to the mark.
  281.     }
  282. }
  283. void * Maze_Class::convert_ll_to_mat(BFS_node * linklist_head, int * write_to_mat_size){
  284.    
  285.     //you will have to change the type here in the parameters
  286.    
  287.     int count_of_ll;
  288.     BFS_node * step;
  289.     unsigned short int * * matrix_to_return;
  290.    
  291.     count_of_ll=0;
  292.     step=linklist_head;
  293.    
  294.     while (step!=0) {
  295.         count_of_ll++;
  296.         step=step->next_node;
  297.         //the next node may have a different name
  298.     }
  299.     //debug//printf("this matrix will have %d cells \n",count_of_ll);//debug//
  300.    
  301.     matrix_to_return=(unsigned short int * *)malloc(sizeof(unsigned short int *)*count_of_ll);
  302.    
  303.     step=linklist_head;
  304.     *write_to_mat_size=count_of_ll;
  305.     count_of_ll--;
  306.     for (; count_of_ll>=0; count_of_ll--) {
  307.         //debug//printf("count_of_ll: %d \n",count_of_ll);//debug//
  308.         matrix_to_return[count_of_ll]=(unsigned short int *)(step->cell);
  309.         step=step->next_node;
  310.         /*/debug//
  311.          for (int i1=count_of_ll; i1<(*write_to_mat_size); i1++) {
  312.          printf("matrix[%d]==%p[]==%d \n",i1,matrix_to_return[i1],*matrix_to_return[i1]);
  313.          }//debug/*/
  314.     }
  315.    
  316.     return (void*)matrix_to_return;
  317. }
  318. void Maze_Class::destroy_ll(BFS_node * head_to_destroy){
  319.     BFS_node * previous;
  320.     BFS_node * current;
  321.    
  322.     previous=head_to_destroy;
  323.     current=previous->next_node;
  324.    
  325.     while (current!=0) {
  326.         current=previous->next_node;
  327.         free(previous);
  328.         previous=current;
  329.     }
  330. }
  331. void disp_node(BFS_node * the_node){
  332.     printf("cell: %p \nnext_node: %p \n",the_node->cell,the_node->next_node);
  333. }
  334. void Maze_Class::clear_out_80s(){
  335.     int i1,i2;
  336.     int * cell_pointer;
  337.    
  338.     for (i1=0; i1<size_y; i1++) {
  339.         for (i2=0; i2<size_x; i2++) {
  340.             cell_pointer=&(maze_map[i1*size_x+i2]);
  341.             if (*cell_pointer>=0X80 && *cell_pointer<(size_x*size_y+0X80)) {
  342.                 *cell_pointer=0X01;
  343.             }
  344.         }
  345.     }
  346.     /*/debug//
  347.      printf("you don't have to worry about the 80s any longer \n");//debug//
  348.      disp_maze(1,0,0);//debug/*/
  349. }
  350. BFS_node * Maze_Class::make_link_list(int * ending_spot){
  351.     BFS_node * the_head;
  352.     BFS_node * trav1;
  353.     BFS_node * trav2;
  354.     int value_to_search_for;
  355.     int flag;
  356.     //int temp_counter;//debug//
  357.    
  358.     //temp_counter=0;//debug//
  359.     flag=0;
  360.    
  361.     //disp_maze(0,0,0);
  362.    
  363.     the_head=(BFS_node*)malloc(sizeof(BFS_node));
  364.     the_head->cell=ending_spot;
  365.     the_head->next_node=0;
  366.     trav1=the_head;
  367.     value_to_search_for=*((int*)(the_head->cell))-1;
  368.     //debug//printf("value to search for = 0X%X \n",value_to_search_for);//debug//
  369.    
  370.     while (1) {
  371.         /*/debug//
  372.          temp_counter++;
  373.          printf("making node: %d \n",temp_counter);//debug/*/
  374.         trav2=(BFS_node*)malloc(sizeof(BFS_node));
  375.         trav2->next_node=0;
  376.         trav1->next_node=trav2;
  377.         flag&=0XFE;
  378.         //debug//disp_node(trav1);
  379.         //debug//disp_node(trav2);
  380.         for (int i1=0; i1<size_x*size_y; i1++) {
  381.             //debug//printf("if (maze_map[%d](0X%X)==0X%X ?) \n",i1,maze_map[i1],value_to_search_for);//debug//
  382.             if (maze_map[i1]==value_to_search_for) {
  383.                 trav2->cell=&(maze_map[i1]);
  384.                 trav1=trav2;
  385.                 value_to_search_for--;
  386.                 flag|=0X01;
  387.                 break;
  388.             }
  389.         }
  390.         if (!flag) {
  391.             //debug//printf("uh, not a good error in make_link_list \n");//debug//
  392.             break;
  393.         }
  394.         if (value_to_search_for<0X80) {
  395.             //debug//printf("done, found everything to make ll out of \n");//debug//
  396.             //debug//
  397.             //disp_maze(0,0,0);//debug//
  398.             *(the_head->cell)=0;//I'm cleaning up the 0X80's series to get it back to normal, all others will turn to 0X01, but this one is 0
  399.             clear_out_80s();
  400.             break;
  401.         }
  402.         //debug//sleep(2);//debug//
  403.     }
  404.     return the_head;
  405. }
  406. void * Maze_Class::bfsearch(int from_where_absol, int * mat_size){
  407.     int look;
  408.     int current_step;
  409.     int current_count;
  410.     int flag;
  411.     BFS_node * list_to_return;
  412.     void * matrix;
  413.     //int mat_size;
  414.    
  415.     current_step=from_where_absol;
  416.     current_count=0X80;
  417.     maze_map[current_step]=current_count;
  418.     flag=0X00;
  419.     /*
  420.      *  [0][0][0][0][0][0][0][0]
  421.      *                        ^  we found a cell with the proper 80s value to mark (turns off after every search aka break flag)
  422.      *                     ^     last find for a cell with a certain 80s value, increments 80s value to look, if none found with
  423.      *                                the next value, we have issues
  424.      *                  ^        3rd bit turns on to kick out of the while loop (to make things looking more neat)
  425.      *                          
  426.      *                          
  427.      *                          
  428.      *                          
  429.      */
  430.    
  431.     while (!(flag&0X04)) {
  432.         flag&=0XFE;//flag turns off first bit
  433.         for (int i1=0; i1<size_y; i1++) {
  434.             for (int i2=0; i2<size_x; i2++) {
  435.                 if (maze_map[i1*size_x+i2]==current_count) {
  436.                     current_step=i1*size_x+i2;
  437.                     //debug//printf("I found a cell with the proper value \n");//debug//
  438.                     flag&=0XFD;//turns off 2nd bit
  439.                     flag|=0X01;//turns on 1st bit
  440.                    
  441.                     //debug//printf("flag=0X%X \n",flag);//debug//
  442.                    
  443.                     //flag=&0XFE;//should not be nessesary
  444.                     for (int i3=0; i3<4; i3++) {
  445.                         look=direction(i3, size_x);
  446.                         //debug//printf("look=%d \n",look);
  447.                         if (maze_map[current_step+look]==0X10 || maze_map[current_step+look]==0X12) {
  448.                             //debug//printf("open or undefined wall \n");//debug//
  449.                             if (maze_map[current_step+2*look]==0X01) {
  450.                                 //debug//printf("this spot has been explored, marking \n");//debug//
  451.                                 maze_map[current_step+2*look]=current_count+1;
  452.                                 //debug//disp_maze(0, 0, 0);//debug//
  453.                             }
  454.                             else if (maze_map[current_step+2*look]==0X00) {
  455.                                 //debug//printf("I found a spot that hasn't been traversed! marking cell\n");//debug//
  456.                                 maze_map[current_step+2*look]=current_count+1;
  457.                                 //do stuff
  458.                                 list_to_return=make_link_list(&(maze_map[current_step+2*look]));
  459.                                 matrix=convert_ll_to_mat(list_to_return, mat_size);//writes to mat_size
  460.                                 destroy_ll(list_to_return);
  461.                                 flag|=0X04;
  462.                                 break;
  463.                                 //return matrix;
  464.                             }
  465.                             else if (maze_map[current_step+2*look]>=0X80 && maze_map[current_step+2*look]<=current_count+1) {
  466.                                 //debug//printf("this space has already been marked \n");//debug//
  467.                             }
  468.                             else {
  469.                                 //debug//printf("we have a problem, maze_map[current_step(%d)+2*look(%d)]==0X%X current_count==0X%X \n",current_step,look,maze_map[current_step+2*look],current_count);//debug//
  470.                             }
  471.                         }
  472.                         else {
  473.                             //debug//printf("wall seen:maze_map[%d] 0X%X \n",current_step+look,maze_map[current_step+look]);//debug//
  474.                         }
  475.                     }
  476.                     //debug//sleep(2);//debug//
  477.                     break;
  478.                 }
  479.             }
  480.             if (flag&0X05) {
  481.                 break;
  482.             }
  483.         }
  484.         //debug//printf("supposedly we've gone through all of the BFS with that value, incrementing \n");//debug//
  485.         if (flag&0X02) {
  486.             free(matrix);
  487.             *mat_size=0;
  488.             return 0;
  489.             //debug//printf("we have a serious problem, shut down this program immediately \n");//debug//
  490.             //debug//printf("flag=0X%X current_step=0X%X current_count=0X%X \n",flag,current_step,current_count);//debug//
  491.         }
  492.         //debug//printf("current_count++ \n");//debug//
  493.         current_count++;
  494.         flag|=0X02;//turns on 2nd bit
  495.        
  496.     }
  497.     //debug//printf("we hit end of bfsearch function \n");//debug//
  498.     return matrix;
  499. }
  500. Simulator::Simulator(char**John_maze) : Maze_Class(5, 5) {
  501.     //debug//printf("Simulator::Simulator() : Maze_Class(5, 5)\n");//debug
  502.     int i1,i2;
  503.     char * temp_cell;
  504.     //struct move_node
  505.    
  506.    
  507.     for (i1=0; i1<((int)(size_y-1)/2); i1++) {
  508.         for (i2=0; i2<((int)(size_x-3)/2); i2++) {
  509.             //printf("%d %d ",i1,i2);
  510.             temp_cell=&(John_maze[(1+2*i1)][(2+2*i2)]);
  511.             if (*temp_cell==' '){
  512.                 maze_write_wall(0X10, (i1*2+1)*size_x+i2*2+2);
  513.             }
  514.             else if (*temp_cell=='X'){
  515.                 maze_write_wall(0X11, (i1*2+1)*size_x+i2*2+2);
  516.             }
  517.             else {
  518.                 printf("error write_walls_to_maze \n");
  519.             }
  520.         }
  521.         //printf("\n");
  522.     }
  523.     //printf("\n");
  524.    
  525.     for (i1=0; i1<((int)(size_y-3)/2); i1++) {
  526.         for (i2=0; i2<((int)(size_x-1)/2); i2++) {
  527.             //printf("%d %d ",i1,i2);
  528.             temp_cell=&(John_maze[(2+2*i1)][(1+2*i2)]);
  529.             if (*temp_cell==' '){
  530.                 maze_write_wall(0X10, (i1*2+2)*size_x+i2*2+1);
  531.             }
  532.             else if (*temp_cell=='X'){
  533.                 maze_write_wall(0X11, (i1*2+2)*size_x+i2*2+1);
  534.             }
  535.             else {
  536.                 printf("error write_walls_to_maze \n");
  537.             }
  538.         }
  539.         //printf("\n");
  540.     }
  541.     //printf("\n");
  542. }
  543. Simulator::~Simulator(){
  544.     //debug//printf("Simulator::~Simulator()\n");//debug
  545. }
  546. int Simulator::mouse_sees_wall(mouse_location location_of_wall){
  547.     //debug//printf("int Simulator::mouse_sees_wall(mouse_location %dx %dy)\n",location_of_wall.x,location_of_wall.y);//debug
  548.     using namespace std;
  549.     /* get the abolute map what the user made
  550.      * tell mouse what number 0X?? it is looking at
  551.      * hopefully all numbers will be:
  552.      * 0XFF , OX10, 0X11
  553.      * returning anything else is bad, print out an error
  554.      */
  555.     int returning;
  556.     returning=maze_map[convert_to_absolute(location_of_wall)];
  557.    
  558.     switch (returning) {
  559.         case 0X10:
  560.         case 0X11:
  561.         case 0XFF:
  562.             //good
  563.             break;
  564.         default:
  565.             cout << "mouse_sees_wall error, mouse sees: 0X" << hex << (int)returning
  566.             << " at position: 0X" << (int)(location_of_wall.y*size_x+location_of_wall.x)
  567.             << dec << endl;
  568.             break;
  569.     }
  570.    
  571.     return returning;
  572. }
  573. void Simulator::mouse_went_step(unsigned short int direction){
  574.    
  575.     struct move_node * new_node;
  576.     new_node=(struct move_node*)malloc(sizeof(struct move_node));
  577.     new_node->direction_went=direction;
  578.     new_node->next=0;
  579.     global_current_node->next=new_node;
  580.     global_current_node=global_current_node->next;//or new node
  581.     the_global_move_list.moves_gone++;
  582.    
  583.     //debug//printf("moves gone: %d \ndirection went: %d \npointer assigned: %p \n",the_global_move_list.moves_gone,new_node->direction_went,new_node);
  584.    
  585.     //give John direction
  586.    
  587.     //debug//printf("void Simulator::mouse_went_step(mouse_location %dx %dy)\n",mouse_current_location.x,mouse_current_location.y);//debug
  588.     //send to GUI where the mouse currently is so it knows what to print out
  589. }
  590.  
  591. int direction(int orientation, int size_x){
  592.     //debug//printf("int direction(int %X, int %d)\n",orientation,size_x);//debug
  593.     int scalar;
  594.     int the_signum;
  595.    
  596.     if (orientation%2) {
  597.         scalar=size_x;
  598.     }
  599.     else {
  600.         scalar=1;
  601.     }
  602.     if ((orientation>>1)%2) {
  603.         the_signum=-1;
  604.     }
  605.     else {
  606.         the_signum=1;
  607.     }
  608.     return scalar*the_signum;
  609.    
  610. }
  611. Mouse_Class::Mouse_Class(char**John_maze) : Maze_Class(5,5) {
  612.     //debug//printf("Mouse_Class::Mouse_Class() : Maze_Class(5,5)\n");//debug
  613.     global_current_node=(move_node*)malloc(sizeof(move_node));
  614.     global_current_node->next=0;
  615.     global_current_node->direction_went=4;
  616.     the_global_move_list.moves_gone=1;
  617.     the_global_move_list.head=global_current_node;
  618.     the_global_move_list.moves_gone=0;
  619.     init_mouse(John_maze);
  620.     set_cheese(John_maze);
  621. }
  622. Mouse_Class::~Mouse_Class(){}
  623. void Mouse_Class::init_mouse(char**maze_matrix){
  624.     //debug//printf("int Mouse_Class::init_mouse()\n");//debug
  625.     //struct mouse_location mouse_to_start;
  626.     //int to_return;
  627.     //take in mouse locations
  628.     //mouse_to_start=get_starting_mouse_location();
  629.     //to_return=confirm_place(mouse_to_start);
  630.     //if (to_return) {
  631.     //mouse=mouse_to_start;
  632.     //this->mouse_start=mouse_to_start;
  633.     //}
  634.     //return to_return;
  635.    
  636.     for (int i1=0; i1<size_y; i1++) {
  637.         for (int i2=0; i2<size_x; i2++) {
  638.             if (maze_matrix[i1][i2]=='m') {
  639.                 mouse.y=i1;
  640.                 mouse.x=i2;
  641.                 mouse_start=mouse;
  642.                 return;
  643.             }
  644.         }
  645.     }
  646.     printf("error no mouse \n");
  647.    
  648. }
  649. void Mouse_Class::set_cheese(char**maze_matrix){
  650.     //debug//printf("int Mouse_Class::set_cheese()\n");//debug
  651.     //struct mouse_location cheese_to_set;
  652.     //int to_return;
  653.     //cheese_to_set=write_cheese_location();
  654.    
  655.     for (int i1=0; i1<size_y; i1++) {
  656.         for (int i2=0; i2<size_x; i2++) {
  657.             if (maze_matrix[i1][i2]=='g') {
  658.                 cheese.y=i1;
  659.                 cheese.x=i2;
  660.                 return;
  661.             }
  662.         }
  663.     }
  664.    
  665.     printf("no cheese \n");
  666.    
  667.     //to_return=confirm_place(cheese_to_set);
  668.     //if (to_return) {
  669.     //cheese=cheese_to_set;
  670.     //}
  671.     //return to_return;
  672. }
  673. int Mouse_Class::confirm_place(struct mouse_location cheese_or_mouse){
  674.     //debug//printf("int Mouse_Class::confirm_place(struct mouse_location %dx %dy)",cheese_or_mouse.x,cheese_or_mouse.y);
  675.     //no longer using //using namespace b_input;
  676.     if (maze_map[cheese_or_mouse.y*size_x+cheese_or_mouse.x]!=0X00) {
  677.         //incorrect_input(0);
  678.         printf("error confirm_place \n");
  679.        
  680.         return 0;
  681.     }
  682.     else {
  683.         //it wortked
  684.         return 1;
  685.     }
  686.     return 0;
  687. }
  688. int Mouse_Class::go_number_of_spaces(int spaces_and_orientation, Simulator * sim){
  689.     //debug//printf("int Mouse_Class::go_number_of_spaces(int %X, Simulator * %p)\n",spaces_and_orientation,sim);//debug
  690.     //debug//printf("go_number_of_spaces(spaces_and_orientation==%X, sim==%p) \n",spaces_and_orientation,sim);//debug
  691.     /*
  692.      *  [0][0][0][0] [0][0][0][0]
  693.      *         ^  ^   ^  ^  ^  ^ spaces to go
  694.      *                           
  695.      *   ^  ^                    orientation
  696.      *             
  697.      */
  698.     /*
  699.      * spaces range from 00-3F, why you would want to go 0 spaces, I don't know
  700.      * 63 being the max, if you have a bigger map, you will need to reconfigure
  701.      * the type to be some bigger unsigned, & make the orientation bits the 2
  702.      * highest bits of the type
  703.      * the orientation bits, being the 2 highest bits are 0-3
  704.      * 0=East
  705.      * 1=North
  706.      * 2=West
  707.      * 3=South
  708.      *
  709.      *
  710.      */
  711.    
  712.     int spaces;
  713.     int orientation;
  714.     int spaces_traveled;
  715.    
  716.     spaces=spaces_and_orientation%0X40;
  717.     orientation=spaces_and_orientation>>(6);
  718.    
  719.     spaces_traveled=0;
  720.     for (int i1=0; i1<spaces; i1++) {
  721.         spaces_traveled+=go_mouse(orientation,sim);
  722.     }
  723.     return spaces_traveled;
  724. }
  725. int Mouse_Class::go_mouse(int orientation, Simulator * sim){
  726.    
  727.    
  728.     //debug//
  729.     //debug//printf("int Mouse_Class::go_mouse(int %X, Simulator * %p)\n",orientation,sim);//debug
  730.     /*
  731.      *  mouse will travel 1 space  
  732.      * 0=East    - east is + 1 on the maze_map
  733.      * 1=south1   - north is + size_x on maze_map
  734.      * 2=West    - west is -1
  735.      * 3=north1   - south is -size_x
  736.      * >=4       - error, no go.
  737.      *  
  738.      */
  739.     using namespace std;
  740.    
  741.     int l_a_w_raw;
  742.     int wall_seen;
  743.     int go_through_wall;
  744.    
  745.     if (orientation>3) {
  746.         cout << "invalid orientation, I'm going nowhere" << endl;
  747.         return 0;
  748.     }
  749.     maze_write_cell(convert_to_absolute(mouse));
  750.    
  751.     //scan this area
  752.     //get the forward & sides of this area & make sure it either:
  753.     //matches the map or is undefined (wall)
  754.     for (int i1=0; i1<3; i1++) {
  755.         l_a_w_raw=mouse.y*size_x+mouse.x+direction((i1+orientation+3)%4, size_x);
  756.         //debug//printf("l_a_w_raw==%d=%d*%d+%d+direction((%d+%d+3)%%4, %d)\n", l_a_w_raw, mouse.y, size_x, mouse.x, i1, orientation, size_x);//debug
  757.         wall_seen=sim->mouse_sees_wall(convert_from_absolute(l_a_w_raw));
  758.         maze_write_wall(wall_seen,l_a_w_raw);
  759.     }
  760.     go_through_wall=convert_to_absolute(mouse)+direction(orientation, size_x);
  761.     //debug//printf("go_through_wall==%d=%d+direction(orientation, size_x) \nmaze_map[go_through_wall]==%d \n",go_through_wall,convert_to_absolute(mouse),maze_map[go_through_wall]);//debug
  762.     if (maze_map[go_through_wall]==0X10) {
  763.         //there is no wall here & we may pass through
  764.         go_through_wall=go_through_wall+direction(orientation, size_x);//done a second time
  765.         mouse=convert_from_absolute(go_through_wall);//landed on next white space
  766.         sim->mouse_went_step(orientation);
  767.         //debug//printf("ping \n");
  768.         //disp_maze(3, &mouse, &cheese);
  769.         return 1;
  770.     }
  771.     else {
  772.         //debug//printf("saw wall \n");//debug//
  773.         //I see a wall or something else which is wrong. so I go nowhere
  774.         //return 0;
  775.     }
  776.     return 0;
  777. }
  778. /*
  779.  *  Mouse logic
  780.  *  mouse travels down way, marks territory
  781.  *  mouse knows paths traveled & paths not traveled
  782.  *  mouse knows how many steps from path untraveled to get back onto untraveled path (from dead end or something)
  783.  *  mouse travels DFS until it runs into dead end or path already traveled
  784.  *  then execute BFS to find shortest path to unexplored route !!! provided it is within maze reach (submaze which is made after a route to finish is found)
  785.  *  mouse finds all available paths to finish from start point.
  786.  *  Execute BFS to find shortest route from start to finish
  787.  *  send mouse to start, send mouse through route found to finish. terminate program.
  788.  */
  789. /*
  790.  *  I don't need to keep track of how many spaces the mouse has gone because I'm just looking around at the walls & stuff;
  791.  *  track of spaces wil occur when I perform BFS's & DFS's
  792.  */
  793. /*
  794.  *  ###########  
  795.  *  # + + + + #
  796.  *  #+@+@+@+@+#
  797.  *  # + + + + #
  798.  *  #+@+@+@+@+#
  799.  *  # + + + + #
  800.  *  #+@+@+@+@+#
  801.  *  # + + + + #
  802.  *  #+@+@+@+@+#
  803.  *  # + + + + #
  804.  *  ###########  
  805.  */
  806. int Mouse_Class::which_way_unexplored(){
  807.     //debug//printf("int Mouse_Class::which_way_unexplored()\n");//debug
  808.     int mouse_absolute;
  809.     int available_travel;
  810.     int location_relative;
  811.     int prospective_location;
  812.    
  813.     mouse_absolute=convert_to_absolute(mouse);
  814.     available_travel=0;
  815.    
  816.     for (int i1=0; i1<4; i1++) {
  817.         location_relative=2*direction(i1, size_x);
  818.         //debug//printf("location relative %d \n",location_relative);//debug//
  819.         if (
  820.             (
  821.              ((i1%2)==0)//eastwest
  822.              &&
  823.              (
  824.               (
  825.                (location_relative+mouse.x)>=size_x)//greater than 5
  826.               ||
  827.               ((location_relative+mouse.x)<0)//less than 0
  828.               )
  829.              )
  830.             ||
  831.             (
  832.              ((i1%2)==1)//northsouth
  833.              &&
  834.              (
  835.               ((location_relative+mouse.y*size_x)>=size_x*size_y)//greater than size of maze
  836.               ||
  837.               ((location_relative+mouse.y*size_x)<0)//less than zero
  838.               )
  839.              )
  840.             ) {
  841.             //debug//printf("this is off map & invalid \n");//debug//
  842.             //this is off the map, & is invalid
  843.         }
  844.         else {
  845.             //debug//printf("if (maze_map[mouse_absolute(%d)+location_relative(%d)/2]==0X%X!=0X11) \n",mouse_absolute,location_relative,maze_map[mouse_absolute+location_relative/2]);//debug//
  846.             if (maze_map[mouse_absolute+location_relative/2]!=0X11) {
  847.                 //not a wall
  848.                 available_travel+=(1<<(4+i1));
  849.             }
  850.             else {
  851.                 //debug//printf("bumped into wall \n");//debug//
  852.                 //nothing
  853.             }
  854.            
  855.             prospective_location=mouse_absolute+location_relative;
  856.             //debug//printf("prospective_location(%d)=mouse_absolute(%d)+location_relative(%d) \n",prospective_location,mouse_absolute,location_relative);//debug//
  857.             //debug//printf("maze_map[prospective_location(%d)]==%d \n",prospective_location,maze_map[prospective_location]);//debug//
  858.             if (maze_map[prospective_location]==0X00) {
  859.                 //debug//printf("nvr been here before \n");//debug//
  860.                 //nothing
  861.             }
  862.             else if (maze_map[prospective_location]==0X01) {
  863.                 //debug//printf("traveled \n");//debug//
  864.                 //traveled before, mark it so
  865.                 available_travel+=(1<<i1);
  866.             }
  867.             else {
  868.                 //debug//printf("I think I had an error \n");//debug//
  869.             }
  870.         }
  871.     }
  872.     //debug//printf("returning 0X%X == available_trqavel \n",available_travel);//debug//
  873.     return available_travel;
  874. }
  875. int Mouse_Class::look_down_maze_to_see_where_stop(int orientation){
  876.     //debug//printf("int Mouse_Class::look_down_maze_to_see_where_stop(int %d)\n",orientation);//debug//
  877.     int crosshair_start;
  878.     int potential_mouse_path[5];
  879.     int mouse_path_start;
  880.     int cheese_crosshair[5];
  881.     int mouse_x_or_y;
  882.    
  883.     /*
  884.      *  here we need to know where the goal is located & which hallway the mouse wants to pass down
  885.      *  with this info, we will pass back how many spaces are possible to go down the hallway until the
  886.      *  mouse becomes orthoganal to the goal, in which case, I am sure the mouse will want to turn.
  887.      *  hmm, I am now thinking that if the space towards the goal is unexplored, I will stop the mouse
  888.      *  but if the space has already been explored, I will ignore it & keep sending the mouse on forward
  889.      */
  890.     /*
  891.      *  # # # # # # # # # # #  
  892.      *  #12 +14 +16 +18 +20 #
  893.      *  # + @ + @ + @ + @ + #
  894.      *  #34 +36 +38 +40 +42 #
  895.      *  # + @ + @ + @ + @ + #
  896.      *  #56 +58 +60 +62 +64 #
  897.      *  # + @ + @ + @ + @ + #
  898.      *  #78 +80 +82 +84 +86 #
  899.      *  # + @ + @ + @ + @ + #
  900.      *  #100+102+104+106+108#
  901.      *  # # # # # # # # # # #  
  902.      */
  903.     if ((orientation%2)==0) {//mouse is facing east/west
  904.         //debug//printf("east/west \n");//debug//
  905.         mouse_x_or_y=(mouse.x-1)/2;
  906.         crosshair_start=cheese.x+size_x;
  907.         mouse_path_start=mouse.y*size_x+1;
  908.         for (int i1=0; i1<(size_y-1)/2; i1++) {
  909.             //debug//printf("i1=%d \n",i1);//debug//
  910.             cheese_crosshair[i1]=crosshair_start+size_x*2*i1;
  911.             potential_mouse_path[i1]=mouse_path_start+2*i1;
  912.         }
  913.         //debug//printf("confirm \n");//debug//
  914.     }
  915.     else {//mouse is facing north/south
  916.         //debug//printf("north/sourth \n");//debug
  917.         mouse_x_or_y=(mouse.y-1)/2;
  918.         mouse_path_start=mouse.x+size_x;
  919.         crosshair_start=cheese.y*size_x+1;
  920.         for (int i1=0; i1<(size_x-1)/2; i1++) {
  921.             cheese_crosshair[i1]=crosshair_start+2*i1;
  922.             potential_mouse_path[i1]=mouse_path_start+size_x*2*i1;
  923.         }          
  924.     }
  925.     //alright, now we have the indexes of where we will stop
  926.     for (int i1=0; i1<5; i1++) {//these are 5 now, if we want variable size mazes, fix this.
  927.         for (int i2=0; i2<5; i2++) {
  928.             //debug//printf("if (potential_mouse_path[%d](%d)==cheese_crosshair[%d](%d)) \n",i1,potential_mouse_path[i1],i2,cheese_crosshair[i2]);//debug//
  929.             if (potential_mouse_path[i1]==cheese_crosshair[i2]) {
  930.                 //debug//printf("I found path in %d steps \n",mouse_x_or_y-i1);//debug//
  931.                 //if true, i1 is the amount of steps it takes to get there.
  932.                 return (int)(b_math::abs(mouse_x_or_y-i1));
  933.             }
  934.         }
  935.     }          
  936.     //debug//std::cout << "apparently no way found in that direction. returning 5" << std::endl;
  937.     return 5;
  938. }
  939. unsigned short int Mouse_Class::get_mouse_back_to_unexplored(Simulator * sim){
  940.     void * move_matrix;
  941.     int matsize;
  942.     int flag;
  943.     //int look;
  944.     /*
  945.      *  this function is designed to put the mouse back on track
  946.      *  after it has explored all possible routes in a certain area
  947.      *  think of explored routes as "yellow" & unexplored routes as
  948.      *  "white". When an area become predominantly yellow, especially
  949.      *  at an intersection, the mouse will seek out the nearest white
  950.      *  area/square which is nearby the goal. When the square of white
  951.      *  is calculated, the mouse will have to figure out how to get there
  952.      *  I suppose the mouse could perform a BFS until it sees a wall
  953.      *  of unknown type. ... wait, if it sees a wall of unknown type,
  954.      *  chances are it has not been on that side of the wall
  955.      *  it will be tricky to navigate to a well-thought out square, but
  956.      *  try something that works
  957.      */
  958.     flag=0;
  959.     move_matrix=bfsearch(convert_to_absolute(mouse),&matsize);
  960.     if (matsize==0){
  961.         return 0;
  962.     }
  963.     /*/debug//
  964.      for (int i1=0; i1<matsize; i1++) {
  965.      printf("move_matrix==%p, move_matrix[%d]==%p, *(move_matrix[%d])==0X%X \n",move_matrix,i1,((int**)move_matrix)[i1],i1,*(((int**)move_matrix)[i1]));//debug//
  966.      }//debug/*/
  967.    
  968.     /*
  969.      *  this move_matrix contains the cells in which to properly traverse
  970.      *  to get to the cell where you want to go, use go mouse. to find the
  971.      *  spot to go, comb the 4 spots around where mouse is currently located
  972.      *  looking for the pointer value of that matrix cell. then go mouse in that
  973.      *  direction
  974.      *  IMPORTANT!!: all go_mouses must return 1 until you come across an undef wall.
  975.      *  once coming to an undef wall, if go_mouse returns zero, then everything is out the
  976.      *  window & you are now left to fend for yourself (aka return to normal mouse nav functions)
  977.      *  at the very least, mouse should figure out if wall exists or not.
  978.      */
  979.     if (((int**)move_matrix)[0]==&(maze_map[convert_to_absolute(mouse)])) {
  980.         for (int i1=1; i1<matsize; i1++) {
  981.             for (int i2=0; i2<4; i2++) {
  982.                 if (((int**)move_matrix)[i1]==&(maze_map[convert_to_absolute(mouse)+2*direction(i2, size_x)])) {
  983.                     flag|=go_mouse(i2, sim);
  984.                     //debug//printf("flag=0X%X \n",flag);//debug//
  985.                     //disp_maze(3, &mouse, &cheese);
  986.                     break;
  987.                 }
  988.             }
  989.         }
  990.     }
  991.     else {
  992.         //debug//printf("we are in trouble, stop program \n");
  993.     }
  994.     free(move_matrix);
  995.     return 1;
  996. }
  997. void Mouse_Class::mark_path_with_green(){
  998.     void * green_mat;
  999.     int green_size;
  1000.    
  1001.     for (int i1=0; i1<size_y; i1++) {
  1002.         for (int i2=0; i2<size_x; i2++) {
  1003.             if (maze_map[i1*size_x+i2]==0X00) {
  1004.                 maze_map[i1*size_x+i2]=0X01;
  1005.             }
  1006.         }
  1007.     }
  1008.     maze_map[convert_to_absolute(mouse_start)]=0X00;
  1009.     /*/debug//
  1010.     disp_maze(1,0,0);
  1011.     printf("bfsearch %d %p\n",convert_to_absolute(mouse),&green_size);//debug/*/
  1012.     green_mat=bfsearch(convert_to_absolute(mouse),&green_size);
  1013.     /*/debug//
  1014.     printf("green \n");
  1015.     printf("green_size %d \n",green_size);//debug/*/
  1016.     for (short unsigned int i1=0; i1<green_size; i1++){
  1017.         *(((short unsigned int **)(green_mat))[i1])=0X03;
  1018.     }
  1019.     free(green_mat);
  1020.     for (unsigned short int i1=0; i1<11; i1++) {
  1021.         for (unsigned short int i2=0; i2<11; i2++) {
  1022.             if (maze_map[i1*size_x+i2]==0X01) {
  1023.                 maze_map[i1*size_x+i2]=0X05;
  1024.             }
  1025.         }
  1026.     }
  1027.     return;
  1028. }
  1029. void Mouse_Class::get_back_to_start(Simulator * sim){
  1030.     short int look;
  1031.     //mouse_location old_mouse;
  1032.     while (maze_map[convert_to_absolute(mouse)]!=0X00){
  1033.         maze_map[convert_to_absolute(mouse)]=0X00;
  1034.         //debug//printf("maze_map[convert_to_absolute(mouse)(%d)](%d)!=0X00 \n",convert_to_absolute(mouse),maze_map[convert_to_absolute(mouse)]);
  1035.         //debug//printf("ping \n");//debug//
  1036.         for (unsigned short int i1=0; i1<4; i1++){
  1037.             look=direction(i1,size_x);
  1038.             //debug//printf("if (maze_map[convert_to_absolute(mouse)(%d)+look(%d)](%X)==0X10)\n",convert_to_absolute(mouse),look,maze_map[convert_to_absolute(mouse)+look]);
  1039.             if (maze_map[convert_to_absolute(mouse)+look]==0X10){
  1040.                 if (maze_map[convert_to_absolute(mouse)+look*2]==0X03){
  1041.                     //old_mouse=mouse;
  1042.                     go_mouse(i1,sim);
  1043.                     //maze_map[convert_to_absolute(old_mouse)]==0X00;
  1044.                     break;
  1045.                 }
  1046.             }
  1047.         }
  1048.         //debug//
  1049.         //disp_maze(3,&mouse,&cheese);
  1050.     }
  1051.     //mouse now back at home. the end
  1052.     return;
  1053.    
  1054. }
  1055. /*
  1056. void Mouse_Class::go_straight_to_finish(Simulator * sim){
  1057.     short int look;
  1058.     mouse_location old_mouse;
  1059.  
  1060.     printf("ping \n");
  1061.     while (convert_to_absolute(mouse)!=convert_to_absolute(cheese)){
  1062.         old_mouse=mouse;
  1063.         for (unsigned short int i1=0; i1<4; i1++){
  1064.             look=direction(i1,size_x);
  1065.             if (maze_map[convert_to_absolute(mouse)+look]==0X10){
  1066.                 if (maze_map[convert_to_absolute(mouse)+look*2]==0X01){
  1067.                     //maze_map[convert_to_absolute(mouse)]=0X03;
  1068.                     go_mouse(i1,sim);
  1069.                     break;
  1070.                 }
  1071.             }
  1072.         }
  1073.         maze_map[convert_to_absolute(old_mouse)]==0X03;
  1074.         disp_maze(1,&mouse,&cheese);
  1075.         getchar(); 
  1076.     }
  1077.     //mouse now back at cheese. the end
  1078.     return;
  1079.    
  1080. }
  1081.  */
  1082. void Mouse_Class::main_mouse_function(Simulator * sim) {
  1083.    
  1084.     unsigned char available_travel;
  1085.     unsigned char unexplored;
  1086.     unsigned char tally_unexplored;
  1087.     unsigned char mouse_went_some_spaces;
  1088.     unsigned char i1;
  1089.     unsigned char orient_and_travel[4];
  1090.    
  1091.    
  1092.     while (1) {
  1093.         //have I achieved cheese yet?
  1094.         if (convert_to_absolute(mouse)==convert_to_absolute(cheese)) {
  1095.            
  1096.             /*/debug//
  1097.             std::cout << "YAY" << std::endl;//debug/*/
  1098.             //do stuff
  1099.            
  1100.             mark_path_with_green();
  1101.             get_back_to_start(sim);
  1102.             //go_straight_to_finish(sim);
  1103.            
  1104.             return;
  1105.         }
  1106.         /*
  1107.          *  BORDER WALL =         0XFF
  1108.          *  INNER WALL DNE =      0X10
  1109.          *  INNER WALL EXISTS =   0X11
  1110.          *  INNER WALL UNKNOWN =  0X12
  1111.          *  WALL CONNECTION =     0X20  
  1112.          *  STEPS:
  1113.          *  UNEXPLORED =          0X00
  1114.          *  EXPLORED =            0X01
  1115.          *  FINISH =              0X02
  1116.          *  SHORTEST ROUTE =      0X03
  1117.          *  MOUSE =               0X04
  1118.          *  
  1119.          *  
  1120.          */    
  1121.        
  1122.         //have I already explored my available spaces?
  1123.         /*
  1124.          *  [0][0][0][0] [0][0][0][0]
  1125.          *                         ^  east
  1126.          *                      ^     north
  1127.          *                   ^        west
  1128.          *                ^           sout
  1129.          *            ^               e-valid
  1130.          *         ^                  n-valid
  1131.          *      ^                     w-valid
  1132.          *   ^                        s-valid
  1133.          *  a good value will be 0XF? where ? is anything, unexplored area yields 0, & explored 1
  1134.          *  if 0X?% ? is not F, it means you are close to an outer boundary in which
  1135.          *  you will not be permitted to venture that way under any circumstance
  1136.          */
  1137.        
  1138.        
  1139.         available_travel=which_way_unexplored();
  1140.         unexplored=(available_travel>>4)&((unsigned char)(~available_travel));
  1141.         //debug//printf("unexplored==%X=(%X)&(%X) \n",unexplored,available_travel>>4,(int)(~available_travel));//debug//
  1142.         tally_unexplored=0;
  1143.         for (i1=0; i1<4; i1++) {
  1144.             tally_unexplored+=((unexplored>>i1)%2);
  1145.             //printf("tally_unexplored==%d+=((%d>>%d)%%2) \n",tally_unexplored,unexplored,i1);//debug
  1146.         }
  1147.         //now only last 4 bits of unexplored are used
  1148.         //debug//printf("%d ways unexplored \n",tally_unexplored);//debug
  1149.         if ((tally_unexplored>=1) && (tally_unexplored<=4)) {
  1150.            
  1151.             for (i1=0; i1<4; i1++) {
  1152.                
  1153.                 if ((unexplored>>i1)%2) {
  1154.                     orient_and_travel[i1]=i1<<6;
  1155.                     //debug//printf("orient&travel[%d]=%X\n",i1,orient_and_travel[i1]);//debug//
  1156.                     //how many spaces do I want to go?
  1157.                     orient_and_travel[i1]+=look_down_maze_to_see_where_stop(i1);
  1158.                     if (!(orient_and_travel[i1]%0X40)) {
  1159.                         orient_and_travel[i1]++;
  1160.                        
  1161.                     }
  1162.                     //debug//printf("orient&travel[%d]=%X\n",i1,orient_and_travel[i1]);//debug//
  1163.                     //mouse_went_some_spaces=go_number_of_spaces(orient_and_travel[i1]);
  1164.                 }
  1165.                 else {
  1166.                     orient_and_travel[i1]=0;
  1167.                 }
  1168.                
  1169.             }
  1170.             int min_of_travel;
  1171.             int min_dir;
  1172.             min_of_travel=size_x+size_y;
  1173.             min_dir=-1;
  1174.             for (i1=0; i1<4; i1++) {
  1175.                 //debug//printf("orient&travel[%d]==%X \n",i1,orient_and_travel[i1]);
  1176.                 if ((orient_and_travel[i1])%0X40) {
  1177.                     //debug//printf("orient&travel[%d]%%40==%X \n",i1,orient_and_travel[i1]%40);//debug//
  1178.                     if (min_of_travel>((orient_and_travel[i1])%0X40)) {
  1179.                         min_of_travel=((orient_and_travel[i1])%0X40);
  1180.                         min_dir=i1;
  1181.                     }
  1182.                     //
  1183.                 }
  1184.             }
  1185.             mouse_went_some_spaces=go_number_of_spaces(orient_and_travel[min_dir],sim);//ok we just went some spaces
  1186.             //debug//printf("I went %d spaces \n",mouse_went_some_spaces);//debug//
  1187.         }
  1188.         else if (tally_unexplored==0) {
  1189.             //debug//printf("all ways are explored \n");//debug//
  1190.             /*
  1191.              *  special case, I've been everywhere before. within the boundary I've set for myself between what I've explored & the finish, if it
  1192.              *  is incomplete, then I must escort myself to finish it. If it is complete, then I should go check out the nearest (apparent) area of
  1193.              *  unexplored territory, because, there HAS to be unexplored territory within the boundary provided I have managed to search all around
  1194.              *  the cheese without finding it, right, there has to be a path I haven't taken... I hope.
  1195.              */
  1196.             if (!get_mouse_back_to_unexplored(sim)){
  1197.                 return;
  1198.             }
  1199.         }
  1200.         else {
  1201.             //debug//printf("it's broken \n");//debug//
  1202.         }
  1203.         //ok, now we have probably just gone some way, we stored how many spaces the mouse went.
  1204.         //disp_maze(3, &mouse, &cheese);
  1205.         //getchar();
  1206.         //debug//sleep(2);/debug//
  1207.     }
  1208.     return;
  1209.    
  1210. }
  1211. void Maze_Class::disp_maze(int properties, mouse_location * mouse, mouse_location * cheese){
  1212.     //debug//printf("void Maze_Class::disp_maze(int %X, mouse_location * %p, mouse_location * %p)\n",properties,mouse,cheese);//debug//
  1213.     int temps[4];
  1214.    
  1215.     /*
  1216.      *  [0][0][0][0] [0][0][0][0]
  1217.      *                         ^ 1 pretty, 0 raw info
  1218.      *                      ^    1 show mouse & goal, 0 no show
  1219.      *                   ^       
  1220.      *                ^          
  1221.      */
  1222.     using namespace std;
  1223.    
  1224.     if (properties&0X02) {
  1225.         temps[0]=convert_to_absolute(*mouse);
  1226.         temps[1]=maze_map[temps[0]];
  1227.         temps[2]=convert_to_absolute(*cheese);
  1228.         temps[3]=maze_map[temps[2]];
  1229.         maze_map[temps[0]]=0X04;
  1230.         maze_map[temps[2]]=0X02;
  1231.     }
  1232.     if ((properties&0X01)) {
  1233.         //print out something pretty
  1234.         for (int i1=0; i1<size_y; i1++) {
  1235.             for (int i2=0; i2<size_x; i2++) {
  1236.                 switch (maze_map[(size_y-1-i1)*size_x+i2]) {
  1237.                     case 0X00:
  1238.                         cout << " ";
  1239.                         break;
  1240.                     case 0X01:
  1241.                         cout << ".";
  1242.                         break;
  1243.                     case 0X04:
  1244.                         cout << "M";
  1245.                         break;
  1246.                     case 0X02:
  1247.                         cout << "G";
  1248.                         break;
  1249.                     case 0X03:
  1250.                         cout << "$";
  1251.                         break;
  1252.                     case 0X10:
  1253.                         cout << "-";
  1254.                         break;
  1255.                     case 0X11:
  1256.                         cout << "+";
  1257.                         break;
  1258.                     case 0X12:
  1259.                         cout << "*";
  1260.                         break;
  1261.                     case 0X20:
  1262.                         cout << "@";
  1263.                         break;
  1264.                     case 0XFF:
  1265.                         cout << "#";
  1266.                         break;
  1267.                     default:
  1268.                         cout << "?";
  1269.                         break;
  1270.                 }
  1271.             }
  1272.             cout << endl;
  1273.         }
  1274.         cout << endl;
  1275.     }
  1276.     else {
  1277.         //cout << hex << uppercase << fixed;
  1278.         for (int i1=0; i1<size_y; i1++) {
  1279.             for (int i2=0; i2<size_x; i2++) {
  1280.                 //cout << (int)(maze_map[i1*size_x+i2]) << " ";
  1281.                 printf("%.2X ",maze_map[(size_y-1-i1)*size_x+i2]);
  1282.             }
  1283.             //cout << endl;
  1284.             printf("\n");
  1285.         }
  1286.         //cout << endl << dec;
  1287.         printf("\n");
  1288.     }
  1289.     if (properties&0X02) {
  1290.         maze_map[temps[0]]=temps[1];
  1291.         maze_map[temps[2]]=temps[3];
  1292.     }
  1293. }
  1294. //mouse_maze.cpp
  1295.  
  1296. char**Johns_maze_matrix;
  1297.  
  1298. Mouse_Class * my_mouse;
  1299. Simulator * my_sim;
  1300.  
  1301. void clearmaze(char**defaultmaze){//clear the random junk out of the array
  1302.     for(int i = 0; i < 11; i++){
  1303.         for(int j = 0; j < 11; j++){
  1304.             //blankmaze[i][j] = ' ';
  1305.             defaultmaze[i][j] = ' ';
  1306.         }
  1307.     }
  1308. }
  1309. void build_default_walls(char**defaultmaze){
  1310.     //creates the default maze and changes adds or removes walls
  1311.    
  1312.     //  Default maze:
  1313.    
  1314.     //  XXXXXXXXXXX
  1315.     //  X X       X
  1316.     //  X X X XXXXX
  1317.     //  X   X     X
  1318.     //  X X XXX XXX
  1319.     //  X X   X   X
  1320.     //  XXX XXXXX X
  1321.     //  X   X     X
  1322.     //  X XXXXXXXXX
  1323.     //  X         X
  1324.     //  XXXXXXXXXXX
  1325.    
  1326.     if (defaultmaze[0][0] != 'X'){
  1327.         clearmaze(defaultmaze);
  1328.         for (int i = 0; i < 11; i++){
  1329.             defaultmaze[0][i] = 'X';
  1330.             defaultmaze[i][0] = 'X';
  1331.             defaultmaze[10][i] = 'X';
  1332.             defaultmaze[i][10] = 'X';
  1333.         }
  1334.         for (int i = 1; i < 3; i++){
  1335.             defaultmaze[i][2] = 'X';
  1336.         }
  1337.         for (int i = 6; i < 10; i++){
  1338.             defaultmaze[2][i] = 'X';
  1339.         }
  1340.         for (int i = 2; i < 5; i++){
  1341.             defaultmaze[i][4] = 'X';
  1342.         }
  1343.         for (int i = 5; i < 10; i++){
  1344.             defaultmaze[4][i] = 'X';
  1345.         }
  1346.         for (int i = 1; i < 9; i++){
  1347.             defaultmaze[6][i] = 'X';
  1348.         }
  1349.         for (int i = 2; i < 10; i++){
  1350.             defaultmaze[8][i] = 'X';
  1351.         }
  1352.         for (int i = 4; i < 6; i++){
  1353.             defaultmaze[i][2] = 'X';
  1354.         }
  1355.         defaultmaze[5][6] = 'X';
  1356.         defaultmaze[7][4] = 'X';
  1357.         defaultmaze[4][7] = ' ';
  1358.         defaultmaze[6][3] = ' ';
  1359.         defaultmaze[1][1] = 'm';
  1360.         defaultmaze[7][5] = 'g';
  1361.     }
  1362.    
  1363. }
  1364. void initialize_maze(){
  1365.    
  1366.     Johns_maze_matrix=(char**)malloc(sizeof(char*)*11);
  1367.     for (int i=0; i<11; i++) {
  1368.         Johns_maze_matrix[i]=(char*)malloc(sizeof(char)*11);
  1369.     }
  1370.     clearmaze(Johns_maze_matrix);
  1371.     build_default_walls(Johns_maze_matrix);
  1372.     return;
  1373. }
  1374. void initiate_sim_mouse(){
  1375.     my_mouse=new Mouse_Class(Johns_maze_matrix);
  1376.     my_sim=new Simulator(Johns_maze_matrix);
  1377.     return;
  1378. }
  1379. int get_move(){
  1380.    
  1381.     unsigned short int the_move;
  1382.     move_node * temp_node;
  1383.     temp_node=the_global_move_list.head;
  1384.     if (temp_node==0){
  1385.         printf("ping \n");
  1386.         return 4;
  1387.     }
  1388.     the_move=temp_node->direction_went;
  1389.     the_global_move_list.head=temp_node->next;
  1390.     free(temp_node);
  1391.     return the_move;
  1392.     //return rand()%4;
  1393.    
  1394. }
  1395. void do_everything(){
  1396.     initialize_maze();
  1397.     initiate_sim_mouse();
  1398.     my_mouse->main_mouse_function(my_sim);//call the main mouse function by passing in the reference to your simulator
  1399.     printf("throw away: %d \n",get_move());
  1400. }
  1401. int main() {
  1402.     int show=0;
  1403.     do_everything();
  1404.     while (show!=4) {
  1405.         show=get_move();
  1406.         printf("show=%d \n",show);
  1407.     }
  1408.     printf("done \n");
  1409.    
  1410.    
  1411.     return 0;
  1412. }
Advertisement
Add Comment
Please, Sign In to add comment