This commit is contained in:
Agata Halik 2023-06-09 00:22:01 +02:00
parent a37c688eef
commit d6152ad658
5 changed files with 7 additions and 42 deletions

View File

@ -25,7 +25,6 @@ class Board:
self.pawn_row = 1 self.pawn_row = 1
self.other_row = 0 self.other_row = 0
#pawns #pawns
#self.boardlist[5][1] = Square(5, 1, King(color)) #DO USUNIĘCIA
for column in range(8): for column in range(8):
self.boardlist[self.pawn_row][column] = Square(self.pawn_row, column, Pawn(color)) #do kazdego square przypisuje poczatkowy pawn self.boardlist[self.pawn_row][column] = Square(self.pawn_row, column, Pawn(color)) #do kazdego square przypisuje poczatkowy pawn
#rooks #rooks
@ -63,31 +62,21 @@ class Board:
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 #random piece
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 = self.boot_piece[self.value]
while len(self.initial.piece.valid_moves) == 0: while len(self.initial.piece.valid_moves) == 0:
#print('xddd')
#print(self.initial.piece)
#print(self.initial.piece.valid_moves)
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 = self.boot_piece[self.value]
self.initial_row=self.initial.row self.initial_row=self.initial.row
self.initial_column = self.initial.column self.initial_column = self.initial.column
#print(self.initial)
#random move #random move
self.value = random.randrange(0, len(self.initial.piece.valid_moves)) self.value = random.randrange(0, len(self.initial.piece.valid_moves))
self.final = self.initial.piece.valid_moves[self.value] self.final = self.initial.piece.valid_moves[self.value]
self.final_row = self.final[0] self.final_row = self.final[0]
self.final_column = self.final[1] self.final_column = self.final[1]
#print(self.final)
#print(self.final_row)
#update #update
self.x = self.board.boardlist[self.initial_row][self.initial_column].piece self.x = self.board.boardlist[self.initial_row][self.initial_column].piece
#print(self.board.boardlist[self.initial_row][self.initial_column].piece.name)
#print(self.initial_row, self.initial_column)
self.board.boardlist[self.initial_row][self.initial_column].piece = None self.board.boardlist[self.initial_row][self.initial_column].piece = None
#print(self.final_row, self.final_column)
if (self.board.boardlist[self.final_row][self.final_column].has_piece()): 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'): 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 self.board.boardlist[self.final_row][self.final_column].piece = self.x
@ -95,9 +84,6 @@ class Board:
self.board.boardlist[self.final_row][self.final_column].piece = self.x self.board.boardlist[self.final_row][self.final_column].piece = self.x
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()
#print(self.board.boardlist[self.final_row][self.final_column].piece.name)
#print(self.initial_row, self.initial_column)
#print(self.final_row, self.final_column)
if self.initial_column == 0: if self.initial_column == 0:
self.initial_column = "A" self.initial_column = "A"
elif self.initial_column == 1: elif self.initial_column == 1:

View File

@ -46,12 +46,11 @@ class Game:
else: else:
self.b.append('Q') self.b.append('Q')
elif self.piece == 'king': elif self.piece == 'king':
if self.color=='white': if self.color == 'white':
self.b.append('k') self.b.append('k')
else: else:
self.b.append('K') self.b.append('K')
else: else:
# wyswietla puste miejsca
self.b.append(' ') self.b.append(' ')
print(self.b) print(self.b)
self.b = [] self.b = []
@ -65,6 +64,7 @@ class Game:
self.b.append('6') self.b.append('6')
self.b.append('7') self.b.append('7')
print(self.b) print(self.b)
print('')
def currently_available_moves(self): def currently_available_moves(self):
for row in range(8): for row in range(8):
@ -73,7 +73,7 @@ class Game:
def nextMove(self, ruch): def nextMove(self, ruch):
self.currently_available_moves() self.currently_available_moves()
if ruch == 'noone': if ruch == None:
return Board.move_boot(self, 'white') return Board.move_boot(self, 'white')
else: else:

10
main.py
View File

