Szachy/move.py
Agata Halik d6152ad658 new
2023-06-09 00:22:01 +02:00

134 lines
5.5 KiB
Python

from typing import Tuple
from pawn import Pawn
from knight import Knight
from bishop import Bishop
from rook import Rook
from queen import Queen
from king import King
from board import Board
from square import Square
from piece import Piece
class Move:
@staticmethod
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 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 direct in self.direction:
self.left, self.right = direct
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 pawn_moves(self, piece, row, column):
if piece.moved:
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))
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 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))
def calc_moves(self, piece, row, column):
# calculate possible moves for specific piece
if piece == None:
pass
elif piece.name == 'rook':
# piece.delete_moves()
Move.straightline_moves(self, piece, column, row, True)
elif piece.name == 'pawn':
Move.pawn_moves(self, piece, column, row)
elif piece.name == 'knight':
Move.knight_moves(self, piece, column, row)
elif piece.name == 'bishop':
Move.straightline_moves(self, piece, column, row, False)
elif piece.name == 'queen':
Move.straightline_moves(self, piece, column, row, True)
Move.straightline_moves(self, piece, column, row, False)
elif piece.name == 'king':
Move.king_moves(self, piece, column, row)