Szachy/move.py

174 lines
8.4 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):
2023-06-10 09:20:27 +02:00
self.from_column = Move.letters_to__numbers(ruch[0])
2023-06-09 23:16:08 +02:00
self.from_row = int(ruch[1])
2023-06-10 09:20:27 +02:00
self.to_column = Move.letters_to__numbers(ruch[3])
2023-06-09 23:16:08 +02:00
self.to_row = int(ruch[4])
2023-06-10 09:20:27 +02:00
#update board
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
self.board.boardlist[self.to_row][self.to_column].piece = self.x
2023-06-15 16:22:32 +02:00
Move.pawn_promotion(self, self.x, self.to_row)
2023-06-09 23:16:08 +02:00
self.board.boardlist[self.to_row][self.to_column].piece.moved = True
return self.board.boardlist[self.to_row][self.to_column].piece.color
2023-06-19 20:11:22 +02:00
def move_boot(self, color):
self.initial_row, self.initial_column, self.final_row, self.final_column = Move.random_valid_move(self, color)
self.x=self.board.boardlist[self.initial_row][self.initial_column].piece
2023-06-10 10:01:34 +02:00
self.board.boardlist[self.initial_row][self.initial_column].piece = None
Move.mat(self, self.x, self.final_row, self.final_column)
self.board.boardlist[self.final_row][self.final_column].piece = self.x
2023-06-15 16:22:32 +02:00
Move.pawn_promotion(self, self.x, self.final_row)
# update piece
2023-06-10 10:01:34 +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()
2023-06-15 16:22:32 +02:00
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)
2023-06-10 10:01:34 +02:00
return self.ruch
def random_valid_move(self, color):
2023-06-19 20:11:22 +02:00
#usuwanie ruchów z valid_moves, które mogłyby doprowadzić do szachowania króla
2023-06-09 22:48:38 +02:00
self.boot_piece = []
self.piece_row_column = []
for r in range(0, 8):
for c in range(0, 8):
2023-06-15 16:22:32 +02:00
if self.board.boardlist[r][c].has_piece():
2023-06-09 22:48:38 +02:00
if self.board.boardlist[r][c].piece.color != color:
2023-06-15 16:22:32 +02:00
self.kopia = []
for move in self.board.boardlist[r][c].piece.valid_moves:
if Move.check_szach(self, color, r, c, move[0], move[1]) is False:
self.kopia.append(move)
self.board.boardlist[r][c].piece.valid_moves=self.kopia
2023-06-19 20:11:22 +02:00
#spis pól z figurami, które mają możliwość ruchu
for r in range(8):
for c in range(8):
if self.board.boardlist[r][c].has_piece() and self.board.boardlist[r][c].piece.color != color:
if (len(self.board.boardlist[r][c].piece.valid_moves)>0):
self.boot_piece.append(self.board.boardlist[r][c])
2023-06-15 16:22:32 +02:00
if (len(self.boot_piece)==0):
print("MAT")
exit(0)
2023-06-19 20:11:22 +02:00
#losowanie figury
2023-06-09 22:48:38 +02:00
self.value = random.randrange(0, len(self.boot_piece))
2023-06-19 20:11:22 +02:00
self.initial_square = self.boot_piece[self.value]
#losowanie ruchu figury
self.move_initial_row = self.initial_square.row
self.move_initial_column = self.initial_square.column
self.value = random.randrange(0, len(self.initial_square.piece.valid_moves))
self.move_final_square = self.initial_square.piece.valid_moves[self.value]
self.move_final_row = self.move_final_square[0]
self.move_final_column = self.move_final_square[1]
return self.move_initial_row, self.move_initial_column, self. move_final_row, self.move_final_column #, self.x
2023-06-13 00:08:24 +02:00
def calc_moves(self, piece, row, column):
2023-06-15 16:22:32 +02:00
#piece.delete_moves()
piece.valid_moves=[]
2023-06-13 00:08:24 +02:00
if piece.name == 'rook':
2023-06-19 20:11:22 +02:00
Rook.straightline_moves(self, piece, row, column)
2023-06-13 00:08:24 +02:00
pass
elif piece.name == 'pawn':
Pawn.pawn_moves(self, piece, row, column)
pass
elif piece.name == 'knight':
Knight.knight_moves(self, piece, row, column)
elif piece.name == 'bishop':
pass
2023-06-19 20:11:22 +02:00
Bishop.straightline_moves(self, piece, row, column)
2023-06-13 00:08:24 +02:00
elif piece.name == 'queen':
pass
Queen.straightline_moves(self, piece, row, column,True)
Queen.straightline_moves(self, piece, row, column,False)
elif piece.name == 'king':
King.king_moves(self, piece, row, column)
pass
2023-06-15 16:22:32 +02:00
def pawn_promotion(self, x, final_row):
2023-06-13 00:30:37 +02:00
if x.name == "pawn":
2023-06-15 16:22:32 +02:00
if (final_row == 0) or (final_row == 7):
2023-06-19 20:11:22 +02:00
x.name = "queen"
2023-06-10 09:20:27 +02:00
def mat(self, x, final_row, final_column):
if self.board.boardlist[final_row][final_column].has_piece():
if self.board.boardlist[final_row][final_column].piece.name == 'king':
exit(0)
2023-06-15 16:22:32 +02:00
pass
2023-06-19 20:11:22 +02:00
def check_szach(self, color, initial_row, initial_column, final_row, final_column):
#zapisanie poprzedniego ustawienia tablicy i wykonanie ruchu
2023-06-15 16:22:32 +02:00
self.piece = self.board.boardlist[initial_row][initial_column].piece
self.previous_piece = self.board.boardlist[final_row][final_column].piece
self.board.boardlist[initial_row][initial_column].piece = None
self.board.boardlist[final_row][final_column].piece = self.piece
2023-06-19 20:11:22 +02:00
for r in range(8):
for c in range(8):
if self.board.boardlist[r][c].has_piece():
if self.board.boardlist[r][c].piece.color == color:
Move.calc_moves(self, self.board.boardlist[r][c].piece, r, c)
elif self.board.boardlist[r][c].piece.name=='king':
self.king_position = (r, c)
2023-06-15 16:22:32 +02:00
for r2 in range(8):
for c2 in range(8):
if self.board.boardlist[r2][c2].has_piece():
if self.board.boardlist[r2][c2].piece.color == color:
if self.king_position in self.board.boardlist[r2][c2].piece.valid_moves:
self.board.boardlist[final_row][final_column].piece=self.previous_piece
self.board.boardlist[initial_row][initial_column].piece= self.piece
for rowxd2 in range(8):
for columnxd2 in range(8):
if self.board.boardlist[rowxd2][columnxd2].has_piece():
if self.board.boardlist[rowxd2][columnxd2].piece.color == color:
Move.calc_moves(self, self.board.boardlist[rowxd2][columnxd2].piece, rowxd2,columnxd2)
return True
self.board.boardlist[final_row][final_column].piece = self.previous_piece
self.board.boardlist[initial_row][initial_column].piece = self.piece
for rowxd in range(8):
for columnxd in range(8):
if self.board.boardlist[rowxd][columnxd].has_piece():
if self.board.boardlist[rowxd][columnxd].has_piece():
if self.board.boardlist[rowxd][columnxd].piece.color == color:
Move.calc_moves(self, self.board.boardlist[rowxd][columnxd].piece, rowxd, columnxd)
return False
2023-06-09 23:16:08 +02:00
@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':
2023-06-10 10:01:34 +02:00
letter = 1
2023-06-09 23:16:08 +02:00
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
2023-06-19 20:11:22 +02:00
return letter