#Is it possible to generalize searching a diagonal?
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
So basically, i had to search for diagonals in the game "connect 4", and while I did suceed in doing it, it's not the prettiest thing which I wrote. I basically brute forced it, I was wondering if it was possible to generalize this with loops?
sure
u just have to work out the pattern
how do diagonals work?
well, its always +1 on x and y
so u start somewhere and then x++ and y++ each time
at some point u run out of bounds
and then u can either stop or just reset back, so that u start all over again
is a brute force a good method lol
i did not use any loops, but im pretty sure theres a better way to do this
not quite sure what u mean by that tbh
maybe u mean hardcoding?
hard to guess since u didnt share anything
if u hardcoded it, it wouldnt work if u increase the size of the game board
im trying to find a pattern for this with loops
good that i just explained it to u
so now u can do it
id say if u absolutely have zero interest or plans in changing the board size, there is technically nothing wrong with hardcoding it - it works after all
that said, one can still make ur code more readable
by getting rid of all the duplication
yes, to be honest give me time ill try to get it
ill respond to you if Im still stuck
btw in my vertical i was able to employ a loop
@modest compass
same for the horizontal
yeah but u still hardcode the offsets and u have a lot of duplication
for example that grille[i][compteur + ...] is duplicated everywhere
also the == 'R'
all of this can be removed
for example with simple helper methods
the code is already written for me for the most part, i only need to code this method
certain methods
} else if (checkAll(1, 2, 3, 4, "R")) {
...
and so on
with such a method u could reduce all of this mess to just
ah and also, all ur cases do exactly the same
so this can be merged as well
if (checkAll(0, 1, 2, 3)
|| checkAll(1, 2, 3, 4)
|| checkAll(2, 3, 4, 5)
|| checkAll(3, 4, 5, 6)) {
trouve = true;
}
at this point u realize the pattern
and can make it a simple loop
for (int offset = 0; offset < board.width; offset++) {
if (checkDiagonal(offset)) {
trouve = true;
}
}
will i need to decrement at some point?
huh?
--
i just literally took ur current code
and moved it around into helper methods
nothing else
i didnt create new code at all
as i said earlier
if u dont intent to change the board size at all, its not
the problem in ur code is not the hardcoding
its the duplication that makes it ugly
hardcoding a few numbers like checkDiagonal(1) checkDiagonal(2), ... is not a problem per se
unless u want to change the size of the board
oh ok
the problem is that u duplicated a lot of ur code
thanks then
ending up with a huge if-else thingy thats just hard to read
and easy to make bugs
yes you'Re right
two dimension array
of what
char
okay so here is the fun solution
still use a char[][] but
to check diagonals, instead of doing that boring math stuff...
(give me 5-10)
Yes it's not going to work i realzie that
but the game is supposed to be connect four though
so the sizes are suppsoed to stay the same
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users ๐
this is what over engineering looks like btw
not actually the approach i would reccomend
but effectively you can turn each char into an object that knows its neighbors
and just ask each cell "hey do you have three neighbors that are the same as you"
@modest compass i fixed my ugly code for something better
for diagonals
i found the pattern finally to be able to do this cleaner
those are the decreasing ones, there's another block for the increasing ones