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

1 messages · Page 1 of 1 (latest)

plush walrusBOT
#

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 use !howto ask.

scarlet mural
#

breakpoint and debug it

reef crow
#

????

#

I'm asking help to do a function

sinful anchor
west dragon
#

well, what do you need to check?

reef crow
#

I need to check if the cases around the block that I want to place are the same id as the block I want to place or 0.
If it's the same id it should return true so I can place the block, if not it returns false.
The verification must be like that : it can touch corners but can't touch borders of the blocks. @west dragon

pine acorn
#

The blocks are on a 2d grid, correct?

reef crow
#

Yep like that

#

the light green one has id 1, dark green 2, etc. for other colors

pine acorn
#

That image doesn’t load for me, might be just discord though

#

Now

pine acorn
# reef crow

Stuff like this is great contextual information to have in the original post

#

How would you do this check manually?

reef crow
#

by checking every borders of the block

pine acorn
#

How would you do it more specifically

reef crow
#

idk that's why I'm asking, bcs I really don't know how to do it

west dragon
#

well, do you know how to check what id a given block has?

reef crow
#

if it's the actual block it's currentBlock.id but when it's place I don't know

west dragon
#

well, there seems to be this grid object

#

that can probably tell you what's in a given cell?

reef crow
#

I can provide the entire file as code it would be more simple

west dragon
#

just look at the grid object ^^

reef crow
#

yep but how can I check the id of this cell now ?

#

The grid is like that after some blocks placed

reef crow
#

Do you have any advices and help ?

humble pike
#

you have this position : grid.grid[item.row][item.column]
so check

grid.grid[item.row + 1][item.column]  == id
grid.grid[item.row - 1][item.column]  == id
grid.grid[item.row][item.column + 1]  == id
grid.grid[item.row][item.column - 1]  == id
#

make sure you don't go out of bound tho

reef crow
#

so I have to do another condition to check if it's out of bound ?

#

if it's correct I already have a function for it so it'll be easier

regal monolith
reef crow
#

Okay thanks a lot ! I'll try to do all of that and tell you the result because I tried something in the same style but it didn't work

regal monolith
#

What helped me when I did this the first time was getting a bit of graph paper and drawing on it till stuff clicked in my head

#

Going out of bounds is equivalent to lifting your pencil off of the paper and writing on the table, which is bad

reef crow
#

Yep I understand, the problem is that I don't have a lot of time left but I still have a lot of things to do on this project, and I think I won't have everything when I will give it so i'm a bit sad

plush walrusBOT
#

@reef crow Has your question been resolved? If so, type !solved :)

reef crow
#

I have a problem, it seems to work but I can't place 2 blocks of the same id one adjacent to the other, each block can't touch any other block, that's a bit problematic

#

@regal monolith @humble pike

humble pike
reef crow
#

I have to avoid 2 blocks of different id to be place adjacently but 2 with the same id should work

humble pike
#

ah

#

and what's your code?

reef crow
#
bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column]  > 0)
        {
            return false;
        } else if(grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column]  > 0) {
            return false;
        } else if(grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1]  > 0) {
            return false;
        } else if(grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1]  > 0) {
            return false;
        }
    }
    return true;
}
#

But I don't know why it doesn't work

humble pike
#

it shouldn't be >

#

0

#

that's where it's wrong

reef crow
#

0 is my grid, so the basic cases

#

And I can place my blocks on it because it represents empty cases without blocks on it

humble pike
#

wait i didn't read the first part sorry

reef crow
#

np ^^

humble pike
#

grid.grid[item.row][item.column - 1] returns the id ?

reef crow
#

I think yes, bcs now I can place 2 blocks adjacently

humble pike
#

it works now? what did you change if you don't mind

reef crow
#

It doesn't work as I want, because blocks can't touch each other even the blocks with the same id

#

I didn't change anything since the screen I gave you, it was already working like I just said

humble pike
reef crow
#

Oh no sorry if I misspoke

humble pike
#

grid.grid[item.row + 1][item.column] != currentBlock.id this is true when the grid id is 0

reef crow
#

that's why I added && it must be different of 0

#

wait gonna send a screen

#

Each color is a different id and the background is full of 0 id, but as you can see, same color can't touch each other and that's a problem, light green should touch light green etc. for other colors but I can't

humble pike
#

ok

reef crow
#

I hope this was a better explanation

humble pike
#

yep

#

thx

#

are you sure this returns false btw?

humble pike
#

like the problem isn't in an other part

reef crow
#

I think because whithout this function I can place block next to each other as I want

humble pike
#

ok

reef crow
#

I'll try to see if the current block has really the id I think it has but normally it must be good

humble pike
#

you can put a break point in the function too if you can debug

reef crow
#

But I really can't understand why it doesn't work because I literally said in my function : if it's different of the currentBlock it doesn't work so that means if it's the same id it should work

humble pike
#

yes, i'm confused too tbh

reef crow
#

bcs colors are = id so if the block I'm holding is green and place it is still green just why

humble pike
#

if you can show more of the code i can look more detail

#

to me it doesn't look like the problem is here

reef crow
#

I'll give the entire file but my project has 13 files linked I hope it'll be enough 1 file

#

The only pb is that I totally forgot to put comments 😓

humble pike
#

it's fine np

reef crow
humble pike
#

void Game::LockBlock() {
std::vector<Position> tiles = currentBlock.GetCellPositions();
if (BlockFits() && NearBlockCheck()) {
for (Position item : tiles) {
currentBlock.id = currentPlayer;
grid.grid[item.row][item.column] = currentBlock.id;
}
if (currentPlayer < numberOfPlayers) {
currentPlayer += 1;
} else {
currentPlayer = 1;
}
currentBlock = nextBlock;
nextBlock = GetRandomBlock();
}
}

#

and shouldn't it be before the checks

#

@reef crow

reef crow
#

oh, currentPlayer change everytime we change block and is just an int so it means that the block id = player id so it's more simple, because they are always the same that the player currently playing

humble pike
#

do all the blocks in your game have id = 1 once they are locked then?

#

because currentPlayer is 1

reef crow
#

yep and after we lock a block it changes to the next player, so after that the player = 2 so the block placed = 2

humble pike
#

each player is a color ?

reef crow
#

approximatively, I associated the id with a color

#

if id = 1, color will be light green, 2 = dark green etc.

humble pike
#

try to change

void Game::LockBlock() {
  std::vector<Position> tiles = currentBlock.GetCellPositions();
  if (BlockFits() && NearBlockCheck()) {
    for (Position item : tiles) {
      currentBlock.id = currentPlayer;
      grid.grid[item.row][item.column] = currentBlock.id;
    }
    if (currentPlayer < numberOfPlayers) {
      currentPlayer += 1;
    } else {
      currentPlayer = 1;
    }
    currentBlock = nextBlock;
    nextBlock = GetRandomBlock();
  }
}

to

void Game::LockBlock() {
  std::vector<Position> tiles = currentBlock.GetCellPositions();
  currentBlock.id = currentPlayer;
  if (BlockFits() && NearBlockCheck()) {
    for (Position item : tiles) {
      grid.grid[item.row][item.column] = currentBlock.id;
    }
    if (currentPlayer < numberOfPlayers) {
      currentPlayer += 1;
    } else {
      currentPlayer = 1;
    }
    currentBlock = nextBlock;
    nextBlock = GetRandomBlock();
  }
}

please

reef crow
#

no way

#

you are a genius 😭

humble pike
#

you didn't change the id of the block before the check

#

so you were checking if id is 2 when, the id of the block you're holding is still 1

sinful anchor
#

@humble pikeYou are a genius bro !

#

I'm watching your conversation on his computer and your help is very important to us! Now, I think we will be able to continue our project 😮

#

omg 😭

humble pike
#

Good luck!

reef crow
#

Thanks a lot ! Now I have to check for borders of the grid because it doesn't work everytime when I try to place my blocks at the border of the grid

humble pike
#

it's because you go out of bounds

#

in your array

#

grid.grid[item.row - 1][item.column]
if item.row is 0
you will have
grid.grid[- 1][item.column]

#

which doesn't exist

plush walrusBOT
#

@reef crow Has your question been resolved? If so, type !solved :)

reef crow
#

In my file I have a IsBlockOutside() function do you that if I add it, it will work ?

#

I'm not sure at all about that bcs it's about the block and not about the verification

humble pike
#

you just have to add if (item.row - 1 >= 0)

reef crow
#

not dumb xD

humble pike
#
bool Game::NearBlockCheck() {
  std::vector<Position> tiles = currentBlock.GetCellPositions();
  for (Position item : tiles) {
    if (item.row + 1 <= grid.grid.length)
      if (grid.grid[item.row + 1][item.column] != currentBlock.id &&
          grid.grid[item.row + 1][item.column] > 0) {
        return false;
      }
    if (item.row - 1 >= 0)
      if (grid.grid[item.row - 1][item.column] != currentBlock.id &&
          grid.grid[item.row - 1][item.column] > 0) {
        return false;
      }
    if (item.column + 1 <= grid.grid[item.row].length)
      if (grid.grid[item.row][item.column + 1] != currentBlock.id &&
          grid.grid[item.row][item.column + 1] > 0) {
        return false;
      }
    if (item.column - 1 >= 0)
      if (grid.grid[item.row][item.column - 1] != currentBlock.id &&
          grid.grid[item.row][item.column - 1] > 0) {
        return false;
      }
  }
  return true;
}

something like that probably

reef crow
#

I'm so thankful for your help

#
bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column]  > 0 && item.row + 1 >= 0)
        {
            return false;
        } else if(grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column]  > 0 && item.row - 1 >= 0) {
            return false;
        } else if(grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1]  > 0 && item.column + 1 >= 0) {
            return false;
        } else if(grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1]  > 0 && item.column - 1 >= 0) {
            return false;
        }
    }
    return true;
}

Mine is like that

humble pike
#

item.column + 1 >= 0 and item.row + 1 >= 0 are wrong

#

let's say your grid is 30 by 30 (0 to 29), if you're in position 29 and you add 1, you would be in position 30 which is greater than 0, but is out of bound

reef crow
#

but why is it not the same for the minus ?

#

oh nevermind

humble pike
#

when you add you have to check the upper bound of the array

reef crow
#

you're right

#

but grid.grid.row doesn't work

humble pike
humble pike
#

it's grid.grid.size() or something for the length of the row

reef crow
#

t'aurais pu me dire que t'étais fr mdr

sinful anchor
humble pike
#

and grid.grid[item.row].size() for the length of the column

#

if you use vectors for the grid it should be size()

#

if it's an array its sizeof(grid.grid)

reef crow
#

you lost me

humble pike
#

what the data type of grid.grid

reef crow
#

actually the top right and bottom right corners don't work while placing a block

reef crow
#

I think it's an array

humble pike
#

int[] ?

reef crow
#

yep

#

(I think)

humble pike
#

go to Position

#

you have the datatypes

reef crow
#

C++ is destroying my brain

humble pike
#

go to position.h

reef crow
humble pike
#

it's grid not position sorry

#

grid.h

reef crow
humble pike
#

okay array

#

so if you use sizeof(grid.grid)

reef crow
#

Np I was asking myself why position but you seem very good in C++ so I trust you

humble pike
#

you get the length of the row

#

and if you use sizeof(grid.grid[item.row])

#

you get the length of the column

#

you can use those to check if the positions you're trying are out of bound

reef crow
#

Top right, bottom right and bottom left are still not working

#

forgot that

#

i'm still dumb

#

and still writing the wrong signs

#

it was with ... >= sizeof because it's over 30 for a grid of 30

#

it really changes something ? because it's working with what I have

humble pike
#

gimme 2 sec

#

okay yeah it was fine

#
bool Game::NearBlockCheck() {
  std::vector<Position> tiles = currentBlock.GetCellPositions();
  for (Position item : tiles) {
    if (grid.grid[item.row + 1][item.column] != currentBlock.id &&
        grid.grid[item.row + 1][item.column] > 0 &&
        item.row + 1 < sizeof(grid.grid)) {
      return false;
    } else if (grid.grid[item.row - 1][item.column] != currentBlock.id &&
               grid.grid[item.row - 1][item.column] > 0 && item.row - 1 >= 0) {
      return false;
    } else if (grid.grid[item.row][item.column + 1] != currentBlock.id &&
               grid.grid[item.row][item.column + 1] > 0 &&
               item.column + 1 < sizeof(grid.grid[item.row])) {
      return false;
    } else if (grid.grid[item.row][item.column - 1] != currentBlock.id &&
               grid.grid[item.row][item.column - 1] > 0 &&
               item.column - 1 >= 0) {
      return false;
    }
  }
  return true;
}

re-posting, it's what you had

humble pike
reef crow
#

I can't, I did a function to avoid that, even if we rotate a block it'll never be out of the grid

humble pike
#

okay nice

#

you must have used sizeof(grid.grid[item.row] this in the function then

reef crow
#

I used IsCellOutside() function that I wrote in another file

humble pike
#

yep i saw that, i meant in this function you must have used something like that sizeof(grid.grid[item.row]

reef crow
#
bool Grid::IsCellOutside(int row, int column)
{
    if(row >= 0 && row < numRows && column >= 0 && column < numCols)
    {
        return false;
    }
    return true;
}
#

pretty similar

humble pike
#

okok, anyway ask if you have more questions

reef crow
#

Thanks a lot, I'll try to not bother you too much, you already helped me a lot

reef crow
#

luken... I have a problem, I thought that it was working but sometimes it can't be place near block of a different id and sometime it can

humble pike
#

can you send screenshots

#

of the different cases

reef crow
#

On this screen we can't see but the strange red L in bottom left can't be placed, but all other blocks have been placed

humble pike
#

it just doesn't work uh

reef crow
#

it seems taht I can't place it under and on the right of a block but I can place it on the top and on the left

humble pike
#

but it did work before the bound changes

humble pike
reef crow
#
bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0 && item.row + 1 >= sizeof(grid.grid))
        {
            return false;
        } else if(grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column] > 0 && item.row - 1 >= 0) {
            return false;
        } else if(grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1] > 0 && item.column + 1 >= sizeof(grid.grid[item.row])) {
            return false;
        } else if(grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1] > 0 && item.column - 1 >= 0) {
            return false;
        }
    }
    return true;
}
#

For the size of I didn't write the same thing as you bcs it was not working with a <, bcs it's over 30, not under

humble pike
#

ah yes

#

wait

#

no

#

its <

#

if the id is different and the id is not 0 and the position is in the grid you return

#

item.row + 1 >= sizeof(grid.grid)) should be item.row + 1 < sizeof(grid.grid))

#

same with the other one

reef crow
#

If I do this I can't place block on the bottom line

humble pike
#

and you can in the right column?

reef crow
#

with the code that I have yes but if I put < instead of >= it doesn't work anymore

humble pike
#

so you're in row 29, if the id in 30 is different from yours and not 0 it will be true but you don't want to check it so you do 30 < 30 which is false so you don't enter in the if and you don't return

#

so it is <

reef crow
#

but it doesn't work...

humble pike
#

instead of size

reef crow
#

Okay I have to include my grid.h file so

humble pike
#

i think i didn't use the sizeof function right

#

oh yeah

reef crow
#

the include changes nothing I can't use numCols and numRows

humble pike
#

oh yeah it's sizeof(grid.grid) / sizeof(int)

#

just add that

#

/ sizeof(int)

reef crow
#

still doesn't work

#

and the strange thing is that I can place some blocks in the row but not the entire row, like sometimes it works and sometimes not

humble pike
#

can you stream it? i can't talk tho

reef crow
#

I can't rn sry

humble pike
#

okay np

#

do these changed block you from puting next to another box at least?

#

< and / sizeof(int)

reef crow
#

blocks can only be placed at the top and left of blocks that are not the same id

#

My conditions should be false

humble pike
#

wtf

humble pike
#

ill have to go for 30 minutes after i look at it

#

my guess would be grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0 is true but then you do item.row + 1 >= sizeof(grid.grid) which is false and you don't enter

reef crow
#

no problem don't worry

#
bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0 && item.row + 1 >= sizeof(grid.grid))
        {
            return false;
        } else if(grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column] > 0 && item.row - 1 >= 0) {
            return false;
        } else if(grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1] > 0 && item.column + 1 >= sizeof(grid.grid[item.row])) {
            return false;
        } else if(grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1] > 0 && item.column - 1 >= 0) {
            return false;
        }
    }
    return true;
}
#

the borders of the grid work but not left and top of blocks

#

but right and bottom of blocks work bcs I can't place a block of different color under and on the right

humble pike
#

you didn't add the changes

bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0 && item.row + 1 < sizeof(grid.grid) / sizeof(int))
        {
            return false;
        } else if(grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column] > 0 && item.row - 1 >= 0) {
            return false;
        } else if(grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1] > 0 && item.column + 1 < sizeof(grid.grid[item.row]) / sizeof(int)) {
            return false;
        } else if(grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1] > 0 && item.column - 1 >= 0) {
            return false;
        }
    }
    return true;
}

retry with this please

reef crow
#

with the change I just can't place blocks on the right and bottom border of the grid

#

but now blocks of different colors can't touch

humble pike
#

nice

#

at least that lol

reef crow
#

yep we're soon here !

humble pike
#

be back in 30 minutes then

reef crow
#

okay !

humble pike
# reef crow okay !

it was faster than i thought,

can you try to get ride of NearBlockCheck() in your LockBlock() function, restart the game and see if you can put your blocks on the last column/row, to make sure the problem is coming from NearBlockCheck

reef crow
#

Sorry didn't tell you that I was going to be afk

#

So

#

I'll test

#

So without it I can put blocks on last row and column

reef crow
#

That's the only problem left

reef crow
#

@humble pike

#

I tried to resolve it but really can't understand why bcs the conditions seems good

regal monolith
#

Add the compilation flag -g to add debugging information to your executable, and run lldb a.out or gdb a.out to start debugging it. If you named your executable something other than a.out, use that name in the lldb/gdb command

#
  1. b main to set a breakpoint in your main function
  2. r to run the program until it hits that breakpoint
  3. s to step a single line, stepping into every function call
  4. n to go to the next line, stepping over every function call
  5. p foo to print a variable called foo
  6. Ctrl+D to exit the debugger
    You can always type help to see a list of available commands
reef crow
#

I'm running with clion rn so didn't do anything with a terminal

regal monolith
reef crow
#

I know where the problem is, but I just don't know how to fix it

regal monolith
reef crow
#

I know that it's this function

bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0 && item.row + 1 < sizeof(grid.grid) / sizeof(int))
        {
            return false;
        } else if(grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column] > 0 && item.row - 1 >= 0) {
            return false;
        } else if(grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1] > 0 && item.column + 1 < sizeof(grid.grid[item.row])) {
            return false;
        } else if(grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1] > 0 && item.column - 1 >= 0) {
            return false;
        }
    }
    return true;
}

Before it was working with the last version that I don't have anymore but might be in this channel, but this version is so useful bcs it prevent the player to put 2 blocks of different colors next to each other

regal monolith
#

Okay, but just knowing which function you suspect is the cause isn't very helpful as I don't have the ability to check which values the grid cells have, and so on. But you do, so please fire up a debugger. If you absolutely don't want to open a terminal you can follow a short online tutorial to learn how to use clion's debugger. But it's really hard for others to help you, you're way more capable to identify the issue than we all are :)

reef crow
#

I'll try a debugger for sure

#

the debugger doesn't seems to change something will putting it on this function

#

When I try to put a block on the right or bottom of the grid I don't have anythig that is returned

regal monolith
reef crow
#

the only function in my main function that can be useful is the handleInput one, should I try with this ?

regal monolith
reef crow
#

If I put it on the first line it doesn't seems to work because the main menu stills open and I can do everything as before

#

I deleted the line but it didn't work

regal monolith
#

Make a screenshot showing you having added that as the first line in your main, and of you having set a breakpoint on the InitWindow line after it.

reef crow
#

this didn't work

regal monolith
#

Wdym by didn't work specifically?

reef crow
#

It works like before, not any change even with the breakpoint

regal monolith
#

Right, but in what manner are you starting your program right now? With a hotkey, or with an on-screen button?

reef crow
#

the on-screen button

regal monolith
#

Click the bug icon and it should work

#

Or this

reef crow
#

oh I thought the breakpoint would work even with standard mode

regal monolith
#

Nope :)

#

Debug mode has debug info, regular mode doesn't

reef crow
#

so yeah it works

#

i'm just dumb xD

regal monolith
#

No worries

#

You can see foo 42 in the debugger now?

reef crow
#

yep

regal monolith
#

Try to step into functions using the debugger's keys that popped up

#

See if you can see all variables

#

You can click on vectors in the debugger's window to inspect them closer

#

Oh, and you can remove int foo = 42; now of course

reef crow
#

So in the function should I put a breakpoint on each if ?

#

Idk where it would be the most useful

regal monolith
#

You don't necessarily need to put more breakpoints, they just allow you to continue running the program as normal until that breakpoint is hit, where the program will be paused

reef crow
#

okay so i'll just try to put blocks on borders and see what is returned

regal monolith
#

I recommend creating a grid that is as small as possible that still shows of your problem you're having. That way you can step through every single line of executed code really quickly.

#

No, I recommend for now just stepping into every function call to get a feel for the debugger. There is the step over button to not go into a function call, and the step into button to go into a function call, that's all you need!

reef crow
#

oh god that's gonna take an eternity

#

I have so much things left to do on my project

regal monolith
#

good luck ;p

#

But by stepping over function calls you can skip huge portions you're not interested in

humble pike
#

the problem is with the size of the array i think, you hardcoded 30, but the actual size you're using is 20

#

so when you check if 20 is less than the size of the array it says true

reef crow
#

But why did it work with the last code ? I coded 30 to have the max size, but I change it when I initalize the grid

humble pike
#

so 2 options,
1 don't hardcode the size,
2 get the size of the grid as a constant which you can use in your function instead of the size

humble pike
reef crow
#

when we first started coding this, I could place blocks on the borders of the grid

#

but after we change some things, we resolved the other problem of blocks next to each other but created the one with borders

humble pike
#

yes but now we are checking at +1,
like we have the grid 0 - 19
and we check the value in 20 which is undefined so we don't know what happens,
what i suggested was to omit the check when the box we were checking was out of the grid,
for that we used the size of the array
but the size of the array is actually 30 not 20, so when we check if the box in 20 is in the grid it says it is

reef crow
#

but if I put it to 30, when i'll have the 30x30 grid it won't work ?

humble pike
#

try it

reef crow
#

forgot to tell you that I also have a 30x30 grid for 4-9 players

humble pike
#

and it doesn't work in this either?

reef crow
#

only on the bottom row

humble pike
humble pike
#

instead of sizeof(grid.grid) / sizeof(int)) and sizeof(grid.grid[item.row])

regal monolith
# reef crow I know that it's this function ```cpp bool Game::NearBlockCheck() { std::v...

Another thing that is immediately sus is that a line of code you shared here checks whether the index is in bounds last, rather than first. You should switch the order of checks so you first check whether the index is in bounds. :)

if(grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0 && item.row + 1 < sizeof(grid.grid) / sizeof(int))
#

And yeah, combine it with Luken's suggestion

regal monolith
#

You two work on this together as a group project I presume?

reef crow
#

nope I work with @sinful anchor

humble pike
#

well i did use an other if at first

regal monolith
#

Ah I see, but anyways Tenebres fix that

reef crow
#

Luken's a very kind guy that's helpin me too

sinful anchor
#

im looking his computer when he's talking with you don't worry, he sits near to me

reef crow
#

fck I don't have the numRows and numCols variables in this file

regal monolith
#

Pass it as an argument somehow

reef crow
#

I don't understand how to do that to have something that works

humble pike
reef crow
#

I think that I did the error to create global variables for some variables in my code

regal monolith
#

Are the width and height globals?

reef crow
#

I totally forgot about getters and setters

#

I can maybe make them global

humble pike
regal monolith
#

Ah, yeah then getter and setter is good

#

May not even need setter

reef crow
#

I'll say this one more time sorry but I really can't understand why it was working before

regal monolith
#

You can get unpredictable results when you have undefined behavior, like reading out of bounds, or many other reasons. It may have been because of that, but it doesn't really matter, all that matters is getting the current version working

reef crow
#

Like if I don't put the function in the lockBlock, I can put blocks on the borders

regal monolith
#

If you want to understand why your program behaves the way it does, the best way is to just use the debugger and closely inspect the values. Right now you're kind of flying blind if you don't

reef crow
#

I really hope that I will fix this today

regal monolith
#

Huh?

humble pike
#

just do what we told you for now, it should work after that

reef crow
#

Creating a getter ?

#

You told me too much things

#

guys I just discovered something

#

It works on some places but not on others, my red and dark green blocks are placed, but I couldn't place them on another place of the border, like I tried to put the red one on the left or right and it wasn't working

#

wtf is that

#

my orange block is placed too

humble pike
#

Welcome to undefined behavior i guess

reef crow
#

It can do strange things at this point ???

reef crow
#

@humble pike So I need to do a Getter ?

humble pike
humble pike
reef crow
#

okay thanks I'm doing that rn

#

Is this ok like this ?

#

I moved the numRows and numCols from private to public so I think it's easier to get them without a getter but with just an include

plush walrusBOT
#

@reef crow Has your question been resolved? If so, type !solved :)

humble pike
#
bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(item.row + 1 < grid.numRows &&  grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0)
        {
            return false;
        } else if(item.row - 1 >= 0 && grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column] > 0) {
            return false;
        } else if(item.column + 1 <  grid.numCols && grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1] > 0) {
            return false;
        } else if(item.column - 1 >= 0 && grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1] > 0) {
            return false;
        }
    }
    return true;
}
reef crow
#

Gonna invite you at my wedding

#

Can I ask you just 1 more thing about another function ?

humble pike
#

yep

velvet stratus
#

@reef crow curious, what are you working on?

reef crow
#

Just a project like that, I'm bad in C and C++

velvet stratus
#

Oh, alright

#

It looks cool!

reef crow
#

it's too hard for me

#

but I have to finish it

velvet stratus
#

That's exactly how you learn 🙂

reef crow
velvet stratus
#

Please don't send screenshots of your code

#

Use codeblocks

#

Like this

reef crow
#

yep i'll do that

#

I have this function to get all blocks in my game and I absolutely need to shuffle it and after that take all blocks one by one each turn but I struggle to shuffle it. Actually I have a function in which each turn the current player takes a random block in it, delete it from the vector and if it's empty, it takes back all blocks.
I would like to do the same but just to shuffle it one time before everything and each turn, so when the player change in the 3rd screen, take the next block.

std::vector<Block> Game::GetAllBlocks()
{
    return {SmallLBlock(), TBlock(), SmallCrossBlock(), ZBlock(), SmallTBlock(), SquareBlock(), UBlock(), ThreeBlock(), LightningBlock(), SmallCornerBlock(), SmallStairsBlock()}; //Continuer de mettre le reste des blocs dont ceux qui se répètent plusieurs fois
}
Block Game::GetRandomBlock() //A remplacer par GetNextBlock()
{
    if(blocks.empty())
    {
        blocks = GetAllBlocks();
    }
    int randomIndex = rand() % blocks.size();
    Block block = blocks[randomIndex];
    blocks.erase(blocks.begin() + randomIndex);
    return block;
}
void Game::LockBlock() {
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    currentBlock.id = currentPlayer;
    if (BlockFits() && NearBlockCheck()) {
        for (Position item : tiles) {
            grid.grid[item.row][item.column] = currentBlock.id;
        }
        if (currentPlayer < numberOfPlayers) {
            currentPlayer += 1;
        } else {
            currentPlayer = 1;
        }
        currentBlock = nextBlock;
        nextBlock = GetRandomBlock();
    }
}
velvet stratus
#

What version of C++ are you using?

#

