Dies ist mein erster Stackoverflow-Post.CS50-Zeichenfunktion zeichnet nur Nullen
Ich tue dem CS50 Kurs auf edx und zur Zeit bin ich bei Problem steckt 3. Setzen mir das Spiel von fünfzehn umzusetzen. Die init() - Funktion initialisiert das Board und die Draw-Funktion sollte es zeichnen, aber es gibt ein Problem.
Die Zeichenfunktion ruft nicht die Werte der Funktion init() ab. Ich habe damit experimentiert, in der Funktion init() sind die Werte korrekt, aber in der Zeichenfunktion sind alle von ihnen Nullen.
Was ist das Problem?
/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
void init(void)
{
//initializing the board
int board[d][d];
int x = (d*d) -1;
//this loop goes trough each row
for(int i = 0; i < d; i++){
//this goes trough each column
for(int j = 0; j < d ; j++){
//this condition handles the case of swapping the 2 and 1 when the grid is even
if((d % 2) == 0 && (x == 2)){
//assigning the number 1
board[i][j] = x-1;
//going to the next column
j++;
//assigning the number 2
board[i][j] = x;
//setting the x = 0 so the loop can end
x=0;
}
//this happens if the above conditions are not met
else {
//assigning the value to the grid
board[i][j]= x;
//decrementing the value
x--;
}
//ending the loop
if(x == 0){
break;
}
}
//ending the loop after the last tile is initialized
if(x == 0){
break;
}
}
}
/**
* Prints the board in its current state.
*/
void draw(void)
{
for(int i = 0; i < d; i++){
for(int j = 0; j < d; j++){
if(board[i][j] != 0){
printf("%2i", board[i][j]);
} else {
printf("_");
}
}
printf("\n");
}
'void init (void) { // Initialisierung der Karte int board [d] [d];' diese 'board' ist lokal. – BLUEPIXY
Das löste es, danke: D –