Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the Board class, define a method reaching_empty_matrix(self) that returns a matrix (list of lists) of Booleans of the same size as the board. An

In the Board class, define a method reaching_empty_matrix(self) that returns a matrix (list of lists) of Booleans of the same size as the board. An element of this matrix is True if the corresponding point on the board is either empty or reaches empty and False otherwise. The method must not modify the board.

Can you please provide a code for this

This is the test case

b = Board(3, ["O.O", ".@.", "@O."]) print(b.reaching_empty_matrix()) b = Board(3, ["O@O", ".O.", "@O."]) print(b.reaching_empty_matrix())

l19 = load_board("l19.txt") print(l19) e = l19.reaching_empty_matrix() for r in range(len(e)): for c in range(len(e[r])): if not e[r][c]: print((r, c))

and the expected output :

[[True, True, True], [True, True, True], [True, True, True]] [[True, False, True], [True, True, True], [True, True, True]]

A B C D E F G H I J K L M N O P Q R S 19 . . . . @ @ @ . O O . . @ . O O O . O 18 @ O O @ O @ . . @ O @ O . . . @ @ . @ 17 @ O . . @ O . O O O O O . O O O O O @ 16 . . @ @ . O O . @ . . O @ O . @ . O . 15 O . @ . @ . O @ . O O @ @ O . . O @ O 14 O . . . O O O @ . @ @ . . . @ . O @ @ 13 . . @ O @ . . @ . . O O O . @ . @ . . 12 . . @ @ . @ @ @ . . . @ O . O . . . @ 11 @ O . . @ . @ @ @ @ O . . @ O O O @ O 10 @ . . O . @ @ O @ O O @ @ . @ . O @ . 9 @ O O O . . @ O . . @ @ O @ @ @ . O O 8 @ @ O @ . O O O . @ . O @ . @ @ @ . @ 7 @ . O . O @ O O . O O . @ O @ . . @ O 6 @ . . . O @ @ O O @ . @ @ . . O . O . 5 O O @ @ . . O @ @ . @ . @ . O @ @ O . 4 @ . O . . O O . @ O @ O @ O O . @ @ . 3 @ @ O O @ . O . @ . O @ . @ O @ O . . 2 . . . O O @ @ O . @ O . O . @ O O @ . 1 @ @ . @ O . @ @ . . @ O O O O O @ @ @

(this is as a grid )

(1, 4) (10, 12)

In the Board class, define a method is_legal(self) that returns True if the position on the board is legal and False otherwise (see Background Information for the definition of a legal position).

Test cases :

l19 = load_board("l19.txt") print(l19) print(l19.is_legal()) l19.set_colour("E19", "E") print(l19.is_legal()) l19.set_colour("M9", "B") print(l19.is_legal()) print(l19)

Expected output :

A B C D E F G H I J K L M N O P Q R S 19 . . . . @ @ @ . O O . . @ . O O O . O 18 @ O O @ O @ . . @ O @ O . . . @ @ . @ 17 @ O . . @ O . O O O O O . O O O O O @ 16 . . @ @ . O O . @ . . O @ O . @ . O . 15 O . @ . @ . O @ . O O @ @ O . . O @ O 14 O . . . O O O @ . @ @ . . . @ . O @ @ 13 . . @ O @ . . @ . . O O O . @ . @ . . 12 . . @ @ . @ @ @ . . . @ O . O . . . @ 11 @ O . . @ . @ @ @ @ O . . @ O O O @ O 10 @ . . O . @ @ O @ O O @ @ . @ . O @ . 9 @ O O O . . @ O . . @ @ O @ @ @ . O O 8 @ @ O @ . O O O . @ . O @ . @ @ @ . @ 7 @ . O . O @ O O . O O . @ O @ . . @ O 6 @ . . . O @ @ O O @ . @ @ . . O . O . 5 O O @ @ . . O @ @ . @ . @ . O @ @ O . 4 @ . O . . O O . @ O @ O @ O O . @ @ . 3 @ @ O O @ . O . @ . O @ . @ O @ O . . 2 . . . O O @ @ O . @ O . O . @ O O @ . 1 @ @ . @ O . @ @ . . @ O O O O O @ @ @

False

False

True

A B C D E F G H I J K L M N O P Q R S 19 . . . . . @ @ . O O . . @ . O O O . O 18 @ O O @ O @ . . @ O @ O . . . @ @ . @ 17 @ O . . @ O . O O O O O . O O O O O @ 16 . . @ @ . O O . @ . . O @ O . @ . O . 15 O . @ . @ . O @ . O O @ @ O . . O @ O 14 O . . . O O O @ . @ @ . . . @ . O @ @ 13 . . @ O @ . . @ . . O O O . @ . @ . . 12 . . @ @ . @ @ @ . . . @ O . O . . . @ 11 @ O . . @ . @ @ @ @ O . . @ O O O @ O 10 @ . . O . @ @ O @ O O @ @ . @ . O @ . 9 @ O O O . . @ O . . @ @ @ @ @ @ . O O 8 @ @ O @ . O O O . @ . O @ . @ @ @ . @ 7 @ . O . O @ O O . O O . @ O @ . . @ O 6 @ . . . O @ @ O O @ . @ @ . . O . O . 5 O O @ @ . . O @ @ . @ . @ . O @ @ O . 4 @ . O . . O O . @ O @ O @ O O . @ @ . 3 @ @ O O @ . O . @ . O @ . @ O @ O . . 2 . . . O O @ @ O . @ O . O . @ O O @ . 1 @ @ . @ O . @ @ . . @ O O O O O @ @ @

--------------------------------------------------------------------------------------

My code for now :

