without class piece_moves

This commit is contained in:
Agata Halik 2023-06-13 00:08:24 +02:00
parent 918a7f7b8f
commit 1a4c25911d
23 changed files with 213 additions and 166 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,39 @@
from piece import Piece
from board import Board
from square import Square
class Bishop(Piece):
def __init__(self, color):
super().__init__("bishop", color)
def straightline_moves(self, piece, row, column, isvertical):
if isvertical is True:
self.direction = [
(0, 1),
(0, -1),
(1, 0),
(-1, 0)]
else:
self.direction = [
(1, 1),
(-1, -1),
(1, -1),
(-1, 1)
]
for dir in self.direction:
self.left, self.right = dir
self.possible_row = row + self.left
self.possible_column = column + self.right
while True:
if Square.on_board(self.possible_row, self.possible_column) is True:
if self.board.boardlist[self.possible_row][self.possible_column].has_piece() is False:
piece.add_moves((self.possible_row, self.possible_column))
self.possible_row = self.possible_row + self.left
self.possible_column = self.possible_column + self.right
elif self.board.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, self.possible_column))
break
else:
break
else:
break

View File

@ -1,10 +1,5 @@
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
from piece import Piece
import random
class Board:
@ -25,20 +20,19 @@ class Board:
self.pawn_row = 1
self.other_row = 0
#pawns
#self.boardlist[5][1] = Square(5, 1, King(color)) #DO USUNIĘCIA
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, Piece("pawn", color)) #do kazdego square przypisuje poczatkowy pawn
#rooks
self.boardlist[self.other_row][0] = Square(self.other_row, 0, Rook(color))
self.boardlist[self.other_row][7] = Square(self.other_row, 7, Rook(color))
self.boardlist[self.other_row][0] = Square(self.other_row, 0, Piece("rook", color))
self.boardlist[self.other_row][7] = Square(self.other_row, 7, Piece("rook", color))
#knigths
self.boardlist[self.other_row][1] = Square(self.other_row, 1, Knight(color))
self.boardlist[self.other_row][6] = Square(self.other_row, 6, Knight(color))
self.boardlist[self.other_row][1] = Square(self.other_row, 1, Piece("knight", color))
self.boardlist[self.other_row][6] = Square(self.other_row, 6, Piece("knight", color))
#bishops
self.boardlist[self.other_row][2] = Square(self.other_row, 2, Bishop(color))
self.boardlist[self.other_row][5] = Square(self.other_row, 5, Bishop(color))
self.boardlist[self.other_row][2] = Square(self.other_row, 2, Piece("bishop",color))
self.boardlist[self.other_row][5] = Square(self.other_row, 5, Piece("bishop",color))
#queen
self.boardlist[self.other_row][3] = Square(self.other_row, 3, Queen(color))
self.boardlist[self.other_row][3] = Square(self.other_row, 3, Piece("queen", color))
#king
self.boardlist[self.other_row][4] = Square(self.other_row, 4, King(color))
self.boardlist[self.other_row][4] = Square(self.other_row, 4, Piece("king", color))

View File

@ -1,6 +1,5 @@
from board import Board
from move import Move
from piece_moves import Piece_moves
import random
class Game:
@ -73,7 +72,7 @@ class Game:
if self.board.boardlist[row][column].has_piece():
self.piece = self.board.boardlist[row][column].piece
#self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
Piece_moves.calc_moves(self, self.piece, row, column)
Move.calc_moves(self, self.piece, row, column)
def nextMove(self, ruch):
self.currently_available_moves()
if ruch == None:

23
king.py
View File

@ -1,4 +1,27 @@
from piece import Piece
from board import Board
from square import Square
class King(Piece):
def __init__(self, color):
super().__init__("king", color)
def king_moves(self, piece, row, column):
self.direction = [
(0, 1),
(0, -1),
(1, 0),
(-1, 0),
(1, 1),
(-1, -1),
(1, -1),
(-1, 1)
]
for dir in self.direction:
self.left, self.right = dir
self.possible_row = row + self.left
self.possible_column = column + self.right
if Square.on_board(self.possible_row, self.possible_column) is True:
if self.board.boardlist[self.possible_row][self.possible_column].has_piece() is False:
piece.add_moves((self.possible_row, self.possible_column))
elif self.board.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, self.possible_column))

View File

