Home > database >  variables in c changing value randomly
variables in c changing value randomly

Time:11-13

I'm learning C at school, and as homework I have to write the tictactoe game. 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. 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("<", 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("<", 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);
}

CodePudding user response:

In C, array indices for an array with n elements run from 0 to n−1.

int coord[2] = {0, 0}; defines coord to have two elements, so their indices are 0 and 1.

Throughout the code, coord[1] and coord[2] are used. coord[2] is outside the defined array, so the behavior of the program is not defined by the C standard.

  • Related