Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import symtable
- class Color(object):
- EMPTY = 0
- BLACK = 1
- WHITE = 2
- @classmethod
- def invert(cls, color):
- if color == cls.EMPTY:
- return color
- return cls.BLACK if color == cls.WHITE else cls.WHITE
- class Empty(object):
- color = Color.EMPTY
- def get_moves(self, board, y, x):
- raise Exception("Error !")
- def __repr__(self):
- return ' . '
- class ChessMan(object):
- fig = None
- def __init__(self, color):
- self.color = color
- def __str__(self):
- return self.fig[0 if self.color == Color.WHITE else 1]
- def enemy_color(self):
- return Color.invert(self.color)
- class King(ChessMan):
- pass
- class Checker(ChessMan):
- fig = (' O ', ' o ')
- def get_moves(self, board, y, x):
- moves = []
- p = [[1, 1], [-1, 1], [-1, -1], [1, -1]]
- # просто сходить
- for k in p:
- y1, x1 = y + k[1], x + k[0]
- if 0 <= y1 <= 7 and 0 <= x1 <= 7:
- if board.get_color(y1, x1) == Color.EMPTY:
- moves.append([y1, x1])
- # ход срубить
- for k in p:
- y1, x1 = y + 2 * k[1], x + 2 * k[0]
- y2, x2 = y + k[1], x + k[0]
- if 0 <= y1 <= 7 and 0 <= x1 <= 7:
- if board.get_color(y1, x1) == Color.EMPTY:
- if board.get_color(y2, x2) == self.enemy_color():
- moves.append([y1, x1])
- return moves
- class Board(object):
- def __init__(self):
- self.board = [[Empty()] * 8 for i in range(8)]
- def start(self):
- self.board = [[Empty()] * 8 for i in range(8)]
- for i in range(8):
- self.board[1][i] = Pawn(Color.BLACK)
- self.board[6][i] = Pawn(Color.WHITE)
- for i in range(8):
- for j in range(8):
- self.
- def get_color(self, y, x):
- return self.board[y][x].color
- def get_moves(self, y, x):
- return self.board[y][x].get_moves(self, y, x)
- def move(self, y1, x1, y2, x2):
- self.board[y2][x2] = self.board[y1][x1]
- if (y1-y2) ** 2 + (x1-x2) ** 2 == 4:
- self.board[y1][x1] = Empty()
- def is_empty(self, y, x):
- self.board[y][x] = Empty()
- def __str__(self):
- res = '\n' + ' '.join([' A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) + '\n\n'
- for y in range(8, 0, -1):
- res = res + f"{y} {''.join(map(str, self.board[8 - y]))} {y} \n"
- res += "\n" + ' '.join([' A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) + '\n'
- return res
- class Game(Board):
- def __init__(self):
- b = Board()
- b.start()
- n = Note()
- # spec = SpecialRules()
- # self.board = b.board
- self.movies = 1
- self.player_color = Color.WHITE
- print(b)
- while True:
- if self.player_color == Color.WHITE and self.movies == 1:
- b.start()
- print(f"Ход {'белых' if self.player_color == Color.WHITE else 'черных'}")
- s = input()
- if len(s) == 0:
- break
- else:
- motion1, motion2 = s.split()
- print(f"Ход от {self.inp_coord(motion1)} в {self.inp_coord(motion2)}")
- # print(b.get_moves(*self.inp_coord(motion1)))
- checks = self.check(b, motion1, motion2)
- if checks == 'Можно':
- xy_from = self.inp_coord(motion1)
- xy_to = self.inp_coord(motion2)
- y1, x1 = xy_from[0], xy_from[1]
- y2, x2 = xy_to[0], xy_to[1]
- n.read(y1, x1, y2, x2)
- b.move(y1, x1, y2, x2)
- print(b)
- if self.player_color == Color.WHITE:
- self.movies += 1
- self.player_color = Color.BLACK
- else:
- self.player_color = Color.WHITE
- else:
- print(checks)
- def check(self, b, xy_from, xy_to):
- if self.check_inp(b, xy_from, xy_to):
- motion1, motion2 = xy_from, xy_to
- xy_from = self.inp_coord(xy_from)
- xy_to = self.inp_coord(xy_to)
- y1, x1 = xy_from[0], xy_from[1]
- y2, x2 = xy_to[0], xy_to[1]
- if self.check_color(b, y1, x1, y2, x2):
- if self.check_move(b, y1, x1, y2, x2):
- return 'Можно'
- else:
- moves = ', '.join(self.return_coords(b.get_moves(y1, x1)))
- return f'У фигуры на {motion1} хода на {motion2} нет. Возможные ходы из {motion1}: {moves}'
- else:
- return 'Нельзя ходить пустой клеткой и чужой фигурой'
- else:
- return 'Такой клетки не существует'
- def inp_coord(self, xy):
- s = "abcdefgh"
- return [8 - int(xy[1]), s.index(xy[0])]
- def return_coord(self, y, x):
- y = 8 - y
- return f'{"abcdefgh"[x]}{y}'
- def return_coords(self, m):
- k = []
- for i in m:
- k.append(self.return_coord(i[0], i[1]))
- return k
- class Note(Game):
- def __init__(self):
- self.notes = []
- def read(self, y1, x1, y2, x2):
- self.notes.append([[y1, x1], [y2, x2]])
- def coords(self, y, x):
- y = 8 - y
- return f'{"abcdefgh"[x]}{y}'
- def pretty_write(self):
- k = ''
- n = self.notes
- for i in range(len(n)):
- ki = ''
- for j in range(2):
- xy = self.coords(n[i][j][0], n[i][j][1])
- ki += xy
- if i % 2 == 0:
- k = f"{k}{str((i + 2) // 2)}. {ki} |"
- else:
- k = f'{k} {ki} \n'
- return k
- def annul(self, b, moves):
- n = self.notes
- b.start()
- for i in range(len(n) - moves):
- y1, x1, y2, x2 = n[i][0][0], n[i][0][1], n[i][1][0], n[i][1][1]
- b.move(y1, x1, y2, x2)
- return b
- Game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement