1

I've been trying to figure out this problem for multiple hours and still no luck. I'm in the process of writing Connect4 in Python for a school assignment and I need a function that checks if the board is full.

Here is my init function

    def __init__( self, width, height ): 
    self.width = width 
    self.height = height 
    self.data = [] # this will be the board 

    for row in range( self.height ): 
        boardRow = [] 
        for col in range( self.width ): 
            boardRow += [' '] 
        self.data += [boardRow] 

My repr function

    def __repr__(self): 
    #print out rows & cols 
    s = '' # the string to return 
    for row in range( self.height ): 
        s += '|' # add the spacer character 
        for col in range( self.width ): 
            s += self.data[row][col] + '|' 
        s += '\n' 

s += '--'*self.width + '-\n'

for col in range( self.width ):
    s += ' ' + str(col % 10)
s += '\n'

return s

And what I have for my isFull function

    def isFull(self):
# check if board is full
for row in range(0,(self.height-(self.height-1))):
    for col in range(0,self.width):
    if (' ') not in self.data[row][col]:
        return True

I want to check and see if there this ' ' (a space) within the data list. At least I think that's my problem, I'm not experienced in python so I may be misreading my issue. If anyone has any ideas I'm glad to listen.

3
  • So, what is your problem? Which method doesn't work? How does it not work? And please align your code properly. Also, what is this range(0,(self.height-(self.height-1)))? It is the same as range(0,1) Commented Dec 10, 2013 at 2:33
  • I need a function that can check if there is a space ' ' anywhere within a 2d list Commented Dec 10, 2013 at 2:35
  • You might like to post your code on CodeReview once you have it working (not with major bugs/errors) and get some ideas for improvement. Commented Dec 10, 2013 at 2:37

2 Answers 2

2

So if there is any space, it means the board is not full?

Various versions:

# straightforward but deep
def is_full(self):
    for row in self.data:
        for cell in row:
            if cell == ' ':
                return False
    return True

# combine the last two
def is_full(self):  # python functions/methods are usually lower case
    for row in self.data:  # no need to index everything like c
        if any(cell == ' ' for cell in row):  # any/all are convenient testers
            return False  # if you find even one, it's done.
    return True  # if you couldn't disqualify it, then it looks full

# one line, not especially readable
def is_full(self):
    return not any(cell == ' ' for row in d for cell in row)
Sign up to request clarification or add additional context in comments.

2 Comments

I honestly can't thank you enough. I'm not much of a programmer so I've been trying to figure this out for hours and this worked perfectly. Thank you again
great! I would appreciate it if you could formally accept the answer. Warm fuzzies all around.
1

Your logic for isFull method is incorrect.

In your current code, you are returning True from isFull as soon as you found a non-empty cell. That is incorrect. You should do the opposite.

You should be doing what kobejohn had posted earlier: to return False as soon as you found an empty cell.

And in Python you should work without indices if possible, and use Python natural looping, like in the code kobejohn had posted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.