@ -1,5 +1,24 @@
from piece import Piece
from board import Board
from square import Square
class Knight(Piece):
def __init__(self, color):
super().__init__("knight", color)
def knight_moves(self, piece, row, column):
self.possible_moves = [
(row + 2, column - 1),
(row + 2, column + 1),
(row + 1, column + 2),
(row - 1, column + 2),
(row - 2, column + 1),
(row - 2, column - 1),
(row - 1, column - 2),
(row + 1, column - 2)
]
for self.possible_move in self.possible_moves:
self.possible_row, self.possible_column = self.possible_move
if Square.on_board(self.possible_row, self.possible_column):
if self.board.boardlist[self.possible_row][self.possible_column].empty_or_rival(piece.color):
self.final = (self.possible_row, self.possible_column)
piece.add_moves(self.final)

27
move.py
View File

@ -66,7 +66,26 @@ class Move:
return self.x, self.move_initial_row, self.move_initial_column, self. move_final_row, self.move_final_column
def calc_moves(self, piece, row, column):
piece.valid_moves=[]
if piece.name == 'rook':
Rook.straightline_moves(self, piece, row, column,True)
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
Bishop.straightline_moves(self, piece, row, column,False)
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
def pawn_promotion(self, x, final_row, final_column):
if isinstance(x, Pawn):
@ -76,9 +95,9 @@ class Move:
if self.board.boardlist[final_row][final_column].has_piece():
if self.board.boardlist[final_row][final_column].piece.name == 'king':
#print(final_column, final_row)
print(x)
print(x.color)
print(self.board.boardlist[final_row][final_column].piece.color)
#print(x)
#print(x.color)
#print(self.board.boardlist[final_row][final_column].piece.color)
self.board.boardlist[final_row][final_column].piece = x
exit(0)
@staticmethod

29
pawn.py
View File

@ -1,5 +1,32 @@
from piece import Piece
from board import Board
from square import Square
class Pawn(Piece):
def __init__(self, color):
super().__init__("pawn", color)
def pawn_moves(self, piece, row, column):
if piece.moved == True:
self.steps = 1
else:
self.steps = 2
if self.steps == 2:
self.final = row + (piece.pawn_direction * (self.steps))
self.previous = row + (piece.pawn_direction * (self.steps - 1))
if Square.on_board(self.final, column) is True:
if self.board.boardlist[self.final][column].has_piece() is False:
if self.board.boardlist[self.previous][column].has_piece() is False:
piece.add_moves((self.final, column))
self.steps = 1
self.final = row + (piece.pawn_direction * (self.steps))
if Square.on_board(self.final, column) is True and self.board.boardlist[self.final][column].has_piece() is False:
piece.add_moves((self.final, column))
# diagonal
self.possible_row = row + piece.pawn_direction
self.possible_columns = []
self.possible_columns = [column - 1, column + 1]
for possible_column in self.possible_columns:
if Square.on_board(self.possible_row, possible_column) is True:
if self.board.boardlist[self.possible_row][possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, possible_column))

View File

@ -8,7 +8,6 @@ class Piece:
self.pawn_direction = -1
else:
self.pawn_direction = 1
def add_moves(self, move):
self.valid_moves.append(move)

View File

@ -1,132 +0,0 @@
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
class Piece_moves:
def calc_moves(self, piece, row, column):
piece.valid_moves=[]
if piece.name == 'rook':
Piece_moves.straightline_moves(self, piece, row, column,True)
pass
elif piece.name == 'pawn':
Piece_moves.pawn_moves(self, piece, row, column)
pass
elif piece.name == 'knight':
Piece_moves.knight_moves(self, piece, row, column)
elif piece.name == 'bishop':
pass
Piece_moves.straightline_moves(self, piece, row, column,False)
elif piece.name == 'queen':
pass
Piece_moves.straightline_moves(self, piece, row, column,True)
Piece_moves.straightline_moves(self, piece, row, column,False)
elif piece.name == 'king':
Piece_moves.king_moves(self, piece, row, column)
pass
#print(piece)
#print(piece.valid_moves)
def knight_moves(self, piece, row, column):
self.possible_moves = [
(row + 2, column - 1),
(row + 2, column + 1),
(row + 1, column + 2),
(row - 1, column + 2),
(row - 2, column + 1),
(row - 2, column - 1),
(row - 1, column - 2),
(row + 1, column - 2)
]
for self.possible_move in self.possible_moves:
self.possible_row, self.possible_column = self.possible_move
if Square.on_board(self.possible_row, self.possible_column):
if self.board.boardlist[self.possible_row][self.possible_column].empty_or_rival(piece.color):
self.final = (self.possible_row, self.possible_column)
piece.add_moves(self.final)
def pawn_moves(self, piece, row, column):
if piece.moved == True:
self.steps = 1
else:
self.steps = 2
if self.steps == 2:
self.final = row + (piece.pawn_direction * (self.steps))
self.previous = row + (piece.pawn_direction * (self.steps - 1))
if Square.on_board(self.final, column) is True:
if self.board.boardlist[self.final][column].has_piece() is False:
if self.board.boardlist[self.previous][column].has_piece() is False:
piece.add_moves((self.final, column))
self.steps = 1
self.final = row + (piece.pawn_direction * (self.steps))
if Square.on_board(self.final, column) is True and self.board.boardlist[self.final][column].has_piece() is False:
piece.add_moves((self.final, column))
# diagonal
self.possible_row = row + piece.pawn_direction
self.possible_columns = []
self.possible_columns = [column - 1, column + 1]
for possible_column in self.possible_columns:
if Square.on_board(self.possible_row, possible_column) is True:
if self.board.boardlist[self.possible_row][possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, possible_column))
def straightline_moves(self, piece, row, column, isvertical):
if isvertical is True:
self.direction = [
(0, 1),
(0, -1),
(1, 0),
(-1, 0)]
else:
self.direction = [
(1, 1),
(-1, -1),
(1, -1),
(-1, 1)
]
for dir in self.direction:
self.left, self.right = dir
self.possible_row = row + self.left
self.possible_column = column + self.right
while True:
if Square.on_board(self.possible_row, self.possible_column) is True:
if self.board.boardlist[self.possible_row][self.possible_column].has_piece() is False:
piece.add_moves((self.possible_row, self.possible_column))
self.possible_row = self.possible_row + self.left
self.possible_column = self.possible_column + self.right
elif self.board.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, self.possible_column))
break
else:
break
else:
break
def king_moves(self, piece, row, column):
self.direction = [
(0, 1),
(0, -1),
(1, 0),
(-1, 0),
(1, 1),
(-1, -1),
(1, -1),
(-1, 1)
]
for dir in self.direction:
self.left, self.right = dir
self.possible_row = row + self.left
self.possible_column = column + self.right
if Square.on_board(self.possible_row, self.possible_column) is True:
if self.board.boardlist[self.possible_row][self.possible_column].has_piece() is False:
piece.add_moves((self.possible_row, self.possible_column))
elif self.board.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, self.possible_column))

View File

@ -1,5 +1,38 @@
from piece import Piece
from board import Board
from square import Square
class Queen(Piece):
def __init__(self, color):
super().__init__("queen", color)
def straightline_moves(self, piece, row, column, isvertical):
if isvertical is True:
self.direction = [
(0, 1),
(0, -1),
(1, 0),
(-1, 0)]
else:
self.direction = [
(1, 1),
(-1, -1),
(1, -1),
(-1, 1)
]
for dir in self.direction:
self.left, self.right = dir
self.possible_row = row + self.left
self.possible_column = column + self.right
while True:
if Square.on_board(self.possible_row, self.possible_column) is True:
if self.board.boardlist[self.possible_row][self.possible_column].has_piece() is False:
piece.add_moves((self.possible_row, self.possible_column))
self.possible_row = self.possible_row + self.left
self.possible_column = self.possible_column + self.right
elif self.board.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, self.possible_column))
break
else:
break
else:
break

35
rook.py
View File

@ -1,5 +1,38 @@
from piece import Piece
from board import Board
from square import Square
class Rook(Piece):
def __init__(self, color):
super().__init__("rook", color)
def straightline_moves(self, piece, row, column, isvertical):
if isvertical is True:
self.direction = [
(0, 1),
(0, -1),
(1, 0),
(-1, 0)]
else:
self.direction = [
(1, 1),
(-1, -1),
(1, -1),
(-1, 1)
]
for dir in self.direction:
self.left, self.right = dir
self.possible_row = row + self.left
self.possible_column = column + self.right
while True:
if Square.on_board(self.possible_row, self.possible_column) is True:
if self.board.boardlist[self.possible_row][self.possible_column].has_piece() is False:
piece.add_moves((self.possible_row, self.possible_column))
self.possible_row = self.possible_row + self.left
self.possible_column = self.possible_column + self.right
elif self.board.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
piece.add_moves((self.possible_row, self.possible_column))
break
else:
break
else:
break

View File

@ -25,7 +25,6 @@ class Square:
return True
else:
return False
@staticmethod
def on_board(*args):
for arg in args: