Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Write your code here
- def check_dimension(_str):
- try:
- c, r = list(map(int, _str.split(' ')))
- except ValueError:
- return False # checks if there are too many given values and value error
- if any([r < 1, c < 1]):
- return False
- return [c, r]
- def check_position(_str, c, r):
- try:
- c_pos, r_pos = list(map(int, _str.split(' ')))
- except ValueError:
- return False
- if any([c_pos > c, r_pos > r]):
- # print(c, r, c_pos, r_pos)
- return False
- return [c_pos, r_pos]
- def print_board(c, r):
- global matrix
- for x in range(matrix[1]):
- print('---', end='')
- # print(' -------------------')
- print()
- '''
- for y in range(r):
- mat[y][0] = r - y
- mat[y][1] = '|'
- mat[y][c - 1] = '|'
- '''
- # mat[8 - n][m - 1] = 'X'
- for i in range(r):
- print(f'{matrix[1] - i}|', end='')
- for j in range(c):
- print(mat[i][j], end=" ")
- print('|')
- for a in range(matrix[0]):
- print('---',end='')
- # print(' ---------------------')
- print()
- for n in range(matrix[0]):
- print(f' {n + 1}', end='')
- def move_knight(c, r):
- global mat
- global matrix
- mat[matrix[1] - r][c - 1] = 'X'
- # column is mat[?][c]
- # row is mat[r][?]
- while True:
- matrix = check_dimension(input("Enter your board dimensions: "))
- # matrix and position are lists of numbers
- if matrix:
- position = check_position(input("Enter the knight's starting position: "), matrix[0], matrix[1])
- if position:
- break
- else:
- print('Invalid position!')
- else:
- print('Invalid dimensions!')
- mat = [['_' for x in range(matrix[0])] for y in range(matrix[1])]
- move_knight(position[0], position[1])
- print_board(matrix[0], matrix[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement