#Can't verify adjacent blocks to the actual one.

1117 messages · Page 2 of 2 (latest)

reef crow
#

All my files are in my cmake

regal monolith
#

is largestPlayerSquare a global, and what are the files called you are trying to use it in?

reef crow
#

int Game::getLargestSquareOfPlayer(int playerId) {
    int largestSize = 0;
    for(int i = 0; i < grid.numRows; i++) {
        for(int j = 0; j < grid.numCols; j++) {
            if(grid.grid[i][j] == playerId) {
                int size = getSquare(i, j);
                if(size > largestSize) {
                    largestSize = size;
                }
            }
        }
    }
    return largestSize;
}

int Game::getSquare(int topLeftX, int topLeftY) {
    int squareSize = 0;
    while(true){
        for(int i = topLeftX; i < grid.numCols && i < topLeftX + squareSize; i++) {
            if(grid.grid[topLeftY + squareSize][i] != playerId) {
                return squareSize;
            }
        }
        for(int j = topLeftY; j < grid.numRows && j < topLeftY + squareSize + 1; j++) {
            if(grid.grid[j][topLeftX + squareSize] != playerId) {
                return squareSize;
            }
        }
        squareSize ++;
    }
}

std::pair<int, int> Game::playerWithLargestSquare(){
    largestPlayerSquare = 0;
    playerId = 1;
    while(playerId <= numberOfPlayers) {
        int size = getLargestSquareOfPlayer(playerId);
        if(size > largestPlayerSquare) {
            largestPlayerSquare = size;
        }
        playerId += 1;
    }

    return std::pair{playerId, largestPlayerSquare};
}

my globals :

#pragma once

extern int numberOfPlayers;
extern int currentPlayer;
extern int screen;
extern int turn;
extern int playerId;
extern int largestPlayerSquare;
regal monolith
#

okay, well where is largestPlayerSquare actually defined though? I think you may have forgot to add int largestPlayerSquare; in a cpp file

#

extern int largestPlayerSquare; just declares it, so says it exists but doesn't actually create it

#

I am pretty sure you don't need it to be a global anymore if these are the only two spots you had it, so just remove extern int largestPlayerSquare; and add int in front of largestPlayerSquare = 0; in Game::playerWithLargestSquare()

reef crow
#

I should do the same with playerId ?

regal monolith
#

yeah, fuck the global and make it local in the function

reef crow
#

if I do that with playerId I have warnings in my other functions

#

undeclared identifier

regal monolith
#

show the code and the error

reef crow
regal monolith
#

yeah so it's supposed to be an argument to the function

reef crow
regal monolith
#

yup

reef crow
#

so now it works but the values are not good xD

regal monolith
#

lol, show

reef crow
#

Normally light green won (player 1) with 4x4 but just before it said me that a player with 4x4 square won with a 2x2 square

regal monolith
#

ok yeah I think I see the issue

regal monolith
# reef crow

so you know how the squareSize++ keeps incrementing it on and on right, in an infinite while-loop?

#

right now if either the bottom or right edge of the map is passed, the for-loop's condition will stop the loop, right?

reef crow
#

yep

regal monolith
#

the issue is that if the bottom or right edge of the map is passed, you don't want to just break the loop, but actually immediately return the squareSize, cause otherwise you're just going to continue incrementing squareSize and doing wrong shenanigans

#

see if you get that by looking at the code

reef crow
#

it's in the if condition

#

I think

regal monolith
#

wdym exactly with "it's"?

reef crow
#

the condition we need

#

must be in the "if"

regal monolith
#

yes!

#

so just move it to be inside of the if, so using the OR statement || to say that in either case you want to return

reef crow
#

and the condition is x < grid.numCols ?

regal monolith
#

the for-loop's job is to just increment x squareSize times, so that's the only thing you want in the for-loop's condition

reef crow
#

so what should I put in the if ?

regal monolith
#

what are you guessing

reef crow
#

for me it was x < grid.numCols

regal monolith
#

yes

#

so || x < grid.numCols in the if

#

but reversed of course, so || x >= grid.numCols

#

or just || x == grid.numCols

reef crow
#

I'll try now

#

...

#

Number 3(red) has to win with a 4x4 square

regal monolith
#

show the code again

reef crow
#
int Game::getLargestSquareOfPlayer(int playerId) {
    int largestSize = 0;
    for(int i = 0; i < grid.numRows; i++) {
        for(int j = 0; j < grid.numCols; j++) {
            if(grid.grid[i][j] == playerId) {
                int size = getSquare(i, j, playerId);
                if(size > largestSize) {
                    largestSize = size;
                }
            }
        }
    }
    return largestSize;
}

int Game::getSquare(int topLeftX, int topLeftY, int playerId) {
    int squareSize = 0;
    while(true){
        for(int i = topLeftX; i < topLeftX + squareSize; i++) {
            if(grid.grid[topLeftY + squareSize][i] != playerId || i == grid.numCols) {
                return squareSize;
            }
        }
        for(int j = topLeftY;  j < topLeftY + squareSize + 1; j++) {
            if(grid.grid[j][topLeftX + squareSize] != playerId || j == grid.numRows ) {
                return squareSize;
            }
        }
        squareSize ++;
    }
}

std::pair<int, int> Game::playerWithLargestSquare(){
    int largestPlayerSquare = 0;
    int playerId = 1;
    while(playerId <= numberOfPlayers) {
        int size = getLargestSquareOfPlayer(playerId);
        if(size > largestPlayerSquare) {
            largestPlayerSquare = size;
        }
        playerId += 1;
    }

    return std::pair{playerId, largestPlayerSquare};
}
regal monolith
#

