Szachy/move.py

119 lines
4.8 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:
2023-06-09 23:16:08 +02:00
def move_rival(self, ruch):
self.from_column = ruch[0]
self.from_row = int(ruch[1])
self.to_column = ruch[3]
self.to_row = int(ruch[4])
self.to_column = Move.letters_to__numbers(self.to_column)
self.from_column = Move.letters_to__numbers(self.from_column)
2023-06-09 22:48:38 +02:00
#update
2023-06-09 23:16:08 +02:00
self.x = self.board.boardlist[self.from_row][self.from_column].piece
self.board.boardlist[self.from_row][self.from_column].piece = None
if (self.board.boardlist[self.to_row][self.to_column].has_piece()):
if (self.board.boardlist[self.to_row][self.to_column].piece.name == 'king'):
self.board.boardlist[self.to_row][self.to_column].piece = self.x
2023-06-09 22:48:38 +02:00
exit(0)
2023-06-09 23:16:08 +02:00
self.board.boardlist[self.to_row][self.to_column].piece = self.x
self.board.boardlist[self.to_row][self.to_column].piece.moved = True
self.board.boardlist[self.to_row][self.to_column].piece.delete_moves()
return self.board.boardlist[self.to_row][self.to_column].piece.color
2023-06-09 22:48:38 +02:00
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
2023-06-09 23:16:08 +02:00
2023-06-09 22:48:38 +02:00
#pawn promotion
2023-06-09 23:16:08 +02:00
Move.pawn_promotion(self, self.x, self.final_row, self.final_column)
#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)
2023-06-09 22:48:38 +02:00
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
2023-06-09 23:16:08 +02:00
def pawn_promotion(self, x, final_row, final_column):
if isinstance(x, Pawn):
if (final_row==0) or (final_row==7):
self.board.boardlist[final_row][final_column].piece = Queen(x.color)
@staticmethod
2023-06-09 22:48:38 +02:00
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
2023-06-09 23:16:08 +02:00
@staticmethod
def letters_to__numbers(letter):
if letter == 'A':
letter = 0
elif letter == 'B':
letter=1
elif letter == 'C':
letter = 2
elif letter == 'D':
letter = 3
elif letter == 'E':
letter = 4
elif letter == 'F':
letter = 5
elif letter == 'G':
letter = 6
elif letter == 'H':
letter = 7
return letter