Szachy/piece_moves.py
2023-06-12 23:48:36 +02:00

133 lines
5.5 KiB
Python

from board import Board
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
class Piece_moves:
def calc_moves(self, piece, row, column):
piece.valid_moves=[]
if piece.name == 'rook':
Piece_moves.straightline_moves(self, piece, row, column,True)
pass
elif piece.name == 'pawn':
Piece_moves.pawn_moves(self, piece, row, column)
pass
elif piece.name == 'knight':
Piece_moves.knight_moves(self, piece, row, column)
elif piece.name == 'bishop':
pass
Piece_moves.straightline_moves(self, piece, row, column,False)
elif piece.name == 'queen':
pass
Piece_moves.straightline_moves(self, piece, row, column,True)
Piece_moves.straightline_moves(self, piece, row, column,False)
elif piece.name == 'king':
Piece_moves.king_moves(self, piece, row, column)
pass
#print(piece)
#print(piece.valid_moves)
def knight_moves(self, piece, row, column):
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.board.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(self, piece, row, column):
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.board.boardlist[self.final][column].has_piece() is False:
if self.board.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.board.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.board.boardlist[self.possible_row][possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, possible_column))
def straightline_moves(self, piece, row, column, 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.board.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.board.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, piece, row, column):
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.board.boardlist[self.possible_row][self.possible_column].has_piece() is False:
piece.add_moves((self.possible_row, self.possible_column))
elif self.board.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, self.possible_column))