Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. [['___' for z in range(x)] for z in range(x)] # z is 8 in this instance
  2.  
  3. a b c d e f g h
  4.  
  5. 8 ['___', '___', '___', '___', '___', '___', '___', '___'] 8
  6.  
  7. 7 ['___', '___', '___', '___', '___', '___', '___', '___'] 7
  8.  
  9. 6 ['___', '___', '___', '___', '___', '___', '___', '___'] 6
  10.  
  11. 5 ['___', '___', '___', '___', '___', '___', '___', '___'] 5
  12.  
  13. 4 ['___', '___', '___', '___', '___', '___', '___', '___'] 4
  14.  
  15. 3 ['___', '___', '___', '___', '___', '___', '___', '___'] 3
  16.  
  17. 2 ['___', '___', '___', '___', '___', '___', '___', '___'] 2
  18.  
  19. 1 ['___', '___', '___', '___', '___', '___', '___', '___'] 1
  20.  
  21. a b c d e f g h
  22.  
  23. def create(self):
  24. Config.board[self.y][self.x] = self.pieceid
  25.  
  26. a b c d e f g h
  27.  
  28. 8 ['___', '___', '___', '___', '___', '___', '___', '___'] 8
  29.  
  30. 7 ['___', '___', '___', '___', '___', '___', '___', '___'] 7
  31.  
  32. 6 ['___', '___', '___', '___', '___', '___', '___', '___'] 6
  33.  
  34. 5 ['___', '___', '___', '___', '___', '___', '___', '___'] 5
  35.  
  36. 4 ['___', '___', '___', '___', '___', '___', '___', '___'] 4
  37.  
  38. 3 ['___', '___', '___', '___', '___', '___', '___', '___'] 3
  39.  
  40. 2 ['___', '___', '___', '___', '___', '___', '___', '___'] 2
  41.  
  42. 1 ['___', '___', '___', '___', 'wN1', '___', '___', '___'] 1
  43.  
  44. a b c d e f g h
  45.  
  46. def possible_moves(self):
  47. pos_moves = []
  48.  
  49. # Up, Right (1 space, 2 spaces)
  50. try:
  51. if 1 <= self.x + 2 <= len(Config.board) and 1 <= self.y - 1 <= len(Config.board):
  52. if Config.board[self.x + 2][self.y - 1] == '___':
  53. pos_moves.append(f'{Config.tile_convert(self.x + 2)}{Config.tile_convert(self.y - 1, True)}')
  54. except IndexError: pass
  55.  
  56. #Up, Left
  57. try:
  58. if 1 <= self.x - 2 <= len(Config.board) and 1 <= self.y - 1 <= len(Config.board):
  59. if Config.board[self.x - 2][self.y - 1] == '___':
  60. pos_moves.append(f'{Config.tile_convert(self.x - 2)}{Config.tile_convert(self.y - 1, True)}')
  61. except IndexError: pass
  62.  
  63. # Down, Right
  64. try:
  65. if 1 <= self.x + 2 <= len(Config.board) and 1 <= self.y + 1 <= len(Config.board):
  66. if Config.board[self.x + 2][self.y + 1] == '___':
  67. pos_moves.append(f'{Config.tile_convert(self.x + 2)}{Config.tile_convert(self.y + 1, True)}')
  68. except IndexError: pass
  69.  
  70. #Down, Left
  71. try:
  72. if 1 <= self.x - 2 <= len(Config.board) and 1 <= self.y + 1 <= len(Config.board):
  73. if Config.board[self.x - 2][self.y + 1] == '___':
  74. pos_moves.append(f'{Config.tile_convert(self.x - 2)}{Config.tile_convert(self.y + 1, True)}')
  75. except IndexError: pass
  76.  
  77. # Right, Up
  78. try:
  79. if 1 <= self.x + 1 <= len(Config.board) and 1 <= self.y - 2 <= len(Config.board):
  80. if Config.board[self.x + 1][self.y - 2] == '___':
  81. pos_moves.append(f'{Config.tile_convert(self.x + 1)}{Config.tile_convert(self.y - 2, True)}')
  82. except IndexError: pass
  83.  
  84. # Right, Down
  85. try:
  86. if 1 <= self.x + 1 <= len(Config.board) and 1 <= self.y + 2 <= len(Config.board):
  87. if Config.board[self.x + 1][self.y + 2] == '___':
  88. pos_moves.append(f'{Config.tile_convert(self.x + 1)}{Config.tile_convert(self.y + 2, True)}')
  89. except IndexError: pass
  90.  
  91. #Left, Up
  92. try:
  93. if 1 <= self.x - 1 <= len(Config.board) and 1 <= self.y - 2 <= len(Config.board):
  94. print('Current: ', self.x, self.y)
  95. print('New: ', self.x - 1, self.y - 2)
  96. if Config.board[self.x - 1][self.y - 2] == '___':
  97. pos_moves.append(f'{Config.tile_convert(self.x - 1)}{Config.tile_convert(self.y - 2, True)}')
  98. except IndexError: pass
  99.  
  100. # Left, Down
  101. try:
  102. if 1 <= self.x - 1 <= len(Config.board) and 1 <= self.y + 2 <= len(Config.board):
  103. if Config.board[self.x - 1][self.y + 2] == '___':
  104. pos_moves.append(f'{Config.tile_convert(self.x - 1)}{Config.tile_convert(self.y + 2, True)}')
  105. except IndexError: pass
  106.  
  107. return pos_moves
  108.  
  109. def move(self, pos):
  110. if pos in self.possible_moves():
  111. Config.board[self.y][self.x] = '___'
  112.  
  113. self.x = Config.tile_convert(pos[0])
  114. self.y = Config.tile_convert(pos[1], True)
  115.  
  116. Config.board[self.y][self.x] = self.pieceid
  117.  
  118. knight1.move('f3')
  119. time.sleep(2)
  120. knight1.move('g5')
  121. time.sleep(2)
  122. knight1.move('h7')
  123. time.sleep(2)
  124. print(knight1.possible_moves()) # f8 should be in here
  125. knight1.move('f8') # fails
  126.  
  127. import time
  128.  
  129. class Config:
  130. letters = tuple('abcdefghijklmnopqrstuvwxyz')
  131.  
  132.  
  133. @classmethod
  134. def new_board(cls, btype):
  135. def size(x):
  136. return [['___' for z in range(x)] for z in range(x)]
  137.  
  138. if 'custom' in btype.lower():
  139. btype = int(btype.replace('custom', '').strip())
  140. cls.board = size(btype)
  141. elif btype.lower() == 'default':
  142. cls.board = size(8)
  143. elif btype.lower() == 'extended':
  144. cls.board = size(10)
  145. elif btype.lower() == 'small':
  146. cls.board = size(5)
  147. elif btype.lower() == 'max':
  148. cls.board = size(25)
  149. elif btype.lower() == 'min':
  150. cls.board = size(1)
  151.  
  152. @classmethod
  153. def print_board(cls):
  154. def printl():
  155. for x in range(len(cls.board)):
  156. print(' '*6 + f'{cls.letters[x]}', end='')
  157. print('n')
  158.  
  159. printl()
  160. for x in range(len(cls.board)):
  161. print(f'{len(cls.board)-x} {cls.board[x]} {len(Config.board)-x}n')
  162. printl()
  163. print('n'*4)
  164.  
  165. @classmethod
  166. def tile_convert(cls, x, disp=False):
  167. if not disp:
  168. if isinstance(x, str):
  169. return cls.letters.index(x)
  170. else:
  171. return cls.letters[x]
  172. else:
  173. return len(Config.board) - int(x)
  174.  
  175. class ChessPiece:
  176. def __init__(self, pos, color, num, piece):
  177. self.x = int(Config.tile_convert(pos[0]))
  178. self.y = len(Config.board) - int(pos[1])
  179. self.color = color
  180. self.pieceid = num
  181. self.set_id()
  182. self.create()
  183.  
  184. def __str__(self):
  185. return self.__class__.__name__
  186.  
  187. def set_id(self):
  188. if self.__class__.__name__ != "Knight":
  189. self.pieceid = f'{piece[0]}{self.pieceid}'
  190. else:
  191. self.pieceid = f'N{self.pieceid}'
  192.  
  193. if self.color is not None:
  194. if self.color.lower() in ('black', 'white', 'b', 'w'):
  195. self.pieceid = self.color.lower()[0] + self.pieceid
  196. if self.color.lower() == 'b':
  197. self.color = 'black'
  198. elif self.color.lower() == 'w':
  199. self.color = 'white'
  200. else:
  201. self.color = None
  202. print("Invalid color input. Color not set.")
  203. self.set_id()
  204. else:
  205. self.pieceid = '_' + self.pieceid
  206.  
  207.  
  208. def create(self):
  209. Config.board[self.y][self.x] = self.pieceid
  210.  
  211. def move(self, pos):
  212. if pos in self.possible_moves():
  213. Config.board[self.y][self.x] = '___'
  214.  
  215. self.x = Config.tile_convert(pos[0])
  216. self.y = Config.tile_convert(pos[1], True)
  217.  
  218. Config.board[self.y][self.x] = self.pieceid
  219.  
  220. Config.print_board()
  221.  
  222. else:
  223. print(f'Unable to move to {pos}')
  224.  
  225.  
  226. def get_info(self):
  227. print(f'{self.__class__.__name__}:n')
  228. print('ID: ', self.pieceid)
  229. print('Position: ', Config.tile_convert(self.x), Config.tile_convert(self.y, True), sep='')
  230. print('Color: ', self.color)
  231.  
  232.  
  233. class Knight(ChessPiece):
  234. def __init__(self, pos, color=None, num=''):
  235. ChessPiece.__init__(self, pos, color, num, self.__class__.__name__)
  236.  
  237.  
  238. def possible_moves(self):
  239. pos_moves = []
  240.  
  241. # Up, Right (1 space, 2 spaces)
  242. try:
  243. if 1 <= self.x + 2 <= len(Config.board) and 1 <= self.y - 1 <= len(Config.board):
  244. if Config.board[self.x + 2][self.y - 1] == '___':
  245. pos_moves.append(f'{Config.tile_convert(self.x + 2)}{Config.tile_convert(self.y - 1, True)}')
  246. except IndexError: pass
  247.  
  248. #Up, Left
  249. try:
  250. if 1 <= self.x - 2 <= len(Config.board) and 1 <= self.y - 1 <= len(Config.board):
  251. if Config.board[self.x - 2][self.y - 1] == '___':
  252. pos_moves.append(f'{Config.tile_convert(self.x - 2)}{Config.tile_convert(self.y - 1, True)}')
  253. except IndexError: pass
  254.  
  255. # Down, Right
  256. try:
  257. if 1 <= self.x + 2 <= len(Config.board) and 1 <= self.y + 1 <= len(Config.board):
  258. if Config.board[self.x + 2][self.y + 1] == '___':
  259. pos_moves.append(f'{Config.tile_convert(self.x + 2)}{Config.tile_convert(self.y + 1, True)}')
  260. except IndexError: pass
  261.  
  262. #Down, Left
  263. try:
  264. if 1 <= self.x - 2 <= len(Config.board) and 1 <= self.y + 1 <= len(Config.board):
  265. if Config.board[self.x - 2][self.y + 1] == '___':
  266. pos_moves.append(f'{Config.tile_convert(self.x - 2)}{Config.tile_convert(self.y + 1, True)}')
  267. except IndexError: pass
  268.  
  269. # Right, Up
  270. try:
  271. if 1 <= self.x + 1 <= len(Config.board) and 1 <= self.y - 2 <= len(Config.board):
  272. if Config.board[self.x + 1][self.y - 2] == '___':
  273. pos_moves.append(f'{Config.tile_convert(self.x + 1)}{Config.tile_convert(self.y - 2, True)}')
  274. except IndexError: pass
  275.  
  276. # Right, Down
  277. try:
  278. if 1 <= self.x + 1 <= len(Config.board) and 1 <= self.y + 2 <= len(Config.board):
  279. if Config.board[self.x + 1][self.y + 2] == '___':
  280. pos_moves.append(f'{Config.tile_convert(self.x + 1)}{Config.tile_convert(self.y + 2, True)}')
  281. except IndexError: pass
  282.  
  283. #Left, Up
  284. try:
  285. if 1 <= self.x - 1 <= len(Config.board) and 1 <= self.y - 2 <= len(Config.board):
  286. print('Current: ', self.x, self.y)
  287. print('New: ', self.x - 1, self.y - 2)
  288. if Config.board[self.x - 1][self.y - 2] == '___':
  289. pos_moves.append(f'{Config.tile_convert(self.x - 1)}{Config.tile_convert(self.y - 2, True)}')
  290. except IndexError: pass
  291.  
  292. # Left, Down
  293. try:
  294. if 1 <= self.x - 1 <= len(Config.board) and 1 <= self.y + 2 <= len(Config.board):
  295. if Config.board[self.x - 1][self.y + 2] == '___':
  296. pos_moves.append(f'{Config.tile_convert(self.x - 1)}{Config.tile_convert(self.y + 2, True)}')
  297. except IndexError: pass
  298.  
  299. return pos_moves
  300.  
  301.  
  302. Config.new_board('default')
  303. Config.print_board()
  304.  
  305. knight1 = Knight('e1', color='w', num=1)
  306.  
  307. Config.print_board()
  308.  
  309. # knight1.get_info()
  310.  
  311. # print('nnnPossible Moves:', knight1.possible_moves())
  312.  
  313. knight1.move('f3')
  314. time.sleep(2)
  315. knight1.move('g5')
  316. time.sleep(2)
  317. knight1.move('h7')
  318. time.sleep(2)
  319. print(knight1.possible_moves())
  320. knight1.move('f8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement