Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Hi can you please provide the python codes for the following methods based on my code so that it can be directly added to my

Hi can you please provide the python codes for the following methods based on my code so that it can be directly added to my code without an error my code is shown after the tasks

task 9 - description and test cases

image text in transcribed

task 9 - expected output

image text in transcribed

task 10 - description and test cases

image text in transcribed

task 10 - expected output

image text in transcribed

image text in transcribed

task 11- description and test cases

image text in transcribed

task 11 - expected output

image text in transcribed

image text in transcribed

task 12 - description and test cases

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

task 12 - expected output

image text in transcribed

image text in transcribed

image text in transcribed

the game rules must be these not any other rule

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

can someone help me out by modifying my code based on the changes required and please ensure that all the test cases meet the expected output. Please make sure that the updated code would pass all the test cases giving the appropriate output. Please don't provide a sample code or something similar I have wasted most of my questions by getting them please modify my code to get the expected this is a code for a game of go in python. the exercise description and expected output of the test cases will be provided below the code. please modify the code and provide the modified version without errors as I have tried as much as i can but i keep on messing it up

my code : 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)) 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 % self.size #3 list1.append(rem) n = n // self.size#3 list1.reverse() list1=["." if item ==0 else item for item in list1]

can someone help me out by modifying my code based on the changes required and please ensure that all the test cases meet the expected output. Please make sure that the updated code would pass all the test cases giving the appropriate output. Please don't provide a sample code or something similar I have wasted most of my questions by getting them please modify my code to get the expected this is a code for a game of go in python. the exercise description and expected output of the test cases will be provided below the code. please modify the code and provide the modified version without errors as I have tried as much as i can but i keep on messing it up 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)) 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 % self.size #3 list1.append(rem) n = n // self.size#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),self.size):#3 p.append(list1[i:i+self.size])#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 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

task 2

image text in transcribed

image text in transcribed

In the Board class, define a method clear_colour(self, colour_name) that clears the given colour ( "B" or " W ") according to rule \#4 (see Background information). Expand Example Executing the following code I'b=Board(9) 2 b.set_from_integer (123480537448700274361724717496626688035) 3 print(b) 4 print(b.is_legal()) 5 b.clear_colour("B") 6 print(b) 7 print(b.is_legal()) should print A BCDEFGH I 9.@OOO@... 8..@@0@... 7.. 60...... 5..0@0 @... 4 ..O 0 @... 3 ...... 2..0000 1.0.0@@@ Task 10 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 "illegal 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) 10 b.play_move("W", "E5") 11 print(b) should print A BCDEFGH I 9. @0000@... 8. @@0 @... 7. 6..00. . . . 5. o. o@.. 4.:0 (a.... 3 2:.0000 1. @ @@e A B CDEFG H I 9. @0 00 @..... 8.. @@0@... 5.0. 0 (a). . 4.0. . . . . 320000 1... @@e @ A BCDEFGHI 9. @0000@. . 8 . @@0 . . . 7 6..0.0@... 50 a . 4 . 0 @... 10. A B C DEFGHI 9.@. (a. @. 8. (a).@. . . 50 @. 4.0@. 100. A B C D E GH I 9. (a. . @. . . 8. @ @ @. . 7 ( . . . 600. 50.0 (a. 4.0. 0. 32.00000 1..0. In the Board class, define a method score (self) that returns the score difference between White and Black, i.e. White's score minus Black's score according to rule \#9 (see Background Information). Expand Example Executing the following code 1 b = Board(9) 2 b.set_from_integer( 618162000026984091812681185497885 ) 3 print(b) 4 print(b.score()) 5 b.set_colour("I5", "E") 6 print(b) 7 print(b.score()) 8 b.set_colour("E2", "E") 9 print(b) print(b.score()) should print A B C D EFGH I 9 . . . . . 8...@.@... 7..@.@@@@@ 6@@@@0 0@0 5 @@0 oo 0 @. 3.0.000 2 . . . 0.. 1 ...... 4 It is now time to put everything together. Implement a class Game that simulates a game of Go according to the Logical Rules (see Background Information). The constructor should take three optional arguments: - an integer specifying the board size, default 19 - a string with the name of the player playing black, default "Black" - a string with the name of the player playing white, default "White" When converted to a string, a Game object should appear in one of the following forms, depending on the state of the game: Game [White] vs. [Biack], [colour] to play Game [White] vs. [Black], B+[score]> Game [White] vs. [Black], B+R \&Game [White] vs. [Black], W+ [score] > KGame [White] vs. [Black], W+R> KGame [White] vs. [Black], jigo> Here, [White] stands for the name of the player playing white, [Black] stands for the name of the player playing black, [colour] is one of black, white and [score] is a positive integer. B+ indicates that Black won, W+ indicates that White won, the letter R indicates a win by resignation and the term jigo is used for a tie. Implement a method get_board(self) that returns a Board object with the current position. Implement a method get_score(self) that returns the score difference, (white - black). If the game has not yet ended, raise an AssertionError with the message "game still in progress". Implement methods - play_move(self, colour_name, coords) - to play a move - play_pass(self, colour_name) - to pass - play_resign(self, colour_name) - to resign For coords and colour_name, see the corresponding methods of class Board above. All methods should raise AssertionError s with the message "invalid move: game has ended" if the game has already ended and with the message "invalid move: it's [colour]'s turn" if the wrong player tries to make a move. If a move would repeat an earlier grid colouring (see rule \#6), raise an AssertionError with message "invalid move: positional superko rule violated". Executing the following code 1 g =Game(9, "Tom", "Harald 2 print(g) 3 g.play_move("B", "G3") 4 print(g) 5 g.play_move("W", "C6") 6 g.play_move("B", "F6") 7 g.play_move("W", "E3") 8 g.play_move("B", "D7") 9 g.play_move("W", "G4") 10 g.play_move("B", "F4") 11 g.play_move("W", "G5") 12 g.play_move("B", "F5") 13 g.play_move("W", "F3") 14 g.play_move("B", "H3") \( \begin{array}{ll}14 & \text { g.play_move("B", "H3") } \\ 15 & \text { g.play_move("W", "H7") } \\ 16 & \text { g.play_move("B", "F2") } \\ 17 & \text { g.play_move("W", "F8") } \\ 18 & \text { g.play_move("B", "D3") } \\ 19 & \text { g.play_move("W", "E4") } \\ 20 & \text { g.play_move("B", "D4") } \\ 21 & \text { g.play_move("W", "D6") } \\ 22 & \text { g.play_move("B", "E5") } \\ 23 . \text { g.play_move("W", "E7") } \\ 24 & \text { g.play_move("B", "H5") } \\ 25 \text { g.play_move("W", "G6") } \\ 26 \text { g.play_move("B", "B5") } \\ 27 \text { g.play_move("W", "C5") }\end{array} \) g.play_move("W", "C5") 28 g.play_move("B", "C4") 29 g.play_move("W", "B6") 30 g.play_move("B", "B4") 31 g.play_move("W", "F7") 32 g.play_move("B", "H6") 33 g.play_move("W", "H4") 34 g.play_move("B", "I4") 35 g.play_move("W",, "G7") 36 g.play_move("B", "I3") 37 g.play_move("W", "A5") 38 g.play_move("B", "A4") 39 g.play_move ("W", "A6") 40 g.play_move("B", "D5") 38. g.play_move("B", "A4") 39 g.play_move("W", "A6") 40.g.play_move("B", "D5") 41 g.play_move("W", "I6") 42 g.play_move( "B", "I5") 43 g.play_move("W", "I7") 44 g.play_move("B", "E6") 45 g.play_move("W", "D8") 46 g.play_move("B", "E2") 47 g.play_move("W", "C7") 48 g.play_pass("B") 49 g.play_pass( "W") 50 print(g.get_board()) 51 print(g) should print 8.00.. 7.0000000 600000@@0@0 50 0@@@0@@ 4 @@@.@oo@ 3...@. @@@ 2.... @@... 1 . . . ... Game Harald vs. Tm, B+5> 1g= Game (9) 2 g.play_move("B", "C5") 3 g.play_move("W", "D5") 4 g.play_move("B", "D6") 5 g.play_move ("W", "E6") 6 g.play_move("B", "D4") 7. g.play_move("W", "E4") 8 g.play_move("B", "E5") 9.g.play_move( "W", "F5") 10 g.play_move("B", "FG") 11 g.play_move("W", "D5") 12 print(g) 13 print(g.get_board()) 14 g.play_move("B", "E5") should print A B C D E F G H I 9 8. 7 . . . . . . . . 6 . . @ 0 @... 5. . @.0. 4. @ 0. 3 2 1 [] Assertionerror: invalid move: positional superko rule violated 1. Go is played on a 1919 square grid of points, by two players called Black and White. 2. Each point on the grid may be coloured black, white or empty. 3. A point P, not coloured C, is said to reach C, if there is a path of (vertically or horizontally) adjacent points of P s colour from P to a point of colour C. 4. Clearing a colour is the process of emptying all points of that colour that don't reach empty. 5. Starting with an empty grid, the players alternate turns, starting with Black. 6. A turn is either a pass; or a move that doesn't repeat an earlier grid colouring. 7. A move consists of colouring an empty point one's own colour; then clearing the opponent colour, and then clearing one's own colour. 8. The game ends after two consecutive passes. 9. A player's score is the number of points of their colour, plus the number of empty points that reach or their colour. 10. The player with the higher score at the end of the game is the winner. Equal scores result in a tie. In the above position, clearing black doesn't change anything because all black points reach empty. Clearing white results in the following position, which is legal: 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. Here's another example for the application of rule \#7. Consider this position: Here, all black stones reach empty, namely the point marked by the triangle. If Black now plays on that point, rule \#7 states that first White is cleared (which results in removing nothing, because each white stone reaches empty) and then Black is cleared. This removes all black stones because none of them reach empty after the move: \#7 states that first White is cleared (which results in removing nothing, because each white stone reaches empty) and then Black is cleared. This removes all black stones because none of them reach empty after the move: So this move is suicide for Black, and it is allowed in the Logical Rules of Go which we are using in this assignment. Note that in the rulesets used by most humans playing Go, suicide is forbidden. Implement a method set_colour (self, coords, colour_name) that changes the colour at the coordinates given by coords. coords must be a string consisting of an upper case letter specifying the column followed by an integer specifying the row. colour_name must be one of the strings "E" (for empty), "B". (for black), "W" (for white). If coords is not a string of length 2 or 3 , raise an AssertionError with the string "invalid coordinates". If column or row are out of range, raise an Assertionerror with the message "column out of range" or "row out of range", respectively. If the colour name is invalid, raise an Assertionerror with the message "invalid colour name". Implement a method get_colour (self, coords) that returns the colour of the point at the given coordinates. Coordinates and colour names are as described above. Check for valid coordinates as above. will print A B C D E 5 4 3 2 1.0@@. B A B C D E 5 43 1.0.@. Traceback (most recent call last): [...] AssertionError: invalid colour name

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

What are the purposes of promotion ?

Answered: 1 week ago