Question
Currently working on this task, would really appreciate some guidance. Attached below is the description of the task: At the moment, my code is still
Currently working on this task, would really appreciate some guidance.
Attached below is the description of the task:
At the moment, my code is still not working for the following test cases, so I am wondering about the possible steps I should take to make this work:
Here is my current 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)]) + ' ' 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(): points = {'E': '.', 'B': '@', 'W': 'O'}
def __init__(self, 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 lines.append(line) else: 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) 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
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 with letters and numbers 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: "." 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 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 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. 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 ". @0". Check the validity of from_strings and raise AssertionError s 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 method. 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 "0" 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(). expected assertion 'invalid coordinates' but got 'ord() expected string of length 1 , but int found' Task 4, examples Illegal board size: must be between 2 and 26Step 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