Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

def init_board(num_rows, num_cols): Given two positive ints, create an empty boa

ID: 3595649 • Letter: D

Question

def init_board(num_rows, num_cols): Given two positive ints, create an empty board. Unlike the physical game, our boards can be any positive dimensions.

• Assume: num_rows and num_cols are positive integers.

• init_board(2,3) [['.', '.', '.'], ['.', '.', '.']]

def show_board(board): Given a board, create and return the string that, when printed, would print each row on consecutive lines, and each space in a row in consecutive characters on the line.

• Assume: board is a board.

• show_board(ex1) '...R... ..YRR.. .RYRYR. YYYRYYR '

• show_board([['.', '.', '.'] ,['A', 'B', 'A']]) "... ABA "

• example usage: >>> print(show_board([['.', '.', '.'] ,['A', 'B', 'A']]) ) ... ABA

def read_board(s): Given a string containing lines of either player pieces or periods, pick apart the string and generate the corresponding board. Blank lines must be ignored, but if the non-blank lines don't create a rectangular shape (different lines have different lengths) or if other characters show up (neither periods nor upper-case letters), return None instead of a generated board.

• Assume: s is a string.

• Reminder: only periods and upper-case letters are in valid boards, and valid boards are always rectangular. You must check both of these!

• read_board("... ABA ") [['.', '.', '.'] ,['A', 'B', 'A']]

• read_board(".. .. OK ") [['.', '.'], ['.', '.'], ['O', 'K']]

• read_board(".... .. NOO ") None #different-length rows

def get_size(board): Given a board, find the dimensions and return as a tuple: (numrows, numcols).

• Assume: board is a valid board.

• get_size([['.', '.', '.'], ['.', '.', '.']]) (2,3)

• get_size(ex3) (5,4)

def is_valid_coord(board, r, c): Given a board and two ints, do r and c describe a valid location on the board? Negative indexes are not allowed here (as a design decision).

• Assume: board is a valid board, r and c are integers.

• Hint: you should be using this function all over the place in the rest of your project!

• is_valid_coord([["B",".","R"],["R","R","B"]], 0, 0) True

• is_valid_coord([["B",".","R"],["R","R","B"]], 2, 3) False

• is_valid_coord([["B",".","R"],["R","R","B"]], -1,-1) False

Explanation / Answer

def init_board(num_rows, num_cols):
# create a new board
board = []
  
# add given number of rows
for i in range(num_rows):
# initialise each row with empty
row = ['.']*num_cols
board.append(row)
return board
# print(init_board(2, 3))

def show_board(board):
# create an empty string which will hold string representation of board
boardstr = ''
# for each row in board, craete its string representation
for row in board:
# string representation is just putting each cell one after another in row and then separating rows with new line
boardstr += ''.join(row) + ' '
# return string repreentation of board
return boardstr
# print(show_board([['.', '.', '.'], ['A', 'B', 'A']]))

def read_board(s):
# check if string is empty or None return None
if not s:
return None
  
# create an empty list which will hold all valid lines (non empty lines) in s
lines = []
  
# get all lines as a list by splitting lines on new line charatcer
tempLines = s.split(' ')
  
# for each line b=obtained by splitting keep only non empty lines
for line in tempLines:
# check if line is not None/empty
if line:
# append line to lines if it is non empty
lines.append(line)
  
# number of column will be entry in one line and all lines should have same length
col = len(lines[0])
row = 0
# for each lines
for i in range(len(lines)):
# check if length of line is different from other line, if yes return None
if len(lines[i]) != col:
return None
# if line is just white space don't consider it
if lines[i] == ' ':
continue
row += 1
# for each char in line
for c in lines[i]:
# check if char is in upper case letter, else its an invalid data
if not (c == '.' or c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
return None
  
# create an empty board as we now know exact number of rows and column
board = init_board(row, col)
  
# fill row and column from lines
for i in range(row):
for j in range(col):
board[i][j] = lines[i][j]
return board
# print(read_board(".... NOO "))

def get_size(board):
# check if board is not defined, if yes return None
if not board:
return None
# number of rows is length of outer list
row = len(board)
if row == 0:
return None
# length of column should be same for all column so taking out from first row
col = len(board[0])
return (row, col)
# print(get_size([['.', '.', '.'], ['A', 'B', 'A']]))

def is_valid_coord(board, r, c):
# a coordinate is valid if it is greater than equal to zero but strictl less than board size
if r < 0 or c < 0:
return False
(row, col) = get_size(board)
if r >= row or c >= col:
return False
  
return True
# print(is_valid_coord([["B", ".", "R"], ["R", "R", "B"]], 0, 0))
# print(is_valid_coord([["B", ".", "R"], ["R", "R", "B"]], 2, 3))
# print(is_valid_coord([["B", ".", "R"], ["R", "R", "B"]], -1, -1))

# copy pastable code link: https://paste.ee/p/ddcU0