Advertisement
ForestFox

Chess 2

Mar 22nd, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.11 KB | None | 0 0
  1. import symtable
  2.  
  3.  
  4. class Color(object):
  5. EMPTY = 0
  6. BLACK = 1
  7. WHITE = 2
  8.  
  9. @classmethod
  10. def invert(cls, color):
  11. if color == cls.EMPTY:
  12. return color
  13. return cls.BLACK if color == cls.WHITE else cls.WHITE
  14.  
  15.  
  16. class Empty(object):
  17. color = Color.EMPTY
  18.  
  19. def get_moves(self, board, y, x):
  20. raise Exception("Error !")
  21.  
  22. def __repr__(self):
  23. return ' . '
  24.  
  25.  
  26. class ChessMan(object):
  27. fig = None
  28.  
  29. def __init__(self, color):
  30. self.color = color
  31.  
  32. def __str__(self):
  33. return self.fig[0 if self.color == Color.WHITE else 1]
  34.  
  35. def enemy_color(self):
  36. return Color.invert(self.color)
  37.  
  38.  
  39. class Pawn(ChessMan):
  40. fig = (' P ', ' p ')
  41.  
  42. def get_moves(self, board, y, x):
  43. moves = []
  44. if self.color == Color.BLACK and y < 7:
  45. if board.get_color(y + 1, x) == Color.EMPTY:
  46. moves.append([y + 1, x])
  47. if board.get_color(y + 2, x) == Color.EMPTY:
  48. moves.append([y + 2, x])
  49. if x < 7 and board.get_color(y + 1, x + 1) == self.enemy_color():
  50. moves.append([y + 1, x + 1])
  51. if x > 1 and board.get_color(y + 1, x - 1) == self.enemy_color():
  52. moves.append([y + 1, x - 1])
  53.  
  54. elif self.color == Color.WHITE and y > 0:
  55. moves = []
  56. if board.get_color(y - 1, x) == Color.EMPTY:
  57. moves.append([y - 1, x])
  58. if board.get_color(y - 2, x) == Color.EMPTY:
  59. moves.append([y - 2, x])
  60. if x < 7 and board.get_color(y - 1, x + 1) == self.enemy_color():
  61. moves.append([y - 1, x + 1])
  62. if x > 1 and board.get_color(y - 1, x - 1) == self.enemy_color():
  63. moves.append([y - 1, x - 1])
  64.  
  65. return moves
  66.  
  67.  
  68. class King(ChessMan):
  69. fig = (' K ', ' k ')
  70.  
  71. def get_moves(self, board, y, x):
  72. moves = []
  73. for y2 in range(y - 1, y + 2):
  74. for x2 in range(x - 1, x + 2):
  75. if not y2 == x2 == 0:
  76. if board.get_color(y2, x2) != board.get_color(y, x):
  77. moves.append([y2, x2])
  78.  
  79. return moves
  80.  
  81.  
  82. class Knight(ChessMan):
  83. fig = (' N ', ' n ')
  84.  
  85. def get_moves(self, board, y, x):
  86. moves = []
  87. for y2 in range(y - 2, y + 3):
  88. for x2 in range(x - 2, x + 3):
  89. if (y2 - y) ** 2 + (x2 - x) ** 2 == 5 and 0 <= y2 <= 7 and 0 <= x2 <= 7:
  90. if board.get_color(y2, x2) != board.get_color(y, x):
  91. moves.append([y2, x2])
  92.  
  93. return moves
  94.  
  95.  
  96. class Bishop(ChessMan):
  97. fig = (' B ', ' b ')
  98.  
  99. def get_moves(self, board, y, x):
  100. moves = []
  101. p = [[1, 1], [-1, 1], [-1, -1], [1, -1]]
  102. for k in p:
  103. for i in range(1, 8):
  104. y1, x1 = y + i * k[1], x + i * k[0]
  105. if not (0 <= y1 <= 7) or not (0 <= x1 <= 7):
  106. break
  107. else:
  108. if board.get_color(y, x) != board.get_color(y1, x1):
  109. moves.append([y1, x1])
  110. if board.get_color(y1, x1) != Color.EMPTY:
  111. break
  112.  
  113. return moves
  114.  
  115.  
  116. class Rook(ChessMan):
  117. fig = (' R ', ' r ')
  118.  
  119. def get_moves(self, board, y, x):
  120. moves = []
  121. for x1 in range(x + 1, 8):
  122. if board.get_color(y, x) != board.get_color(y, x1):
  123. moves.append([y, x1])
  124. if board.get_color(y, x1) != Color.EMPTY:
  125. break
  126. for x1 in range(x - 1, -1, -1):
  127. if board.get_color(y, x) != board.get_color(y, x1):
  128. moves.append([y, x1])
  129. if board.get_color(y, x1) != Color.EMPTY:
  130. break
  131. for y1 in range(y + 1, 8):
  132. if board.get_color(y, x) != board.get_color(y1, x):
  133. moves.append([y1, x])
  134. if board.get_color(y1, x) != Color.EMPTY:
  135. break
  136. for y1 in range(y - 1, -1, -1):
  137. if board.get_color(y, x) != board.get_color(y1, x):
  138. moves.append([y1, x])
  139. if board.get_color(y1, x) != Color.EMPTY:
  140. break
  141. return moves
  142.  
  143.  
  144. class Queen(ChessMan):
  145. fig = (' Q ', ' q ')
  146.  
  147. def get_moves(self, board, y, x):
  148. moves = []
  149.  
  150. p = [[1, 1], [-1, 1], [-1, -1], [1, -1], [1, 0], [0, 1], [-1, 0], [0, -1]]
  151. for k in p:
  152. for i in range(1, 8):
  153. y1, x1 = y + i * k[1], x + i * k[0]
  154. if not (0 <= y1 <= 7) or not (0 <= x1 <= 7):
  155. break
  156. else:
  157. if board.get_color(y, x) != board.get_color(y1, x1):
  158. moves.append([y1, x1])
  159. if board.get_color(y1, x1) != Color.EMPTY:
  160. break
  161. return moves
  162.  
  163.  
  164. class Board(object):
  165. def __init__(self):
  166. self.board = [[Empty()] * 8 for i in range(8)]
  167.  
  168. def start(self):
  169. self.board = [[Empty()] * 8 for i in range(8)]
  170. for i in range(8):
  171. self.board[1][i] = Pawn(Color.BLACK)
  172. self.board[6][i] = Pawn(Color.WHITE)
  173. self.board[0][0] = Rook(Color.BLACK)
  174. self.board[7][0] = Rook(Color.WHITE)
  175. self.board[0][1] = Knight(Color.BLACK)
  176. self.board[7][1] = Knight(Color.WHITE)
  177. self.board[0][2] = Bishop(Color.BLACK)
  178. self.board[7][2] = Bishop(Color.WHITE)
  179. self.board[0][3] = Queen(Color.BLACK)
  180. self.board[7][3] = Queen(Color.WHITE)
  181. self.board[0][4] = King(Color.BLACK)
  182. self.board[7][4] = King(Color.WHITE)
  183. self.board[0][5] = Bishop(Color.BLACK)
  184. self.board[7][5] = Bishop(Color.WHITE)
  185. self.board[0][6] = Knight(Color.BLACK)
  186. self.board[7][6] = Knight(Color.WHITE)
  187. self.board[0][7] = Rook(Color.BLACK)
  188. self.board[7][7] = Rook(Color.WHITE)
  189.  
  190. # self.board[4][4] = Queen(Color.WHITE)
  191.  
  192. def get_color(self, y, x):
  193. return self.board[y][x].color
  194.  
  195. def get_moves(self, y, x):
  196. return self.board[y][x].get_moves(self, y, x)
  197.  
  198. def move(self, y1, x1, y2, x2):
  199. self.board[y2][x2] = self.board[y1][x1]
  200. self.board[y1][x1] = Empty()
  201.  
  202. def is_empty(self, y, x):
  203. self.board[y][x] = Empty()
  204.  
  205. def b_with_possibilities(self, y1, x1):
  206. c = self.board
  207. if self.board[y1][x1].color == Color.EMPTY:
  208. return c
  209. pos = self.get_moves(y1, x1)
  210.  
  211. for i in pos:
  212. y, x = i[0], i[1]
  213. c[y][x] = f"*{str(c[y][x]).replace(' ', '')}*"
  214.  
  215. return c
  216.  
  217. def __str__(self):
  218. res = '\n' + ' '.join([' A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) + '\n\n'
  219. for y in range(8, 0, -1):
  220. res = res + f"{y} {''.join(map(str, self.board[8 - y]))} {y} \n"
  221. res += "\n" + ' '.join([' A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) + '\n'
  222. return res
  223.  
  224.  
  225. class Game(Board):
  226. def __init__(self):
  227. b = Board()
  228. b.start()
  229. n = Note()
  230. # spec = SpecialRules()
  231. # self.board = b.board
  232. self.movies = 1
  233. self.player_color = Color.WHITE
  234. print(b)
  235. while True:
  236. if self.player_color == Color.WHITE and self.movies == 1:
  237. b.start()
  238. print(f"Ход {'белых' if self.player_color == Color.WHITE else 'черных'}")
  239. s = input()
  240.  
  241. if len(s) == 0:
  242. break
  243. elif s == 'сначала':
  244. b.start()
  245. self.player_color = Color.WHITE
  246. print(b)
  247.  
  248. elif s == 'запись':
  249. print(n.pretty_write())
  250.  
  251. elif 'вернуть на' in s:
  252. moves = int(s.split()[2])
  253. b = n.annul(b, moves)
  254. print(b)
  255. if moves % 2 != 0: self.player_color = Color.invert(self.player_color)
  256.  
  257. elif 'возможные ходы' in s:
  258. motion1 = self.inp_coord(s.split()[2])
  259. y, x = motion1[0], motion1[1]
  260. c = b
  261. # c = self.b_with_possibilities(b, y, x)
  262. c.b_with_possibilities(y, x)
  263. print(c)
  264. b = n.annul(b, 0)
  265. else:
  266. motion1, motion2 = s.split()
  267. print(f"Ход от {self.inp_coord(motion1)} в {self.inp_coord(motion2)}")
  268. # print(b.get_moves(*self.inp_coord(motion1)))
  269. checks = self.check(b, motion1, motion2)
  270. if checks == 'Можно':
  271. xy_from = self.inp_coord(motion1)
  272. xy_to = self.inp_coord(motion2)
  273. y1, x1 = xy_from[0], xy_from[1]
  274. y2, x2 = xy_to[0], xy_to[1]
  275.  
  276. n.read(y1, x1, y2, x2)
  277. b.move(y1, x1, y2, x2)
  278.  
  279. print(b)
  280. if self.player_color == Color.WHITE:
  281. self.movies += 1
  282. self.player_color = Color.BLACK
  283. else:
  284. self.player_color = Color.WHITE
  285. else:
  286. print(checks)
  287.  
  288. def check(self, b, xy_from, xy_to):
  289. if self.check_inp(b, xy_from, xy_to):
  290. motion1, motion2 = xy_from, xy_to
  291. xy_from = self.inp_coord(xy_from)
  292. xy_to = self.inp_coord(xy_to)
  293. y1, x1 = xy_from[0], xy_from[1]
  294. y2, x2 = xy_to[0], xy_to[1]
  295. if self.check_color(b, y1, x1, y2, x2):
  296. if self.check_move(b, y1, x1, y2, x2):
  297. return 'Можно'
  298. else:
  299. moves = ', '.join(self.return_coords(b.get_moves(y1, x1)))
  300. return f'У фигуры на {motion1} хода на {motion2} нет. Возможные ходы из {motion1}: {moves}'
  301. else:
  302. return 'Нельзя ходить пустой клеткой и чужой фигурой'
  303. else:
  304. return 'Такой клетки не существует'
  305.  
  306. def check_inp(self, b, xy_from, xy_to):
  307. if xy_from[0] in 'abcdefgh' and xy_from[1] in '12345678':
  308. if xy_to[0] in 'abcdefgh' and xy_to[1] in '12345678':
  309. return True
  310. return False
  311.  
  312. def check_color(self, b, y1, x1, y2, x2):
  313. return b.board[y1][x1].color == self.player_color and b.board[y2][x2].color != self.player_color
  314.  
  315. def check_move(self, b, y1, x1, y2, x2):
  316. return [y2, x2] in b.get_moves(y1, x1)
  317. # or SpecialRules().get_moves_spec(b, n, y1, x1)
  318.  
  319. def inp_coord(self, xy):
  320. s = "abcdefgh"
  321. return [8 - int(xy[1]), s.index(xy[0])]
  322.  
  323. def return_coord(self, y, x):
  324. y = 8 - y
  325. return f'{"abcdefgh"[x]}{y}'
  326.  
  327. def return_coords(self, m):
  328. k = []
  329. for i in m:
  330. k.append(self.return_coord(i[0], i[1]))
  331. return k
  332.  
  333. # def b_with_possibilities(self, b, y1, x1):
  334. # c = b.board
  335. # pos = b.get_moves(y1, x1)
  336. # for i in pos:
  337. # y, x = i[0], i[1]
  338. # c[y][x] = f'*{c[y][x]}*'
  339. # return c
  340.  
  341. class Note(Game):
  342. def __init__(self):
  343. self.notes = []
  344.  
  345. def read(self, y1, x1, y2, x2):
  346. self.notes.append([[y1, x1], [y2, x2]])
  347.  
  348. def coords(self, y, x):
  349. y = 8 - y
  350. return f'{"abcdefgh"[x]}{y}'
  351.  
  352. def pretty_write(self):
  353. k = ''
  354. n = self.notes
  355. for i in range(len(n)):
  356. ki = ''
  357. for j in range(2):
  358. xy = self.coords(n[i][j][0], n[i][j][1])
  359. ki += xy
  360. if i % 2 == 0:
  361. k = f"{k}{str((i + 2) // 2)}. {ki} |"
  362. else:
  363. k = f'{k} {ki} \n'
  364. return k
  365.  
  366. def annul(self, b, moves):
  367. n = self.notes
  368. b.start()
  369. for i in range(len(n) - moves):
  370. y1, x1, y2, x2 = n[i][0][0], n[i][0][1], n[i][1][0], n[i][1][1]
  371. b.move(y1, x1, y2, x2)
  372.  
  373. return b
  374.  
  375.  
  376.  
  377.  
  378.  
  379. Game()
  380.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement