def valid_moves(self, squares): moves = [] if self.team == 'w': step = 1 base_rank = 2 else: step = -1 base_rank = SETTINGS.dnum - 1 # Captures for file_dir in (-1, 1): idx = Square.lookup((chr(ord(self.square.file) + file_dir), self.square.rank + step)) if idx: square = squares[idx] if square.occupied() and square.piece.team != self.team: moves.append(square) # Forward single moves idx = Square.lookup((self.square.file, self.square.rank + step)) if idx: square = squares[idx] if not square.occupied(): moves.append(square) # Forward double move if self.square.rank == base_rank: idx = Square.lookup((self.square.file, self.square.rank + 2 * step)) if idx: square = squares[idx] if not square.occupied(): moves.append(square) return moves
def valid_moves(self, squares): moves = [] if self.team == 'w': step = 1 base_rank = 2 else: step = -1 base_rank = SETTINGS.dnum - 1 # Captures for file_dir in (-1, 1): idx = Square.lookup((chr(ord(self.square.file) + file_dir), self.square.rank + step)) if idx: square = squares[idx] if square.occupied() and square.piece.team != self.team: moves.append(square) # Forward single moves idx = Square.lookup((self.square.file, self.square.rank + step)) if idx: square = squares[idx] if not square.occupied(): moves.append(square) # Forward double move if self.square.rank == base_rank: idx = Square.lookup( (self.square.file, self.square.rank + 2 * step)) if idx: square = squares[idx] if not square.occupied(): moves.append(square) return moves
def __init__(self): """New game board.""" self.squares = [ Square(idx) for idx in range( SETTINGS.dnum * SETTINGS.dnum)] self.white = [] self.black = [] self.add('w', King, ('e', 1)) self.add('w', Queen, ('d', 1)) self.add('w', Rook, ('a', 1)) self.add('w', Rook, ('h', 1)) self.add('w', Bishop, ('c', 1)) self.add('w', Bishop, ('f', 1)) self.add('w', Knight, ('b', 1)) self.add('w', Knight, ('g', 1)) for file in 'abcdefgh': self.add('w', Pawn, (file, 2)) self.add('b', King, ('e', 8)) self.add('b', Queen, ('d', 8)) self.add('b', Rook, ('a', 8)) self.add('b', Rook, ('h', 8)) self.add('b', Bishop, ('c', 8)) self.add('b', Bishop, ('f', 8)) self.add('b', Knight, ('b', 8)) self.add('b', Knight, ('g', 8)) for file in 'abcdefgh': self.add('b', Pawn, (file, 7))
def valid_moves(self, squares): moves = [] for method in (self.fr_ur, self.fr_dr, self.fr_dl, self.fr_ul): for file, rank in method(): square = squares[Square.lookup((file, rank))] if not square.occupied(): moves.append(square) elif square.piece.team != self.team: moves.append(square) break else: break return moves
def valid_moves(self, squares): moves = [] for method in (self.file_right, self.file_left): for file in method(): square = squares[Square.lookup((file, self.square.rank))] if not square.occupied(): moves.append(square) elif square.piece.team != self.team: moves.append(square) break else: break for method in (self.rank_up, self.rank_down): for rank in method(): square = squares[Square.lookup((self.square.file, rank))] if not square.occupied(): moves.append(square) elif square.piece.team != self.team: moves.append(square) break else: break return moves
def move(self, team, echelon, from_str, to_str): """TODO""" if to_str == '0': pass elif to_str == '00': pass else: to_sq = Square.lookup(to_str) if from_str: pass else: pass return from_str, to_str pass
def valid_moves(self, squares): idxs = [] for file, rank in ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)): flu = chr(ord(self.square.file) + file) rlu = self.square.rank + rank idx = Square.lookup((flu, rlu)) if idx: idxs.append(idx) moves = [] for idx in idxs: square = squares[idx] if square.occupied() and square.piece.team == self.team: continue moves.append(square) return moves
def lookup(self, pos): idx = Square.lookup(pos) return self.squares[idx] if idx is not None else None