Advertisement
MeowalsoMeow

Knight_tour_stage2

Aug 15th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. # Write your code here
  2.  
  3.  
  4. def check_dimension(_str):
  5. try:
  6. c, r = list(map(int, _str.split(' ')))
  7. except ValueError:
  8. return False # checks if there are too many given values and value error
  9. if any([r < 1, c < 1]):
  10. return False
  11. return [c, r]
  12.  
  13.  
  14. def check_position(_str, c, r):
  15. try:
  16. c_pos, r_pos = list(map(int, _str.split(' ')))
  17. except ValueError:
  18. return False
  19. if any([c_pos > c, r_pos > r]):
  20. # print(c, r, c_pos, r_pos)
  21. return False
  22. return [c_pos, r_pos]
  23.  
  24.  
  25. def print_board(c, r):
  26. global matrix
  27. for x in range(matrix[1]):
  28. print('---', end='')
  29. # print(' -------------------')
  30. print()
  31.  
  32. '''
  33. for y in range(r):
  34. mat[y][0] = r - y
  35. mat[y][1] = '|'
  36. mat[y][c - 1] = '|'
  37. '''
  38. # mat[8 - n][m - 1] = 'X'
  39. for i in range(r):
  40. print(f'{matrix[1] - i}|', end='')
  41. for j in range(c):
  42. print(mat[i][j], end=" ")
  43. print('|')
  44. for a in range(matrix[0]):
  45. print('---',end='')
  46. # print(' ---------------------')
  47. print()
  48. for n in range(matrix[0]):
  49. print(f' {n + 1}', end='')
  50.  
  51.  
  52. def move_knight(c, r):
  53. global mat
  54. global matrix
  55. mat[matrix[1] - r][c - 1] = 'X'
  56. # column is mat[?][c]
  57. # row is mat[r][?]
  58.  
  59.  
  60. while True:
  61. matrix = check_dimension(input("Enter your board dimensions: "))
  62. # matrix and position are lists of numbers
  63. if matrix:
  64. position = check_position(input("Enter the knight's starting position: "), matrix[0], matrix[1])
  65. if position:
  66. break
  67. else:
  68. print('Invalid position!')
  69. else:
  70. print('Invalid dimensions!')
  71. mat = [['_' for x in range(matrix[0])] for y in range(matrix[1])]
  72. move_knight(position[0], position[1])
  73. print_board(matrix[0], matrix[1])
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement