Question: The class GameBoard is used to implement the game board ( grid ) . In Section 1 you will implement basic functionality including adding a

The class GameBoard is used to implement the game board (grid). In Section 1 you will implement basic functionality including adding a disk into the board, testing whether a game is finished, and displaying the game board.
The class GameBoard is defined as follows:
class GameBoard:
def __init__(self, size):
self.size = size
self.num_disks =[0]* size
self.items =[[0]* size for i in range(size)]
self.points =[0]*2
The variable size specifies the size of the game board, i.e. the number size of columns and rows. You can assume that 1= size =10 and you dont need to test for this.
The variable num_disks is a list, which specifies the number of disks in each column. The initial value for each column is zero.
The game board itself is represented by the 2D array items. This is a list of lists. The first dimension refers to the column number and the second dimension to the row number, i.e. items[i][j] is the value of the item in the i-th column and j-th row. Each item is initialised with the value 0(meaning, no disk). The value 1 will represent a disk of player 1 and the value 2 a disk of player 2.
The variable points represents the points (number of lines of 4 connected pieces) of each player: points[0] are the points of player 1, and points[1] are the points of player 2.
So here is the source code from me so far.
class GameBoard:
def __init__(self, size):
self.size = size
self.num_disks =[0]* size
self.items =[[0]* size for i in range(size)]
self.points =[0]*2
def num_free_positions_in_column(self, column):
return self.size - self.num_disks[column]
def game_over(self):
return all(num_disk == self.size for num_disk in self.num_disks)
def display(self):
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))
for col in range(self.size):
print(col, end="")
print()
print("Points player 1:", self.points[0])
print("Points player 2:", self.points[1])
def num_new_points(self, column, row, player):
return 0
def add(self, column, player):
if column 0 or column >= self.size or self.num_disks[column]>= self.size:
return False
row = self.num_disks[column]
self.items[column][row]= player
self.num_disks[column]+=1
self.points[player-1]+= self.num_new_points(column, row, player)
return True
and I need help with the question in the image below with some test cases and expected result.
The class GameBoard is used to implement the game

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!