0

I've just finished coding init and have started draw but I'm not sure how to pass on my grid values to the draw functions. Especially as the values may change as the user plays the game. I've tried to declare grid as a global variable or in main but I've encountered errors. If anyone could help me out, it would mean a lot.

If you could explain it in layman's terms, it would help much more. :)

2

The board was already declared for you as a global array, so you should be setting the initial values in your init() and then drawing that array in your draw(). The draw() function is also called after every move. Your move() function will be updating the values in the array.

Notice here in the fifteen.c code we were given, near the top:

// board, whereby board[i][j] represents row i and column j
int board[MAX][MAX];

// board's dimension
int d;

This is the array that your functions should be using.

| improve this answer | |
  • Hi, Brenda! As far as I remember, init() is used to just initialize the tiles initially as per Zamyla's video. So init() has nothing to do with updating the board. – Kareem Jul 20 '14 at 0:36
  • Kareem, I've edited my answer to be clearer. – curiouskiwi Jul 20 '14 at 1:20
  • Thank you! I didn't see "board" at the top, so I tried to implement another global variable using d, which didn't work out since d hasn't been entered yet. But thanks a lot, now I've got it! – Yao Li Jul 20 '14 at 13:00
0

Well, I assume the board was already declared as a global variable when you implemented init().

A global variable is typically used when you need more than one function in your source code file to access it (as it's the case here).

Global variables also can be accessed directly from the functions defined in the same source code files. For example, consider the following simple program

#include <stdio.h>

// prototypes
void incrementX(int i);
int getX(void);

int x; // global variable

int main(void)
{
    x = 5; // NOTICE that we're accessing x directly from main()
    printf("Before incrementing:\nx: %d\n", getX()); // print the value of x

    incrementX(10); // increment x by 10
    printf("\nAfter incrementing:\nx: %d\n", getX()); // print the value of x
}

/*
increments x by i
*/
void incrementX(int i)
{
    x = x + i; // NOTICE that we're accessing x directly from incrementX()
}

/*
returns the value of x
*/
int getX(void)
{
    return x; // NOTICE that we're accessing x directly from getX()
}

Output:

Before incrementing:
x: 5

After incrementing:
x: 15

For draw(), you'll need to print the 2D-array row by row and column by column. For example, in case of 3x3 array, it should be printed like that (regardless of the order)

1    2    3
4    5    6
7    8    _

I assume you didn't manually initialized the elements of that array when you implemented init(), so FYI the process is very similar (you'll probably need to use loops to achieve that).

Also, don't forget to check for the value that represents the blank tile in your code so that you print it appropriately!

Lastly, you don't have to care about the values of the elements in this array being changed as the user makes moves since every time draw() is called, it accesses the elements of this array and prints them no matter what their values are. You may refer to Zamyla's walkthrough on draw() for more information!

| improve this answer | |
  • Thank you for your answer! I didn't see board at the top, so I was having difficulties. Thank you again! – Yao Li Jul 20 '14 at 13:01

You must log in to answer this question.

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