C -15幻灯片拼图游戏:分段故障(核心倾倒)错误



我第一次试图在这里寻求帮助,所以如果我的问题的解释不佳/写作,我的不好。我正在为魔术15益智游戏编写代码,但是我认为我的update_board函数有错误,这会导致分段故障(核心倾倒)运行时错误。任何反馈或帮助将不胜感激。抱歉,代码过多,我只是编程的新手,我还不确定我的错误在哪里,所以我只是把整个东西都放在那里。

只是为了澄清,这是游戏:https://www.youtube.com/watch?v=Etxe08bovzm

#include <stdio.h>
#include <math.h>
#include <string.h>

//fn that allows user input at game start
void scan_board(int board[4][4]){
    printf("Enter initial board state:n");
    for (int i = 0; i <= 3; i++){
        scanf("%d %d %d %d", &board[i][0], &board[i][1], &board[i][2], &board[i][3]);
    }
}

//fn that prints the user inputted starting board
void print_board(int board[4][4]){
    for (int i = 0; i <= 3; i++){
        for (int j = 0; j<= 3; j++){
            if (board[i][j] == -1){
                printf("%-2s ", "_");
            }else{
                printf("%-2d ", board[i][j]);
            }
        }
        printf("n");
    }
}
//fn that updates the game board after the user makes a move and also tells user if their move is invalid
int update_board(const char command[100], int board[4][4]){
    //char up[3] = "up";
    //char down[5] = "down";
    //char left[5] = "left";
    //char right[6] = "right";
    int x, y;
    int check = 1;
    for (int i = 0; i <= 3; i++){
        for (int j = 0; j <= 3; j++){
            if (board[i][j] == -1){
                x = i;
                y = j;
                check = 0;
                break;
            }
        }
        if (check == 0){
            break;
        }
    }
    if (strcmp(command, "up" == 0)){
        if (x == 0){
            printf("invalid moven");
        }else{
            board[x][y] = board[x + 1][y];
            board[x + 1][y] = -1;
        }
    }else if (strcmp(command, "down" == 0)){
        if (x == 3){
            printf("invalid moven");
        }else{
            board[x][y] = board[x - 1][y];
            board[x - 1][y] = -1;
        }           
    }else if (strcmp(command, "left" == 0)){
        if (y == 0){
            printf("invalid moven");
        }else{
            board[x][y] = board[x][y - 1];
            board[x][y - 1] = -1;
        }
    }else if (strcmp(command, "right" == 0)){
        if (y == 3){
            printf("invalid moven");
        }else{
            board[x][y] = board[x][y + 1];
            board[x][y + 1] = -1;
        }
    }else{
        printf("invalid commandn");
    }
}
//fn that checks how many tiles are in the wrong place
int check_state(int board[4][4]){
    int num = 1;
    int count = 0;
    for (int i = 0; i <= 3; i++){
        for (int j = 0; j <= 3; j++){
            if (board[i][j] != num){
                count++;    
            }
            num++;
        }
    }
    count--;
    if (count == 0){
        return 0;
    }else{
        return count;
    }
}
//main fn
int main(void){
    int board[4][4];
    char command[100];
    scan_board(board);
    printf("n");
    //print_board(board);
    //int state = check_state(board);
    //printf("number of tiles out of position: %dnn", state);
    int state = check_state(board);
    while (state > 0){
        print_board(board);
        printf("number of tiles out of position: %dnn", state);
        printf("make a move: ");
        gets(command);
        printf("%sn", command);
        update_board(command, board);
        state = check_state(board);
    }
    printf("final board statenn");
    print_board(board);
    printf("you won!");
    return 0;
}

就像我在评论中指向您一样,您应该修复所有这些strcmp调用。

另一个问题,您需要将update_board声明为void,因为它没有返回任何内容。

和最后一个替换gets,因为用fgetsscanf()弃用了: fgets(command, 100, stdin);

void scan_board(int board[4][4])内部,您应该使用第二个循环而不是使用此行:

