これは私の最初のstackoverflowポストです。CS50描画関数はゼロのみを描画します
私はedxでCS50コースをやっていますが、現在私は問題3に固執しています。私は15のゲームを実装しています。 init()関数はボードを初期化し、draw関数はそれを描画する必要がありますが、問題があります。
draw関数は、init()関数から値を取得しません。私はそれを試してみました、init()関数では値は正しいですが、描画関数ではすべて0です。
問題が何ですか?
/**
* 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) { //ボードの初期化 int board [d] [d];'この 'board'はローカルです。 – BLUEPIXY
これが解決しました。ありがとうございました:D –