function random_valid_move
This commit is contained in:
parent
c50e466595
commit
bf069ba0fe
Binary file not shown.
69
move.py
69
move.py
@ -27,6 +27,21 @@ class Move:
|
|||||||
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):
|
||||||
|
self.x, self.initial_row, self.initial_column, self.final_row, self.final_column = Move.random_valid_move(self, color)
|
||||||
|
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
|
||||||
|
Move.pawn_promotion(self, self.x, self.final_row, self.final_column)
|
||||||
|
#update piece
|
||||||
|
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.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 random_valid_move(self, color):
|
||||||
self.boot_piece = []
|
self.boot_piece = []
|
||||||
self.piece_row_column = []
|
self.piece_row_column = []
|
||||||
for r in range(0, 8):
|
for r in range(0, 8):
|
||||||
@ -34,56 +49,34 @@ class Move:
|
|||||||
if self.board.boardlist[r][c].piece != None:
|
if self.board.boardlist[r][c].piece != None:
|
||||||
if self.board.boardlist[r][c].piece.color != color:
|
if self.board.boardlist[r][c].piece.color != color:
|
||||||
self.boot_piece.append(self.board.boardlist[r][c])
|
self.boot_piece.append(self.board.boardlist[r][c])
|
||||||
|
#random piece with valid_moves>0
|
||||||
self.value = random.randrange(0, len(self.boot_piece))
|
self.value = random.randrange(0, len(self.boot_piece))
|
||||||
self.initial = self.boot_piece[self.value]
|
self.initial_piece = self.boot_piece[self.value]
|
||||||
while len(self.initial.piece.valid_moves) == 0:
|
while len(self.initial_piece.piece.valid_moves) == 0:
|
||||||
self.value = random.randrange(0, len(self.boot_piece))
|
self.value = random.randrange(0, len(self.boot_piece))
|
||||||
self.initial = self.boot_piece[self.value]
|
self.initial_piece = self.boot_piece[self.value]
|
||||||
self.initial_row=self.initial.row
|
self.move_initial_row = self.initial_piece.row
|
||||||
self.initial_column = self.initial.column
|
self.move_initial_column = self.initial_piece.column
|
||||||
self.value = random.randrange(0, len(self.initial.piece.valid_moves))
|
#random move for piece
|
||||||
self.final = self.initial.piece.valid_moves[self.value]
|
self.value = random.randrange(0, len(self.initial_piece.piece.valid_moves))
|
||||||
self.final_row = self.final[0]
|
self.move_final = self.initial_piece.piece.valid_moves[self.value]
|
||||||
self.final_column = self.final[1]
|
self.move_final_row = self.move_final[0]
|
||||||
self.x = self.board.boardlist[self.initial_row][self.initial_column].piece
|
self.move_final_column = self.move_final[1]
|
||||||
self.board.boardlist[self.initial_row][self.initial_column].piece = None
|
self.x = self.board.boardlist[self.move_initial_row][self.move_initial_column].piece
|
||||||
#mat
|
|
||||||
Move.mat(self, self.x, self.final_row, self.final_column)
|
|
||||||
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
|
||||||
|
|
||||||
#pawn promotion
|
return self.x, self.move_initial_row, self.move_initial_column, self. move_final_row, self.move_final_column
|
||||||
Move.pawn_promotion(self, self.x, self.final_row, self.final_column)
|
|
||||||
|
|
||||||
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 pawn_promotion(self, x, final_row, final_column):
|
def pawn_promotion(self, x, final_row, final_column):
|
||||||
if isinstance(x, Pawn):
|
if isinstance(x, Pawn):
|
||||||
if (final_row==0) or (final_row==7):
|
if (final_row==0) or (final_row==7):
|
||||||
self.board.boardlist[final_row][final_column].piece = Queen(x.color)
|
self.board.boardlist[final_row][final_column].piece = Queen(x.color)
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
print(final_column, final_row)
|
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
print("promotion")
|
|
||||||
def mat(self, x, final_row, final_column):
|
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].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(final_column, final_row)
|
#print(final_column, final_row)
|
||||||
print(x)
|
#print(x)
|
||||||
#self.board.boardlist[final_row][final_column].piece = x
|
#self.board.boardlist[final_row][final_column].piece = x
|
||||||
exit(0)
|
exit(0)
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -111,7 +104,7 @@ class Move:
|
|||||||
if letter == 'A':
|
if letter == 'A':
|
||||||
letter = 0
|
letter = 0
|
||||||
elif letter == 'B':
|
elif letter == 'B':
|
||||||
letter=1
|
letter = 1
|
||||||
elif letter == 'C':
|
elif letter == 'C':
|
||||||
letter = 2
|
letter = 2
|
||||||
elif letter == 'D':
|
elif letter == 'D':
|
||||||
|
Loading…
Reference in New Issue
Block a user