Hi everyone, I am writing an 8-puzzle Problem Solving Program that utilizes the A* (A Star) search algorithm.

I've come this far in completing the desired program, and somehow summed up to this: (entire source code posted below).

But I can't compile it just yet due to this builder error: "undefined reference to `a_star()'"

I use Windows XP as my OS, Code Blocks as my IDE. I could provide more of my system specifications if it had to do anything with the error.

I usually get this same type of error with every other projects I make. Everything seemed written right, and yet...

Can anyone help me out with this? Any criticisms, advices, and handy solutions are most welcome and looked forward to. Thanks.

#include <iostream>

using namespace std;

int i[9];
int temp[9];
int g[9] = {1,8,7,2,0,6,3,4,5};
int direction[5];
int position;

void input();
void print();
void direct(int k);
int chk(int k);
void copy();
void copyt();

int min(int a,int b,int c,int d);

void up(int k);
void down(int k);
void left(int k);
void right(int k);
void breadth_first();
void depth_first();
int a_star();

void input()
{
    int k;
    cout << "Input 9 numbers (0,1.....8) following this position:" << endl;
    cout << "i0 i3 i6" << endl;
    cout << "i1 i4 i7" << endl;
    cout << "i2 i5 i8" << endl << endl;
    cout << "i0....i8:";
    cin >> i[0] >> i[1] >> i[2] >> i[3] >> i[4] >> i[5] >> i[6] >> i[7] >> i[8];
    cout << endl;
    for(k = 0; k < 9; k++)
    {
        if(i[k] == 0)
            position = k;
    }
}

void print()
{
    cout << i[0] << " " << i[3] << " " << i[6] << endl;
    cout << i[1] << " " << i[4] << " " << i[7] << endl;
    cout << i[2] << " " << i[5] << " " << i[8] << endl << endl;
}

void direct(int k)
{
    k = position;
    if(k == 0 || k == 3 || k == 6)
        direction[0] = 0;
    else
        direction[0] = 1;

    if( k == 2 || k == 5 || k == 8)
        direction[1] = 0;
    else
        direction[1] = 1;

    if(k == 0 || k == 1 || k == 2)
        direction[2] = 0;
    else
        direction[2] = 1;

    if(k == 6 || k == 7 || k == 8)
        direction[3] = 0;
    else
        direction[3] = 1;
}

int chk(int k)
{
    int counter = 0;
    for(k = 0; k < 9; k++)
    {
        if(temp[k] != g[k])
            counter++;
    }
    return counter;
}

void copy()
{
    int k;
    for(k = 0; k < 9; k++)
    temp[k] = i[k];
}

void copyt()
{
    int k;
    for(k = 0; k < 9; k++)
    i[k] = temp[k];
}

int min(int a, int b, int c, int d)
{
    if(a < b)
    {
        if(a < c)
        {
            if(a < d)
                return 0;
            else
                return 3;
        }
        else
        {
            if(c < d)
                return 2;
            else
                return 3;
        }
    }
    else
    {
        if(b < c)
        {
            if(b < d)
                return 1;
            else
                return 3;
        }
        else
        {
            if(c < d)
                return 2;
            else
                return 3;
        }
    }
}

void up(int k)
{
    int temp;
    temp = i[k];
    i[k] = i[k - 1];
    i[k-1] = temp;
}

void down(int k)
{
    int temp;
    temp = i[k];
    i[k] = i[k + 1];
    i[k+1] = temp;
}

void left(int k)
{
    int temp;
    temp = i[k];
    i[k] = i[k - 3];
    i[k-3] = temp;
}

void right(int k)
{
    int temp;
    temp = i[k];
    i[k] = i[k + 3];
    i[k+3] = temp;
}

void breadth_first()
{
}

void depth_first()
{
}

int a_start()
{
    int k,u,d,l,r;
    u = 99;
    d = 99;
    l = 99;
    r = 99;
    k = position;
    if(chk(k) == 0)
    {
        cout << "The puzzle is sorted." <<endl;
        return 0;
    }
    direct(k);
    if(direction[0] == 1)
    {
        copy();
        up(k);
        u = chk(k);
    }
    if(direction[1] == 1)
    {
        copy();
        down(k);
        d = chk(k);
    }
    if(direction[2] == 1)
    {
        copy();
        left(k);
        l = chk(k);
    }
    if(direction[3] == 1)
    {
        copy();
        right(k);
        r = chk(k);
    }
    switch (min(u,d,l,r))
    {
        case 0:
            copy();
            up(k);
            cout << "u ";
            position--;
            copyt();
            a_star();
        case 1:
            copy();
            down(k);
            cout << "d ";
            position++;
            copyt();
            a_star();
        case 2:
            copy();
            left(k);
            cout << "l ";
            position = position - 3;
            copyt();
            a_start();
        case 3:
            copy();
            right(k);
            cout << "r ";
            position = position + 3;
            copyt();
            a_start();
    }
    return 0;
}

int main()
{
    input();
    print();
    cout << "Movement: ";
    a_star();
    return 0;
}

You are referring to two different functions in your code.
a_star and a_start. Try renaming them to the same name.

Ex: a_star is defined at line 26.
a_start is implemented at line 179.

Rename them both to a_star and modifying your function calls also should help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.