Question: please create a code for the method play_move(self,colour_name,coords) which would be included in the class Board please provide a python code which can be directly








please create a code for the method play_move(self,colour_name,coords) which would be included in the class Board please provide a python code which can be directly added into my code please make in accordance with my code as I have to use with that and according to the description as that's what's given please stick to the descriptions and rules given.
the following code below is my code for the rest of the game
from collections import deque 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, "wt") as f: f.write(str(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,board,size=19,from_strings=None): assert 2
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) 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
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): new_list = [] new_list2 = [] for x in self.from_strings: for y in x: if y == ".": y = "E" elif y == "@": y = "B" elif y == "O": y = "W" new_list.append(y) for i in range(0, len(new_list), self.size): new_list2.append(new_list[i:i + self.size]) self.grid = new_list2 if r = self.size or c = self.size: #Checking whether the number of rows are within the size of the grid return False if visited[r][c] or self.grid[r][c] != colour_name:#Checking whether the number of columns are within the size of the grid return False if self.grid[r][c] == reach_name: return True visited[r][c] = True if self.fill_reaching(colour_name, reach_name, visited, r + 1, c): return True if self.fill_reaching(colour_name, reach_name, visited, r - 1, c): return True if self.fill_reaching(colour_name, reach_name, visited, r, c + 1): return True if self.fill_reaching(colour_name, reach_name, visited, r, c - 1): return True return False
def reaching_empty_matrix(self): # Create a matrix of the same size as the board, filled with False values matrix = [[False for i in range(self.size)] for j in range(self.size)] #false for the empty grid # Iterate over the points on the board print(self.grid[1][0]) for i in range(self.size): for j in range(self.size): # If the current point is empty or an "O" point, perform a BFS from this point if self.grid[i][j] == "." or self.grid[i][j] == "O": queue = deque([(i, j)]) matrix[i][j] = True while queue: r, c = queue.popleft() for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): new_r = r + dr new_c = c + dc # If the new point is within the bounds of the board and has not been visited, add it to the queue and mark it as visited if 0
def is_legal(self): # Iterate over the points on the board #print(self.grid) for i in range(self.size): for j in range(self.size): # Skip empty points if self.board[i][j] == ".": continue # Check the surrounding points for any illegal positions for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): new_r = i + dr new_c = j + dc # If the new point is out of bounds or empty, or if it has a different color than the current point, the position is illegal if not (0 please help me out by providing a code that I can directly add to my code as I'm not sure what should I do about it In the Board class, define a method play_move(self, colour_name, coords) that plays a move according rule \#7 (see Background Informationi). For coords and colour_name, see Task 2. If the point at the given coordinates is not empty, raise an AssertionError with message "illegal move: point not empty". rule 7 Rule 7 (Capturing) Consider this example: The three black points all reach empty according to the definition: From each of them there is a path of black points to the point F9, which is empty. After White plays F9, Black's three stones don't reach empty any more, so they are removed: Note that the definition of reaching (rule \#3) taks about a path of vertically or horizontally adjacent points. This is visualised on the Go board by following the lines. The two points B8 and F8 are not adjacent to the chain of three black stones. Rule 7 (Suicide) Here's another example for the application of rule \#7. Consider this position: It is now Black's turn and Black can capture White's stone number 6 by playing above it: So this move is suicide for Black, and it is allowed in the Logical Rules of Go which we are Note that in the rulesets used by most humans playing Go, suicide is forbidden. A game of Go might have proceeded like this, where the numbers indicate the order in which the stones were played: It is now Black's turn and Black can capture White's stone number 6 by playing above it: It is now Black's turn and Black can capture White's stone number 6 by playing above it: So this move is suicide for Black, and it is allowed in the Logical Rules of Go which we are Note that in the rulesets used by most humans playing Go, suicide is forbidden. that is all information I received to code the method play_move further more I can provide the test cases and the expected output In the Board class, define a method play_move(self, colour_name, coords) that plays a move according rule \#7 (see Background Information). For coords and colour_name, see Task 2. If the point at the given coordinates is not empty, raise an AssertionError with message "itlegal move: point not empty". Expand Example Executing the following code 1 b = Board(9) 2 b.set_from_integer(123480537448700274288778724742285115148) 3 print(b) 4 b.play_move("B", "D5") 5 print(b) 6 b.play_move("W", "E1") 7 print(b) 8 b.play_move("B", "E7") 9 print(b) io b.play_move("W", "E5") il print(b) A B C D E F GH I 9.@0000@. 8.@@0@ 7 . 6... O@. 5.0@ 4.0 3.0 3 . 2.0000 1. . 0... A B C DEFGH I 9.@...@@. 8..@@.@. 7....@. 6.00. 50 e. (a 4..0 (a. 3 . 2.0000 1.0.0. A B C DEFGHI 9.@...@... 8.. (a). @... 7.0. 6.0.0 5.0.0 4..0 @. 3.2..0.000 1 ... 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
