from square import Square from pawn import Pawn from knight import Knight from bishop import Bishop from rook import Rook from queen import Queen from king import King import random class Board: def __init__(self): self.boardlist = [[0, 0, 0, 0, 0, 0, 0, 0] for x in range(8)] #creating board for row in range(8): for column in range(8): self.boardlist[row][column] = Square(row, column) self._add_pieces('white') self._add_pieces('black') def _add_pieces(self, color): if color == 'white': self.pawn_row = 6 self.other_row = 7 else: self.pawn_row = 1 self.other_row = 0 #pawns #self.boardlist[5][1] = Square(5, 1, King(color)) #DO USUNIĘCIA for column in range(8): self.boardlist[self.pawn_row][column] = Square(self.pawn_row, column, Pawn(color)) #do kazdego square przypisuje poczatkowy pawn #rooks self.boardlist[self.other_row][0] = Square(self.other_row, 0, Rook(color)) self.boardlist[self.other_row][7] = Square(self.other_row, 7, Rook(color)) #knigths self.boardlist[self.other_row][1] = Square(self.other_row, 1, Knight(color)) self.boardlist[self.other_row][6] = Square(self.other_row, 6, Knight(color)) #bishops self.boardlist[self.other_row][2] = Square(self.other_row, 2, Bishop(color)) self.boardlist[self.other_row][5] = Square(self.other_row, 5, Bishop(color)) #queen self.boardlist[self.other_row][3] = Square(self.other_row, 3, Queen(color)) #king self.boardlist[self.other_row][4] = Square(self.other_row, 4, King(color)) def calc_moves(self, piece, row, column): def knight_moves(): self.possible_moves = [ (row + 2, column - 1), (row + 2, column + 1), (row + 1, column + 2), (row - 1, column + 2), (row - 2, column + 1), (row - 2, column - 1), (row - 1, column - 2), (row + 1, column - 2) ] for self.possible_move in self.possible_moves: self.possible_row, self.possible_column = self.possible_move if Square.on_board(self.possible_row, self.possible_column): if self.boardlist[self.possible_row][self.possible_column].empty_or_rival(piece.color): self.final = (self.possible_row, self.possible_column) piece.add_moves(self.final) def pawn_moves(): if piece.moved == True: self.steps = 1 else: self.steps = 2 if self.steps == 2: self.final = row + (piece.pawn_direction * (self.steps)) self.previous = row + (piece.pawn_direction * (self.steps - 1)) if Square.on_board(self.final, column) is True: if self.boardlist[self.final][column].has_piece() is False: if self.boardlist[self.previous][column].has_piece() is False: piece.add_moves((self.final, column)) self.steps = 1 self.final = row + (piece.pawn_direction * (self.steps)) if Square.on_board(self.final, column) is True and self.boardlist[self.final][column].has_piece() is False: piece.add_moves((self.final, column)) #diagonal self.possible_row = row + piece.pawn_direction self.possible_columns = [] self.possible_columns = [column-1, column+1] for possible_column in self.possible_columns: if Square.on_board(self.possible_row, possible_column) is True: if self.boardlist[self.possible_row][possible_column].is_rival(piece.color) is True: piece.add_moves((self.possible_row, possible_column)) def straightline_moves(isvertical): if isvertical is True: self.direction = [ (0, 1), (0, -1), (1, 0), (-1, 0)] else: self.direction = [ (1, 1), (-1, -1), (1, -1), (-1, 1) ] for dir in self.direction: self.left, self.right = dir self.possible_row = row + self.left self.possible_column = column + self.right while True: if Square.on_board(self.possible_row, self.possible_column) is True: if self.boardlist[self.possible_row][self.possible_column].has_piece() is False: piece.add_moves((self.possible_row, self.possible_column)) self.possible_row = self.possible_row + self.left self.possible_column = self.possible_column + self.right elif self.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True: piece.add_moves((self.possible_row, self.possible_column)) break else: break else: break def king_moves(): self.direction = [ (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1) ] for dir in self.direction: self.left, self.right = dir self.possible_row = row + self.left self.possible_column = column + self.right if Square.on_board(self.possible_row, self.possible_column) is True: if self.boardlist[self.possible_row][self.possible_column].has_piece() is False: piece.add_moves((self.possible_row, self.possible_column)) elif self.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True: piece.add_moves((self.possible_row, self.possible_column)) #calculate possible moves for specific piece if piece.name == 'rook': straightline_moves(True) elif piece.name == 'pawn': pawn_moves() elif piece.name == 'knight': knight_moves() elif piece.name == 'bishop': straightline_moves(False) elif piece.name == 'queen': straightline_moves(True) straightline_moves(False) elif piece.name == 'king': king_moves()