HELP!!!!

Started by
3 comments, last by Betrayer_of_Code 20 years, 3 months ago
I have been stumbling over this code, trying to figure out why it doesnt work for a few weeks now and I am totally stuck. The whole point of this is for a "mouse" to walk through a maze and get to some cheese. Its not nearly done, but I cant even get the mouse to move without it spazzing out. I think they prolbem is in logic_row or logic_column.

#include <iostream>
#include <stdlib.h>
#include <stdlib.h>

using namespace std;

int logic_column(int prev_column, int columns, int next_column, int row, int pass_maze[][10]);
int logic_row(int prev_row, int rows, int next_row, int columns, int pass_maze[][10]);
void draw_maze(int pass_maze[][10]), init_maze(), walk_maze(int pass_maze[][10]);
int maze[10][10];

const char mouse = ''M'';
const char cheese = ''C'';
const char space = '' '';
const char wall = ''X'';
const char used_space = '' '';


int main()
{
    
    
    init_maze();
    
    system("pause");
    return 0;
}



int logic_row(int prev_row, int rows, int next_row, int columns, int pass_maze[][10])
{    
    rows = -1;
    
    cout << prev_row << endl << next_row << endl;
    if(prev_row == 0)
    {
        return prev_row;
    }
    
    else if(next_row == 0)
    {
        return next_row;
    }
    
    else if(prev_row != 0 && next_row != 0)
    {
        return rows;
    }
    
    return rows;
    
}

int logic_column(int prev_column, int columns, int next_column, int row, int pass_maze[][10])
{    
    columns = -1;
    
    cout << prev_column << endl << next_column << endl;
    if(prev_column == 0)
    {
        return prev_column;
    }
    
    else if(next_column == 0)
    {
        return next_column;
    }
    
    else if(prev_column != 0 && next_column != 0)
    {
        return columns;
    }
    
    return columns;
    
}

    

void walk_maze(int pass_maze[][10])
{
    
    int k = 0;
    cout << "\nBegin Walk Maze\n";
    int walked_maze[10][10];
    int rows, row = 0;
    int columns, column = 1;
    int prev_row = 0, next_row = 0;
    int prev_column = 0, next_column = 0;
    
    for(int c = 0; c < 10; c++)
    {
        for(int r = 0; r < 10; r++)
        {
             walked_maze[c][r] = pass_maze[c][r];
             
                k++;
                cout << walked_maze[c][r];
                
                if ( k == 10 )
                {
                                cout << "\n";
                                k = 0;
                }
                if(walked_maze[c][r] == 3)
                {
                                prev_row = walked_maze[c][r-1];
                                next_row = walked_maze[c][r+1];
                                rows = walked_maze[c][r];
                                
                                prev_column = walked_maze[c-1][r];
                                next_column = walked_maze[c+1][r];
                                columns = walked_maze[c][r];
                                
                }
        }
    }
    
       
       cout << "\nLogic\n";
       row = logic_row(prev_row, rows, next_row, columns, walked_maze);
       if(row == -1)
       {
       cout << "Columns\n";
       row = rows;
       column = logic_column(prev_column, columns, next_column, rows, walked_maze);
       }
       
       cout << "\nAssign 3 to Maze\n";
       system("pause");
       
       walked_maze[row][column] = 3;
       
       cout << "\nre-Draw Maze\n";
       draw_maze(walked_maze);
       
       cout << "\nEnd Walk Maze\n";
       return;
}   






//Draw and Initilize the Maze//

void draw_maze(int pass_maze[][10])
{
    char char_maze[10][10];
    int switch_char;
    int k = 0;
    
    for (int c = 0; c < 10; c++)
    {
        for(int r = 0; r < 10; r++)
        {
                switch_char = pass_maze[c][r];
                
                switch(switch_char)
                {
                
                                case -1:
                                
                                char_maze[c][r] = used_space;
                                break;
                                
                                case 0:
                                
                                char_maze[c][r] = space;
                                break;
                                
                                case 1:
                                
                                char_maze[c][r] = wall;
                                break;
                                
                                case 2:
                                
                                char_maze[c][r] = cheese;
                                break;
                                
                                case 3:
                                
                                char_maze[c][r] = mouse;
                                break;
                                
                                default:
                                
                                char_maze[c][r] = ''!'';
                                break;
                }
        }
    }
    
    for (int c = 0; c < 10; c++)
    {
        for(int r = 0; r < 10; r++)
        {
                k++;
                cout << char_maze[c][r];
                
                if ( k == 10 )
                {
                                cout << "\n";
                                k = 0;
                }
        }
    }
    
    cout << "Draw Maze End\n";
    walk_maze(pass_maze);
}



void init_maze()
{

    
    int maze[10][10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                        1, 3, 1, 0, 0, 0, 1, 1, 1, 1,
                        1, 0, 0, 0, 1, 0, 0, 0, 1, 1,
                        1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
                        1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
                        1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
                        1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
                        1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
                        1, 1, 1, 1, 0, 0, 0, 0, 2, 1,
                        1, 1, 1, 1, 1, 1, 1, 1, 1, 1
                        };
    
    
    draw_maze(maze);
    return;
}
Pick apart the code as you feel neccesary :D Thanks in Advance
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
Advertisement
If you dont understand the question just ask, i may be able to better explain.
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
What are ''logic_row'' and ''logic_column'' supposed to do ?


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
They are supposed to tell where the mouse goes. If the cloeset row is clear then it goes that way, if the cloest column is clear it goes there
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
Right now, they are both equivalent to this:
int func(int foo, int bar, int quux){  return (foo && bar)?quux:0;} 


Explain which maze-solving algorithm you are trying to use, and maybe I''ll be able to give you a hint. Because from just looking at them, I really have no clue.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement