Question: class GameBoard: def _ _ init _ _ ( self , size ) : self.size = size self.num _ disks = [ 0 ] *

class GameBoard:
def __init__(self, size):
self.size = size
self.num_disks =[0]* size
self.items =[[0]* size for _ in range(size)]
self.points =[0]*2 # points[0] for player 1, points[1] for player 2
def num_free_positions_in_column(self, column):
"""Returns the number of free positions in the given column."""
if column <0 or column >= self.size:
return 0
return self.size - self.num_disks[column]
def game_over(self):
"""Returns True if the game is over (no free positions), otherwise False."""
return all(disk_count >= self.size for disk_count in self.num_disks)
def display(self):
"""Displays the current state of the game board."""
for row in range(self.size -1,-1,-1):
for col in range(self.size):
if self.items[col][row]==0:
print("", end="")
elif self.items[col][row]==1:
print("o", end="")
elif self.items[col][row]==2:
print("x", end="")
print()
print("-"*(self.size *2-1))
print("".join(str(col) for col in range(self.size)))
print(f"Points player 1: {self.points[0]}")
print(f"Points player 2: {self.points[1]}")
def num_new_points(self, column, row, player):
"""Calculates the number of newly created 4-in-a-row sequences after adding a disk."""
directions =[
(1,0), # Horizontal
(0,1), # Vertical
(1,1), # Diagonal down-right
(1,-1) # Diagonal down-left
]
total_points =0 # To count newly created 4-in-a-row sequences
for dx, dy in directions:
count =1 # Start counting with the current disk
# Check in the positive direction
for step in range(1,4): # Check 3 steps in the positive direction
new_col = column + step * dx
new_row = row + step * dy
if 0<= new_col < self.size and 0<= new_row < self.size:
if self.items[new_col][new_row]== player:
count +=1
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(1,4): # Check 3 steps in the negative direction
new_col = column - step * dx
new_row = row - step * dy
if 0<= new_col < self.size and 0<= new_row < self.size:
if self.items[new_col][new_row]== player:
count +=1
else:
break # Stop if we hit a different disk
else:
break # Stop if out of bounds
# If count is 4, we've created a new 4-in-a-row
if count >=4:
total_points +=1 # Count this as a new point
return total_points
def add(self, column, player):
"""Inserts a disk into the specified column for the given player."""
if column <0 or column >= self.size or self.num_disks[column]>= self.size:
return False # Invalid column or column is full
# Determine the first available row
row = self.num_disks[column]
# Place the disk on the board
self.items[column][row]= player
# Increment the number of disks in this column
self.num_disks[column]+=1
# Calculate new points created by this move
new_points = self.num_new_points(column, row, player)
self.points[player -1]+= new_points # 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!