et pourquoi as-tu besoin d'une fonction pour les blocs aléatoires?

#

Not entirely sure of the use there

reef crow
#

i need to shuffle blocks just one time to get them in the order it is after the shuffle because I need to print the 5 next blocks on my screen.
I also need to add 1x1 blocks at the first turn for all the player to place their starting block with a specific condition, but if I take a random block in the vector each turn I won't have these 1x1 blocks at the first turn.
Like if there are 5 players, I'll need to add 5 1x1 blocks at the beginning of the list.
Actually I see the thing like that.

#

And my version might be 3.11

#

that's what I found in my cmake

humble pike
reef crow
#

the problem is that my vector is a function that returns it and I don't know how I can change it

humble pike
#

blocks is a vector

#

blocks = GetAllBlocks();

reef crow
#

oh okay so I can just shuffle blocks

humble pike
#

yep

reef crow
#

After the shuffle do you know how I can do to always have the next block in the list ?
I thought about
currrentBlock = blocks[currentBlock +1]
But not totally sure about that

humble pike
#

there are some ways,
you can use a counter for the index of the block which you can put in your class,

#
#include <iostream>
class Yours {
private:
  int counter = 0;
public:
  void foo() {
    std::cout << counter;
    counter++;
  }
};
int main() {
  Yours y;
  y.foo();
  y.foo();
  y.foo();
}
#

you can use a static int in the function and increment the int every time you enter the function,

like that

#include <iostream>
void foo(){
  static int i = 0;
  std::cout << i;
  i ++;
}

int main(){
foo();
foo();
foo();
}
reef crow
#

Okay, i'm actually creating the shuffle function but I don't know if it worked xD
Idk why but it seems that I can't print the vector, like the names in it

#

if I print names I could see if it really shuffled or not

humble pike
#

how are you trying to print it

reef crow
#

Trying different methods from chatgpt

humble pike
#

you could also just put a breakpoint and look at the vector in debug

#

way easier

reef crow
#

oh it can work like this

#

I can see the id but I don't know what it represents

humble pike
#

you should be able to see the values too

reef crow
#

It's actually like this

humble pike
#

expand them

#

click the arrow

reef crow
#

the only difference is the id

humble pike
#

if you expand cells

#

and expand whatever is inside again and again

#

you will have the positions of the boxs of that block

#

then you can interpret which forme it is

reef crow
#

okay I can see the positions

#

now I have to compare 😂

humble pike
#

ye

#

you don't have to look at every single block just find the first 3 restart the game and make sure they are different

reef crow
#

Nice, I only looked at the first one bcs the next ones are with GetRandomBlock()

#

so now I have to implement something to change the block

regal monolith
# reef crow

Just fyi, now you know debugger basics you're able to debug significantly harder problems than before in a much shorter time 🥳

#

It's great to see you able to use it to inspect values

reef crow
#

Yeah I can see that I never really used it bcs I had such easy codes but rn it's pretty useful

reef crow
#

Hello guys ! Sorry to bother you again but I'm still struggling with the block change after each turn, these are my 2 functions :

Game::Game()
{
    grid = Grid();
    blocks = GetAllBlocks();
    shuffleBlocks();

    currentBlock = blocks[0];
    nextBlock = GetRandomBlock();

}
void Game::LockBlock() {
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    currentBlock.id = currentPlayer;
    if (BlockFits() && NearBlockCheck()) {
        for (Position item : tiles) {
            grid.grid[item.row][item.column] = currentBlock.id;
        }
        if (currentPlayer < numberOfPlayers) {
            currentPlayer += 1;
        } else {
            currentPlayer = 1;
        }
        currentBlock = nextBlock;
        nextBlock = GetRandomBlock();
    }
}

I have to delete the GetRandomBlock function to replace it by the next block in the vector, I have another function that prints the next block and I have to print the 5 next blocks but I don't know how I can always have the next block in the vector.
In the second function I tried with i++ and getting the next block with currentBlock = blocks[i] but it doesn't seems to work.

#

Actually it always take a random block after I play the first one

humble pike
#

oh nvm

#

its the constructor lmao

reef crow
#

The Game::Game happens just one time I think because it doesn't get all the blocks every time

#

yep

humble pike
#

can you send the code where you tried to use i++

reef crow
#
void Game::LockBlock() {
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    currentBlock.id = currentPlayer;

    int i = 0;

    if (BlockFits() && NearBlockCheck()) {
        for (Position item : tiles) {
            grid.grid[item.row][item.column] = currentBlock.id;
        }
        if (currentPlayer < numberOfPlayers) {
            currentPlayer += 1;
        } else {
            currentPlayer = 1;
        }

        i++;

        currentBlock = nextBlock;
        nextBlock = blocks[i];
    }
}
#

I might have lost msyelf on this one

humble pike
#

in this code the declaration of i is in the function, so every time this function is called a new 'i' is created

reef crow
#

-_-

humble pike
#

so you will always get blocks[1] into nextblock

reef crow
#

you're right

#

should I do a variable in all my file for it ?

humble pike
#

you can create an int in your class named nextBlockIndex or some, and use that instead of i

reef crow
#

okay that sounds better

humble pike
#

what do you do when all the blocks have been used btw?

#

do you restart at 0, or do you shffle again and restart at 0

reef crow
#

Normally I have to implement some bonuses in my game and one is : you have to choose one of the next 5 blocks to replace the one you actually have, you continue at the new block index and previous blocks stays in the list. once all blocks are used, you come back to the beginning of the list and use the blocks you left before your new block

#

But I won't have time for this I think

#
int currentPlayer = 1;
int nextBlockIndex;

Game::Game()
{
    grid = Grid();
    blocks = GetAllBlocks();
    shuffleBlocks();

    for(int i = 0; i < numberOfPlayers; i++)
    {
        blocks.insert(blocks.begin(), oneBlock());
    }

    currentBlock = blocks[0];
    nextBlock = getNextBlock();

}

Block Game::getNextBlock() //A remplacer par GetNextBlock()
{

    nextBlockIndex ++;
    if(blocks.empty())
    {
        blocks = GetAllBlocks();
    }
}
void Game::LockBlock() {
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    currentBlock.id = currentPlayer;
    if (BlockFits() && NearBlockCheck()) {
        for (Position item : tiles) {
            grid.grid[item.row][item.column] = currentBlock.id;
        }
        if (currentPlayer < numberOfPlayers) {
            currentPlayer += 1;
        } else {
            currentPlayer = 1;
        }
        currentBlock = nextBlock;
        nextBlock = getNextBlock();
    }
}
#

I did that but it doesn't work

humble pike
#

you need to initialise NextBlockIndex

reef crow
#

I tried with 0 or 1 but my window close instantly when the game launches

humble pike
#

you don't return anything from getNextBlock btw

#

return blocks[nextBlockIndex];

#

and int nextBlockIndex = 0;

#

at the top

humble pike
#

If it still doesn't work you can try to debug the constructor

reef crow
#

yep it works !

reef crow
#

My brain is burning

#

I have to do the same thing that we did to avoid blocks touching each others while not the same id, but now I have to force the player to touch a block with the same id, conditions are the same but I can't see how I can do that.
It can be place on blocks or touch blocks with id 0, can't touch blocks with different id, but have to touch a block with same id...

#

I think with XOR conditions

humble pike
#

try this

bool Game::isInGrid(int row, int column) {
  return (row < 0 || row >= grid.numRows || column < 0 ||
          column >= grid.numCols);
}

bool Game::isTouchingSameColor(int row, int column) {
  return grid.grid[row][column] == currentBlock.id;
}

bool Game::isTouchingDifferentColor(int row, int column) {
  return (grid.grid[row][column] != currentBlock.id &&
          grid.grid[row][column] != 0);
}

bool Game::doChecks(int row, int column, bool &isTouching) {
  if (isInGrid(row, column)) {
    if (isTouchingDifferentColor(row, column)) {
      return false;
    }
    if (isTouchingSameColor(row, column)) {
      isTouching = true;
    }
  }
  return true;
}

bool Game::NearBlockCheck() {
  std::vector<Position> tiles = currentBlock.GetCellPositions();
  bool isTouching = false;
  for (Position item : tiles) {
    if (doChecks(item.row + 1, item.column, isTouching) == false)
      return false;
    if (doChecks(item.row - 1, item.column, isTouching) == false)
      return false;
    if (doChecks(item.row, item.column + 1, isTouching) == false)
      return false;
    if (doChecks(item.row, item.column - 1, isTouching) == false)
      return false;
  }

  return isTouching;
}
#

there might be errors correct them, you can change names etc

reef crow
#

I'll try, the only problem is that now I have turns, because first turns i have to let what we implemented before with the NearBlockCheck, but since the 2nd round starts I have to implement this new rule

#

can't I just add something in the NearBlockCheck function to do this ?
Like this :

bool Game::NearBlockCheck()
{
    std::vector<Position> tiles = currentBlock.GetCellPositions();
    for(Position item: tiles)
    {
        if(turn == 1)
        {
            if(item.row + 1 < grid.numRows && grid.grid[item.row + 1][item.column] != currentBlock.id && grid.grid[item.row + 1][item.column] > 0)
            {
                return false;
            } else if(item.row - 1 >= 0 && grid.grid[item.row - 1][item.column] != currentBlock.id && grid.grid[item.row - 1][item.column] > 0) {
                return false;
            } else if(item.column + 1 <  grid.numCols && grid.grid[item.row][item.column + 1] != currentBlock.id && grid.grid[item.row][item.column + 1] > 0) {
                return false;
            } else if(item.column - 1 >= 0 && grid.grid[item.row][item.column - 1] != currentBlock.id && grid.grid[item.row][item.column - 1] > 0) {
                return false;
            }
        } else {

        }

    }
    return true;
}