thanks

reef crow
#

np

regal monolith
#

hmm, it could be because grid.grid[topLeftY + squareSize][i] != playerId || i == grid.numCols will actually try to read out of bounds memory in the left check before it even realizes that it's out of bounds with the right check

#

so reverse the order of checks here

#

reading out of bounds can make demons fly out of your nose, don't do it!

reef crow
#

Okay I'll try with that

regal monolith
#

int size = getSquare(i, j, playerId); I swear to god please change the i and j mentions to x and y cause you swapped the two because you got confused here again x)

reef crow
#

So the 4x4 is good but not the player, it's like it's +2

regal monolith
#

if you change it to x and y correctly everywhere you'll see the bug is fixed

reef crow
#

but i'll have to change that in all my code, bcs every loop is like that, hopefull it's a square

regal monolith
reef crow
#

this time player 4 won with a 5x5

regal monolith
reef crow
#
int Game::getLargestSquareOfPlayer(int playerId) {
    int largestSize = 0;
    for(int x = 0; x < grid.numRows; x++) {
        for(int y = 0; y < grid.numCols; y++) {
            if(grid.grid[x][y] == playerId) {
                int size = getSquare(y, x, playerId);
                if(size > largestSize) {
                    largestSize = size;
                }
            }
        }
    }
    return largestSize;
}

int Game::getSquare(int topLeftX, int topLeftY, int playerId) {
    int squareSize = 0;
    while(true){
        for(int x = topLeftX; x < topLeftX + squareSize; x++) {
            if(x == grid.numCols || grid.grid[topLeftY + squareSize][x] != playerId) {
                return squareSize;
            }
        }
        for(int y = topLeftY;  y < topLeftY + squareSize + 1; y++) {
            if(y == grid.numRows || grid.grid[y][topLeftX + squareSize] != playerId) {
                return squareSize;
            }
        }
        squareSize ++;
    }
}

std::pair<int, int> Game::playerWithLargestSquare(){
    int largestPlayerSquare = 0;
    int playerId = 1;
    while(playerId <= numberOfPlayers) {
        int size = getLargestSquareOfPlayer(playerId);
        if(size > largestPlayerSquare) {
            largestPlayerSquare = size;
        }
        playerId += 1;
    }

    return std::pair{playerId, largestPlayerSquare};
}
regal monolith
#

Thanks, so now it should be way clearer if I say this:

  1. You do int size = getSquare(y, x, playerId);
  2. Your function is int Game::getSquare(int topLeftX, int topLeftY, int playerId)
#

Look at the arguments

#

Hope you get why not doing x and y is so silly now :)

reef crow
#

yep...

regal monolith
#

if(grid.grid[x][y] == playerId) {

reef crow
#

Yep that's strange

#

player 1 won with 4x4

regal monolith
#

I think this is the third or maybe fourth i, j issue

reef crow
#

yep...

#

but still player 5 wins even with that

#

I think that it always take the highest number of player +1

#

bcs it stops at the last playerId but I never take the playerId of the biggest square

regal monolith
#

So you do

if(grid.grid[x][y] == playerId) {
  int size = getSquare(y, x, playerId);

which means you already know that it's at least a 1x1 square, right?

#

cause a tile is basically a 1x1 square

regal monolith
reef crow
#

solved it

regal monolith
#

wondering whether you did it in the way I was about to suggest

#

tell me

reef crow
#
std::pair<int, int> Game::playerWithLargestSquare(){
    int largestPlayerSquare = 0;
    int playerId = 1;
    int winner = 0;
    while(playerId <= numberOfPlayers) {
        int size = getLargestSquareOfPlayer(playerId);
        if(size > largestPlayerSquare) {
            largestPlayerSquare = size;
            winner = playerId;
        }
        playerId += 1;
    }

    return std::pair{winner, largestPlayerSquare};
}
regal monolith
#

ahhh yeah you're completely correct, nice!!

regal monolith
#

see if you can find it with this hint

reef crow
#

I was asking myself about this since the beginning of the code but wanted to see the result, bcs you never take the player with the largestSquare

reef crow
#

I'd say to add +1's but not sure about that

regal monolith
#

I haven't said where anything's wrong, I am just asking whether you get that you at least always expect that function to return 1, right?

reef crow
#

yep

regal monolith
#

okay, well look at what you actually do in the fn

#

the function getSquare(), that is

#

what variable are you returning from the fn?

reef crow
#

squareSize

#

so we can put it at 1

regal monolith
#

yes, it shouldn't be 0 at the start

#

this is I think the actual last bug fixed now if you change that 0 to a 1

reef crow
#

Everything works as expected

regal monolith
#

yooo

#

poggers

reef crow
#

finally 😭

#

I'm so so so thankful, you can't imagine

regal monolith
#

kekw so after a billion messages, I guess finally it's time for the holy ! solved?

reef crow
#

I think it's time

regal monolith
#

gl man, feel free to ping me if you ever need more help

#

and lmk what happens with the grade

reef crow
#

I'll for sure !

#

Bye and good night ! (if it's night for you)

regal monolith
#

it is, I'm Dutch so I think it's also 1 AM for you?

#

o/

reef crow
#

Yep !

regal monolith
#

Let's end it with !solved

reef crow
#

!solved

plush walrusBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

reef crow
#

o/

regal monolith
#

o/