.
This commit is contained in:
parent
861fa8c9b4
commit
3310217728
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2
board.py
2
board.py
@ -2,7 +2,9 @@ from square import Square
|
|||||||
from piece import Piece
|
from piece import Piece
|
||||||
class Board:
|
class Board:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
#utworzenie listy przechowującej obiekty square
|
||||||
self.boardlist = [[0, 0, 0, 0, 0, 0, 0, 0] for x in range(8)]
|
self.boardlist = [[0, 0, 0, 0, 0, 0, 0, 0] for x in range(8)]
|
||||||
|
|
||||||
#creating board
|
#creating board
|
||||||
for row in range(8):
|
for row in range(8):
|
||||||
for column in range(8):
|
for column in range(8):
|
||||||
|
13
game.py
13
game.py
@ -62,17 +62,18 @@ class Game:
|
|||||||
self.b.append('H')
|
self.b.append('H')
|
||||||
print(self.b)
|
print(self.b)
|
||||||
print('')
|
print('')
|
||||||
def currently_available_moves(self):
|
def currently_available_moves(self): #obliczenie dostępnych ruchów dla wszystkich figur
|
||||||
for row in range(8):
|
for row in range(8):
|
||||||
for column in range(8):
|
for column in range(8):
|
||||||
if self.board.boardlist[row][column].has_piece():
|
if self.board.boardlist[row][column].has_piece():
|
||||||
self.piece = self.board.boardlist[row][column].piece
|
self.piece = self.board.boardlist[row][column].piece
|
||||||
Move.calc_moves(self, self.piece, row, column)
|
Move.calc_moves(self, self.piece, row, column)
|
||||||
def nextMove(self, ruch):
|
def nextMove(self, ruch):
|
||||||
self.currently_available_moves()
|
#sprawdzenie ruchu przeciwnika
|
||||||
if ruch == None:
|
if ruch == None:
|
||||||
return Move.move_boot(self, 'black')
|
|
||||||
else:
|
|
||||||
self.color = Move.move_rival(self, ruch)
|
|
||||||
self.currently_available_moves()
|
self.currently_available_moves()
|
||||||
return Move.move_boot(self, self.color)
|
return Move.move_boot(self, 'black') #wykonanie naszego ruchu i zwrócenie go
|
||||||
|
else:
|
||||||
|
self.color = Move.move_rival(self, ruch) #wykonanie ruchu rywala, funkcja zwaraca kolor rywala
|
||||||
|
self.currently_available_moves()
|
||||||
|
return Move.move_boot(self, self.color) #wykonanie naszego ruchu i zwrócenie go
|
47
move.py
47
move.py
@ -24,49 +24,61 @@ class Move:
|
|||||||
self.board.boardlist[self.to_row][self.to_column].piece.moved = True
|
self.board.boardlist[self.to_row][self.to_column].piece.moved = True
|
||||||
return self.board.boardlist[self.to_row][self.to_column].piece.color
|
return self.board.boardlist[self.to_row][self.to_column].piece.color
|
||||||
def move_boot(self, color):
|
def move_boot(self, color):
|
||||||
|
#random move
|
||||||
self.initial_row, self.initial_column, self.final_row, self.final_column = Move.random_valid_move(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
|
|
||||||
|
#update board
|
||||||
|
self.x = self.board.boardlist[self.initial_row][self.initial_column].piece
|
||||||
self.board.boardlist[self.initial_row][self.initial_column].piece = None
|
self.board.boardlist[self.initial_row][self.initial_column].piece = None
|
||||||
Move.mat(self, self.final_row, self.final_column)
|
Move.mat(self, self.final_row, self.final_column)
|
||||||
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
||||||
Move.pawn_promotion(self, self.x, self.final_row)
|
Move.pawn_promotion(self, self.x, self.final_row)
|
||||||
|
|
||||||
# update piece
|
# update piece
|
||||||
self.board.boardlist[self.final_row][self.final_column].piece.moved = True
|
self.board.boardlist[self.final_row][self.final_column].piece.moved = True
|
||||||
self.board.boardlist[self.final_row][self.final_column].piece.delete_moves()
|
self.board.boardlist[self.final_row][self.final_column].piece.delete_moves()
|
||||||
|
|
||||||
self.initial_column = Move.numbers_to_letters(self.initial_column)
|
self.initial_column = Move.numbers_to_letters(self.initial_column)
|
||||||
self.final_column = Move.numbers_to_letters(self.final_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.ruch = str(self.initial_column) + str(self.initial_row) + ' ' + str(self.final_column) + str(self.final_row)
|
||||||
self.final_row)
|
|
||||||
return self.ruch
|
return self.ruch
|
||||||
def random_valid_move(self, color):
|
def random_valid_move(self, color):
|
||||||
#usuwanie ruchów z valid_moves, które mogłyby doprowadzić do szachowania króla
|
|
||||||
self.boot_piece = []
|
self.boot_piece = []
|
||||||
self.piece_row_column = []
|
self.piece_row_column = []
|
||||||
self.amount_of_pieces = 0
|
self.amount_of_pieces = 0
|
||||||
|
|
||||||
|
# usuwanie ruchów z valid_moves, które mogłyby doprowadzić do szachowania króla
|
||||||
|
# usuwanie ruchów z valid_moves, które nie ochronią szachowanego króla przed matem
|
||||||
for r in range(0, 8):
|
for r in range(0, 8):
|
||||||
for c in range(0, 8):
|
for c in range(0, 8):
|
||||||
if self.board.boardlist[r][c].has_piece():
|
if self.board.boardlist[r][c].has_piece():
|
||||||
self.amount_of_pieces = self.amount_of_pieces +1
|
self.amount_of_pieces = self.amount_of_pieces + 1
|
||||||
if self.board.boardlist[r][c].piece.color != color:
|
if self.board.boardlist[r][c].piece.color != color:
|
||||||
self.kopia = []
|
self.kopia = []
|
||||||
for move in self.board.boardlist[r][c].piece.valid_moves:
|
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:
|
if Move.check_szach(self, color, r, c, move[0], move[1]) is False:
|
||||||
self.kopia.append(move)
|
self.kopia.append(move)
|
||||||
self.board.boardlist[r][c].piece.valid_moves = self.kopia
|
self.board.boardlist[r][c].piece.valid_moves = self.kopia
|
||||||
if self.amount_of_pieces == 2:
|
|
||||||
|
if self.amount_of_pieces == 2: #jeśli na planszy zostaną tylko dwa króle to kończy się gra
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
#spis pól z figurami, które mają możliwość ruchu
|
#spis pól z figurami, które mają możliwość ruchu
|
||||||
for r in range(8):
|
for r in range(8):
|
||||||
for c 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 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:
|
if len(self.board.boardlist[r][c].piece.valid_moves) > 0:
|
||||||
self.boot_piece.append(self.board.boardlist[r][c])
|
self.boot_piece.append(self.board.boardlist[r][c])
|
||||||
#pat
|
|
||||||
|
#jeśli nie ma żadnego dostępnego ruchu który nie doprowadziłby do szachowania króla
|
||||||
|
#lub obronił króla (jeśli król jest szachowany) przed matem, to kończy się gra
|
||||||
if len(self.boot_piece) == 0:
|
if len(self.boot_piece) == 0:
|
||||||
exit(0)
|
exit(0)
|
||||||
#losowanie figury
|
|
||||||
|
#losowanie figury z figur z dostępnymi ruchami
|
||||||
self.value = random.randrange(0, len(self.boot_piece))
|
self.value = random.randrange(0, len(self.boot_piece))
|
||||||
self.initial_square = self.boot_piece[self.value]
|
self.initial_square = self.boot_piece[self.value]
|
||||||
|
|
||||||
#losowanie ruchu figury
|
#losowanie ruchu figury
|
||||||
self.move_initial_row = self.initial_square.row
|
self.move_initial_row = self.initial_square.row
|
||||||
self.move_initial_column = self.initial_square.column
|
self.move_initial_column = self.initial_square.column
|
||||||
@ -74,6 +86,7 @@ class Move:
|
|||||||
self.move_final_square = self.initial_square.piece.valid_moves[self.value]
|
self.move_final_square = self.initial_square.piece.valid_moves[self.value]
|
||||||
self.move_final_row = self.move_final_square[0]
|
self.move_final_row = self.move_final_square[0]
|
||||||
self.move_final_column = self.move_final_square[1]
|
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
|
return self.move_initial_row, self.move_initial_column, self. move_final_row, self.move_final_column
|
||||||
def calc_moves(self, piece, row, column):
|
def calc_moves(self, piece, row, column):
|
||||||
piece.delete_moves()
|
piece.delete_moves()
|
||||||
@ -102,15 +115,17 @@ class Move:
|
|||||||
def mat(self, final_row, final_column):
|
def mat(self, final_row, final_column):
|
||||||
if self.board.boardlist[final_row][final_column].has_piece():
|
if self.board.boardlist[final_row][final_column].has_piece():
|
||||||
if self.board.boardlist[final_row][final_column].piece.name == 'king':
|
if self.board.boardlist[final_row][final_column].piece.name == 'king':
|
||||||
print("mat")
|
|
||||||
exit(0)
|
exit(0)
|
||||||
pass
|
pass
|
||||||
def check_szach(self, color, initial_row, initial_column, final_row, final_column):
|
def check_szach(self, color, initial_row, initial_column, final_row, final_column):
|
||||||
|
|
||||||
#zapisanie poprzedniego ustawienia tablicy i wykonanie ruchu
|
#zapisanie poprzedniego ustawienia tablicy i wykonanie ruchu
|
||||||
self.piece = self.board.boardlist[initial_row][initial_column].piece
|
self.piece = self.board.boardlist[initial_row][initial_column].piece
|
||||||
self.previous_piece = self.board.boardlist[final_row][final_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[initial_row][initial_column].piece = None
|
||||||
self.board.boardlist[final_row][final_column].piece = self.piece
|
self.board.boardlist[final_row][final_column].piece = self.piece
|
||||||
|
|
||||||
|
#szukanie króla i liczenie dostępnych ruchów dla każdego pionka
|
||||||
for r in range(8):
|
for r in range(8):
|
||||||
for c in range(8):
|
for c in range(8):
|
||||||
if self.board.boardlist[r][c].has_piece():
|
if self.board.boardlist[r][c].has_piece():
|
||||||
@ -118,19 +133,23 @@ class Move:
|
|||||||
Move.calc_moves(self, self.board.boardlist[r][c].piece, r, c)
|
Move.calc_moves(self, self.board.boardlist[r][c].piece, r, c)
|
||||||
elif self.board.boardlist[r][c].piece.name == 'king':
|
elif self.board.boardlist[r][c].piece.name == 'king':
|
||||||
self.king_position = (r, c)
|
self.king_position = (r, c)
|
||||||
|
|
||||||
|
#szukanie króla w możliwych ruchach przeciwnika
|
||||||
for r2 in range(8):
|
for r2 in range(8):
|
||||||
for c2 in range(8):
|
for c2 in range(8):
|
||||||
if self.board.boardlist[r2][c2].has_piece():
|
if self.board.boardlist[r2][c2].has_piece():
|
||||||
if self.board.boardlist[r2][c2].piece.color == color:
|
if self.board.boardlist[r2][c2].piece.color == color:
|
||||||
if self.king_position in self.board.boardlist[r2][c2].piece.valid_moves:
|
if self.king_position in self.board.boardlist[r2][c2].piece.valid_moves:
|
||||||
|
#przywracanie poprzednich ustawień tablicy z pionkami
|
||||||
self.board.boardlist[final_row][final_column].piece = self.previous_piece
|
self.board.boardlist[final_row][final_column].piece = self.previous_piece
|
||||||
self.board.boardlist[initial_row][initial_column].piece = self.piece
|
self.board.boardlist[initial_row][initial_column].piece = self.piece
|
||||||
for rowxd2 in range(8):
|
for r4 in range(8):
|
||||||
for columnxd2 in range(8):
|
for c4 in range(8):
|
||||||
if self.board.boardlist[rowxd2][columnxd2].has_piece():
|
if self.board.boardlist[r4][c4].has_piece():
|
||||||
if self.board.boardlist[rowxd2][columnxd2].piece.color == color:
|
if self.board.boardlist[r4][c4].piece.color == color:
|
||||||
Move.calc_moves(self, self.board.boardlist[rowxd2][columnxd2].piece, rowxd2, columnxd2)
|
Move.calc_moves(self, self.board.boardlist[r4][c4].piece, r4, c4)
|
||||||
return True
|
return True
|
||||||
|
#przywracanie poprzednich ustawień tablicy z pionkami
|
||||||
self.board.boardlist[final_row][final_column].piece = self.previous_piece
|
self.board.boardlist[final_row][final_column].piece = self.previous_piece
|
||||||
self.board.boardlist[initial_row][initial_column].piece = self.piece
|
self.board.boardlist[initial_row][initial_column].piece = self.piece
|
||||||
for r3 in range(8):
|
for r3 in range(8):
|
||||||
|
4
piece.py
4
piece.py
@ -2,8 +2,8 @@ class Piece:
|
|||||||
def __init__(self, name, color):
|
def __init__(self, name, color):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.color = color
|
self.color = color
|
||||||
self.valid_moves = []
|
self.valid_moves = [] #dostępne ruchy dla figury
|
||||||
self.moved = False
|
self.moved = False #informacja czy figura wykonała już swój pierwszy ruch, potrzebne do policzenia ruchow pawn
|
||||||
if color == 'white':
|
if color == 'white':
|
||||||
self.pawn_direction = -1
|
self.pawn_direction = -1
|
||||||
else:
|
else:
|
||||||
|
@ -3,17 +3,14 @@ class Square:
|
|||||||
self.row = row
|
self.row = row
|
||||||
self.column = column
|
self.column = column
|
||||||
self.piece = piece
|
self.piece = piece
|
||||||
|
|
||||||
def has_piece(self):
|
def has_piece(self):
|
||||||
if self.piece is None:
|
if self.piece is None:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def is_rival(self, color):
|
def is_rival(self, color):
|
||||||
if self.has_piece() is True and self.piece.color != color:
|
if self.has_piece() is True and self.piece.color != color:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def empty_or_rival(self, color):
|
def empty_or_rival(self, color):
|
||||||
if self.is_rival(color) is True or self.has_piece() is False:
|
if self.is_rival(color) is True or self.has_piece() is False:
|
||||||
return True
|
return True
|
||||||
|
Loading…
Reference in New Issue
Block a user