简体   繁体   English

c中的变量随机改变值

[英]variables in c changing value randomly

I'm learning C at school, and as homework I have to write the tictactoe game.我在学校学习 C,作为作业,我必须编写 tictactoe 游戏。 No problem with the "algorithm", but I do not understand why if I change the order of the variables declaration, the program output drastically changes or even the programme stops working. “算法”没问题,但我不明白为什么如果我改变变量声明的顺序,程序输出会发生巨大变化,甚至程序停止工作。 For example, if I swap line 12 with line 13, the element of the array coord change values at random points of the programme.例如,如果我将第 12 行与第 13 行交换,则数组坐标的元素会在程序的随机点处更改值。 Can someone explain me why this happen?有人可以解释我为什么会这样吗?

#include <stdio.h>

#define DIM 3
#define MAX 11

int main(void) {
    char c;
    int state = 0;   //Variable for the switch case
    int nC, nR;     //Variables used to count how many X or O there are in the rows and columns of the grid
    int i, j;       
    int coord[2] = {0, 0};     //Array for the coordinates

    char grid[DIM][DIM];        //Grid 3x3
    char player1[MAX] = "", player2[MAX] = "";       //Name of the players

    printf("Player 1, insert your name (max 10 characters): ");
    gets(player1);
    fflush(stdin);
    printf("Player 2, insert your name (max 10 characters): ");
    gets(player2);

    for (i = 0; i < DIM; i++) {                       //Inizialize the grid with '.' 
        for (j = 0; j < DIM; j++) {
            grid[i][j] = '.';
            printf("%3c", grid[i][j]);
            if (j == 0 || j == 1) printf(" |");
        }

        if (i == 0 || i == 1) printf("\n- - - - - - - -\n");
    }

    do{
        switch (state) {
            case 0:             //State 0: Player 1 is asked for the coordinates corresponding to the position where you want to insert the X symbol
            printf("\n%s your turn: ", player1);
            scanf("%d %d", &coord[1], &coord[2]);

            if (grid[coord[1] - 1][coord[2] - 1] == '.' && grid[coord[1] - 1][coord[2] - 1] != 'O') {         //Check that the selected coordinates are free. Otherwise it prints an error message
                grid[coord[1] - 1][coord[2] - 1] = 'X';
                c = 'X';
                state = 2;
            }
            else{
                state = 0;
                printf("Invalid coordinates!\n");
            }
            
            break;

            case 1:             //State 1: Player 2 is asked for the coordinates corresponding to the position where you want to insert the O symbol         
            printf("\n%s your turn: ", player2);
            scanf("%d %d", &coord[1], &coord[2]);

            if (grid[coord[1] - 1][coord[2] - 1] == '.' && grid[coord[1] - 1][coord[2] - 1] != 'X') {         //Check that the selected coordinates are free. Otherwise it prints an error message
                grid[coord[1] - 1][coord[2] - 1] = 'O';
                c = 'O';
                state = 2;
            }
            else{
                printf("Invalid coordinates!\n");
                state = 1;
            } 

            break;

            case 2:                 //State 2: Check if there a right combination of X or O
            printf("\n");

            for (i = 0; i < DIM; i++) {
                for (j = 0; j < DIM; j++) {
                    printf("%3c", grid[i][j]);
                    if(j == 0 || j == 1) printf(" |");
                }

                if (i == 0 || i == 1) printf("\n- - - - - - - -\n");
            }

            nC = 0;
            nR = 0;
            
            i = coord[1] - 1;
            for (j = 0; j < DIM; j++) {
                if(grid[i][j] != c){
                    break;
                }
                else{
                    nR++;
                }
            }

            j = coord[2] - 1;
            for (i = 0; i < DIM; i++) {
                if (grid[i][j] != c) {
                    break;
                }
                else{
                    nC++;
                }
            }
            
            if (nC == 3 || nR == 3) state = 3;
            else if (c == 'X') state = 1;
            else state = 0;

            break;

            case 3:
            if (c == 'X') printf("\n%s IS THE WINNER!\n", player1);
            else printf("\n%s IS THE WINNER!\n", player2);
            return 0;

            break;
        }
    } while (1);
}

In C, array indices for an array with n elements run from 0 to n −1.在 C 中,具有n 个元素的数组的数组索引从 0 到n -1。

int coord[2] = {0, 0}; defines coord to have two elements, so their indices are 0 and 1.定义coord有两个元素,所以它们的索引是 0 和 1。

Throughout the code, coord[1] and coord[2] are used.在整个代码中,使用了coord[1]coord[2] coord[2] is outside the defined array, so the behavior of the program is not defined by the C standard. coord[2]在定义的数组之外,所以程序的行为不是由 C 标准定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM