I'm in my first AND LAST computer science class and I really need help with our final project. Up to this point all of my assignments have been completed from my teacher and TA standing over my shoulder telling me exactly what to do. I'm pretty sure they're just as frustrated with my inability to comprehend this program as I am. With the first part of the assignment I have about half of it done, but can't figure out 3 functions; isFull (to check if the board is full), winsFor (to determine if somebody has won horizontally or vertically), and hostGame (to host the game..? I think). Here is what I have thus far....help is greatly appreciated!
class Board:
""" a datatype representing a C4 board
with an arbitrary number of rows and cols
"""
def __init__( self, width, height ):
""" the constructor for objects of type Board """
self.width = width
self.height = height
self.data = [] # this will be the board
for row in range( 6 ):
boardRow = []
for col in range( 7 ):
boardRow += [' '] # add a space to this row
self.data += [boardRow] # add this row to the board
# do not need to return inside a constructor!
def __repr__(self):
""" this method returns a string representation
for an object of type Board
"""
s = ''
for row in range( self.height ):
s += '|'
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
def addMove(self, col, ox):
""" This method adds an O or an X in the appropriate column
"""
if self.allowsMove(col) == True:
for row in range( self.height ):
if self.data[row][col] != ' ':
self.data[row-1][col] = ox
return
self.data[self.height-1][col] = ox
else:
print('That move is not allowed!')
def clear(self):
"""This method clears the board for a new game"""
for x in range(self.height):
for y in range(self.width):
if self.data[x][y] != ' ':
self.data = [x][y] = ' '
def setBoard( self, moveString ):
""" takes in a string of columns and places
alternating checkers in those columns,
starting with 'X'
For example, call b.setBoard('012345')
to see 'X's and 'O's alternate on the
bottom row, or b.setBoard('000000') to
see them alternate in the left column.
moveString must be a string of integers
"""
nextCh = 'X'
for colString in moveString:
col = int(colString)
if 0 <= col <= self.width:
self.addMove(col, nextCh)
if nextCh == 'X': nextCh = 'O'
else: nextCh = 'X'
def allowsMove(self, col):
"""This method checks if the move being added is allowed
"""
if 0 <= col < self.width:
if (self.data[0][col] == ' '):
return True
else:
return False
def isFull(self):
""" This method returns True if the board is full and False if
it is not
"""
for x in range(self.height):
for y in range(self.width):
if
def hostGame ( self ):
"""Hosts a full game of Connect 4"""
def winsFor ( self, ox ):
"""Checks if someone has won the game"""
# check for horizontal wins
for row in range(0,self.height):
for col in range(0,self.width-3):
if self.data[row][col] == ox and \
self.data[row][col+1] == ox and \
self.data[row][col+2] == ox and \
self.data[row][col+3] == ox:
return True
# checks for vertical wins