3

I'm trying to build a tic tac toe game and I'm having trouble with making a working function to check if the board is full. my list for the board is
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
and my function is

def fullBoard():
"""Returns a boolean value reporting the state of the board, full or not.
True for full, False for not full."""
for y in board:
    if board[y] == ' ':
        return False 
        print(False)
        break

else:
    print(True)
    return True

what I want it to do is search each item in the list and when it finds a ' ' or space, it returns false and breaks the loop. but if there is no ' ' it returns true meaning the board is full (I put the print functions in just for my sake while I was trying to fix the darn thing.)

I thought this would work but I get TypeError: list indices must be integers or slices, not str refering to my if board[y] == ' ': line. If anyone knows how to get this to work the way I want that would be great. Also take note I'm only in comp prog 1 and am at a very basic level, so simplicity would be best. (also please don't try to make it shorter or run faster. I just want to know what the mistake I've made is so I can learn from it.) Thank you very much!

  • Another option is to avoid coding the loop yourself by using the `in` or `not in` operator (see [here](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range) in the Python manual). In Python 2, there were also more generalized functions `reduce`, `all`, and `any` that you could use when you're checking for something more complex than the presence/absence of a particular value. I imagine they're still available in Python 3 somewhere, but I think I remember something about `reduce` as a global function being deprecated for being unintuitive. –  May 10 '16 at 01:53

4 Answers4

2

It's even easier than what you are doing. The misunderstanding is in your for loop. When iterating over a list, the value of y is not the index of the the item in your board list, it is the value of each item.

So:

for y in board:
    if y == ' ':
        return False

return True

The source of your confusion probably comes from the fact that, when iterating over a python dict using a for loop, the variable (in this case y) would in fact be the dictionary key. And then you'd need to use the key to access the value at that position in the dictionary. But that's for a dictionary.. you have a list. Hence no need for that.

But there's an even quicker way to do it:

    if ' ' in board:
        return False
    return True

That's it.. no for, no nothing. Python's in operator tells you if a value is contained within a list (or certain other types).

If you want to get even more minimal.. just:

return not ' ' in board

That's it.. just one line of code. The way it works is.. ' ' in board has a value of True if any of the items in board are a space, or False if there is no item with a value of space in board. But you want a value of False if there is a space, True if not.. so by putting not in front, you invert the value.. making the True into False and vice versa.

Since you are learning, it's not necessarily good to take this minimal approach but I figured I would add it to my answer so that you would understand better how python handles these things.

little_birdie
  • 5,600
  • 3
  • 23
  • 28
1

When you do the loop (for y in board) y is already the element in board list and you do not need to index it (board[y]). Simply replace board[y] with y and that should solve your problem.

sj47sj
  • 185
  • 6
0

Here is simpler one:

def space_check(board):
    '''
    Returns True if the board is full
    and Returns False if the board is not full
    '''
    for y in board:
        if y == ' ' or y == '': 
            return False

    return True
AcK
  • 2,063
  • 2
  • 20
  • 27
Omar Zahi
  • 1
  • 1
-1

Your code is completely correct, but for one thing. See how board isn't a dictionary, but a list? Lists don't have key/value pairs.

Here's the fixed code:

def fullBoard():
   for y in board:
       if board[y] == ' ':
           return False 
       else:
           return True
phriol
  • 101
  • 1
  • 1
  • 7