#Is it possible to generalize searching a diagonal?

1 messages ยท Page 1 of 1 (latest)

iron apex
#

Power of 4

hot sorrelBOT
#

<@&987246399047479336> please have a look, thanks.

hot sorrelBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

iron apex
#

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?

modest compass
#

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

iron apex
#

i did not use any loops, but im pretty sure theres a better way to do this

modest compass
#

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

iron apex
modest compass
#

well yeah

#

so thats hardcoded

iron apex
#

example

#

i was not able to generalize it

modest compass
#

yeah but its not that difficult

#

as said

iron apex
#

im trying to find a pattern for this with loops

modest compass
#

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

iron apex
#

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

modest compass
#

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

iron apex
#

the code is already written for me for the most part, i only need to code this method

#

certain methods

modest compass
#
} 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;
  }
}
iron apex
#

will i need to decrement at some point?

modest compass
#

huh?

iron apex
#

--

modest compass
#

i just literally took ur current code

#

and moved it around into helper methods

#

nothing else

#

i didnt create new code at all

iron apex
#

ok give me some time to wrap my head around this

#

btw is hardcoding something "bad" ?

modest compass
#

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

iron apex
#

oh ok

modest compass
#

the problem is that u duplicated a lot of ur code

iron apex
#

thanks then

modest compass
#

ending up with a huge if-else thingy thats just hard to read

#

and easy to make bugs

iron apex
#

yes you'Re right

signal aurora
#

well here is one fun way

#

@iron apex can you share how you represent your board?

iron apex
#

it's a 6*7 board

signal aurora
#

in memory

#

what is the data structure holding this

iron apex
#

two dimension array

signal aurora
#

of what

iron apex
#

char

signal aurora
#

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)

iron apex
#

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

signal aurora
hot sorrelBOT
# signal aurora

I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users ๐Ÿ‘

signal aurora
#

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"

iron apex
#

@modest compass i fixed my ugly code for something better

#

for diagonals

#

i found the pattern finally to be able to do this cleaner

iron apex
#

those are the decreasing ones, there's another block for the increasing ones