#Simple Connect Four Game

29 messages · Page 1 of 1 (latest)

trail pewter
#

Hey there, im currently working on a connect 4 game, the issue with this is sometimes it returns true sometimes it returns false, even though the player has won

CheckForWin(Team: string) : logic =
        # team one is yellow, team two is red
        Board:[][]int = array{
            array{Row1_6, Row2_6, Row3_6, Row4_6, Row5_6, Row6_6, Row7_6},
            array{Row1_5, Row2_5, Row3_5, Row4_5, Row5_5, Row6_5, Row7_5},
            array{Row1_4, Row2_4, Row3_4, Row4_4, Row5_4, Row6_4, Row7_4},
            array{Row1_3, Row2_3, Row3_3, Row4_3, Row5_3, Row6_3, Row7_3},
            array{Row1_2, Row2_2, Row3_2, Row4_2, Row5_2, Row6_2, Row7_2},
            array{Row1_1, Row2_1, Row3_1, Row4_1, Row5_1, Row6_1, Row7_1}
        }
        var current_coin : int = 0
            if (Team = "Yellow"):
                set current_coin = 1

            if (Team = "Red"):
                set current_coin = 2

        for (C := 0..4):
            for (R := 0..6):

                if (Board[R][C] = current_coin and Board[R][C + 1] = current_coin and Board[R][C + 2] = current_coin and Board[R][C + 3] = current_coin):
                    return true

        for (C := 0..7):
            for (R := 0..3):
                if (Board[R][C] = current_coin and Board[R + 1][C] = current_coin and Board[R + 2][C] = current_coin and Board[R +3][C] = current_coin):
                    return true  

        for (C := 0..4):
            for (R := 0..3):
                if (Board[R][C] = current_coin and Board[R + 1][C + 1] = current_coin and Board[R + 2][C + 2] = current_coin and Board[R +3][C + 3] = current_coin):
                    return true  

        for (C := 0..4):
            for (R := 3..6):
                if (Board[R][C] = current_coin and Board[R - 1][C + 1] = current_coin and Board[R - 2][C + 2] = current_coin and Board[R - 3][C + 3] = current_coin):
                    return true  

                    
        return false```
true ocean
#

This doen't look like Javascript😁

#

But I don't see where you swich the team after inut?

trail pewter
#

the board basically uses 1 to determine if a coin is yellow, 2 if a coin is red and 0 if no coin is placed

#

and the for loops check if a coin on either team 1 or 2 is placed and if so, it uses math to see if the next ones match up as the same color coin

trail pewter
true ocean
trail pewter
#

no idea why they didnt just use c++ lol

true ocean
#

ah ok, just had googled it.
I##ll try to read it now 😉

trail pewter
#

🙏

true ocean
#

do the indices begin with 1, not with 0?

trail pewter
#

the entire board begins with only zeros

#

this is an example

#

for placement like this, if i were to put a red one in the empty space it would show in the array, but the code wouldnt detect it as a win

true ocean
#

I wonder whether this must not loop over 0..3 only?

for (C := 0..4):
            for (R := 0..6):

                if (Board[R][C] = current_coin and Board[R][C + 1] = current_coin and Board[R][C + 2] = current_coin and Board[R][C + 3] = current_coin):
                    return true```
#

aaah... I think you can not go with current-coin, but you need to predifine an array with win-states (or I totally misunderstand your approach)

trail pewter
trail pewter
true ocean
#

yeah, but column3 is the last one were 3 others follow

trail pewter
true ocean
#

It does? although you loop from 0..4?

#

Maybe this language is just different lol. In js it would throw an error

trail pewter
#

oh u right

#

i just did number of collumns - 3

true ocean
#

But I still think it would be much more effucient to have an array with winconditions and to check against that instead of looping over each field on each set

trail pewter
true ocean