Hi, I am working on a project for my C programming class. We have not covered pointers yet, and this project is primarily focused on Arrays mainly.
I have coded most of it but I'm stuck at the function I've created to "shuffle" or "scramble" the elements in the "game board"
my function is as follows:
Code:
void scramble(char gameBoard[][COLS], int w) // incomplete
{
int g;
char *b;
if ( w < 50)
{
srand(time(NULL));
// search for zero in array and store position in x (row) & y (col)
char x = find_Zero_In_Row(gameBoard);
char y = find_Zero_In_Col(gameBoard);
char unsigned z = rand()%4; // random number between 0 and 3, 0 and 3 included
if ( z == 0 && y+1 < COLS) // case 1
{
gameBoard[x][y] = gameBoard[x][y+1];
gameBoard[x][y+1] = 0;
}
else if ( z == 1 && y-1 > COLS) // case 2
{
gameBoard[x][y] = gameBoard[x][y]-1;
gameBoard[x][y-1] = 0;
}
else if ( z == 2 && x+1 < ROWS) // case 3
{
gameBoard[x][y] = gameBoard[x+1][y];
gameBoard[x+1][y] = 0;
}
else if ( z== 3 && x-1 > ROWS) // case 4
{
gameBoard[x][y] = gameBoard[x-1][y];
gameBoard[x-1][y] = 0;
}
w += 1;
scramble(gameBoard, w);
}
else print_Board(gameBoard);
}
I called my function from the Menu() funciton as:
Code:
scramble(gameBoard, 0);
I know its pretty naiive, and it obviously isn't working as I would hope because my output is as follows:
$./a.out
Initial configuration [1-Random, 2-Specified configuration]: 1
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15
as you can see my board is still in order and not scrambled. Is there anything in my code that isn't working? I'm thinking it might just be bypassing all my conditions and just printing the array as it was passed by reference initially.
TIA