Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "Blake_Math.h"
- typedef struct BFS_node BFS_node;
- struct BFS_node {
- int * cell;
- BFS_node * next_node;
- };
- typedef struct move_node move_node;
- typedef struct move_list move_list;
- struct move_node {
- move_node * next;
- unsigned short int direction_went;
- };
- struct move_list {
- unsigned int moves_gone;
- move_node * head;
- };
- struct mouse_location {
- int x;
- int y;
- };
- move_node * global_current_node;
- move_list the_global_move_list;
- class Maze_Class {
- private:
- protected:
- public:
- void initialize_maze();
- /*
- * ###########
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * ###########
- */
- /*
- * BORDER WALL = 0XFF
- * INNER WALL DNE = 0X10
- * INNER WALL EXISTS = 0X11
- * INNER WALL UNKNOWN = 0X12
- * WALL CONNECTION = 0X20
- * STEPS:
- * UNEXPLORED = 0X00
- * EXPLORED = 0X01
- * FINISH = 0X02
- * SHORTEST ROUTE = 0X03
- * MOUSE = 0X04
- *
- *
- */
- int * maze_map;//our map of the maze
- int size_x;
- int size_y;
- Maze_Class(int xside, int yside);
- ~Maze_Class();
- void * convert_ll_to_mat(BFS_node * linklist_head, int * write_to_mat_size);
- void destroy_ll(BFS_node * head_to_destroy);
- void * bfsearch(int from_where_absol, int * mat_size);
- BFS_node * make_link_list(int * ending_spot);
- int convert_to_absolute(mouse_location from_m_l);
- mouse_location convert_from_absolute(int from_abs);
- int maze_write_wall(int type_seen, int location);
- void maze_write_cell(int location);
- void clear_out_80s();
- void disp_maze(int properties, mouse_location * mouse, mouse_location * cheese);
- };
- class Simulator : public Maze_Class {
- private:
- protected:
- public:
- //Maze_Class absolute_maze(5,5);
- Simulator(char**John_maze);
- ~Simulator();
- int mouse_sees_wall(mouse_location location_of_wall);
- void mouse_went_step(unsigned short int direction);
- //void user_clicks_wall_on_off();
- //void temp_function_to_make_maze_walls();
- };
- //user interaction functions
- //mouse_location write_starting_mouse_location();
- //struct mouse_location write_cheese_location();
- //void write_walls_to_maze(Maze_Class*, char**);
- //end user interaction functions
- int direction(int orientation, int size_x);
- class Mouse_Class : public Maze_Class {
- private:
- protected:
- public:
- mouse_location mouse_start;
- mouse_location mouse;
- mouse_location cheese;
- Mouse_Class(char**);
- ~Mouse_Class();
- void init_mouse(char**);
- void set_cheese(char**);
- int confirm_place(struct mouse_location cheese_or_mouse);
- int go_number_of_spaces(int spaces_and_orientation, Simulator * sim);
- int go_mouse(int orientation, Simulator * sim);
- /*
- * Mouse logic
- * mouse travels down way, marks territory
- * mouse knows paths traveled & paths not traveled
- * mouse knows how many steps from path untraveled to get back onto untraveled path (from dead end or something)
- * mouse travels DFS until it runs into dead end or path already traveled
- * 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)
- * mouse finds all available paths to finish from start point.
- * Execute BFS to find shortest route from start to finish
- * send mouse to start, send mouse through route found to finish. terminate program.
- */
- /*
- * 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;
- * track of spaces wil occur when I perform BFS's & DFS's
- */
- /*
- * ###########
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * ###########
- */
- int which_way_unexplored();
- int look_down_maze_to_see_where_stop(int orientation);
- unsigned short int get_mouse_back_to_unexplored(Simulator * sim);
- void mark_path_with_green();
- void get_back_to_start(Simulator * sim);
- //void go_straight_to_finish(Simulator * sim);
- void main_mouse_function(Simulator * sim);
- };
- //mouse_maze.h
- //mouse_maze.cpp
- void Maze_Class::initialize_maze(){
- //debug//printf("void Maze_Class::initialize_maze()\n");//debug
- for (int i1=0; i1<size_x; i1++) {
- maze_map[i1 ]=0XFF;
- maze_map[i1+(size_y-1)*size_x]=0XFF;
- }
- for (int i1=1; i1<(size_y-1); i1++) {
- maze_map[ i1*size_x ]=0XFF;
- maze_map[(i1+1)*size_x-1]=0XFF;
- }
- for (int i1=0; i1<((int)(size_y-3)/2); i1++) {
- for (int i2=0; i2<((int)(size_x-3)/2); i2++) {
- maze_map[(2+2*i1)*size_x+(2+2*i2)]=0X20;
- }
- }
- for (int i1=0; i1<((int)(size_y-1)/2); i1++) {
- for (int i2=0; i2<((int)(size_x-3)/2); i2++) {
- maze_map[(1+2*i1)*size_x+(2+2*i2)]=0X12;
- }
- }
- for (int i1=0; i1<((int)(size_y-3)/2); i1++) {
- for (int i2=0; i2<((int)(size_x-1)/2); i2++) {
- maze_map[(2+2*i1)*size_x+(1+2*i2)]=0X12;
- }
- }
- for (int i1=0; i1<((int)(size_y-1)/2); i1++) {
- for (int i2=0; i2<((int)(size_x-1)/2); i2++) {
- maze_map[2*(i2+i1*size_x)+size_x+1]=0X00;
- //size_x+1+i1*size_x*2+i2*2
- }
- }
- //disp_maze(1,0,0);
- }
- /*
- * ###########
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * ###########
- */
- /*
- * BORDER WALL = 0XFF
- * INNER WALL DNE = 0X10
- * INNER WALL EXISTS = 0X11
- * INNER WALL UNKNOWN = 0X12
- * WALL CONNECTION = 0X20
- * STEPS:
- * UNEXPLORED = 0X00
- * EXPLORED = 0X01
- * FINISH = 0X02
- * SHORTEST ROUTE = 0X03
- * MOUSE = 0X04
- *
- * BFS = 0X80+
- *
- *
- */
- Maze_Class::Maze_Class(int xside, int yside){
- //debug//printf("Maze_Class::Maze_Class(int %d, int %d)\n",xside,yside);//debug
- size_x=xside*2+1;
- size_y=yside*2+1;
- maze_map=(int*)malloc(sizeof(int)*(size_x)*(size_y));
- initialize_maze();
- }
- Maze_Class::~Maze_Class(){
- //debug//printf("Maze_Class::~Maze_Class()\n");//debug
- free(maze_map);
- }
- int Maze_Class::convert_to_absolute(mouse_location from_m_l){
- //debug//printf("int Maze_Class::convert_to_absolute(mouse_location %dx %dy)\n",from_m_l.x,from_m_l.y);//debug
- return from_m_l.y*size_x+from_m_l.x;
- }
- mouse_location Maze_Class::convert_from_absolute(int from_abs){
- //debug//printf("mouse_location Maze_Class::convert_from_absolute(int %d)\n",from_abs);//debug
- using namespace b_math;
- mouse_location to_m_l;
- to_m_l.y=fix((double)from_abs/((double)size_x));
- to_m_l.x=(from_abs%size_x);
- return to_m_l;
- }
- int Maze_Class::maze_write_wall(int type_seen, int location){
- //debug//printf("int Maze_Class::maze_write_wall(int %X, int %d)",type_seen,location);
- int * wall_replaced;
- wall_replaced=&(maze_map[location]);
- //*wall_replaced=type_seen;
- //uncomment this when undoing the auto-maze write because this is a very important if/else line to have
- using namespace std;
- if (*wall_replaced==0X12) {
- *wall_replaced=type_seen;
- }
- else {
- if (maze_map[location]!=type_seen) {
- //debug//cout << "Error with maze_write_wall, your maze appears to be warping" << endl;
- //debug//printf("maze_write_wall(type_seen==%X, location==%d) maze_map[location]==%X\n",type_seen,location,maze_map[location]);//debug
- }
- else {
- //everything is good. we've seen that wall before
- }
- }
- return * wall_replaced;
- }
- void Maze_Class::maze_write_cell(int location){
- //debug//printf("void Maze_Class::maze_write_cell(int %d)\n",location);//debug
- using namespace std;
- if (maze_map[location]==0X00) {
- maze_map[location]=0X01;//I'm just marking my territory
- }
- else if (maze_map[location]==0X02) {
- //yay I win now I can mark shortest path from here. perform BFS & then get your a** back to start to do the speedrun
- //wait, I take this back, there might be a shorter way
- }
- else {
- //else it is either: explored or shortest route, in either case, I am indifferent to the mark.
- }
- }
- void * Maze_Class::convert_ll_to_mat(BFS_node * linklist_head, int * write_to_mat_size){
- //you will have to change the type here in the parameters
- int count_of_ll;
- BFS_node * step;
- unsigned short int * * matrix_to_return;
- count_of_ll=0;
- step=linklist_head;
- while (step!=0) {
- count_of_ll++;
- step=step->next_node;
- //the next node may have a different name
- }
- //debug//printf("this matrix will have %d cells \n",count_of_ll);//debug//
- matrix_to_return=(unsigned short int * *)malloc(sizeof(unsigned short int *)*count_of_ll);
- step=linklist_head;
- *write_to_mat_size=count_of_ll;
- count_of_ll--;
- for (; count_of_ll>=0; count_of_ll--) {
- //debug//printf("count_of_ll: %d \n",count_of_ll);//debug//
- matrix_to_return[count_of_ll]=(unsigned short int *)(step->cell);
- step=step->next_node;
- /*/debug//
- for (int i1=count_of_ll; i1<(*write_to_mat_size); i1++) {
- printf("matrix[%d]==%p[]==%d \n",i1,matrix_to_return[i1],*matrix_to_return[i1]);
- }//debug/*/
- }
- return (void*)matrix_to_return;
- }
- void Maze_Class::destroy_ll(BFS_node * head_to_destroy){
- BFS_node * previous;
- BFS_node * current;
- previous=head_to_destroy;
- current=previous->next_node;
- while (current!=0) {
- current=previous->next_node;
- free(previous);
- previous=current;
- }
- }
- void disp_node(BFS_node * the_node){
- printf("cell: %p \nnext_node: %p \n",the_node->cell,the_node->next_node);
- }
- void Maze_Class::clear_out_80s(){
- int i1,i2;
- int * cell_pointer;
- for (i1=0; i1<size_y; i1++) {
- for (i2=0; i2<size_x; i2++) {
- cell_pointer=&(maze_map[i1*size_x+i2]);
- if (*cell_pointer>=0X80 && *cell_pointer<(size_x*size_y+0X80)) {
- *cell_pointer=0X01;
- }
- }
- }
- /*/debug//
- printf("you don't have to worry about the 80s any longer \n");//debug//
- disp_maze(1,0,0);//debug/*/
- }
- BFS_node * Maze_Class::make_link_list(int * ending_spot){
- BFS_node * the_head;
- BFS_node * trav1;
- BFS_node * trav2;
- int value_to_search_for;
- int flag;
- //int temp_counter;//debug//
- //temp_counter=0;//debug//
- flag=0;
- //disp_maze(0,0,0);
- the_head=(BFS_node*)malloc(sizeof(BFS_node));
- the_head->cell=ending_spot;
- the_head->next_node=0;
- trav1=the_head;
- value_to_search_for=*((int*)(the_head->cell))-1;
- //debug//printf("value to search for = 0X%X \n",value_to_search_for);//debug//
- while (1) {
- /*/debug//
- temp_counter++;
- printf("making node: %d \n",temp_counter);//debug/*/
- trav2=(BFS_node*)malloc(sizeof(BFS_node));
- trav2->next_node=0;
- trav1->next_node=trav2;
- flag&=0XFE;
- //debug//disp_node(trav1);
- //debug//disp_node(trav2);
- for (int i1=0; i1<size_x*size_y; i1++) {
- //debug//printf("if (maze_map[%d](0X%X)==0X%X ?) \n",i1,maze_map[i1],value_to_search_for);//debug//
- if (maze_map[i1]==value_to_search_for) {
- trav2->cell=&(maze_map[i1]);
- trav1=trav2;
- value_to_search_for--;
- flag|=0X01;
- break;
- }
- }
- if (!flag) {
- //debug//printf("uh, not a good error in make_link_list \n");//debug//
- break;
- }
- if (value_to_search_for<0X80) {
- //debug//printf("done, found everything to make ll out of \n");//debug//
- //debug//
- //disp_maze(0,0,0);//debug//
- *(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
- clear_out_80s();
- break;
- }
- //debug//sleep(2);//debug//
- }
- return the_head;
- }
- void * Maze_Class::bfsearch(int from_where_absol, int * mat_size){
- int look;
- int current_step;
- int current_count;
- int flag;
- BFS_node * list_to_return;
- void * matrix;
- //int mat_size;
- current_step=from_where_absol;
- current_count=0X80;
- maze_map[current_step]=current_count;
- flag=0X00;
- /*
- * [0][0][0][0][0][0][0][0]
- * ^ we found a cell with the proper 80s value to mark (turns off after every search aka break flag)
- * ^ last find for a cell with a certain 80s value, increments 80s value to look, if none found with
- * the next value, we have issues
- * ^ 3rd bit turns on to kick out of the while loop (to make things looking more neat)
- *
- *
- *
- *
- */
- while (!(flag&0X04)) {
- flag&=0XFE;//flag turns off first bit
- for (int i1=0; i1<size_y; i1++) {
- for (int i2=0; i2<size_x; i2++) {
- if (maze_map[i1*size_x+i2]==current_count) {
- current_step=i1*size_x+i2;
- //debug//printf("I found a cell with the proper value \n");//debug//
- flag&=0XFD;//turns off 2nd bit
- flag|=0X01;//turns on 1st bit
- //debug//printf("flag=0X%X \n",flag);//debug//
- //flag=&0XFE;//should not be nessesary
- for (int i3=0; i3<4; i3++) {
- look=direction(i3, size_x);
- //debug//printf("look=%d \n",look);
- if (maze_map[current_step+look]==0X10 || maze_map[current_step+look]==0X12) {
- //debug//printf("open or undefined wall \n");//debug//
- if (maze_map[current_step+2*look]==0X01) {
- //debug//printf("this spot has been explored, marking \n");//debug//
- maze_map[current_step+2*look]=current_count+1;
- //debug//disp_maze(0, 0, 0);//debug//
- }
- else if (maze_map[current_step+2*look]==0X00) {
- //debug//printf("I found a spot that hasn't been traversed! marking cell\n");//debug//
- maze_map[current_step+2*look]=current_count+1;
- //do stuff
- list_to_return=make_link_list(&(maze_map[current_step+2*look]));
- matrix=convert_ll_to_mat(list_to_return, mat_size);//writes to mat_size
- destroy_ll(list_to_return);
- flag|=0X04;
- break;
- //return matrix;
- }
- else if (maze_map[current_step+2*look]>=0X80 && maze_map[current_step+2*look]<=current_count+1) {
- //debug//printf("this space has already been marked \n");//debug//
- }
- else {
- //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//
- }
- }
- else {
- //debug//printf("wall seen:maze_map[%d] 0X%X \n",current_step+look,maze_map[current_step+look]);//debug//
- }
- }
- //debug//sleep(2);//debug//
- break;
- }
- }
- if (flag&0X05) {
- break;
- }
- }
- //debug//printf("supposedly we've gone through all of the BFS with that value, incrementing \n");//debug//
- if (flag&0X02) {
- free(matrix);
- *mat_size=0;
- return 0;
- //debug//printf("we have a serious problem, shut down this program immediately \n");//debug//
- //debug//printf("flag=0X%X current_step=0X%X current_count=0X%X \n",flag,current_step,current_count);//debug//
- }
- //debug//printf("current_count++ \n");//debug//
- current_count++;
- flag|=0X02;//turns on 2nd bit
- }
- //debug//printf("we hit end of bfsearch function \n");//debug//
- return matrix;
- }
- Simulator::Simulator(char**John_maze) : Maze_Class(5, 5) {
- //debug//printf("Simulator::Simulator() : Maze_Class(5, 5)\n");//debug
- int i1,i2;
- char * temp_cell;
- //struct move_node
- for (i1=0; i1<((int)(size_y-1)/2); i1++) {
- for (i2=0; i2<((int)(size_x-3)/2); i2++) {
- //printf("%d %d ",i1,i2);
- temp_cell=&(John_maze[(1+2*i1)][(2+2*i2)]);
- if (*temp_cell==' '){
- maze_write_wall(0X10, (i1*2+1)*size_x+i2*2+2);
- }
- else if (*temp_cell=='X'){
- maze_write_wall(0X11, (i1*2+1)*size_x+i2*2+2);
- }
- else {
- printf("error write_walls_to_maze \n");
- }
- }
- //printf("\n");
- }
- //printf("\n");
- for (i1=0; i1<((int)(size_y-3)/2); i1++) {
- for (i2=0; i2<((int)(size_x-1)/2); i2++) {
- //printf("%d %d ",i1,i2);
- temp_cell=&(John_maze[(2+2*i1)][(1+2*i2)]);
- if (*temp_cell==' '){
- maze_write_wall(0X10, (i1*2+2)*size_x+i2*2+1);
- }
- else if (*temp_cell=='X'){
- maze_write_wall(0X11, (i1*2+2)*size_x+i2*2+1);
- }
- else {
- printf("error write_walls_to_maze \n");
- }
- }
- //printf("\n");
- }
- //printf("\n");
- }
- Simulator::~Simulator(){
- //debug//printf("Simulator::~Simulator()\n");//debug
- }
- int Simulator::mouse_sees_wall(mouse_location location_of_wall){
- //debug//printf("int Simulator::mouse_sees_wall(mouse_location %dx %dy)\n",location_of_wall.x,location_of_wall.y);//debug
- using namespace std;
- /* get the abolute map what the user made
- * tell mouse what number 0X?? it is looking at
- * hopefully all numbers will be:
- * 0XFF , OX10, 0X11
- * returning anything else is bad, print out an error
- */
- int returning;
- returning=maze_map[convert_to_absolute(location_of_wall)];
- switch (returning) {
- case 0X10:
- case 0X11:
- case 0XFF:
- //good
- break;
- default:
- cout << "mouse_sees_wall error, mouse sees: 0X" << hex << (int)returning
- << " at position: 0X" << (int)(location_of_wall.y*size_x+location_of_wall.x)
- << dec << endl;
- break;
- }
- return returning;
- }
- void Simulator::mouse_went_step(unsigned short int direction){
- struct move_node * new_node;
- new_node=(struct move_node*)malloc(sizeof(struct move_node));
- new_node->direction_went=direction;
- new_node->next=0;
- global_current_node->next=new_node;
- global_current_node=global_current_node->next;//or new node
- the_global_move_list.moves_gone++;
- //debug//printf("moves gone: %d \ndirection went: %d \npointer assigned: %p \n",the_global_move_list.moves_gone,new_node->direction_went,new_node);
- //give John direction
- //debug//printf("void Simulator::mouse_went_step(mouse_location %dx %dy)\n",mouse_current_location.x,mouse_current_location.y);//debug
- //send to GUI where the mouse currently is so it knows what to print out
- }
- int direction(int orientation, int size_x){
- //debug//printf("int direction(int %X, int %d)\n",orientation,size_x);//debug
- int scalar;
- int the_signum;
- if (orientation%2) {
- scalar=size_x;
- }
- else {
- scalar=1;
- }
- if ((orientation>>1)%2) {
- the_signum=-1;
- }
- else {
- the_signum=1;
- }
- return scalar*the_signum;
- }
- Mouse_Class::Mouse_Class(char**John_maze) : Maze_Class(5,5) {
- //debug//printf("Mouse_Class::Mouse_Class() : Maze_Class(5,5)\n");//debug
- global_current_node=(move_node*)malloc(sizeof(move_node));
- global_current_node->next=0;
- global_current_node->direction_went=4;
- the_global_move_list.moves_gone=1;
- the_global_move_list.head=global_current_node;
- the_global_move_list.moves_gone=0;
- init_mouse(John_maze);
- set_cheese(John_maze);
- }
- Mouse_Class::~Mouse_Class(){}
- void Mouse_Class::init_mouse(char**maze_matrix){
- //debug//printf("int Mouse_Class::init_mouse()\n");//debug
- //struct mouse_location mouse_to_start;
- //int to_return;
- //take in mouse locations
- //mouse_to_start=get_starting_mouse_location();
- //to_return=confirm_place(mouse_to_start);
- //if (to_return) {
- //mouse=mouse_to_start;
- //this->mouse_start=mouse_to_start;
- //}
- //return to_return;
- for (int i1=0; i1<size_y; i1++) {
- for (int i2=0; i2<size_x; i2++) {
- if (maze_matrix[i1][i2]=='m') {
- mouse.y=i1;
- mouse.x=i2;
- mouse_start=mouse;
- return;
- }
- }
- }
- printf("error no mouse \n");
- }
- void Mouse_Class::set_cheese(char**maze_matrix){
- //debug//printf("int Mouse_Class::set_cheese()\n");//debug
- //struct mouse_location cheese_to_set;
- //int to_return;
- //cheese_to_set=write_cheese_location();
- for (int i1=0; i1<size_y; i1++) {
- for (int i2=0; i2<size_x; i2++) {
- if (maze_matrix[i1][i2]=='g') {
- cheese.y=i1;
- cheese.x=i2;
- return;
- }
- }
- }
- printf("no cheese \n");
- //to_return=confirm_place(cheese_to_set);
- //if (to_return) {
- //cheese=cheese_to_set;
- //}
- //return to_return;
- }
- int Mouse_Class::confirm_place(struct mouse_location cheese_or_mouse){
- //debug//printf("int Mouse_Class::confirm_place(struct mouse_location %dx %dy)",cheese_or_mouse.x,cheese_or_mouse.y);
- //no longer using //using namespace b_input;
- if (maze_map[cheese_or_mouse.y*size_x+cheese_or_mouse.x]!=0X00) {
- //incorrect_input(0);
- printf("error confirm_place \n");
- return 0;
- }
- else {
- //it wortked
- return 1;
- }
- return 0;
- }
- int Mouse_Class::go_number_of_spaces(int spaces_and_orientation, Simulator * sim){
- //debug//printf("int Mouse_Class::go_number_of_spaces(int %X, Simulator * %p)\n",spaces_and_orientation,sim);//debug
- //debug//printf("go_number_of_spaces(spaces_and_orientation==%X, sim==%p) \n",spaces_and_orientation,sim);//debug
- /*
- * [0][0][0][0] [0][0][0][0]
- * ^ ^ ^ ^ ^ ^ spaces to go
- *
- * ^ ^ orientation
- *
- */
- /*
- * spaces range from 00-3F, why you would want to go 0 spaces, I don't know
- * 63 being the max, if you have a bigger map, you will need to reconfigure
- * the type to be some bigger unsigned, & make the orientation bits the 2
- * highest bits of the type
- * the orientation bits, being the 2 highest bits are 0-3
- * 0=East
- * 1=North
- * 2=West
- * 3=South
- *
- *
- */
- int spaces;
- int orientation;
- int spaces_traveled;
- spaces=spaces_and_orientation%0X40;
- orientation=spaces_and_orientation>>(6);
- spaces_traveled=0;
- for (int i1=0; i1<spaces; i1++) {
- spaces_traveled+=go_mouse(orientation,sim);
- }
- return spaces_traveled;
- }
- int Mouse_Class::go_mouse(int orientation, Simulator * sim){
- //debug//
- //debug//printf("int Mouse_Class::go_mouse(int %X, Simulator * %p)\n",orientation,sim);//debug
- /*
- * mouse will travel 1 space
- * 0=East - east is + 1 on the maze_map
- * 1=south1 - north is + size_x on maze_map
- * 2=West - west is -1
- * 3=north1 - south is -size_x
- * >=4 - error, no go.
- *
- */
- using namespace std;
- int l_a_w_raw;
- int wall_seen;
- int go_through_wall;
- if (orientation>3) {
- cout << "invalid orientation, I'm going nowhere" << endl;
- return 0;
- }
- maze_write_cell(convert_to_absolute(mouse));
- //scan this area
- //get the forward & sides of this area & make sure it either:
- //matches the map or is undefined (wall)
- for (int i1=0; i1<3; i1++) {
- l_a_w_raw=mouse.y*size_x+mouse.x+direction((i1+orientation+3)%4, size_x);
- //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
- wall_seen=sim->mouse_sees_wall(convert_from_absolute(l_a_w_raw));
- maze_write_wall(wall_seen,l_a_w_raw);
- }
- go_through_wall=convert_to_absolute(mouse)+direction(orientation, size_x);
- //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
- if (maze_map[go_through_wall]==0X10) {
- //there is no wall here & we may pass through
- go_through_wall=go_through_wall+direction(orientation, size_x);//done a second time
- mouse=convert_from_absolute(go_through_wall);//landed on next white space
- sim->mouse_went_step(orientation);
- //debug//printf("ping \n");
- //disp_maze(3, &mouse, &cheese);
- return 1;
- }
- else {
- //debug//printf("saw wall \n");//debug//
- //I see a wall or something else which is wrong. so I go nowhere
- //return 0;
- }
- return 0;
- }
- /*
- * Mouse logic
- * mouse travels down way, marks territory
- * mouse knows paths traveled & paths not traveled
- * mouse knows how many steps from path untraveled to get back onto untraveled path (from dead end or something)
- * mouse travels DFS until it runs into dead end or path already traveled
- * 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)
- * mouse finds all available paths to finish from start point.
- * Execute BFS to find shortest route from start to finish
- * send mouse to start, send mouse through route found to finish. terminate program.
- */
- /*
- * 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;
- * track of spaces wil occur when I perform BFS's & DFS's
- */
- /*
- * ###########
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * #+@+@+@+@+#
- * # + + + + #
- * ###########
- */
- int Mouse_Class::which_way_unexplored(){
- //debug//printf("int Mouse_Class::which_way_unexplored()\n");//debug
- int mouse_absolute;
- int available_travel;
- int location_relative;
- int prospective_location;
- mouse_absolute=convert_to_absolute(mouse);
- available_travel=0;
- for (int i1=0; i1<4; i1++) {
- location_relative=2*direction(i1, size_x);
- //debug//printf("location relative %d \n",location_relative);//debug//
- if (
- (
- ((i1%2)==0)//eastwest
- &&
- (
- (
- (location_relative+mouse.x)>=size_x)//greater than 5
- ||
- ((location_relative+mouse.x)<0)//less than 0
- )
- )
- ||
- (
- ((i1%2)==1)//northsouth
- &&
- (
- ((location_relative+mouse.y*size_x)>=size_x*size_y)//greater than size of maze
- ||
- ((location_relative+mouse.y*size_x)<0)//less than zero
- )
- )
- ) {
- //debug//printf("this is off map & invalid \n");//debug//
- //this is off the map, & is invalid
- }
- else {
- //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//
- if (maze_map[mouse_absolute+location_relative/2]!=0X11) {
- //not a wall
- available_travel+=(1<<(4+i1));
- }
- else {
- //debug//printf("bumped into wall \n");//debug//
- //nothing
- }
- prospective_location=mouse_absolute+location_relative;
- //debug//printf("prospective_location(%d)=mouse_absolute(%d)+location_relative(%d) \n",prospective_location,mouse_absolute,location_relative);//debug//
- //debug//printf("maze_map[prospective_location(%d)]==%d \n",prospective_location,maze_map[prospective_location]);//debug//
- if (maze_map[prospective_location]==0X00) {
- //debug//printf("nvr been here before \n");//debug//
- //nothing
- }
- else if (maze_map[prospective_location]==0X01) {
- //debug//printf("traveled \n");//debug//
- //traveled before, mark it so
- available_travel+=(1<<i1);
- }
- else {
- //debug//printf("I think I had an error \n");//debug//
- }
- }
- }
- //debug//printf("returning 0X%X == available_trqavel \n",available_travel);//debug//
- return available_travel;
- }
- int Mouse_Class::look_down_maze_to_see_where_stop(int orientation){
- //debug//printf("int Mouse_Class::look_down_maze_to_see_where_stop(int %d)\n",orientation);//debug//
- int crosshair_start;
- int potential_mouse_path[5];
- int mouse_path_start;
- int cheese_crosshair[5];
- int mouse_x_or_y;
- /*
- * here we need to know where the goal is located & which hallway the mouse wants to pass down
- * with this info, we will pass back how many spaces are possible to go down the hallway until the
- * mouse becomes orthoganal to the goal, in which case, I am sure the mouse will want to turn.
- * hmm, I am now thinking that if the space towards the goal is unexplored, I will stop the mouse
- * but if the space has already been explored, I will ignore it & keep sending the mouse on forward
- */
- /*
- * # # # # # # # # # # #
- * #12 +14 +16 +18 +20 #
- * # + @ + @ + @ + @ + #
- * #34 +36 +38 +40 +42 #
- * # + @ + @ + @ + @ + #
- * #56 +58 +60 +62 +64 #
- * # + @ + @ + @ + @ + #
- * #78 +80 +82 +84 +86 #
- * # + @ + @ + @ + @ + #
- * #100+102+104+106+108#
- * # # # # # # # # # # #
- */
- if ((orientation%2)==0) {//mouse is facing east/west
- //debug//printf("east/west \n");//debug//
- mouse_x_or_y=(mouse.x-1)/2;
- crosshair_start=cheese.x+size_x;
- mouse_path_start=mouse.y*size_x+1;
- for (int i1=0; i1<(size_y-1)/2; i1++) {
- //debug//printf("i1=%d \n",i1);//debug//
- cheese_crosshair[i1]=crosshair_start+size_x*2*i1;
- potential_mouse_path[i1]=mouse_path_start+2*i1;
- }
- //debug//printf("confirm \n");//debug//
- }
- else {//mouse is facing north/south
- //debug//printf("north/sourth \n");//debug
- mouse_x_or_y=(mouse.y-1)/2;
- mouse_path_start=mouse.x+size_x;
- crosshair_start=cheese.y*size_x+1;
- for (int i1=0; i1<(size_x-1)/2; i1++) {
- cheese_crosshair[i1]=crosshair_start+2*i1;
- potential_mouse_path[i1]=mouse_path_start+size_x*2*i1;
- }
- }
- //alright, now we have the indexes of where we will stop
- for (int i1=0; i1<5; i1++) {//these are 5 now, if we want variable size mazes, fix this.
- for (int i2=0; i2<5; i2++) {
- //debug//printf("if (potential_mouse_path[%d](%d)==cheese_crosshair[%d](%d)) \n",i1,potential_mouse_path[i1],i2,cheese_crosshair[i2]);//debug//
- if (potential_mouse_path[i1]==cheese_crosshair[i2]) {
- //debug//printf("I found path in %d steps \n",mouse_x_or_y-i1);//debug//
- //if true, i1 is the amount of steps it takes to get there.
- return (int)(b_math::abs(mouse_x_or_y-i1));
- }
- }
- }
- //debug//std::cout << "apparently no way found in that direction. returning 5" << std::endl;
- return 5;
- }
- unsigned short int Mouse_Class::get_mouse_back_to_unexplored(Simulator * sim){
- void * move_matrix;
- int matsize;
- int flag;
- //int look;
- /*
- * this function is designed to put the mouse back on track
- * after it has explored all possible routes in a certain area
- * think of explored routes as "yellow" & unexplored routes as
- * "white". When an area become predominantly yellow, especially
- * at an intersection, the mouse will seek out the nearest white
- * area/square which is nearby the goal. When the square of white
- * is calculated, the mouse will have to figure out how to get there
- * I suppose the mouse could perform a BFS until it sees a wall
- * of unknown type. ... wait, if it sees a wall of unknown type,
- * chances are it has not been on that side of the wall
- * it will be tricky to navigate to a well-thought out square, but
- * try something that works
- */
- flag=0;
- move_matrix=bfsearch(convert_to_absolute(mouse),&matsize);
- if (matsize==0){
- return 0;
- }
- /*/debug//
- for (int i1=0; i1<matsize; i1++) {
- 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//
- }//debug/*/
- /*
- * this move_matrix contains the cells in which to properly traverse
- * to get to the cell where you want to go, use go mouse. to find the
- * spot to go, comb the 4 spots around where mouse is currently located
- * looking for the pointer value of that matrix cell. then go mouse in that
- * direction
- * IMPORTANT!!: all go_mouses must return 1 until you come across an undef wall.
- * once coming to an undef wall, if go_mouse returns zero, then everything is out the
- * window & you are now left to fend for yourself (aka return to normal mouse nav functions)
- * at the very least, mouse should figure out if wall exists or not.
- */
- if (((int**)move_matrix)[0]==&(maze_map[convert_to_absolute(mouse)])) {
- for (int i1=1; i1<matsize; i1++) {
- for (int i2=0; i2<4; i2++) {
- if (((int**)move_matrix)[i1]==&(maze_map[convert_to_absolute(mouse)+2*direction(i2, size_x)])) {
- flag|=go_mouse(i2, sim);
- //debug//printf("flag=0X%X \n",flag);//debug//
- //disp_maze(3, &mouse, &cheese);
- break;
- }
- }
- }
- }
- else {
- //debug//printf("we are in trouble, stop program \n");
- }
- free(move_matrix);
- return 1;
- }
- void Mouse_Class::mark_path_with_green(){
- void * green_mat;
- int green_size;
- for (int i1=0; i1<size_y; i1++) {
- for (int i2=0; i2<size_x; i2++) {
- if (maze_map[i1*size_x+i2]==0X00) {
- maze_map[i1*size_x+i2]=0X01;
- }
- }
- }
- maze_map[convert_to_absolute(mouse_start)]=0X00;
- /*/debug//
- disp_maze(1,0,0);
- printf("bfsearch %d %p\n",convert_to_absolute(mouse),&green_size);//debug/*/
- green_mat=bfsearch(convert_to_absolute(mouse),&green_size);
- /*/debug//
- printf("green \n");
- printf("green_size %d \n",green_size);//debug/*/
- for (short unsigned int i1=0; i1<green_size; i1++){
- *(((short unsigned int **)(green_mat))[i1])=0X03;
- }
- free(green_mat);
- for (unsigned short int i1=0; i1<11; i1++) {
- for (unsigned short int i2=0; i2<11; i2++) {
- if (maze_map[i1*size_x+i2]==0X01) {
- maze_map[i1*size_x+i2]=0X05;
- }
- }
- }
- return;
- }
- void Mouse_Class::get_back_to_start(Simulator * sim){
- short int look;
- //mouse_location old_mouse;
- while (maze_map[convert_to_absolute(mouse)]!=0X00){
- maze_map[convert_to_absolute(mouse)]=0X00;
- //debug//printf("maze_map[convert_to_absolute(mouse)(%d)](%d)!=0X00 \n",convert_to_absolute(mouse),maze_map[convert_to_absolute(mouse)]);
- //debug//printf("ping \n");//debug//
- for (unsigned short int i1=0; i1<4; i1++){
- look=direction(i1,size_x);
- //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]);
- if (maze_map[convert_to_absolute(mouse)+look]==0X10){
- if (maze_map[convert_to_absolute(mouse)+look*2]==0X03){
- //old_mouse=mouse;
- go_mouse(i1,sim);
- //maze_map[convert_to_absolute(old_mouse)]==0X00;
- break;
- }
- }
- }
- //debug//
- //disp_maze(3,&mouse,&cheese);
- }
- //mouse now back at home. the end
- return;
- }
- /*
- void Mouse_Class::go_straight_to_finish(Simulator * sim){
- short int look;
- mouse_location old_mouse;
- printf("ping \n");
- while (convert_to_absolute(mouse)!=convert_to_absolute(cheese)){
- old_mouse=mouse;
- for (unsigned short int i1=0; i1<4; i1++){
- look=direction(i1,size_x);
- if (maze_map[convert_to_absolute(mouse)+look]==0X10){
- if (maze_map[convert_to_absolute(mouse)+look*2]==0X01){
- //maze_map[convert_to_absolute(mouse)]=0X03;
- go_mouse(i1,sim);
- break;
- }
- }
- }
- maze_map[convert_to_absolute(old_mouse)]==0X03;
- disp_maze(1,&mouse,&cheese);
- getchar();
- }
- //mouse now back at cheese. the end
- return;
- }
- */
- void Mouse_Class::main_mouse_function(Simulator * sim) {
- unsigned char available_travel;
- unsigned char unexplored;
- unsigned char tally_unexplored;
- unsigned char mouse_went_some_spaces;
- unsigned char i1;
- unsigned char orient_and_travel[4];
- while (1) {
- //have I achieved cheese yet?
- if (convert_to_absolute(mouse)==convert_to_absolute(cheese)) {
- /*/debug//
- std::cout << "YAY" << std::endl;//debug/*/
- //do stuff
- mark_path_with_green();
- get_back_to_start(sim);
- //go_straight_to_finish(sim);
- return;
- }
- /*
- * BORDER WALL = 0XFF
- * INNER WALL DNE = 0X10
- * INNER WALL EXISTS = 0X11
- * INNER WALL UNKNOWN = 0X12
- * WALL CONNECTION = 0X20
- * STEPS:
- * UNEXPLORED = 0X00
- * EXPLORED = 0X01
- * FINISH = 0X02
- * SHORTEST ROUTE = 0X03
- * MOUSE = 0X04
- *
- *
- */
- //have I already explored my available spaces?
- /*
- * [0][0][0][0] [0][0][0][0]
- * ^ east
- * ^ north
- * ^ west
- * ^ sout
- * ^ e-valid
- * ^ n-valid
- * ^ w-valid
- * ^ s-valid
- * a good value will be 0XF? where ? is anything, unexplored area yields 0, & explored 1
- * if 0X?% ? is not F, it means you are close to an outer boundary in which
- * you will not be permitted to venture that way under any circumstance
- */
- available_travel=which_way_unexplored();
- unexplored=(available_travel>>4)&((unsigned char)(~available_travel));
- //debug//printf("unexplored==%X=(%X)&(%X) \n",unexplored,available_travel>>4,(int)(~available_travel));//debug//
- tally_unexplored=0;
- for (i1=0; i1<4; i1++) {
- tally_unexplored+=((unexplored>>i1)%2);
- //printf("tally_unexplored==%d+=((%d>>%d)%%2) \n",tally_unexplored,unexplored,i1);//debug
- }
- //now only last 4 bits of unexplored are used
- //debug//printf("%d ways unexplored \n",tally_unexplored);//debug
- if ((tally_unexplored>=1) && (tally_unexplored<=4)) {
- for (i1=0; i1<4; i1++) {
- if ((unexplored>>i1)%2) {
- orient_and_travel[i1]=i1<<6;
- //debug//printf("orient&travel[%d]=%X\n",i1,orient_and_travel[i1]);//debug//
- //how many spaces do I want to go?
- orient_and_travel[i1]+=look_down_maze_to_see_where_stop(i1);
- if (!(orient_and_travel[i1]%0X40)) {
- orient_and_travel[i1]++;
- }
- //debug//printf("orient&travel[%d]=%X\n",i1,orient_and_travel[i1]);//debug//
- //mouse_went_some_spaces=go_number_of_spaces(orient_and_travel[i1]);
- }
- else {
- orient_and_travel[i1]=0;
- }
- }
- int min_of_travel;
- int min_dir;
- min_of_travel=size_x+size_y;
- min_dir=-1;
- for (i1=0; i1<4; i1++) {
- //debug//printf("orient&travel[%d]==%X \n",i1,orient_and_travel[i1]);
- if ((orient_and_travel[i1])%0X40) {
- //debug//printf("orient&travel[%d]%%40==%X \n",i1,orient_and_travel[i1]%40);//debug//
- if (min_of_travel>((orient_and_travel[i1])%0X40)) {
- min_of_travel=((orient_and_travel[i1])%0X40);
- min_dir=i1;
- }
- //
- }
- }
- mouse_went_some_spaces=go_number_of_spaces(orient_and_travel[min_dir],sim);//ok we just went some spaces
- //debug//printf("I went %d spaces \n",mouse_went_some_spaces);//debug//
- }
- else if (tally_unexplored==0) {
- //debug//printf("all ways are explored \n");//debug//
- /*
- * special case, I've been everywhere before. within the boundary I've set for myself between what I've explored & the finish, if it
- * 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
- * unexplored territory, because, there HAS to be unexplored territory within the boundary provided I have managed to search all around
- * the cheese without finding it, right, there has to be a path I haven't taken... I hope.
- */
- if (!get_mouse_back_to_unexplored(sim)){
- return;
- }
- }
- else {
- //debug//printf("it's broken \n");//debug//
- }
- //ok, now we have probably just gone some way, we stored how many spaces the mouse went.
- //disp_maze(3, &mouse, &cheese);
- //getchar();
- //debug//sleep(2);/debug//
- }
- return;
- }
- void Maze_Class::disp_maze(int properties, mouse_location * mouse, mouse_location * cheese){
- //debug//printf("void Maze_Class::disp_maze(int %X, mouse_location * %p, mouse_location * %p)\n",properties,mouse,cheese);//debug//
- int temps[4];
- /*
- * [0][0][0][0] [0][0][0][0]
- * ^ 1 pretty, 0 raw info
- * ^ 1 show mouse & goal, 0 no show
- * ^
- * ^
- */
- using namespace std;
- if (properties&0X02) {
- temps[0]=convert_to_absolute(*mouse);
- temps[1]=maze_map[temps[0]];
- temps[2]=convert_to_absolute(*cheese);
- temps[3]=maze_map[temps[2]];
- maze_map[temps[0]]=0X04;
- maze_map[temps[2]]=0X02;
- }
- if ((properties&0X01)) {
- //print out something pretty
- for (int i1=0; i1<size_y; i1++) {
- for (int i2=0; i2<size_x; i2++) {
- switch (maze_map[(size_y-1-i1)*size_x+i2]) {
- case 0X00:
- cout << " ";
- break;
- case 0X01:
- cout << ".";
- break;
- case 0X04:
- cout << "M";
- break;
- case 0X02:
- cout << "G";
- break;
- case 0X03:
- cout << "$";
- break;
- case 0X10:
- cout << "-";
- break;
- case 0X11:
- cout << "+";
- break;
- case 0X12:
- cout << "*";
- break;
- case 0X20:
- cout << "@";
- break;
- case 0XFF:
- cout << "#";
- break;
- default:
- cout << "?";
- break;
- }
- }
- cout << endl;
- }
- cout << endl;
- }
- else {
- //cout << hex << uppercase << fixed;
- for (int i1=0; i1<size_y; i1++) {
- for (int i2=0; i2<size_x; i2++) {
- //cout << (int)(maze_map[i1*size_x+i2]) << " ";
- printf("%.2X ",maze_map[(size_y-1-i1)*size_x+i2]);
- }
- //cout << endl;
- printf("\n");
- }
- //cout << endl << dec;
- printf("\n");
- }
- if (properties&0X02) {
- maze_map[temps[0]]=temps[1];
- maze_map[temps[2]]=temps[3];
- }
- }
- //mouse_maze.cpp
- char**Johns_maze_matrix;
- Mouse_Class * my_mouse;
- Simulator * my_sim;
- void clearmaze(char**defaultmaze){//clear the random junk out of the array
- for(int i = 0; i < 11; i++){
- for(int j = 0; j < 11; j++){
- //blankmaze[i][j] = ' ';
- defaultmaze[i][j] = ' ';
- }
- }
- }
- void build_default_walls(char**defaultmaze){
- //creates the default maze and changes adds or removes walls
- // Default maze:
- // XXXXXXXXXXX
- // X X X
- // X X X XXXXX
- // X X X
- // X X XXX XXX
- // X X X X
- // XXX XXXXX X
- // X X X
- // X XXXXXXXXX
- // X X
- // XXXXXXXXXXX
- if (defaultmaze[0][0] != 'X'){
- clearmaze(defaultmaze);
- for (int i = 0; i < 11; i++){
- defaultmaze[0][i] = 'X';
- defaultmaze[i][0] = 'X';
- defaultmaze[10][i] = 'X';
- defaultmaze[i][10] = 'X';
- }
- for (int i = 1; i < 3; i++){
- defaultmaze[i][2] = 'X';
- }
- for (int i = 6; i < 10; i++){
- defaultmaze[2][i] = 'X';
- }
- for (int i = 2; i < 5; i++){
- defaultmaze[i][4] = 'X';
- }
- for (int i = 5; i < 10; i++){
- defaultmaze[4][i] = 'X';
- }
- for (int i = 1; i < 9; i++){
- defaultmaze[6][i] = 'X';
- }
- for (int i = 2; i < 10; i++){
- defaultmaze[8][i] = 'X';
- }
- for (int i = 4; i < 6; i++){
- defaultmaze[i][2] = 'X';
- }
- defaultmaze[5][6] = 'X';
- defaultmaze[7][4] = 'X';
- defaultmaze[4][7] = ' ';
- defaultmaze[6][3] = ' ';
- defaultmaze[1][1] = 'm';
- defaultmaze[7][5] = 'g';
- }
- }
- void initialize_maze(){
- Johns_maze_matrix=(char**)malloc(sizeof(char*)*11);
- for (int i=0; i<11; i++) {
- Johns_maze_matrix[i]=(char*)malloc(sizeof(char)*11);
- }
- clearmaze(Johns_maze_matrix);
- build_default_walls(Johns_maze_matrix);
- return;
- }
- void initiate_sim_mouse(){
- my_mouse=new Mouse_Class(Johns_maze_matrix);
- my_sim=new Simulator(Johns_maze_matrix);
- return;
- }
- int get_move(){
- unsigned short int the_move;
- move_node * temp_node;
- temp_node=the_global_move_list.head;
- if (temp_node==0){
- printf("ping \n");
- return 4;
- }
- the_move=temp_node->direction_went;
- the_global_move_list.head=temp_node->next;
- free(temp_node);
- return the_move;
- //return rand()%4;
- }
- void do_everything(){
- initialize_maze();
- initiate_sim_mouse();
- my_mouse->main_mouse_function(my_sim);//call the main mouse function by passing in the reference to your simulator
- printf("throw away: %d \n",get_move());
- }
- int main() {
- int show=0;
- do_everything();
- while (show!=4) {
- show=get_move();
- printf("show=%d \n",show);
- }
- printf("done \n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment