1

In Python (V2.7), I'm trying to make a ConnectFour game against a computer player. I've worked out a simple function to find 4 in a row (to determine the end of the game) and return TRUE if so, and now I'm trying use the same function to locate any 3 in a rows and return the location.

def finder(matrix, player,number):
    for row in matrix:
            count = 0
            for item in row:
                if item == player:
                    count +=1
                    if count == number:
                        return True
                else:
                    count = 0

Where I can enter: finder(board, "X", 4) to know if four in a row is TRUE or still at default FALSE (and this DOES work). Now I want to try something like this:

def finder(matrix, player,number):
    for row in matrix:
            count = 0
            for item in row:
                if item == player:
                    count +=1
                    if count == number:
                        return True
                        location = row, item
                        return location
                else:
                    count = 0

However this calls an error that I haven't initialized location, so I then set location = 0, 0. Now it just returns TRUE and the tuple (0,0). How can I get it to give the location of the last element of the 3 in a row?

Edit: I've tried returning the three-tuple: TRUE, row, item. However, the second part in the problem is how do I get the row and col numbers when the function is TRUE? The following code works to realize that there is a threat, but I can't get find a way to obtain the location of a threat given there exists a threat.

if finder(board, "X", 3) is True:
    horizontal_threat = True
    print row, item
Ra31513
  • 23
  • 1
  • 7
  • 4
    _"Now it just returns TRUE and the tuple (0,0)"_. That doesn't sound possible to me. A function can't return twice. When your function reaches `return True`, it terminates immediately and hands `True` to the calling context, without ever getting to `return location`. Can you provide a [mcve] that demonstrates your claimed behavior? – Kevin Oct 06 '17 at 13:48
  • 1
    _"I can enter: finder(board, "X", 4) to know if four in a row is TRUE or still at default FALSE"_. Are you saying that your function returns `False` by default if you don't hit any return statements? If a function ends without hitting a return statement, it returns None, not False. In fairness, None is [falsey](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-in-python-how-is-it-different-from-true-and-false), so in some contexts they behave the same but conventionally it's best to explicitly return False rather than None if all code paths are expected to return a boolean. – Kevin Oct 06 '17 at 13:58
  • I wasn't aware a function could only return one value, I set location = 0,0 at the beginning of the program. Now I realize I was just calling a variable I set a while back. – Ra31513 Oct 06 '17 at 15:39

3 Answers3

0

You could do it by

for (i, row) in enumerate(matrix):
    ...
    for (j, item) in row:
        ...
        if (count==number):
           return (i, j)

However, there are a few things wrong in the current code.

  • you have two return statements. I don't think this is possible to return (0, 0) as it is
  • you count the total number of Xs in the matrix. you don't check if they are in a line
blue_note
  • 27,712
  • 9
  • 72
  • 90
0

Your second return statement is not getting executed for reason specified by @Kevin in his comment above.

You need to write your statement like

return True, location

and wherver you are calling finder you have to do like

found, values = finder(board, "X", 3)

Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61
0
# Complete, working, Python 3 solution


def finder(matrix, player, number):
    location = None
    for row in matrix:
        count = 0
        for item in row:
            if item == player:
                count +=1
                if count == number:
                    location = row, item
                    break
            else:
                count = 0

    return location


matrixMain = [ [1, 0, 0, 0], [2, 2, 2, 2], [1, 0, 0, 0], [1, 0, 0, 0] ]

for itemToFind in range( 1, 3 ):
    locationFound = finder( matrixMain, itemToFind, 3 )
    if locationFound is None:
        print( "item {} not found".format( itemToFind ) )
    else:
        rowFound = locationFound[0]
        itemFound = locationFound[1]
        print( "found item {}".format( itemFound ) )


# Is forcing users to indent code by four spaces really the best Stackoverflow can do ?
Mah Moh
  • 1
  • 2