scanf("%d %d %d %d", &board[i][0], &board[i][1], &board[i][2], &board[i][3]);

,还要检查scanf是否错误:

void scan_board(int board[4][4]){
    printf("Enter initial board state:n");
    for (int i = 0; i < 4; i++){
        for (int j = 0 ; j < 4 ; j++){
            if ( scanf("%d", &board[i][j] ) != 1 ){
                printf("Error, scanfn");
            }
        }
    }
}

这样做之后,应该看起来像这样:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//fn that allows user input at game start
void scan_board(int board[4][4]);
void scan_board(int board[4][4]){
    printf("Enter initial board state:n");
    for (int i = 0; i < 4; i++){
        for (int j = 0 ; j < 4 ; j++){
            if ( scanf("%d", &board[i][j] ) != 1 ){
                printf("Error, scanfn");
                exit(EXIT_FAILURE);
            }
        }
    }
}

//fn that prints the user inputted starting board
void print_board(int board[4][4]);
void print_board(int board[4][4]){
    for (int i = 0; i <= 3; i++){
        for (int j = 0; j<= 3; j++){
            if (board[i][j] == -1){
                printf("%-2s ", "_");
            }else{
                printf("%-2d ", board[i][j]);
            }
        }
        printf("n");
    }
}
//fn that updates the game board after the user makes a move and also tells user if their move is invalid
void update_board(const char command[100], int board[4][4]);
void update_board(const char command[100], int board[4][4]){
    //char up[3] = "up";
    //char down[5] = "down";
    //char left[5] = "left";
    //char right[6] = "right";
    int x, y;
    int check = 1;
    for (int i = 0; i <= 3; i++){
        for (int j = 0; j <= 3; j++){
            if (board[i][j] == -1){
                x = i;
                y = j;
                check = 0;
                break;
            }
        }
        printf("Y = %dn", y);
        if (check == 0){
            break;
        }
    }
    if (strcmp(command, "up") == 0){
        if (x == 0){
            printf("invalid moven");
        }else{
            board[x][y] = board[x + 1][y];
            board[x + 1][y] = -1;
        }
    }else if (strcmp(command, "down") == 0){
        if (x == 3){
            printf("invalid moven");
        }else{
            board[x][y] = board[x - 1][y];
            board[x - 1][y] = -1;
        }
    }else if (strcmp(command, "left") == 0){
        if (y == 0){
            printf("invalid moven");
        }else{
            board[x][y] = board[x][y - 1];
            board[x][y - 1] = -1;
        }
    }else if (strcmp(command, "right") == 0){
        if (y == 3){
            printf("invalid moven");
        }else{
            board[x][y] = board[x][y + 1];
            board[x][y + 1] = -1;
        }
    }else{
        printf("invalid commandn");
    }
}
//fn that checks how many tiles are in the wrong place
int check_state(int board[4][4]);
int check_state(int board[4][4]){
    int num = 1;
    int count = 0;
    for (int i = 0; i <= 3; i++){
        for (int j = 0; j <= 3; j++){
            if (board[i][j] != num){
                count++;
            }
            num++;
        }
    }
    count--;
    if (count == 0){
        return 0;
    }else{
        return count;
    }
}
//main fn
int main(void){
    int board[4][4];
    char command[100];
    scan_board(board);
    printf("n");
    //print_board(board);
    //int state = check_state(board);
    //printf("number of tiles out of position: %dnn", state);
    int state = check_state(board);
    while (state > 0){
        print_board(board);
        printf("number of tiles out of position: %dnn", state);
        printf("make a move: ");
        if ( scanf("%99s", command) != 1){
            printf("error scanf, againn");
            exit(EXIT_FAILURE);
        }
        update_board(command, board);
        state = check_state(board);
    }
    printf("final board statenn");
    print_board(board);
    printf("you won!");
    return 0;
}

输出:

Enter initial board state:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
final board state
1  2  3  4  
5  6  7  8  
9  10 11 12 
13 14 15 16 
you won!

我不知道您的程序应该如何表现,但至少您可以编译和测试。

最新更新