manoman181
manoman181

Reputation: 53

Find a winning condition in Connect4

My program is a game similar to connect 4 but it is a 15x15 board. The winning condition is 5 adjacent pieces. The method is supposed to return True if there are 5 pieces and False otherwise. Right now this is my horizontal check, but it just skips my for loops and returns None. Really all I need is some advice on how to get the for loops to work as they are just skipped over

def current_player_is_winner(self):

    print(self.__current_player)
    piece = GoPiece(self.__current_player)
    print(piece)
    win_count = 0

    #Horizontal test
    row = 0
    col = 0
    for col in range(self.__board_size):
        for row in (range(self.__board_size)):
            if self.__go_board[row][col] == piece:
                win_count += 1
                col += 1
                print(win_count)
                if win_count == 5:
                    return True
                else:
                    return False
            else:
                row+=1
                win_count = 0

Upvotes: 0

Views: 80

Answers (1)

shrewmouse
shrewmouse

Reputation: 6030

Your are calling return False in your for loop. You really only want to do that in the True case.

You should also look into the for:else: construct.

Do the return False AFTER you have searched through the whole matrix. This is what your logic should look like:

def current_player_is_winner(self):
    ''' Depending on the win_count parameter once that many pieces are adjacendt a winer is declared'''
    print(self.__current_player)
    piece = GoPiece(self.__current_player)
    print(piece)
    win_count = 0

    #Horizontal test
    row = 0
    col = 0
    for col in range(self.__board_size):
        for row in (range(self.__board_size)):
            if self.__go_board[row][col] == piece:
                win_count += 1
                col += 1
                print(win_count)
                if win_count == 5:
                    return True
            else:
                row+=1
                win_count = 0
    return False

Upvotes: 1

Related Questions