def load_board(filename): result = " " with open(filename) as f: print(f) for index, line in enumerate(f): if index == 0: result += ' '+' '.join([chr(alphabets + 65) for alphabets in range(len(line) - 1)]) + ' ' # the alphabetical column heading result += f"{19 - index:2d}" result += ' ' + ' '.join(line.strip()) + ' ' return result def save_board(filename, board): with open(filename, 'w') as f: f.write(''.join([''.join(line) + ' ' for line in board])) def print_board(board): result = f" {' '.join([chr(ord('A') + i) for i in range(len(board[0]))])} " for y, row in enumerate(board): result += f"{str(abs(y - len(board))):>3s} {' '.join(row)} " print(result) # from string import ascii_uppercase as letters class Board: #Dictionary created for the colours and the respected symbols points = {'E': '.', 'B': '@', 'W': 'O'} #Constructor def __init__(self,size=19,from_strings=None): assert 2 <= size <= 26, "Illegal board size: must be between 2 and 26." if from_strings != None: if type(from_strings) != list: raise AssertionError("input is not a list") if len(from_strings) != size: raise AssertionError("length of input list does not match size") for i in range(size): if type(from_strings[i]) != str: raise AssertionError("row " + str(i) + " is not a string") if len(from_strings[i]) != size: raise AssertionError("length of row " + str(i) + " does not match size") for j in range(size): if from_strings[i][j] not in ".@O": raise AssertionError("invalid character in row " + str(i)) self.size = size self.grid = [['E'] * size for _ in range(size)] #self.from_strings = [] if from_strings is None else from_strings self.from_strings=from_strings def get_size(self): #Returns the size of the grid created by the constructor return self.size def __str__(self): # creating the grid padding = ' ' # Creating a variable with a space assigned so that it acts as a padding to the rows that have a single digit heading = ' ' + ' '.join(letters[:self.size]) # Alphabetical heading is created lines = [heading] # adding the alphabetical heading into a list named lines to which the rows will be added later for r, row in enumerate(self.grid): if len(self.grid) < 10: # for the grid with a size less than 10 to add the space to the start of the row for the single digits to be aligned if (self.from_strings): line = " " + f'{self.size - r} ' + ' '.join(self.from_strings[r]) else: line = " " + f'{self.size - r} ' + ' '.join(self.points[x] for x in row) lines.append(line) else: # for the grids that are larger than 9 if r > 9: # for rows 1 to 9 the single digits are aligned according to the first digit from the right of the two digit rows if (self.from_strings): line = f'{self.size - r} ' + ' '.join(self.from_strings[r]) else: line = f'{self.size - r} ' + ' '.join(self.points[x] for x in row) line = padding + line # adding the space using the variable padding to the row created lines.append(line) # adding the row to the list of rows else: # for the rows 10 onwards - as there is no requirement to add a padding it is not added here if (self.from_strings): line = f'{self.size - r} ' + ' '.join(self.from_strings[r]) else: line = f'{self.size - r} ' + ' '.join(self.points[x] for x in row) # creation of the row lines.append(line) # adding the newly created row to the list of rows return ' '.join(lines) def _to_row_and_column(self, coords): # destructure coordinates like "B2" to "B" and 2 alpha, num = coords colnum = ord(alpha) - ord('A') + 1 rownum = self.size - int(num) + 1 assert 1 <= rownum <= self.size,"row out of range" assert 1 <= colnum <= self.size,'column out of range' return rownum, colnum def set_colour(self, coords, colour_name): rownum, colnum = self._to_row_and_column(coords) assert len(coords)==2 or len(coords)==3, "invalid coordinates" assert colour_name in self.points,"invalid colour name" self.grid[rownum - 1][colnum - 1] = colour_name def get_colour(self, coords): rownum, colnum = self._to_row_and_column(coords) return self.grid[rownum - 1][colnum - 1] def to_strings(self): padding = ' ' lines = [] for r, row in enumerate(self.grid): if self.from_strings: lines.append(''.join(self.from_strings[r])) else: lines.append(''.join(self.points[x] for x in row)) return lines def to_integer(self): digit_colour="" for line in self.to_strings(): for character in line: if character=='.': character=0 elif character=='O': character=1 elif character=='@': character=2 character=str(character) digit_colour+=character return int(digit_colour,3) # return ''.join(self.to_int[x] for line in self.grid for x in line) def set_from_integer(self, integer_encoding): n = int(integer_encoding) list1 = [] p=[] m=[] t=[] while n != 0: rem = n % 3 list1.append(rem) n = n // 3 list1.reverse() list1=["." if item ==0 else item for item in list1] list1=["O" if item ==1 else item for item in list1] list1=["@" if item ==2 else item for item in list1] for i in range(0,len(list1),3): p.append(list1[i:i+3]) #print(p) for x in p: m=''.join(map(str,x)) t.append(m) #print(Board(self.size,t)) self.from_strings=t def fill_reaching(self,colour_name,reach_name, visited, r, c): self.visited=visited self.colour_name=colour_name self.reach_name=reach_name if r < 0 or r > self.size: #Checking whether the number of rows are within the size of the grid return False if c < 0 or c > self.size:#Checking whether the number of columns are within the size of the grid return False if self.visited[r][c]: return False if self.grid[r][c] == self.reach_name: return True if self.grid[r][c] != self.colour_name: return False elif self.grid[r][c]==self.colour_name: self.visited[r][c] = True for row in range(r - 1, r + 2): for col in range(c - 1, c + 2): if row == r and col == c: continue if self.fill_reaching(self.reach_name, self.colour_name, self.visited, row, col): return True return False 

and i think there are some errors in certain areas can you please correct it in the code as I'm very confused right now regarding the code

Please provide me the codes with comments so that i can clearly understand

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

2 What are the key barriers to implementing HRM?

Answered: 1 week ago

Question

1 What are three of the formative traditions in HRM?

Answered: 1 week ago