0

I have checked draw() and init() and they seem to be right. However, the problem lies with the move() portion of the code. After I run the code, every move entered is named Illegal.... I have no clue at all what's wrong (since gdb cannot run on function declaration...) The following is my code for move():

int a = 0;
int b = 0;
int c = 0;
int d = 0;
for (int i = 0; i < d; i++)
{
    //create variable for tile
    if (board[i][j] == tile)
    {
        a = i;
        b = j; 
    }
    else if (board[i][j] == 0)
    {
        c = i;
        d = j; 
    }
}
if (b == d)
{
    if (a - c == 1 || a - c = -1)
    {
        //swap blank space and tile
        board[c][d] == tile; 
        board[a][b] == 0;
        return true;  
    }
    else return false;
}
else if (a == c)
{
   if (b - d == 1 || b - d = -1)
    {
        //swap blank space and tile
        board[c][d] == tile; 
        board[a][b] == 0; 
        return true; 
    }
    else return false;
}
else return false; 

I am also having with the storing blank space as a global variable... Any hints???

1
  • Why don't you try char ch = ' ';? – sinister Dec 11 '14 at 3:59
2

The thing is:

The program has declared a global variable d already.

//board dimension int d;

In your program, there's another declaration of d, in your a b c d initialisation.

Thus, your for loop will stop right after its 1st time of running, b/c:

for (int i = 0; i < d; i++) equals for (int i = 0; i < 0; i++), leaves every single a b c d = 0

and your if will only executes:

if (b == d) //correct b/c b = d = 0
{
    if (a - c == 1 || a - c = -1)
    {
        //note execute b/c a - c = 0 - 0 = 0 
    }
    else return false;`

Also, your code has some other problems:

  • In your for loop, you don't loop for j, thus false values of b and d

  • Failed usage of = and ==

For example:

if (a - c == 1 || a - c = -1)

board[a][b] == 0;

1
  • Thank you very much. I have fixed the error and it works!!! :)))) – Happytreat Dec 11 '14 at 6:23
1

What do you mean by "gdb cannot run on function declaration"? Do you mean you don't know how to parse command line input to gdb? Try running your program like this with gdb:

>gdb ./fifteen

hi youre running gdb here is thirty lines of 
useless garbage before the command prompt 
and here is the command prompt
>break main
>run 5 (where 5 is your number of tiles parsed as command line input) 

That should really be it. You can also parse input files the same way:

>run 5 < inputfile.file
1
  • or gdb --args fifteen 5 – cuongtn Dec 11 '14 at 4:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .