Question
https://www.chegg.com/homework-help/questions-and-answers/issue-code-expected-output-returned-m-new-python-exercise-m-supposed-create-game-class-nee-q107110553?new=true in relation to this if possible can you make the corrections on my code cause i've gotton very confused right now and i keep
https://www.chegg.com/homework-help/questions-and-answers/issue-code-expected-output-returned-m-new-python-exercise-m-supposed-create-game-class-nee-q107110553?new=true in relation to this if possible can you make the corrections on my code cause i've gotton very confused right now and i keep on messing up and how can i request for further assistance cause i have more questions the following is my exercise for which i have created the code Task 1 In the file go.py, implement a class Board representing a square Go board. The constructor should take an optional integer parameter size with a default value of 19 to indicate the size of the board. The size must be between 2 and 26, inclusive. If this is not the case, raise an AssertionError with the error message "Illegal board size: must be between 2 and 26.". The constructor should create a square board of the given size with all points initialised as empty. Implement the method get_size(self) that returns the size of the board. Implement the method __str__(self) to convert a Board to a string that when printed gives an "ASCII art" representation of the board with coordinates marked as letters along the columns and rows. Click on the "Expand" button below for an example. Use the following characters to represent the points on the board empty: "." black: "@" white: "O" Task 2 Implement a method set_colour(self, coords, colour_name) that changes the colour at the coordinates given by coords. coords must be a string of two lower case letters where the first letter specifies the column and the second letter specifies the row. colour_name must be one of the strings "empty", "black", "white". If coords is not a string of length 2, raise an AssertionError with the string "invalid coordinates". If the letter for 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, corrds) 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. Task 3 Extend the constructor of class Board to accept an additional optional argument from_strings. If present, this argument must be a list of strings where each string represents a row of the board with the character encoding used in the __str__() method, but without the intervening spaces and the coordinate letters. For example, for a board of size 19, from_strings must be a list of 19 strings, each having exactly 19 characters from the set ".@O". Check the validity of from_strings and raise AssertionErrors with the following messages for invalid inputs (the letter x should indicate the row coordinate of the invalid row): "input is not a list" "length of input list does not match size" "row x is not a string" "length of row x does not match size" "invalid character in row x" Implement a method to_strings(self) that returns a representation of the board as a list of strings in the same format as that accepted by the __init__() method. Task 4 Outside the class, define a function load_board(filename) that takes as an argument the name of a text file, reads a Go position from that file and returns a Board object representing the position. The file must contain one line for each row of the board, using the characters ".", "@", and "O" as above to represent the colours. Define a function save_board(filename, board) that takes as arguments a filename and a Board object and saves the position to a text file in the same format as that accepted by load_board(). Task 5 In class Board, define two new methods: to_integer(self) returns an integer encoding of the position set_from_integer(self, integer_encoding) sets the colours of all points of the board according to the given integer_encoding. This doesnt change the size of the board! The integer encoding of a board position is based on the ternary number system. The ternary system uses the digits 00, 11, and 22 to represent numbers as follows. If d_0, d_1, \ldots, d_n \in \{0, 1, 2\}d0,d1,,dn{0,1,2} are ternary digits, then n = d_n \cdot 3^n + d_{n-1} \cdot 3^{n-1} + \cdots + d_0 \cdot 3^0n=dn3n+dn13n1++d030 is the integer represented by the sequence (d_n d_{n-1}\cdots d_0)_3(dndn1d0)3 of digits. For example, (21)_3 = 2\cdot 3 + 1 = 7(21)3=23+1=7 and (210)_3 = 2 \cdot 9 + 1 \cdot 3 + 0 = 21(210)3=29+13+0=21. If we replace the colours of the points of the Go board by digits (empty = 0, black = 1, white = 2) and concatenate all rows of the board to a single row of digits, we get a ternary number that can then be converted to an integer. The method to_integer(self) is supposed to do exactly that. For example, the 3 \times 333 board used in the example for Task 2 looked like this: a b c a O . O b . @ . c @ O . Reading this off as ternary digits gives us (202010120)_3(202010120)3, which equals 14676 in decimal. Hint: To convert an integer nn to ternary representation, as required by set_from_integer(), note that you get the last digit by computing n \bmod 3nmod3. You get the next digit by computing \lfloor n/3 floor \bmod 3n/3mod3 and so on. For example, heres how to get the last two digits of the ternary representation of 14676: 14676 \bmod 3 = \textbf{0}14676mod3=0. 14676/3 = 489214676/3=4892. 4892 \bmod 3 = \textbf{2}4892mod3=2. Task 6 In the Board class, define a method fill_reaching(self, reach_name, colour_name, visited, r, c) that receives the following parameters: reach_name: the name of a colour ("empty", "black", or "white") to be tested for reachability colour_name: the name of the colour of the chain to be marked 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 The method 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. The method must not modify the Board object. You have implemented the flood fill algorithm in W11.3 Post-Class. This is a variation of that algorithm that uses a different matrix to mark visited points and also returns whether reach_name was touched at any time during filling. 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 in the background information) and False otherwise. Rule 3 : A point PP, not coloured CC, is said to reach CC, if there is a path of (vertically or horizontally) adjacent points of PPs colour from PP to a point of colour CC
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started