为什么array [i] [ - 1] == 0?如何防止使用未定义的变量?

时间:2016-09-07 04:25:34

标签: c cs50

/**
 * fifteen.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Implements Game of Fifteen (generalized to d x d).
 *
 * Usage: fifteen d
 *
 * whereby the board's dimensions are to be d x d,
 * where d must be in [DIM_MIN,DIM_MAX]
 *
 * Note that usleep is obsolete, but it offers more granularity than
 * sleep and is simpler to use than nanosleep; `man usleep` for more.
 */

#define _XOPEN_SOURCE 500

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// constants
#define DIM_MIN 3
#define DIM_MAX 9

// board
int board[DIM_MAX][DIM_MAX];
int b;

// dimensions
int d;

// prototypes
void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);

int main(int argc, string argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: fifteen d\n");
        return 1;
    }

    // ensure valid dimensions
    d = atoi(argv[1]);
    if (d < DIM_MIN || d > DIM_MAX)
    {
        printf("Board must be between %i x %i and %i x %i, inclusive.\n",
            DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
        return 2;
    }

    // open log
    FILE* file = fopen("log.txt", "w");
    if (file == NULL)
    {
        return 3;
    }

    // greet user with instructions
    greet();

    // initialize the board
    init();

    // accept moves until game is won
    while (true)
    {
        // clear the screen
        clear();

        // draw the current state of the board
        draw();

        // log the current state of the board (for testing)
        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                fprintf(file, "%i", board[i][j]);
                if (j < d - 1)
                {
                    fprintf(file, "|");
                }
            }
            fprintf(file, "\n");
        }
        fflush(file);

        // check for win
        if (won())
        {
            printf("ftw!\n");
            break;
        }

        // prompt for move
        printf("Tile to move: ");
        int tile = GetInt();

        // quit if user inputs 0 (for testing)
        if (tile == 0)
        {
            break;
        }

        // log move (for testing)
        fprintf(file, "%i\n", tile);
        fflush(file);

        // move if possible, else report illegality
        if (!move(tile))
        {
            printf("\nIllegal move.\n");
            usleep(500000);
        }

        // sleep thread for animation's sake
        usleep(500000);
    }

    // close log
    fclose(file);

    // success
    return 0;
}

/**
 * Clears screen using ANSI escape sequences.
 */
void clear(void)
{
    printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);
}

/**
 * Greets player.
 */
void greet(void)
{
    clear();
    printf("WELCOME TO GAME OF FIFTEEN\n");
    usleep(2000000);
}

/**
 * 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)
{
    for(int i = 0, j = 0, k = ((d*d)-1); i < d; j++, k--)
    {
        if(j == d)
        {
            i = i + 1;
            j = 0;
        }
        board[i][j] = k;
    }
    if((d*d)%2 == 0)
    {
        board[(d-1)][(d-2)] = 2;
        board[(d-1)][(d-3)] = 1;
    }
    board[(d-1)][(d-1)] = 0;
    b = board[(d-1)][(d-1)];


}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    for(int i = 0, j = 0; i !=(d-1) || j!=d; j++)
    {
        if(j == d)
        {
          i = i + 1;
          j = 0;
          printf("\n");
         }
        if(board[i][j] == 0) //b used to be 99
        {
            printf("  _");
        }
        else
        {
            printf(" %2d", board[i][j]);
        }
    }
    printf("\n");
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    //find tile
    for(int i = 0, j = 0; i !=(d-1) || j!=d; j++)
    {
        if(j == d)
        {
          i = i + 1;
          j = 0;
        }
        if (board[i][j] == tile)
        {
            //check if tile position is in valid perimeter of blank space
            if (board[i+1][j] == b)
            {
               board[i+1][j] = tile;
               board[i][j] = 0;
               b = board[i][j];
                return true;
            }
            if (board[i-1][j] == b)
            {
               board[i-1][j] = tile;
               board[i][j] = 0;
               b = board[i][j];
                return true;
            }
            if (board[i][j+1] == b)
            {
               board[i][j+1] = tile;
               board[i][j] = 0;
               b = board[i][j];
                return true;
            }
            if (board[i][j-1] == b)
            {
               printf("%i", board[i][j-1]);
               board[i][j-1] = tile;
               board[i][j] = 0;
               b = board[i][j];
                return true;
            }
        }
    }
    return false;
}

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
    for(int i = 0, j = 0, k = 1; i !=(d-1) || j!=d; j++)
    {
        if(j == d)
        {
          i = i + 1;
          j = 0;
        }
        if (k == (d*d)-1)
        {
            return true;
        }
        if (board[i][j] == k)
        {
            k = k + 1;
        }
    }
    return false;
}

我最初有

board[(d-1)][(d-1)] = 0;

等于99以及移动函数查找99.对于我的问题集我应该使用0.一旦我将99改为0,由于某种原因,如果董事会[i] [j-1]找到0即使这意味着董事会[2] [ - 1]。为什么允许/为什么这等于0?以及如何禁用此功能?

2 个答案:

答案 0 :(得分:2)

您有int board[DIM_MAX][DIM_MAX];其中#define DIM_MIN 3且为元素分配的内存是连续的,因此通常您会使用board[1][2]访问board[2][-1]。但这是未定义的行为,它允许任何事情发生,你不能使用它。

引自N1570 J.2未定义的行为:

  

数组下标超出范围,即使某个对象显然可以使用   给定下标(如左边的表达式a [1] [7]给出声明int   a [4] [5])(6.5.6)。

答案 1 :(得分:2)

  

板[2] [ - 1]。为什么允许

C允许您访问超出阵列的范围。但它是未定义的行为

  

为什么它等于0?

偶然。它是未定义的行为,它可以是任何