Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have an issue in my code the expected output is not being returned I'm new to python and for an exercise I'm supposed to

I have an issue in my code the expected output is not being returned I'm new to python and for an exercise I'm supposed to create this game. In the class, i need to define a method fill_reaching(self, colour_name, reach_name, visited, r, c) that receives the following parameters colour_name: the name of the colour ("E", "B", or "W") of the chain to be marked reach_name: the name of a colour to be tested for reachability visited: a matrix (list of lists) of Booleans of the same size as the board r, c: the row and column number indexed from 0, i.e. 0, 0 is the top left corner it executes the flood fill algorithm, starting from the position given by r and c, to mark all points of colour colour_name that are connected to the starting position by a path of colour colour_name moving only vertically and horizontally. The method modifies the matrix visited: marking a point is defined as setting the corresponding element in visited to True. Some elements of visited may be True to begin with the corresponding grid points are treated as if they were of a colour different from colour_name, this method must not modify the board object and In addition to marking all visited points, the method returns True if the starting point reaches the colour specified in reach_name (see rule #3) and False otherwise. rule 3 enter image description here The black point marked with a triangle reaches empty because there is a path of (vertically or horizontally adjacent) black points, marked with circles, to a point coloured empty (marked with a square). The white point below the triangle does not reach empty. By following a path of white points only vertically or horizontally, you only ever reach black, not empty. A legal position is a position in which every point coloured black or white reaches empty. The example above is not a legal position because there are points coloured white that dont reach empty. It is a consequence of the rules that every valid move creates a legal position. This is because according to rule #7, both colours are cleared, removing any stones that dont reach empty. My code is as follows 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()) + ' ' print(type(result)) result=list(result) print(type(result)) size=len(result) #return (result) print(len(result)) return Board(from_strings = result) def save_board(filename, board): with open(filename, "wt") as f: f.write(board) 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 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 b = Board(3, ["@O.", "OOO", ".O."]) print(b) visited = [[False] * 3 for i in range(3)] e = b.fill_reaching("W", "E", visited, 1, 1) print(e) print(visited) visited = [[False] * 3 for i in range(3)] e = b.fill_reaching("B", "E", visited, 0, 0) print(e) print(visited) visited = [[False] * 3 for i in range(3)] e = b.fill_reaching("W", "E", visited, 0, 0) print(e) print(visited) however I'm not getting the expected output the output i get : A B C 3 @ O . 2 O O O 1 . O . True [[False, False, False], [False, False, False], [False, False, False]] True [[False, False, False], [False, False, False], [False, False, False]] True [[False, False, False], [False, False, False], [False, False, False]] Process finished with exit code 0 the expected: A B C 3 @ O . 2 O O O 1 . O . True [[False, True, False], [True, True, True], [False, True, False]] False [[True, False, False], [False, False, False], [False, False, False]] False [[False, False, False], [False, False, False], [False, False, False]]

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

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

compare and contrast positivity and negativity;

Answered: 1 week ago

Question

What conflicts of interest had to be resolved?

Answered: 1 week ago