Advertisement
ForestFox

Checkers

Mar 22nd, 2022
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.23 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 King(ChessMan):
  40.     pass
  41.  
  42.  
  43. class Checker(ChessMan):
  44.     fig = (' O ', ' o ')
  45.  
  46.     def get_moves(self, board, y, x):
  47.         moves = []
  48.         p = [[1, 1], [-1, 1], [-1, -1], [1, -1]]
  49.         # просто сходить
  50.         for k in p:
  51.             y1, x1 = y + k[1], x + k[0]
  52.             if 0 <= y1 <= 7 and 0 <= x1 <= 7:
  53.                 if board.get_color(y1, x1) == Color.EMPTY:
  54.                     moves.append([y1, x1])
  55.  
  56.         # ход срубить
  57.         for k in p:
  58.             y1, x1 = y + 2 * k[1], x + 2 * k[0]
  59.             y2, x2 = y + k[1], x + k[0]
  60.             if 0 <= y1 <= 7 and 0 <= x1 <= 7:
  61.                 if board.get_color(y1, x1) == Color.EMPTY:
  62.                     if board.get_color(y2, x2) == self.enemy_color():
  63.                         moves.append([y1, x1])
  64.  
  65.         return moves
  66.  
  67.  
  68. class Board(object):
  69.     def __init__(self):
  70.         self.board = [[Empty()] * 8 for i in range(8)]
  71.  
  72.     def start(self):
  73.         self.board = [[Empty()] * 8 for i in range(8)]
  74.         for i in range(8):
  75.             self.board[1][i] = Pawn(Color.BLACK)
  76.             self.board[6][i] = Pawn(Color.WHITE)
  77.         for i in range(8):
  78.             for j in range(8):
  79.                 self.
  80.  
  81.     def get_color(self, y, x):
  82.         return self.board[y][x].color
  83.  
  84.     def get_moves(self, y, x):
  85.         return self.board[y][x].get_moves(self, y, x)
  86.  
  87.     def move(self, y1, x1, y2, x2):
  88.         self.board[y2][x2] = self.board[y1][x1]
  89.         if (y1-y2) ** 2 + (x1-x2) ** 2 == 4:
  90.  
  91.         self.board[y1][x1] = Empty()
  92.  
  93.     def is_empty(self, y, x):
  94.         self.board[y][x] = Empty()
  95.  
  96.     def __str__(self):
  97.         res = '\n' + '  '.join(['   A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) + '\n\n'
  98.         for y in range(8, 0, -1):
  99.             res = res + f"{y} {''.join(map(str, self.board[8 - y]))} {y} \n"
  100.         res += "\n" + '  '.join(['   A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) + '\n'
  101.         return res
  102.  
  103.  
  104. class Game(Board):
  105.     def __init__(self):
  106.         b = Board()
  107.         b.start()
  108.         n = Note()
  109.         # spec = SpecialRules()
  110.         # self.board = b.board
  111.         self.movies = 1
  112.         self.player_color = Color.WHITE
  113.         print(b)
  114.         while True:
  115.             if self.player_color == Color.WHITE and self.movies == 1:
  116.                 b.start()
  117.             print(f"Ход {'белых' if self.player_color == Color.WHITE else 'черных'}")
  118.             s = input()
  119.  
  120.             if len(s) == 0:
  121.                 break
  122.  
  123.             else:
  124.                 motion1, motion2 = s.split()
  125.                 print(f"Ход от {self.inp_coord(motion1)} в {self.inp_coord(motion2)}")
  126.                 # print(b.get_moves(*self.inp_coord(motion1)))
  127.                 checks = self.check(b, motion1, motion2)
  128.                 if checks == 'Можно':
  129.                     xy_from = self.inp_coord(motion1)
  130.                     xy_to = self.inp_coord(motion2)
  131.                     y1, x1 = xy_from[0], xy_from[1]
  132.                     y2, x2 = xy_to[0], xy_to[1]
  133.  
  134.                     n.read(y1, x1, y2, x2)
  135.                     b.move(y1, x1, y2, x2)
  136.  
  137.                     print(b)
  138.                     if self.player_color == Color.WHITE:
  139.                         self.movies += 1
  140.                         self.player_color = Color.BLACK
  141.                     else:
  142.                         self.player_color = Color.WHITE
  143.                 else:
  144.                     print(checks)
  145.  
  146.     def check(self, b, xy_from, xy_to):
  147.         if self.check_inp(b, xy_from, xy_to):
  148.             motion1, motion2 = xy_from, xy_to
  149.             xy_from = self.inp_coord(xy_from)
  150.             xy_to = self.inp_coord(xy_to)
  151.             y1, x1 = xy_from[0], xy_from[1]
  152.             y2, x2 = xy_to[0], xy_to[1]
  153.             if self.check_color(b, y1, x1, y2, x2):
  154.                 if self.check_move(b, y1, x1, y2, x2):
  155.                     return 'Можно'
  156.                 else:
  157.                     moves = ', '.join(self.return_coords(b.get_moves(y1, x1)))
  158.                     return f'У фигуры на {motion1} хода на {motion2} нет. Возможные ходы из {motion1}: {moves}'
  159.             else:
  160.                 return 'Нельзя ходить пустой клеткой и чужой фигурой'
  161.         else:
  162.             return 'Такой клетки не существует'
  163.  
  164.     def inp_coord(self, xy):
  165.         s = "abcdefgh"
  166.         return [8 - int(xy[1]), s.index(xy[0])]
  167.  
  168.     def return_coord(self, y, x):
  169.         y = 8 - y
  170.         return f'{"abcdefgh"[x]}{y}'
  171.  
  172.     def return_coords(self, m):
  173.         k = []
  174.         for i in m:
  175.             k.append(self.return_coord(i[0], i[1]))
  176.         return k
  177.  
  178.  
  179. class Note(Game):
  180.     def __init__(self):
  181.         self.notes = []
  182.  
  183.     def read(self, y1, x1, y2, x2):
  184.         self.notes.append([[y1, x1], [y2, x2]])
  185.  
  186.     def coords(self, y, x):
  187.         y = 8 - y
  188.         return f'{"abcdefgh"[x]}{y}'
  189.  
  190.     def pretty_write(self):
  191.         k = ''
  192.         n = self.notes
  193.         for i in range(len(n)):
  194.             ki = ''
  195.             for j in range(2):
  196.                 xy = self.coords(n[i][j][0], n[i][j][1])
  197.                 ki += xy
  198.             if i % 2 == 0:
  199.                 k = f"{k}{str((i + 2) // 2)}. {ki} |"
  200.             else:
  201.                 k = f'{k} {ki} \n'
  202.         return k
  203.  
  204.     def annul(self, b, moves):
  205.         n = self.notes
  206.         b.start()
  207.         for i in range(len(n) - moves):
  208.             y1, x1, y2, x2 = n[i][0][0], n[i][0][1], n[i][1][0], n[i][1][1]
  209.             b.move(y1, x1, y2, x2)
  210.  
  211.         return b
  212.  
  213.  
  214. Game()
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement