#Can't verify adjacent blocks to the actual one.
1117 messages · Page 2 of 2 (latest)
is largestPlayerSquare a global, and what are the files called you are trying to use it in?
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;
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()
I should do the same with playerId ?
yeah, fuck the global and make it local in the function
if I do that with playerId I have warnings in my other functions
undeclared identifier
show the code and the error
yeah so it's supposed to be an argument to the function
So here in the first function where int size = getSquare(i, j) I should add playerId ?
yup
so now it works but the values are not good xD
lol, show
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
ok yeah I think I see the issue
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?
yep
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
wdym exactly with "it's"?
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
and the condition is x < grid.numCols ?
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
so what should I put in the if ?
what are you guessing
for me it was x < grid.numCols
yes
so || x < grid.numCols in the if
but reversed of course, so || x >= grid.numCols
or just || x == grid.numCols
show the code again
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};
}
thanks
np
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!
Okay I'll try with that
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)
So the 4x4 is good but not the player, it's like it's +2
this is why
fck you saw it
if you change it to x and y correctly everywhere you'll see the bug is fixed
but i'll have to change that in all my code, bcs every loop is like that, hopefull it's a square
at the very very least change it in the functions you screenshot here
this time player 4 won with a 5x5
send the new code once you did this
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};
}
Thanks, so now it should be way clearer if I say this:
- You do
int size = getSquare(y, x, playerId); - 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 :)
yep...
if(grid.grid[x][y] == playerId) {
.
I think this is the third or maybe fourth i, j issue
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
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
I think the rest of the code is fine
solved it
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};
}
ahhh yeah you're completely correct, nice!!
I missed that one actually, you have another issue here
see if you can find it with this hint
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
do you follow this?
I'd say to add +1's but not sure about that
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?
yep
okay, well look at what you actually do in the fn
the function getSquare(), that is
what variable are you returning from the fn?
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
Everything works as expected
so after a billion messages, I guess finally it's time for the holy ! solved?
I think it's time
gl man, feel free to ping me if you ever need more help
and lmk what happens with the grade
Yep !
Let's end it with !solved
!solved
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
o/
o/