Szachy/move.py

83 lines
3.6 KiB
Python
Raw Normal View History

2023-06-09 22:48:38 +02:00
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
import random
class Move:
@staticmethod
def move_rival(self, from_row, from_column, to_row, to_column):
#update
self.x = self.board.boardlist[from_row][from_column].piece
self.board.boardlist[from_row][from_column].piece = None
if (self.board.boardlist[to_row][to_column].has_piece()):
if (self.board.boardlist[to_row][to_column].piece.name == 'king'):
self.board.boardlist[to_row][to_column].piece = self.x
exit(0)
self.board.boardlist[to_row][to_column].piece = self.x
self.board.boardlist[to_row][to_column].piece.moved = True
self.board.boardlist[to_row][to_column].piece.delete_moves()
return self.board.boardlist[to_row][to_column].piece.color
def move_boot(self, color):
self.boot_piece = []
self.piece_row_column = []
for r in range(0, 8):
for c in range(0, 8):
if self.board.boardlist[r][c].piece != None:
if self.board.boardlist[r][c].piece.color != color:
self.boot_piece.append(self.board.boardlist[r][c])
self.value = random.randrange(0, len(self.boot_piece))
self.initial = self.boot_piece[self.value]
while len(self.initial.piece.valid_moves) == 0:
self.value = random.randrange(0, len(self.boot_piece))
self.initial = self.boot_piece[self.value]
self.initial_row=self.initial.row
self.initial_column = self.initial.column
self.value = random.randrange(0, len(self.initial.piece.valid_moves))
self.final = self.initial.piece.valid_moves[self.value]
self.final_row = self.final[0]
self.final_column = self.final[1]
self.x = self.board.boardlist[self.initial_row][self.initial_column].piece
self.board.boardlist[self.initial_row][self.initial_column].piece = None
if (self.board.boardlist[self.final_row][self.final_column].has_piece()):
if (self.board.boardlist[self.final_row][self.final_column].piece.name== 'king'):
self.board.boardlist[self.final_row][self.final_column].piece = self.x
exit(0)
self.board.boardlist[self.final_row][self.final_column].piece = self.x
#pawn promotion
if isinstance(self.x, Pawn):
if (self.final_row==0) or (self.final_row==7):
self.board.boardlist[self.final_row][self.final_column].piece = Queen(self.x.color)
self.board.boardlist[self.final_row][self.final_column].piece.moved = True
self.board.boardlist[self.final_row][self.final_column].piece.delete_moves()
#zmiana ruchu na litery
self.initial_column=Move.numbers_to_letters(self.initial_column)
self.final_column=Move.numbers_to_letters(self.final_column)
self.ruch = str(self.initial_column) + str(self.initial_row) + ' ' + str(self.final_column) + str(self.final_row)
return self.ruch
def numbers_to_letters(number):
if number == 0:
number = "A"
elif number == 1:
number = "B"
elif number == 2:
number = "C"
elif number == 3:
number = "D"
elif number == 4:
number = "E"
elif number == 5:
number = "F"
elif number == 6:
number = "G"
elif number == 7:
number = "H"
return number