#Trying to compare elements in a 2d list to see if their adjacent

15 messages · Page 1 of 1 (latest)

merry lake
#

I know you cannot do whole array or structure comparison which is why i'm trying to put this function in a for loop and check each element one by one but for some reason i'm still getting "Invalid operands to binary expression ('Building' and 'Building')". I might have to make some changes to the other adjacent checks but for the first one of //check left die I think that one should be correct logically.

Example 2d array output:

X-1|X-1|X-1|
X-1|X-1|X-1|
X-1|X-1|X-1|
X-1|X-1|R6|
X-1|R3|S6|
G4|W2|R6|
polar kestrelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

merry lake
#

code:

int adjacent_die_score(Building building[MAX_ROWS][MAX_COLS], int current_row_index, int current_col_index)
{
    int total_adjacent_die = 0;

    // Check left die
    if (building[current_row_index > 0][current_col_index] ==
            building[current_row_index][current_col_index - 1]) {
        total_adjacent_die += 2;
    }

    // Check right die
    if (current_col_index < MAX_COLS - 1 &&
    building[current_row_index][current_col_index + 1] == building[current_row_index][current_col_index]) {
        total_adjacent_die += 2;
    }

    // Check top die
    if (current_row_index > 0 && building[current_row_index - 1][current_col_index] == building[current_row_index][current_col_index]) {
        total_adjacent_die += 2;
    }

    // Check bottom die
    if (current_row_index < MAX_ROWS - 1 && building[current_row_index +
    1][current_col_index] == building[current_row_index][current_col_index]) {
        total_adjacent_die += 2;
    }

    return total_adjacent_die;
}



int get_wood_points(Building building[][MAX_COLS])
{
    int current_row_index,
        current_col_index,
        wood_points;


    for (current_row_index = 0; current_row_index < MAX_ROWS; current_row_index++)
    {
        for (current_col_index = 0; current_col_index < MAX_COLS; current_col_index++)
        {
            if (building[current_row_index][current_col_index].material_type == 'W')
            {
              wood_points += adjacent_die_score(building, current_row_index, current_col_index);
            }
        }
    }

    return wood_points;
}
unborn crown
#

@merry lake You can't just compare two Buildings like that, yet

#

You must define an operator== in your class in order to do Building == Building

merry lake
unborn crown
#

!cppref operator overloading

polar kestrelBOT
merry lake
alpine bronze
#

Am I reading the line under //check left die correctly?

#

I’m on mobile so no highlighting

unborn crown
#

Alternatively, you could make a method to do the check: building[current_row_index][current_col_index + 1].equals(building[current_row_index][current_col_index])

#

If you aren't allowed to do that, then you have to compare the members of the struct manually.

merry lake
#

!solved