Question: class GameBoard: def _ _ init _ _ ( self , size ) : self.size = size self.num _ disks = [ 0 ] *
class GameBoard:
def initself size:
self.size size
self.numdisks size
self.items size for in rangesize
self.points # points for player points for player
def numfreepositionsincolumnself column:
Returns the number of free positions in the given column."""
if column or column self.size:
return
return self.size self.numdiskscolumn
def gameoverself:
Returns True if the game is over no free positions otherwise False."""
return alldiskcount self.size for diskcount in self.numdisks
def displayself:
Displays the current state of the game board."""
for row in rangeselfsize :
for col in rangeselfsize:
if self.itemscolrow:
print end
elif self.itemscolrow:
printo end
elif self.itemscolrow:
printx end
print
printselfsize
printjoinstrcol for col in rangeselfsize
printfPoints player : selfpoints
printfPoints player : selfpoints
def numnewpointsself column, row, player:
Calculates the number of newly created inarow sequences after adding a disk."""
directions
# Horizontal
# Vertical
# Diagonal downright
# Diagonal downleft
totalpoints # To count newly created inarow sequences
for dx dy in directions:
count # Start counting with the current disk
# Check in the positive direction
for step in range: # Check steps in the positive direction
newcol column step dx
newrow row step dy
if newcol self.size and newrow self.size:
if self.itemsnewcolnewrow player:
count
else:
break # Stop if we hit a different disk
else:
break # Stop if out of bounds
# Check in the negative direction
for step in range: # Check steps in the negative direction
newcol column step dx
newrow row step dy
if newcol self.size and newrow self.size:
if self.itemsnewcolnewrow player:
count
else:
break # Stop if we hit a different disk
else:
break # Stop if out of bounds
# If count is we've created a new inarow
if count :
totalpoints # Count this as a new point
return totalpoints
def addself column, player:
Inserts a disk into the specified column for the given player."""
if column or column self.size or self.numdiskscolumn self.size:
return False # Invalid column or column is full
# Determine the first available row
row self.numdiskscolumn
# Place the disk on the board
self.itemscolumnrow player
# Increment the number of disks in this column
self.numdiskscolumn
# Calculate new points created by this move
newpoints self.numnewpointscolumn row, player
self.pointsplayer newpoints # Update the player's points
return True
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
