0

I'm having a problem on making a move on my 9 x 9 tic tac toe program.

When I enter a coordinate such as (5,5), The x is displayed correctly on the grid.

MY PROBLEM IS THAT when I enter a coordinate which has the number 7 in it such as (4,7), TWO X's ARE DISPLAYED ON THE GRID.

I previously did the program declaring my array as a global variable. All was working fine. The issue started when I switched to a dynamically allocated array and passed the array using a double-pointer. So I'm guessing that my problem is because of my array. Can someone tell me where does this issue occur and how to fix it please.

I have declared the array in my main

//previously the array was declared here
//char grid[ROW][COLUMN];

int main() 
{
    //dynamically create an array of pointers os size ROW
    char **grid = (char **)malloc(ROW * sizeof(char *));

    // dynamically allocate memory of size ROW*COLUMN and let *grid point to it
    *grid = (char *)malloc(sizeof(char) * ROW * COLUMN);

make move method

int make_move(char **grid, int x, int y,int row, int col, char letter) 
{
    if (x < 0 || x >= row || y < 0 || y >= col || grid[x][y] != ' ' )
    { 
        // checks to see if the input is valid
        return 1;
    }
    if( grid[x][y] == ' ')
    grid[x][y] = letter; 
    // sets the coordinates in the grid to the letter
    return 0;
}

update grid method


// Updates the grid accordingly every time a move is made
void update_grid(char **grid,int x, int y, int row, int col)
{
   // int counter = 1;

    //checks the input
    while  (x < 0 || x >= row || y < 0 || y >= col || grid[x][y] != ' ')
    {
        fputs("Error, Move not valid! Please reenter: ", stderr);
    scanf("%d,%d", &x, &y);
    }



    ++counter; 
    { 
        //Acts as an increment for the turns of the players
        if(counter % 2 == 0)
        { 
            //checks to see if it is player X's turn
        grid[x][y] = 'X';
        }
        if(counter % 2 != 0)
        {  
            //checks to see if it is player O's turn
            grid[x][y] = 'O';
        }

//prints grid

        printf(" ");
        for (int c = 0; c < col; c++) 
        {
            printf(" ");
        printf(" %d", c);
        }
        printf("\n");

        for (int r = 0; r < row; ++r) 
        {
            printf("%d", r);
            printf("|");
        for (int dot = 0; dot < (col*row); ++dot) 
            {

            printf("|");
            printf("%c", grid[r][dot]);
            printf(" ");

            if (dot == col - 1) 
                { 
                    // stops j from over printing
                printf("|| \n");
                break;
            }
            }
        }
    }
}

2
  • 1
    I think your mallocs are wrong, as you define only the value for the first *grid, but not the other members of your array **grid !? I was expecting a loop of mallocs over *grid... Commented Oct 25, 2019 at 19:26
  • stackoverflow.com/questions/2565039/… Commented Oct 25, 2019 at 19:30

1 Answer 1

1

As B. Go said your malloc is wrong, here's how it should look.

//dynamically create an array of pointers os size ROW
char **grid = malloc(ROW * sizeof(char *));

for(size_t i = 0; i < ROW; i++){
    grid[i] = malloc(COLUMN);
}

you allocated ROW pointers but you only filled the first one, you need to give them each COLUMN bytes of data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.