Search code examples
cmatrixchars

Trying to compare chars from matrix C


I'm tryingto compare chars from a matrix, but it's not adding any values and i don't know why

so here's my code:

#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LINES 1000
#define MAX_LINE_LENGTH 1000

//---------------------
//READING & WRITING
//---------------------

char *ints_new(int n)
{
    return (char *) malloc(n * sizeof(char));
} 

char **ints2_new(int rows, int cols)
{
    char **result = (char **) malloc(rows * sizeof(char *));
    char *p = ints_new(rows * cols);
    for (int i = 0; i < rows; i++, p += cols)
        result[i] = p;
    return result;
} 


int str_readline(FILE *f, char *s) 
{   
    int result = EOF; 
    char *p = fgets(s, INT_MAX, f); 
    if (p != NULL) 
    { 
        result = (int) strlen(s); 
        if (result > 0 && s[result-1] == '\n') 
            s[--result] = '\0'; 
    } 
    return result; 
} 

char *str_dup(const char *s) 
{ 
    char *result = (char *) malloc(strlen(s) + 1); 
    strcpy(result, s); 
    return result; 
} 

int strings_read(FILE *f, char **a) 
{ 
    int result = 0; 
    char line[MAX_LINE_LENGTH + 2]; 
    while (str_readline(f, line) != EOF) 
        a[result++] = str_dup(line); 
    return result; 
}  
// --------------------
//    Problema A
// --------------------

void values_to_m(char **m, int rows, int cols, char **readings)
{
    int i;
    int j;
    int k = 0;
    int l = 0;
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            m[i][j] = readings[k][l];
            l++;
        }
        k++;
        l = 0;
    }
}

int count_points(char **m, int i, int j, int rows, int cols)
{
    int result = 0;
    if(i < rows-2)
    {
        if(m[i][j] == m[i+1][j] == m[i+2][j])
            result++;
        if(j < cols-2)
        {
            if(m[i][j] == m[i][j+1] == m[i][j+2])
                result++;
            if(m[i][j] == m[i+1][j+1] == m[i+2][j+2])
                result++;
        }
        if(j > 1)
        {
            if(m[i][j] == m[i+1][j-1] == m[i+2][j-2])
                result++;
        }
    }
    else
    {
        if(j < cols-2)
        {
            if(m[i][j] == m[i][j+1] == m[i][j+2])
                result++;
        }
    }
    printf("%d\n", result);
    return result;
}

void points(char **m, int rows, int cols)
{
    int i;
    int j;
    int player1 = 0; //O's
    int player2 = 0; //X's
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            int count;
            count = count_points(m, i, j, rows, cols); //counts points
            if (m[i][j] == 'X') //if values i'm couning are X, points go to player 2
                player2 += count;
            else if(m[i][j] == 'O') //if O go to player 1
                player1 += count;
        }
    }
    printf("%d %d\n", player1, player2);
}

// --------------------
// --------------------

void test_problem_A()
{
    char **readings = malloc((MAX_LINES * MAX_LINE_LENGTH) * sizeof(char) + 1);
    int rows = strings_read(stdin, readings); //to read from console
    int cols = strlen(readings[0]);

    printf("%d\n%d\n", rows, cols); //just to make sure nr of rows and cols is right
    char **m = ints2_new(rows, cols); //create matrix

    values_to_m(m, rows, cols, readings); //put the values to matrix
    points(m, rows, cols); //calculate points

    ints2_printf(m, rows, cols, "%c");
}

// --------------------
// --------------------

int main(int argc, char **argv)
{
    test_problem_A();
    return 0;
}

My programm has to read a bunch of 'X', 'O' and '.'.

If there are 3 'X' in a row(vertical, horizontal or diagonal) player 2 gets 1 point, if the same happens to 'O', player 1 gets 1 point. '.' don't count any points.

my matrix had to have minimum 3 rows and cols and maximum 1000 rows and cols.

example: If i put in console

XXO
OXO
OXO

player 1 and 2 each get 1 point

if i put:

XXXXXO  //(int this line Player 2 get 3 points because there are 3 times 3 X in a row)
OXOXOO
OXOOXO
OXOXOO

player 1 gets 5 points and player 2 gets 6 points

So my problema is with function "count_points" it's not counting any points, when I print "result" it always gives me 0 points.

Can't I compare 2 chars if they belong in a matrix?

Thanks


Solution

  • In count_points, you try to compare three values with expressions like

    if (a == b == c) ...
    

    This doesn't do what you think it does. You treat it like a comparison in mathematical notation, but C interprets it as:

    if ((a == b) == c) ...
    

    The comparison a == b yields either 0 or 1. That result is then compared with c.

    You could rewrite your desired expression as

    if (a == b && b == c) ...
    

    Given that your a, b and c are compound expressions, you could write a small function for that:

    static int eq3(int a, int b, int c)
    {
        return (a == b && b == c);
    }
    
    int count_points(char **m, int i, int j, int rows, int cols)
    {
        int result = 0;
    
        if (i < rows-2) {
            if (eq3(m[i][j], m[i+1][j], m[i+2][j]))
                result++;
    
            if (j < cols - 2) {
                if (eq3(m[i][j], m[i][j+1], m[i][j+2]))
                    result++;
                if (eq3(m[i][j], m[i+1][j+1], m[i+2][j+2]))
                    result++;
            }
    
            if (j > 1) {
                if (eq3(m[i][j], m[i+1][j-1], m[i+2][j-2]))
                    result++;
            }
        } else {
            if (j < cols-2) {
                if (eq3(m[i][j], m[i][j+1], m[i][j+2]))
                    result++;
            }
        }
    
        return result;
    }
    

    As for the allocation of your matrix, see alk's answer. Your method of allocation - one char ** for the rows and then string duplication for the row data, could leave you with a ragged array and you may not safely access m[j + 1][i] for some cases where i is a valid index for row j, but not for row j + 1.