So in the else, it's after the turn 1, so when I have to do what I said before

humble pike
#

If (turn == 1) isTouching = true;

reef crow
#

Okay I'll try

#

arf it doesn't work, now every blocks can be placed next to each others

humble pike
#

try to debug it, i don't have much time rn

#
bool Game::isInGrid(int row, int column) {
  return (row >= 0 && row < grid.numRows && column >= 0 &&
          column < grid.numCols);
}
#

actually this was wrong

#

try with that

reef crow
#

I'll try

humble pike
#

if it still doesn't work and you're not comfortable with this code try to implement the method you were suggesting

reef crow
#

Yep i'll check to do that

#

clion tells me that the isInGrid fct can be made const, what will it change ?

regal monolith
reef crow
#

Damn that works so well

#

I'll analyze the code to really understand how you did

reef crow
#

i've never seen this nodiscard thing

reef crow
#

Damn I have one last thing to do, I have to calculate the biggest square/player and when the game is finished I have to print which player have the biggest square

regal monolith
reef crow
#

I should check all the grid, for that i'll do a for loop that'll check it for each player, if I have a block on the grid with the id of the player I'm checking I'll start checking for a square and this for each block. I'll have to compare all squares of the same player and take the largest, after that we'll switch to another player and compare both largest square.
The player with the largest square will be in a variable and we'll return this player to display it on the screen.

#

I have to finish it tonight

#

So I'm pretty nervous about that

#

I would see something like that :

for(each player)
for(board size)
if(board cell == player)
check square (I don't know how to actually do this)
stock it in a var to compare it with other players square but I have to associate it with the player that I'm checking for
after all of this done compare the squares between each players and take the largest which is associated to a player
return player

#

@regal monolith

reef crow
#

or anyone else

plush walrusBOT
#

Did you really just try to ping 31,195 people?

regal monolith
reef crow
#

I would really like to have a hint bcs rn... I'm mental boom

regal monolith
#

Can you explain to me what makes one square "bigger" than another, cause if you can then that helps with writing code

#

And can each player have multiple, separate squares, or can every player only have one?

reef crow
#

We'll check if every single blocks together makes a square, and bigger is like 4x4 bigger than 3x3, even if there is blocks around I only have to check squares

#

We'll only take the biggest square/player

reef crow
#

?

regal monolith
#

Answer that one

reef crow
#

Oh okay, they can have multiple squares but we have to compare to only take the biggest one

#

That's why we have to check square everytime we see a block of a player

regal monolith
#

Alright, so what if someone has this 2x3 shape:
XXX
XXX
Does this count as a 2x2 square, or does it not count as a square because the thing as a whole is a rectangle, so not a square?

reef crow
#

2x2

regal monolith
#

Programming is not about writing code, but about thinking of all the edge cases

reef crow
#

Yep, I thought abt that all the day

regal monolith
#

Okay, and this?
XX
XXX

reef crow
#

still 2x2

#

however what it creates, if there is a square in it, we have to take it

regal monolith
#

Right, so you understand that we'll at the very least need a loop to count how wide it is, right?

#

Here is the last edge case, what size is this?
XXX
XX
X

reef crow
#

yep

#

still 2x2

regal monolith
#

Yup

reef crow
#

it can be something like that :

XXXXX
X XXX
XXXXX

It will be 3x3

regal monolith
#

So the naïve and wrong approach would be this:

  1. Starting in the top-left, go right to count how wide it is.
  2. Go back to the top-left, and go down to count how tall it is.
  3. Take the minimum of the two, so with this example:
    XXXX
    XX
    X
    the minimum would be between a width of 4 and a height of 3, so the answer is it's a 3x3 square, which is wrong.
    Can you follow this?
reef crow
#

Yep because if there is a hole in the middle it won't work

regal monolith
#

Yup, so what we've learnt with this example is that you simply can't figure out what the largest square in it is, if you only check the top-left. Got that?

#

My naïve algorithm only checked the top-left.

reef crow
#

So you'll have to check every block of the diagonal I think ?

regal monolith
#

Can you elaborate why? It might be right, I just need more

reef crow
#

because it will check every block of the square I don't know how to explain it but top-left will be :
XXX
X
X

and diagonal will be :

XXX
XXX
XXX

#

bcs we check to the right and the bottom of the block we want to scan on

regal monolith
#

Okay, but if you only check the diagonals, see how in this case it would think the largest square is 2, rather than 3?
XX
XX
XX
XX
XXX
XXX
XXX
XX
XX
XX
XX

reef crow
#

but if we check every block of the grid, it will find the 3x3 square after the verification

regal monolith
#

Yes, nice! "If we check every block of the grid" is the magic phrase I was trying to pull out of you :)

reef crow
#

oh xD I had it since the beginning, I was already thinking about that, it's pretty normal for a verification

regal monolith
#

Yeah, but I didn't know you knew, so ;p

reef crow
regal monolith
#

But just checking the diagonals only still wouldn't be enough, cause what if you have this:
XX
XXX
XXX
If you say you loop over every tile in the grid and then check the diagonals growing down-right, then the tile at (0,0) would check tiles (1,1) and (2,2), and would conclude it's a 3x3 square.

reef crow
#

yep you alo have to check the corners and go to right and bottom as you said before

regal monolith
#

I'm just going to say it more simply: you need to check every single tile, not just the diagonals and the corners.

reef crow
#

yep said like that it's pretty simple

regal monolith
#

So basically, if you have this input:
XX
XXX
XXX
You start with growing a square out of (0,0):
%X
XXX
XXX
This worked, so the square's size is at least 1x1.

And then you have a loop that checks the column to the right of it, and another, separate loop that checks the row under it:
%%
%%X
XXX
This worked, so it's at least 2x2.

And it then tries to check the next column and row:
%%!
%%%
%%%
This fails at the !, as it isn't the player's tile, and so you return that the largest square you could grow from position (0,0) is 2x2.

#

Got an idea of how to program it now? :)

reef crow
#

So just for the square check it's a loop in a loop in a loop

#

maybe more

regal monolith
#

The important part is that you start from some point, in this case (0,0), but you do this for every other tile as well, and then keep adding another tile and row until you encounter a tile that doesn't belong to the player, or until you reach the edge of the map.

reef crow
#

for me it would be :
for(i < board size)
for(j < board size)
for(k < board size && l < board size)
if(grid case != player.id)
return false;

Am I right ?

#

i think not xD

regal monolith
#

What does returning false here mean? Don't you want to return an int?

reef crow
#

oh fck

#

already forgot that

regal monolith
#

You know how to make a function, yes?

reef crow
#

yep

#

I have to implement the comparison thing in the function too

regal monolith
#

Alright, so do something like this:

def get_largest_player_square(player_id):
  largest_size = 0
  for y in height:
    for x in width:
      if grid[y][x] == player_id:
        size = grow_tile(x, y, player_id)
        if size > largest_size:
          largest_size = size
  return largest_size

def grow_tile(x, y, player_id):
  # You can do this yourself with my earlier description ;)
regal monolith
reef crow
#

But how can I create in the function something to say : that square blongs to this player

regal monolith
#

You're right, I edited it so it's passed in

#

You'd call get_largest_player_square() for every player ID

reef crow
#

so : for(numberOfPlayers)
and you call get_largest_player_square

regal monolith
#

Assuming their IDs go like 0, 1, 2, ..., then yes

#

I am presuming you have a 2D grid that has a player ID for every value, where -1 means no player or something

reef crow
#

okay, and my final question (maybe not the last we'll see 😅), in the grow_tile, how can you associate the player to its square to compare it to others square.

#

0 are empty cases, 1 are player 1's cases, etc.

regal monolith
#

So you're saying that no player can have ID 0, or in other words that player IDs start at 1?

reef crow
#

yep

#

I know that's not good but I thought abt that to late

regal monolith
#

No, that's absolutely fine! Just checking :)

reef crow
#

oh okay !

regal monolith
#

So the goal of get_largest_player_square() is that you give it a player ID, and it returns you what that player ID's largest square is. So if you call this in a loop, you can track which player had the largest one and print that.

#

get_largest_player_square() doesn't loop over other players

reef crow
#

For the player I only have currentPlayer where I can do currentPlayer.id
Should I create another variable or is it correct to use this one ?

regal monolith
#

get_largest_square_of_player() would be a better name, I'd use that

regal monolith
#

Or are you referring to making player_id a function argument? Cause that's what my Python code recommends

reef crow
#

I don't know... If I do it it would be in the second function bcs it's the one in which i'll do a loop changing the player everytime to check squares

regal monolith
#

Can you give the name of the "second" function, idk what you're referring to

reef crow
regal monolith
#

player_id is passed as an argument in both functions, because that's how they work

#

makes sense?

reef crow
#

yep, but like for the player id I'm using currentPlayer, which is the player actually playing

regal monolith
#

If I do it it would be in the second function bcs it's the one in which i'll do a loop changing the player everytime to check squares
The point is that neither get_largest_player_square() nor grow_tile() loop over the players! The idea is that you do this instead:

largest_player_size = 0
for player_id in player_ids:
  size = get_largest_player_square(player_id)
  if size > largest_player_size:
    largest_player_size = size
print(f"Player {player_id} had the largest square size of {largest_player_size}")
#

You want to print the largest square size, along with which player id's square that was, right? That's what this does

reef crow
#

Yes totally but what I want to say is that we have to increment the id we're checking everytime, so player_id would be a new variable that we increment after every check of the full board

#

i'll try something

#

I maybe have a cool idea

regal monolith
reef crow
#

yep that's right

#

I didn't look at the code correctly

regal monolith
#

Yeah, that's what I meant, I just wasn't sure whether it'd be possible for a player to drop out of the game so wrote for player_id in player_ids: to be sure

reef crow
#

i'm tired

regal monolith
#

no worries

reef crow
#

Do you know why ?

regal monolith
#

The c makes sure it has nice C colors

#

The ` key is called the backtick and is to the left of your 1 key

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();
                if(size > largestSize) {
                    largestSize = size;
                }
            }
        }
        return largestSize;
    }
}

int Game::getSquare(){
    int largestPlayerSquare = 0;
    int playerId = 1;
    while(playerId <= numberOfPlayers) {
        int size = getLargestSquareOfPlayer(playerId);
        if(size > largestPlayerSquare) {
            largestPlayerSquare = size;
        }
        playerId += 1;
    }
    return  largestPlayerSquare;
}
regal monolith
#

Right, so it's complaining about getLargestSquareOfPlayer()

reef crow
#

I have orange warning all over these, I put the functions in public bcs I have to access it on my main file

regal monolith
#

Take a veeery close look at it

reef crow
#

that's strange bcs If I put my fct in private they don't have all of these warnings

#

only largestPlayerSquare and playerId

#

in the getSquare one

regal monolith
#

That's not right though, it should be public if you want to call it outside of the class

#

Look closely at getLargestSquareOfPlayer()'s return statement, something's wrong with it

reef crow
#

yep but when I put them in public them have a lot of warnings

regal monolith
#

Well that's what you want because the warnings are correct, the function's code isn't correct

reef crow
#

the return isn't at the good line I think, it's in a for loop

regal monolith
#

Yes!

reef crow
#

wtf I have an unreachable code on the largestSize = size line in the getLargestSquareOfPlayer

regal monolith
#

hmm, that's strange, cause the code you sent is fine

#

can you send a screenshot of the error

reef crow
#

same for largestPlayerSquare = size in the other function

#

Just so you can see all my other warnings

regal monolith
#

So the first thing you want to do is add the methods to the class its header, cause I am pretty sure that's what might be causing the lines under the method names

#

So add them as declarations

reef crow
#

They are already in the header file

regal monolith
#

well then hover over this orange line to get info

reef crow
#

it doesn't tell me anything on this one, just a clang-tidy

regal monolith
#

can you screenshot the clang-tidy? I'm curious

reef crow
#

oh

#

now I have something, Idk why did it change

regal monolith
#

ahhh I see the issue now

#

nevermind me, the original code you sent is a bit jank

reef crow
#

ah

regal monolith
#

so tell me, what function are you calling inside getSquare()?

reef crow
#

getLargestSquareOfPlayer

#

for the int size

regal monolith
#

And tell me, what function are you calling inside getLargestSquareOfPlayer()?

reef crow
#

oh

regal monolith
#

hehe

reef crow
#

nice recursive loop xD

regal monolith
#

don't just ignore the clang-tidy, it could've saved you a lot of time

reef crow
#

yep I always check warnings

#

How could I break this recursive loop ? I know that it's in the getLargestSquareOfPlayer(), but what should I put to replace getSquare() ?

regal monolith
reef crow
#

oooooh okay

regal monolith
reef crow
#

Basically I'll need playerId

regal monolith
#

And an x and y to grow down-right from, hence def grow_tile(x, y, player_id):

#

Maybe def grow_tile(top_left_x, top_left_y, player_id):

reef crow
#

yep, I can just use i and j that I defined before for the loop

regal monolith
#

yup! exactly

#

I do recommend replacing the names i and j with x and y, since I have absolutely no clue without carefully inspecting your code whether i means x or y

reef crow
#

i is x and j is y

regal monolith
#

but I have a really hard time remembering that

reef crow
#

x and y are more logical like in maths but idk why they always learn this to us in french university or schools when we code

regal monolith
#

it's natural to start a single loop with i as it just means index, but when you add another dimension it makes more sense to go with x and y, and if you add a third dimension of course x, y, z

reef crow
#

yep totally

regal monolith
#

so minecraft of course lists block positions as x, y, z instead of i, j, k in the F3 debug menu

#

I recommend applying the same logic in your case, renaming i and j to x and y

reef crow
#

yep that's logic af, but I never think about that bcs they always tell us to use i j k l m etc for loops

regal monolith
#

well I am telling you your teacher sucks xD

#

you don't have to change it if you don't want to, but people are more likely to ask why you used i and j than asking why you used x and y

#

"good" code is all about the details

reef crow
#

If we think with x and y, can you explain me what is wrong with it plz ?

#

I usually initialize i and j in the for loop but they are already initialize that's why Idk how I can do that

regal monolith
# reef crow

wait, but the getSquare() you wrote here already seemed correct?

#

or did you move the code inside it to a different fn?

reef crow
#

I just changed the name don't worry

regal monolith
#

ah ok, good

reef crow
#

this getSquare is the grow_tile you were speaking

regal monolith
#

so you initialize the squaresize to 0, which is good, as you'll be returning that at the end of the function

#

after all, you want this function to return an int, right? so do that

regal monolith
reef crow
#

but what can I return in both loops ?

#

moreover my i and j in my both loops are in grey which means they are not used but why

#

I can do i = i and it seems to work but it's pretty dumb xD

regal monolith
#

So the numbers at the top indicate how many rows and column out from the top-left you're checking

#

The green crosses are done in a single loop, and after that the orange crosses are done in another loop

#

If the green or orange (column or row, respectively) loop tells you there is a tile in it which doesn't belong to the player, or if the row/column would lie outside the map, you immediately return the squareSize in your function

#

So you do the green and orange loops in an infinite while(true) to keep growing the square

#

At the end of every while-loop you increment squareSize by 1. squareSize is that blue number I wrote at the top that indicates how many columns and rows to go out from the top-left

reef crow
#

but both for loops are empty now, should I return something in both ?

regal monolith
#

If the green or orange (column or row, respectively) loop tells you there is a tile in it which doesn't belong to the player, or if the row/column would lie outside the map, you immediately return the squareSize in your function

reef crow
#

i don't really understand how it is possible bcs both for loops are independant

regal monolith
#

You can have multiple places where you return from a function inside of a single function!

#

It is often very useful to not only return at the end

#

though you can also probably figure out a way to only return at the end if you really wanted to

reef crow
#
int Game::getSquare(int i, int j) {
    int squareSize = 0;
    while(true){
        for(i; i == playerId; i++) {
            if(grid.[i] != playerId) {
                return squareSize;
            } else {
                squareSize ++;
            }
        }
        for(j; j == playerId; j++) {
            if(grid.[j] != playerId) {
                return squareSize;
            } else {
                squareSize ++;
            }
        }
        squareSize ++;
    }
}

smth like this ?

regal monolith
reef crow
#

my brain's burning

regal monolith
#

good

#

now, yes or no?

reef crow
#

I'm between both, like a bit of yes and a bit of no

#

I can see it but not fully understand how I can do it

regal monolith
#

that's understandable, when you're beginning with programming it's normal to struggle with translating loop ideas into actual code

#

this'd be infinitely easier for me to explain if you renamed stuff to use x and y, could you pretty please do that?

reef crow
#
int Game::getSquare(int x, int y) {
    int squareSize = 0;
    while(true){
        for(x; x == playerId; x++) {
            if(grid.[x] != playerId) {
                return squareSize;
            } else {
                squareSize ++;
            }
        }
        for(y; y == playerId; y++) {
            if(grid.[y] != playerId) {
                return squareSize;
            } else {
                squareSize ++;
            }
        }
        squareSize ++;
    }
}
#

tadaaa

regal monolith
#

ty! ❤️

#

So what (x, y) coordinate is this green x at, assuming the top-left gray dot is at (x=0, y=0)?

reef crow
#

yep I understand that

regal monolith
#

it was a question ;p

reef crow
#

oooh xD

#

x=2 y=0

#

my blocks are literally coded like that xD

regal monolith
#

well, with for(x; x == playerId; x++) { the x; doesn't do anything

#

you can just remove that x

#

same for the y loop

reef crow
#

but if I delete it :

regal monolith
#

I'd personally turn it into a while-loop instead of doing for(; x == playerId; x++) though

#

nah, you would keep the ;

reef crow
#

Okay I just learned a new functionnality of c++

regal monolith
#

an expression that ends with a ; (semicolon) is called a "statement" btw

reef crow
#

I didn't that you could write nothing and still put a ;

regal monolith
#

so a single line of code like this:

int foo()
{
  int a = 1;
  int b = 2;
  a += b; return a;
}

would return 3, and we'd say that the line a += b; return a; contains 2 statements

#

and yeah, empty statements are always valid:

int foo()
{
  int a = 1;
  ;
  int b = 2;
  ;
  a += b; return a;
  ;
  ;
}
#
int foo()
{
  int a = 1;
  ;;;;;
  int b = 2;
  a += b; return a;
}

this contains a bunch of empty statements, you see

reef crow
#

okay I understand now

regal monolith
#

adding them is like adding comments, they don't compile to anything

#

but for-loops require two semicolons in their condition

reef crow
#

yep I didn't think it was a must have

regal monolith
#

so 1 character shorter than while(1) is actually for(;;), which you'll recognize quite often now that I told you to look out for it

#

using it is a sign of understanding

#

but it's also not very beginner friendly ;p

reef crow
#

totally

regal monolith
#

and your x == playerId condition there doesn't make much sense, as the x position has nothing to do with player IDs

reef crow
#

is it better like this ?

#
if(grid.grid[i][j] != playerId || grid.grid[i][j] > grid.numRows) {
regal monolith
#

close, but you want that second check inside of the for-loop's condition instead

regal monolith
#

and you'll want to flip the > to be < then of course

reef crow
#

Yep I just saw that

#
for(;grid.grid[i][j] < grid.numRows; i++) {
            if(grid.grid[i][j] != playerId) {
                return squareSize;
            } else {
                squareSize ++;
            }
        }
regal monolith
#

grid.grid[i][j] < grid.numRows is equivalent to id < grid.numRows, which doesn't make sense ;p

regal monolith
# reef crow ```cpp int Game::getSquare(int x, int y) { int squareSize = 0; while(tru...

Another hint: in the code you sent here your function gets the top-left x and top-left y of the square and you call it x and y, and then inside of those for-loops you edit those x and y.

The problem with that is that you'll need to restore the x and y values to the top-left corner with a new squareSize offset every while-loop, which is now impossible as you edited the function's x and y arguments.

So rename them to int Game::getSquare(int topLeftX, int topLeftY) {, and then do for(int x = topLeftX + squareSize; x < grid.numCols; x++) {

reef crow
#

done

#

so for the if it would be this ?

if(grid.grid[x][topLeftY] != playerId) {
regal monolith
#

I am not sure whether it should be [x][topLeftY] or [topLeftY][x] since it depends on how you initialized your grid

#

and instead of topLeftY you'll want to put some other value there, look at my pics closely for inspiration

reef crow
#
void Grid::initialize()
{
    for(int row = 0; row < numRows; row++)
    {
        for(int column = 0; column < numCols; column++)
        {
            grid[row][column] = 0;
        }
    }
}
#

I initalitzed it like that

regal monolith
#

okay, so [y][x], got it

reef crow
#

fck

#

In every project I reverse both...

regal monolith
#

same lul

reef crow
#

why x can't be rows 😭

reef crow
#

why ? Isn't topLeftY in this not good ?

for(int x = topLeftX + squareSize; x < grid.numCols; x++) {
            if(grid.grid[x][topLeftY] != playerId) {
                return squareSize;
            } else {
                squareSize ++;
            }
        }
regal monolith
#

try to visualize in your head or on a piece of paper if you want what that loop is doing now

#

also pls rename that i to x again ty

reef crow
#

ups you didn't see anything

#

it's like that everywhere in my code and I don't have much time left to rename it now

#

I prefer finishing these functions first

regal monolith
#

try to visualize in your head or on a piece of paper if you want what that loop is doing now

#

take a toilet break and grab a cup of coffee/tea or whatever and give yourself the time to mull it over

reef crow
#

I don't have the time 😅

#

I need to finish this before 00:00 and have to write a technical documentation for it

regal monolith
#

school experience be like

reef crow
#

sadly yes

#

I'm on this project since 3weeks and worked almost everyday but struggling a lot

regal monolith
#

but for real, you can definitely spare a few minutes to look at what those literal 6 lines of code are actually saying, cause it's kind of dumb haha

#

try to execute the code line by line in your head including looping back around, pretending like you are a computer

regal monolith
#

I'd make sure you get the x loop right first, and then copy-paste and edit that into the green crosses y loop last

reef crow
#

so the problem is in the if

regal monolith
#

yes

#

the for-loop line is perfect

reef crow
#

I have to exchange the x and topLeftY values

regal monolith
#

topLeftY in red

#

think about whether you really meant to use that here

#

and if so, maybe in a different way

#

the goal is to produce the orange coordinates

reef crow
#

so it's squareSize instead of topLeftY

regal monolith
#

and my bad, x < grid.numCols isn't correct either LUL

reef crow
#

<=

regal monolith
#

no matter how wide the grid is (numCols), you want to limit how many orange checks you do here with the squareSize

#

whereas rn x < grid.numCols would try to make orange crosses in a loop until the right edge of the map is reached

reef crow
#

but we want to stop it

regal monolith
#

we do, so we do need the check in order to make sure we don't cross the edge, that's true

#

but you'll need to use the AND statement && in order to do a second check in that for-loop's condition

reef crow
#

so the < was correct ?

regal monolith
#

yes, x < grid.numCols && ??, where you need to put something instead of ??

#

the first condition checks whether it's in bounds, the second whether you've slid off the bottom edge of the new square size

reef crow
#

so it would be something with a comparison with x and the squareSize

regal monolith
#

so if the grid is 3x3, and you're checking whether a squareSize of 1 is valid, you want this

#

instead of only your current condition which is this

reef crow
#

x < squareSize + 2 ? Like you want to only check one time for the squareSize + 1

regal monolith
#

I realized that the initialization of x was also wrong, and I misled you by saying that only the condition was wrong, so have the final, correct version as my apology
for(int x = topLeftX; x < grid.numCols && x < topLeftX + squareSize; x++) {

#

the x < topLeftX + squareSize is because if the squareSize is 2 you only have 2 orange checks

reef crow
#

oh okay

#

So I'll do the same with y

#

arf

#

we need to check 1 more

regal monolith
#

note that the number of green is always 1 higher

#

yup

reef crow
#

so + squareSize + 1

regal monolith
#

and make sure you remember to correctly change everything if you are going to literally copy-paste it

reef crow
#

Don't worry I wrote it correctly

regal monolith
#

can you send me your current code?

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

so it's topLeftY first and x second

regal monolith
#

yes

#

but not topLeftY ;p

#

you have to add smth to it

#

if the red top-left tile is at Y=10, what Y value is the orange at, and how can you calculate it?

reef crow
#

the orange will also be at 12

#

wait no

regal monolith
#

remember that Y is vertical aka up and down

#

yes, 12

#

and how do you get to 12?

reef crow
#
  • squareSize
regal monolith
#

yup

#

make sure to apply the same kind of fix for the y loop

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

grid.grid[y][topLeftY + squareSize] is nearly correct but not quite

#

remember, [y][x]

#

and the same for your grid.grid[topLeftX + squareSize][x]

reef crow
#

i'm lost

regal monolith
#

so you know how if you want to access (y=3, x=5) in your grid you have to do grid.grid[3][5] and not grid.grid[5][3], because your grid was initialized as grid[y][x]?

regal monolith
reef crow
#

So I have to reverse them ?

regal monolith
#

if not that's okay too, I just need to know

reef crow
#

oh yes

#

yeah I got this

regal monolith
#

so you get it's [y][x]?

reef crow
#

yep

regal monolith
#

okay, well you're doing grid.grid[topLeftX + squareSize][x]

#

mighty strange to see the letter X in the first brackets, eh?

reef crow
#

so topLeftY + squareSize

regal monolith
#

yup

#

and you made the same mistake in the other loop, so check that too

#

just remember, [y][x]

reef crow
#

yep that's corrected

#

I misunderstood bcs I asked you If I had to correct like it was and you told me yes

regal monolith
#

where? sorry if I missed it

#

can you reply to it

reef crow
#

but the second msg you corrected so I was lost xD

regal monolith
#

yeah my bad, when I said "but not topLeftY ;p - you have to add smth to it" I meant that it should still be topLeftY, but that it alone isn't enough and that you needed to also add squareSize to it :)

reef crow
#

Yep, now I understand but on the moment I was lost

#

So the function should be finished no ?

regal monolith
#

can you send the current version?

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

Bcs I have some problems on the other functions we wrote 🥲

regal monolith
#

thanks. so it's almost correct, but look more closely at how many places you're doing squareSize++;, and think about which of those is/are wrong

reef crow
#

the wrong one for me is the last one, bcs it will increase the squareSize a 2nd time after the for-loops

regal monolith
#

try to keep in mind that the first for-loop is adding the orange Xes here, and the second for-loop is adding the green Xes here, and that the squareSize in blue at the top is a constant 2 for this entire screenshot

reef crow
#

yep I lost myself, the both elses are bad, because it increases squareSize each time it's correct

regal monolith
#

🎉

#

and now the final version 🥁

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

all you need to do to shut up the compiler about the function never returning a value at the end of it is adding abort(). This tells the compiler "this is impossible"

#

assert(false) also works

reef crow
#

I'm really sorry about that, but others functions have a problem 😭 , especially the playerWithLargestSquare

regal monolith
#

no worries

reef crow
#

But you don't know how thankful I am actually

regal monolith
#

I was also helped by others, just pay it forward by helping others some day

#

show me the next issue

reef crow
#

I always help people when I can

regal monolith
#

two returns after one another makes the second one unreachable

#

no bueno

reef crow
#

And both of the orange warnings are : shadows of variable in the global namespace

regal monolith
#

use the search tool to see where those variables have already been defined

#

the magnifying glass button

reef crow
#

nevermind I'm just dumb, forgot that if I already initialized it in my global file I don't have to put int before

#

so the only thing left to do is to return both of the variables

regal monolith
#

so you are keeping these two variables, right?

reef crow
#

yep these are corrected

regal monolith
#

what had to be changed with them?

reef crow
#

delete the int x)

regal monolith
#

oh I see what you said, nvm me I'm dumb

#

okay fair enough, but why did you have them as globals as well?

#

I recommend using globals as little as possible

reef crow
#

I need them in another file

#

and forgot to do getters

regal monolith
#

okay well keep in mind next time that globals are bad

#

cause they allow cross-contamination, making debugging way harder if a variable can be edited by loads of places

reef crow
#

ah yeah I understand

#

For mine it should be ok but I won't forget that later

regal monolith
#

so since this is C++ and not C, you can just return a std::pair<>, instead of creating a struct specifically for those two variables and returning such a struct

reef crow
#

to return both ?

regal monolith
#

yes

#

I presume you want that?

reef crow
#

Yeah I just want these 2 variables to print them on my screen in my main file

regal monolith
#

well you might as well just print them right here then, right?

#

makes things easiest

reef crow
#

Nope bcs it's with a drawText that I have in my main file

#

I already have the players and turns etc.

regal monolith
#

ahh ok, well then look at this example:

#include <iostream>
#include <tuple>
 
int main()
{
    std::pair<int, double> p2(42, 3.1415);
    
    std::cout << p2.first << std::endl; // 42
    std::cout << p2.second << std::endl; // 3.1415
}
#

pairs are basically shorthand for defining a struct containing exactly two values

reef crow
#

I'll try that

regal monolith
#

(and when I say struct, remember that a struct is the same as a class)

reef crow
#

but there isn't a way to just have 2 returns or something to do the same ? Or is it only pair ?

regal monolith
#

if you have a return statement the function is immediately exited, there is no way to do a return directly after another return unfortunately

#

hence std::pair

reef crow
#

okay okay

regal monolith
#

(these are good things to mention in technical documentation, if you want to explain your thoughtprocess)

reef crow
#

how should I use that in another file ? p2. first doesn't work

regal monolith
#

send code so I can know what you did

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

    std::pair<int, double> results{playerId, largestPlayerSquare};
}

in my first file

char winText[60];
                        sprintf(winText, "Le joueur %d ", playerId, "a gagné avec un carré de %d", largestPlayerSquare, "x", largestPlayerSquare);

in my second file

regal monolith
#

right, and then you want to return that

#
#include <iostream>
#include <tuple>

std::pair<int, double> foo()
{
    return std::pair(42, 3.1415);
}

int main()
{
    std::pair<int, double> p = foo();
    
    std::cout << p.first << std::endl; // 42
    std::cout << p.second << std::endl; // 3.1415
}
reef crow
#

because results.first doesn't work

regal monolith
#

Make sure you have #include <tuple> in all .cpp files you're doing stuff with tuples

regal monolith
reef crow
regal monolith
#

well yeah because it doesn't know results

reef crow
regal monolith
reef crow
#

oh

#

but std::pair is a function, so I can't do it in my function which I want to return values

regal monolith
#

std::pair() returns a pair, which is a value

regal monolith
reef crow
#

the thing I don't understand is that you create a function that you call foo in which you put the values, but if I don't return the values in my playerWithLargestSquare how can I get'em

regal monolith
#

This:

std::pair<int, double> foo()
{
    return std::pair(42, 3.1415);
}

is basically what you would've tried like so, but doesn't work:

int, double foo()
{
    return 42;
    return 3.1415;
}
#

foo() is a function returning two values, but they are packed inside of a std::pair in the actual working version

reef crow
#

yes but you created a pair function

regal monolith
#

no, it's from the standard library

reef crow
#

and I can't create a function inside a function

regal monolith
#

it's a container, just like std::vector

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

    std::pair<int, double> results{playerId, largestPlayerSquare};
}

so in here I can create what you did before ?

regal monolith
#

yes

#

first off you need to change this function's return type

regal monolith
#

compare your code with the example

reef crow
#

so it would be :
std::pair<int, double> Game::playerWithLargestSquare()

regal monolith
#

yes! though in your case you will want to change that double to int as well I think

reef crow
#

yep

regal monolith
#

since you're trying to return two ints, right? cool

reef crow
#

I'll try my code now

#

how ?

regal monolith
#

the first screenshot is saying you are making a variable called results and aren't doing anything with it in the function

#

which is correct, refer to my example where I just outright return it without ever making a results variable in the foo() function

reef crow
#

okay no error left on this but when I try to launch the game i have an error

regal monolith
#

ooh, okay, progress!

reef crow
#

but error is not something in red or else in my code

regal monolith
#

run it with the debugger button and it should tell you which line it had that issue on, cause by pressing the run button as you did here you get 0 debug info

reef crow
#

even with debugger

#

0 info

regal monolith
#

can you screenshot more, that thing is super cropped

#

maybe the entire screen even

reef crow
#

[1/1] Linking CXX executable C++Project.exe
FAILED: C++Project.exe
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\G__~1.EXE -g CMakeFiles/C++Project.dir/main.cpp.obj CMakeFiles/C++Project.dir/position.cpp.obj CMakeFiles/C++Project.dir/grid.cpp.obj CMakeFiles/C++Project.dir/colors.cpp.obj CMakeFiles/C++Project.dir/blocks.cpp.obj CMakeFiles/C++Project.dir/block.cpp.obj CMakeFiles/C++Project.dir/game.cpp.obj -o C++Project.exe -Wl,--out-implib,libC++Project.dll.a -Wl,--major-image-version,0,--minor-image-version,0 _deps/raylib-build/raylib/libraylib.a -lopengl32 -lglu32 -lwinmm _deps/raylib-build/raylib/external/glfw/src/libglfw3.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2023.2.2\bin\mingw\bin/ld.exe: CMakeFiles/C++Project.dir/game.cpp.obj:game.cpp:(.rdata$.refptr.largestPlayerSquare[.refptr.largestPlayerSquare]+0x0): undefined reference to largestPlayerSquare' C:\Program Files\JetBrains\CLion 2023.2.2\bin\mingw\bin/ld.exe: CMakeFiles/C++Project.dir/game.cpp.obj:game.cpp:(.rdata$.refptr.playerId[.refptr.playerId]+0x0): undefined reference to playerId'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

regal monolith
#

hmm rip

reef crow
#

no plz...

#

don't tell me that I can't do anything to that

regal monolith
#

lol no bro I just meant rip about it not giving helpful errors

#

mingw is ass, I had a similar issue with it yesterday and just said fuck it and used ubuntu instead of windows lmao

#

anyways, try setting a breakpoint on the first line inside your main() function, and press the debug button to run it till that line. does it crash before it reaches that breakpoint?

reef crow
#

Is it linked ? I think not, just trying to search

regal monolith
#

I need bigger screenshots man

reef crow
regal monolith
#

so are you trying to print something like Le jouer 42 a gagne avec un carre de 69 x1337?

reef crow
#

It would be like, The player 5 won with a square of 3x3 for example

regal monolith
#

yeah, this is indeed a wrong use of sprintf(), you'll also want to fix any other spot you use it

reef crow
#

sorry didn't show you the entire thing

#

I use it in the else

#

fck 20mins left and my code won't start

regal monolith
#

Ah ok no worries, so instead of using sprintf I highly recommend doing this instead. It's way safer and you can put the values in between the strings like you wrongly tried to do with sprintf():

#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream ss;
    ss << "hello " << 42 << " world" << std::endl;
    std::cout << ss.str(); // prints "hello 42 world\n"
}
regal monolith
reef crow
#

I can't x), the thing will maybe close

regal monolith
#

I know how it is, I also always handed things in at the very last moment

#

well still, you can whine or w/e

reef crow
#

And I tried to put a breakpoint on the first line in my main

#

It remains the same thing while not launching anything

regal monolith
#

probably because of the sprintf misuse

reef crow
#

but it worked with others

regal monolith
regal monolith
#

you don't need to create an array with an arbitrary limit of 60 like you did with char winText[60]; either

#

if that overflows your program can easily crash or even get hacked, though I get that's not top priority rn

reef crow
#

now the DrawText won't work

regal monolith
#

don't forget the .str()

#

nvm

#

yeah okay so get rid of the %d in the string that you left on accident first of all

reef crow
#

yep

regal monolith
#

second of all I don't think you want that std::endl in the string

#

maybe you do, but I suspect not

reef crow
#

did it

regal monolith
#

okay, and because it's apparently a C function that expects a char * and not a std::string &, you'll need to use .str().c_str() to get its underlying char *

reef crow
#

okay so no more error on this but the code still doesn't work

regal monolith
#

in what sense

reef crow
#

the big terminal error

reef crow
#

yep

regal monolith
#

yeah so with clion's magnifying glass look for the mentions of largestPlayerSquare

reef crow
#

it might be this

#

but

#

I need this as a global

regal monolith
#

give it a different name in the function then if you want to use the same name for the global

#

just rename it to playerId_ or smth

reef crow
#

so I put it too in the if ?

regal monolith
#

yes

reef crow
#

FCKKKK

#

still the same error

regal monolith
#

well yeah

#

look for every mention of largestPlayerSquare

#

use the search tool

reef crow
#

as you can see I only have these 2

#

I also have playerId in playerWithLargestSquare but it's the global fct that I want to retun

regal monolith
#

I think the compilation is failing because you might have a Makefile or ninja build system or something that you forgot to add your new .cpp files to

reef crow
#

I need this variable in the 3 functions

#

not possible