error in comparing elements of a 2d array using if-else statements

59 Views Asked by At

i tried to make a game using 2d character arrays. but the if else statements are not working and the loop is not breaking. Also when i assign a character to the last element of my row , the next row's first element automatically changes.

my code is :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void scoreprinter( int m , int n , char matrix[m][n]);
void newscore( int m , int n , char matrix[m][n]) ;
void maingame(int m , int n , char matrix[m][n]);

int main() {

    system("cls");

    char columns[2][2] ;

    newscore(2,2,columns);

    maingame(2,2,columns);

    getch();

    return 0;

}

void scoreprinter( int m , int n , char matrix[m][n]) {

    printf("\n\n\t\tTICTACTOE GAME\n");

    for(int i=0 ; i<=2 ; i++ ) {

        for(int j=0 ; j<=2 ; j++) {
        
            printf("\t%c\t",matrix[i][j]);

        }

        printf("\n");

    }

}

void newscore( int m , int n , char matrix[m][n]) {

    for(int i=0 ; i<=2 ; i++) {

        for(int j=0 ; j<=2 ; j++){

            matrix[i][j] = '_' ;

        }

    }

}

void maingame(int m , int n , char matrix[m][n]) {

    int  k=0;
    int a,b;

    for(k ; k<=9 ; k++) {
    
        system("cls");
        scoreprinter(2,2,matrix);
    
        if (k%2 == 0)
        {
            if  ( (matrix[0][0] == matrix[0][1] == matrix[0][2]) || (matrix[1][0] == matrix[1][1] == matrix[1][2]) || (matrix[2][0] == matrix[2][1] == matrix[2][2]) || (matrix[0][0] == matrix[1][0] == matrix[2][0]) || (matrix[0][1] == matrix[1][1] == matrix[2][1]) || (matrix[0][2] == matrix[1][2] == matrix[2][2]) || (matrix[0][0] == matrix[1][1] == matrix[2][2]) || (matrix[0][3] == matrix[1][1] == matrix[3][0]) )  
            {
                printf("condition matched");
                break;
            }
        
            printf("\nPlayer 1\nEnter row for marking\n");
            scanf("%d",&a);

            printf("\nEnter column for marking\n");
            scanf("%d",&b);

            matrix[(a-1)][(b-1)] = 'X';

            scoreprinter(2,2,matrix);

        
        }



        else if (k%2 == 1)
        {
            if  ( (matrix[0][0] == matrix[0][1] == matrix[0][2]) || (matrix[1][0] == matrix[1][1] == matrix[1][2]) || (matrix[2][0] == matrix[2][1] == matrix[2][2]) || (matrix[0][0] == matrix[1][0] == matrix[2][0]) || (matrix[0][1] == matrix[1][1] == matrix[2][1]) || (matrix[0][2] == matrix[1][2] == matrix[2][2]) || (matrix[0][0] == matrix[1][1] == matrix[2][2]) || (matrix[0][3] == matrix[1][1] == matrix[3][0]) )  
            {
                printf("condition matched");
                break;
            }

            printf("\nPlayer 2\nEnter row for marking\n");
            scanf("%d",&a);

            printf("\nEnter column for marking\n");
            scanf("%d",&b);

            matrix[(a-1)][(b-1)] = '0';

            scoreprinter(2,2,matrix);

        
        }
    

    } 

}

i tried creating another loop but it's not working.

3

There are 3 best solutions below

0
SmellyCat On

When I assign a character to the last element of my row, the next row's first element automatically changes.

For a noughts and crosses game, your array is normally 3x3. Your declaration is 2x2, so amending columns[0][2] is really amending columns[1][0].

char columns[3][3]; /* not [2][2] */

Since most of your for-loops seem to be hard-coded i<=2 or j<=2, they should work correctly once the array size is corrected.

0
SmellyCat On

The line (matrix[0][0] == matrix[0][1] == matrix[0][2]) || (matrix[1][0] == matrix[1][1] == matrix[1][2]) || (matrix[2][0] == matrix[2][1] == matrix[2][2]) || (matrix[0][0] == matrix[1][0] == matrix[2][0]) || (matrix[0][1] == matrix[1][1] == matrix[2][1]) || (matrix[0][2] == matrix[1][2] == matrix[2][2]) || (matrix[0][0] == matrix[1][1] == matrix[2][2]) || (matrix[0][3] == matrix[1][1] == matrix[3][0]) , as well as running over array bounds many times, is also doing a lot of comparisons of a boolean and a char ie comparing the result of a == operation with a character value.

It would have fewer errors if you broke it into a couple of functions:

column_matches(int col, char **matrix)
{
    return (matrix[0][col] == matrix[1][col]) && (matrix[0][col] == matrix[2][col]);
} 
row_matches(int row, char **matrix)
{
    return (matrix[row][0] == matrix[row][1]) && (matrix[row][0] == matrix[row][2]);
} 

If the array is 3x3, you shouldn't be accessing a [3] element.

1
Vlad from Moscow On

You declared a character array that has only two "rows" each of which contains two elements

 char columns[2][2] ;

So valid ranges for "rows" and elements in "rows" is [0, 2).

But within your function you are using ranges [0, 3) in for loops like

for(int i=0 ; i<=2 ; i++ ) {

    for(int j=0 ; j<=2 ; j++) {

As a result these loops produce an access to memory outside the array.

It seems you mean the following declaration of the array

 char columns[3][3] ;

Moreover your functions declared with parameters m and n like

void scoreprinter( int m , int n , char matrix[m][n]);
void newscore( int m , int n , char matrix[m][n]) ;
void maingame(int m , int n , char matrix[m][n]);

but these parameters are not used within the functions. Instead there are used the magic number 2. The compiler should issue messages about unused local variables. Do you read compiler messages? You should use these function parameters in loops instead of the magic number.

This if statement

if  ( (matrix[0][0] == matrix[0][1] == matrix[0][2]) || (matrix[1][0] == matrix[1][1] == matrix[1][2]) || (matrix[2][0] == matrix[2][1] == matrix[2][2]) || (matrix[0][0] == matrix[1][0] == matrix[2][0]) || (matrix[0][1] == matrix[1][1] == matrix[2][1]) || (matrix[0][2] == matrix[1][2] == matrix[2][2]) || (matrix[0][0] == matrix[1][1] == matrix[2][2]) || (matrix[0][3] == matrix[1][1] == matrix[3][0]) )  

is too long and as an result is unreadable. Moreover its expressions with the comparison operator are wrong.

For example, instead of this expression

(matrix[0][0] == matrix[0][1] == matrix[0][2])

you have to write

(matrix[0][0] == matrix[0][1] && matrix[0][1] == matrix[0][2])

You could use loops to check elements of the array instead of this unreadable and wrong if statement.

Pay attention to that you should check the user input that is that the entered values for a row and a column are in the valid range.