@ -8,26 +8,18 @@ game2.printBoard()
print('') print('')
boot_color = 'white' boot_color = 'white'
#a=0 #a=0
ruch='noone' ruch=None
while True: while True:
print('')
#a=a+1
#print(a)
ruch = game.nextMove(ruch) ruch = game.nextMove(ruch)
if(ruch==1): if(ruch==1):
game.printBoard() game.printBoard()
#print(game)
exit(0) exit(0)
game.printBoard() game.printBoard()
print('')
#a = a + 1
#print(a)
ruch = game2.nextMove(ruch) ruch = game2.nextMove(ruch)
if (ruch == 1): if (ruch == 1):
game2.printBoard() game2.printBoard()
#print(game2)
exit(0) exit(0)
game2.printBoard() game2.printBoard()

12
move.py
View File

@ -14,7 +14,6 @@ from piece import Piece
class Move: class Move:
@staticmethod @staticmethod
def knight_moves(self, piece, row, column): def knight_moves(self, piece, row, column):
# piece.delete_moves()
self.possible_moves = [ self.possible_moves = [
(row + 2, column - 1), (row + 2, column - 1),
(row + 2, column + 1), (row + 2, column + 1),
@ -66,7 +65,6 @@ class Move:
break break
def pawn_moves(self, piece, row, column): def pawn_moves(self, piece, row, column):
# piece.delete_moves()
if piece.moved: if piece.moved:
self.steps = 1 self.steps = 1
else: else:
@ -81,16 +79,10 @@ class Move:
piece.add_moves((self.final, column)) piece.add_moves((self.final, column))
self.steps = 1 self.steps = 1
self.final = row + (piece.pawn_direction * self.steps) self.final = row + (piece.pawn_direction * self.steps)
# problem z wierszem
# if self.final>7 or self.final<0 or column>7 or column <0:
# print(piece.name)
# print(self.final, column)
if Square.on_board(self.final, column) is True and self.board.boardlist[self.final][ if Square.on_board(self.final, column) is True and self.board.boardlist[self.final][
column].has_piece() is False: column].has_piece() is False:
piece.add_moves((self.final, column)) piece.add_moves((self.final, column))
# diagonal
# czy wychodzi poza tablice i czy stoi rywal
self.possible_row = row + piece.pawn_direction self.possible_row = row + piece.pawn_direction
self.possible_columns = [] self.possible_columns = []
self.possible_columns = [column - 1, column + 1] self.possible_columns = [column - 1, column + 1]
@ -100,7 +92,6 @@ class Move:
piece.add_moves((self.possible_row, possible_column)) piece.add_moves((self.possible_row, possible_column))
def king_moves(self, piece, row, column): def king_moves(self, piece, row, column):
# piece.delete_moves()
self.direction = [ self.direction = [
(0, 1), (0, 1),
(0, -1), (0, -1),
@ -133,12 +124,9 @@ class Move:
Move.pawn_moves(self, piece, column, row) Move.pawn_moves(self, piece, column, row)
elif piece.name == 'knight': elif piece.name == 'knight':
Move.knight_moves(self, piece, column, row) Move.knight_moves(self, piece, column, row)
#knight_moves()
elif piece.name == 'bishop': elif piece.name == 'bishop':
# piece.delete_moves()
Move.straightline_moves(self, piece, column, row, False) Move.straightline_moves(self, piece, column, row, False)
elif piece.name == 'queen': elif piece.name == 'queen':
# piece.delete_moves()
Move.straightline_moves(self, piece, column, row, True) Move.straightline_moves(self, piece, column, row, True)
Move.straightline_moves(self, piece, column, row, False) Move.straightline_moves(self, piece, column, row, False)
elif piece.name == 'king': elif piece.name == 'king':

View File

@ -1,5 +1,4 @@
class Square: class Square:
# tworzenie pól
def __init__(self, row, column, piece=None): def __init__(self, row, column, piece=None):
self.row = row self.row = row
self.column = column self.column = column
@ -15,13 +14,13 @@ class Square:
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): # sprawdz def empty_or_rival(self, color):
if self.has_piece() is False or self.is_rival(color): # 1 has piece (color) if self.has_piece() is False or self.is_rival(color):
return True return True
else: else:
return False return False
def empty_or_rival_knight(self, color): # sprawdz def empty_or_rival_knight(self, color): # sprawdz
if self.has_piece() is False or self.is_rival(color): # 1 has piece (color) if self.has_piece() is False or self.is_rival(color):
return True return True
else: else:
return False return False