new1
This commit is contained in:
parent
d6152ad658
commit
4ec8af3fd7
178
board.py
178
board.py
@ -25,6 +25,7 @@ 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
|
||||||
@ -41,6 +42,126 @@ class Board:
|
|||||||
#king
|
#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, King(color))
|
||||||
|
|
||||||
|
def calc_moves(self, piece, row, column):
|
||||||
|
def knight_moves():
|
||||||
|
#piece.delete_moves()
|
||||||
|
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.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():
|
||||||
|
#piece.delete_moves()
|
||||||
|
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.boardlist[self.final][column].has_piece() is False:
|
||||||
|
if self.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))
|
||||||
|
#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.boardlist[self.final][column].has_piece() is False:
|
||||||
|
piece.add_moves((self.final, column))
|
||||||
|
|
||||||
|
#diagonal
|
||||||
|
#czy wychodzi poza tablice i czy stoi rywal
|
||||||
|
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.boardlist[self.possible_row][possible_column].is_rival(piece.color) is True:
|
||||||
|
piece.add_moves((self.possible_row, possible_column))
|
||||||
|
def straightline_moves(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.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.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():
|
||||||
|
#piece.delete_moves()
|
||||||
|
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.boardlist[self.possible_row][self.possible_column].has_piece() is False:
|
||||||
|
piece.add_moves((self.possible_row, self.possible_column))
|
||||||
|
elif self.boardlist[self.possible_row][self.possible_column].is_rival(piece.color) is True:
|
||||||
|
piece.add_moves((self.possible_row, self.possible_column))
|
||||||
|
#calculate possible moves for specific piece
|
||||||
|
if piece.name == 'rook':
|
||||||
|
#piece.delete_moves()
|
||||||
|
straightline_moves(True)
|
||||||
|
elif piece.name == 'pawn':
|
||||||
|
pawn_moves()
|
||||||
|
elif piece.name == 'knight':
|
||||||
|
knight_moves()
|
||||||
|
elif piece.name == 'bishop':
|
||||||
|
#piece.delete_moves()
|
||||||
|
straightline_moves(False)
|
||||||
|
elif piece.name == 'queen':
|
||||||
|
#piece.delete_moves()
|
||||||
|
straightline_moves(True)
|
||||||
|
straightline_moves(False)
|
||||||
|
elif piece.name == 'king':
|
||||||
|
king_moves()
|
||||||
def move_rival(self, from_row, from_column, to_row, to_column):
|
def move_rival(self, from_row, from_column, to_row, to_column):
|
||||||
#update
|
#update
|
||||||
self.x = self.board.boardlist[from_row][from_column].piece
|
self.x = self.board.boardlist[from_row][from_column].piece
|
||||||
@ -48,7 +169,7 @@ class Board:
|
|||||||
if (self.board.boardlist[to_row][to_column].has_piece()):
|
if (self.board.boardlist[to_row][to_column].has_piece()):
|
||||||
if (self.board.boardlist[to_row][to_column].piece.name == 'king'):
|
if (self.board.boardlist[to_row][to_column].piece.name == 'king'):
|
||||||
self.board.boardlist[to_row][to_column].piece = self.x
|
self.board.boardlist[to_row][to_column].piece = self.x
|
||||||
return 1
|
exit(0)
|
||||||
self.board.boardlist[to_row][to_column].piece = self.x
|
self.board.boardlist[to_row][to_column].piece = self.x
|
||||||
self.board.boardlist[to_row][to_column].piece.moved = True
|
self.board.boardlist[to_row][to_column].piece.moved = True
|
||||||
self.board.boardlist[to_row][to_column].piece.delete_moves()
|
self.board.boardlist[to_row][to_column].piece.delete_moves()
|
||||||
@ -59,67 +180,44 @@ class Board:
|
|||||||
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].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
|
#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
|
||||||
return 1
|
exit(0)
|
||||||
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()
|
||||||
if self.initial_column == 0:
|
#print(self.board.boardlist[self.final_row][self.final_column].piece.name)
|
||||||
self.initial_column = "A"
|
#print(self.initial_row, self.initial_column)
|
||||||
elif self.initial_column == 1:
|
#print(self.final_row, self.final_column)
|
||||||
self.initial_column = "B"
|
|
||||||
elif self.initial_column == 2:
|
|
||||||
self.initial_column = "C"
|
|
||||||
elif self.initial_column == 3:
|
|
||||||
self.initial_column = "D"
|
|
||||||
elif self.initial_column == 4:
|
|
||||||
self.initial_column = "E"
|
|
||||||
elif self.initial_column == 5:
|
|
||||||
self.initial_column = "F"
|
|
||||||
elif self.initial_column == 6:
|
|
||||||
self.initial_column = "G"
|
|
||||||
elif self.initial_column == 7:
|
|
||||||
self.initial_column = "H"
|
|
||||||
|
|
||||||
if self.final_column == 0:
|
self.ruch = str(self.initial_row) + str(self.initial_column) + ' ' + str(self.final_row) + str(self.final_column)
|
||||||
self.final_column = "A"
|
|
||||||
elif self.final_column == 1:
|
|
||||||
self.final_column = "B"
|
|
||||||
elif self.final_column == 2:
|
|
||||||
self.final_column = "C"
|
|
||||||
elif self.final_column == 3:
|
|
||||||
self.final_column = "D"
|
|
||||||
elif self.final_column == 4:
|
|
||||||
self.final_column = "E"
|
|
||||||
elif self.final_column == 5:
|
|
||||||
self.final_column = "F"
|
|
||||||
elif self.final_column == 6:
|
|
||||||
self.final_column = "G"
|
|
||||||
elif self.final_column == 7:
|
|
||||||
self.final_column = "H"
|
|
||||||
self.ruch = str(self.initial_column)+str(self.initial_row) + ' ' +str(self.final_column)+ str(self.final_row)
|
|
||||||
return self.ruch
|
return self.ruch
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
86
game.py
86
game.py
@ -1,5 +1,4 @@
|
|||||||
from board import Board
|
from board import Board
|
||||||
from move import Move
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
@ -25,32 +24,39 @@ class Game:
|
|||||||
if self.color=='white':
|
if self.color=='white':
|
||||||
self.b.append('r')
|
self.b.append('r')
|
||||||
else: self.b.append('R')
|
else: self.b.append('R')
|
||||||
|
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||||
elif self.piece == 'pawn':
|
elif self.piece == 'pawn':
|
||||||
if self.color=='white':
|
if self.color=='white':
|
||||||
self.b.append('p')
|
self.b.append('p')
|
||||||
else:
|
else:
|
||||||
self.b.append('P')
|
self.b.append('P')
|
||||||
|
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||||
elif self.piece == 'knight':
|
elif self.piece == 'knight':
|
||||||
if self.color=='white':
|
if self.color=='white':
|
||||||
self.b.append('h')
|
self.b.append('h')
|
||||||
else:
|
else:
|
||||||
self.b.append('H')
|
self.b.append('H')
|
||||||
|
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||||
elif self.piece == 'bishop':
|
elif self.piece == 'bishop':
|
||||||
if self.color=='white':
|
if self.color=='white':
|
||||||
self.b.append('b')
|
self.b.append('b')
|
||||||
else:
|
else:
|
||||||
self.b.append('B')
|
self.b.append('B')
|
||||||
|
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||||
elif self.piece == 'queen':
|
elif self.piece == 'queen':
|
||||||
if self.color=='white':
|
if self.color=='white':
|
||||||
self.b.append('q')
|
self.b.append('q')
|
||||||
else:
|
else:
|
||||||
self.b.append('Q')
|
self.b.append('Q')
|
||||||
|
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||||
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')
|
||||||
|
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||||
else:
|
else:
|
||||||
|
# wyswietla puste miejsca
|
||||||
self.b.append(' ')
|
self.b.append(' ')
|
||||||
print(self.b)
|
print(self.b)
|
||||||
self.b = []
|
self.b = []
|
||||||
@ -65,59 +71,35 @@ class Game:
|
|||||||
self.b.append('7')
|
self.b.append('7')
|
||||||
print(self.b)
|
print(self.b)
|
||||||
print('')
|
print('')
|
||||||
|
# sprawdzenie
|
||||||
def currently_available_moves(self):
|
# H
|
||||||
for row in range(8):
|
# self.board.calc_moves(self.board.boardlist[7][6].piece, 7, 6)
|
||||||
for column in range(8):
|
# print(self.board.boardlist[7][6].piece.valid_moves)
|
||||||
Move.calc_moves(self, self.board.boardlist[row][column].piece, row, column)
|
# P
|
||||||
|
# print(self.board.boardlist[6][0].piece.valid_moves)
|
||||||
|
# R
|
||||||
|
#print(self.board.boardlist[5][1].piece.color)
|
||||||
|
#print(self.board.boardlist[5][1].piece.valid_moves)
|
||||||
def nextMove(self, ruch):
|
def nextMove(self, ruch):
|
||||||
self.currently_available_moves()
|
if ruch == 'noone':
|
||||||
if ruch == None:
|
return Board.move_boot(self, 'black')
|
||||||
return Board.move_boot(self, 'white')
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.from_column = ruch[0]
|
self.from_row = int(ruch[0])
|
||||||
self.from_row = int(ruch[1])
|
self.from_column=int(ruch[1])
|
||||||
self.to_column = ruch[3]
|
self.to_row=int(ruch[3])
|
||||||
self.to_row = int(ruch[4])
|
self.to_column=int(ruch[4])
|
||||||
|
|
||||||
if self.from_column == 'A':
|
|
||||||
self.from_column = 0
|
|
||||||
elif self.from_column == 'B':
|
|
||||||
self.from_column = 1
|
|
||||||
elif self.from_column == 'C':
|
|
||||||
self.from_column = 2
|
|
||||||
elif self.from_column == 'D':
|
|
||||||
self.from_column = 3
|
|
||||||
elif self.from_column == 'E':
|
|
||||||
self.from_column = 4
|
|
||||||
elif self.from_column == 'F':
|
|
||||||
self.from_column = 5
|
|
||||||
elif self.from_column == 'G':
|
|
||||||
self.from_column = 6
|
|
||||||
elif self.from_column == 'H':
|
|
||||||
self.from_column = 7
|
|
||||||
|
|
||||||
if self.to_column == 'A':
|
|
||||||
self.to_column = 0
|
|
||||||
elif self.to_column == 'B':
|
|
||||||
self.to_column = 1
|
|
||||||
elif self.to_column == 'C':
|
|
||||||
self.to_column = 2
|
|
||||||
elif self.to_column == 'D':
|
|
||||||
self.to_column = 3
|
|
||||||
elif self.to_column == 'E':
|
|
||||||
self.to_column = 4
|
|
||||||
elif self.to_column == 'F':
|
|
||||||
self.to_column = 5
|
|
||||||
elif self.to_column == 'G':
|
|
||||||
self.to_column = 6
|
|
||||||
elif self.to_column == 'H':
|
|
||||||
self.to_column = 7
|
|
||||||
|
|
||||||
self.color = Board.move_rival(self, self.from_row, self.from_column, self.to_row, self.to_column)
|
self.color = Board.move_rival(self, self.from_row, self.from_column, self.to_row, self.to_column)
|
||||||
return Board.move_boot(self, self.color)
|
return Board.move_boot(self, self.color)
|
||||||
|
|
||||||
|
"""
|
||||||
|
def make_move_rival(self):
|
||||||
|
self.przeciwnik = input(str('Podaj ruch: '))
|
||||||
|
self.from_row = int(self.przeciwnik[0])
|
||||||
|
self.from_column = int(self.przeciwnik[1])
|
||||||
|
self.to_row = int(self.przeciwnik[3])
|
||||||
|
self.to_column = int(self.przeciwnik[4])
|
||||||
|
return Board.move_rival(self, self.from_row, self.from_column, self.to_row, self.to_column)
|
||||||
|
def make_move_boot(self, color):
|
||||||
|
return Board.move_boot(self, color)
|
||||||
|
"""
|
19
main.py
19
main.py
@ -1,5 +1,6 @@
|
|||||||
from game import Game
|
from game import Game
|
||||||
|
#def mainloop(self):
|
||||||
|
# game = self.game
|
||||||
game = Game()
|
game = Game()
|
||||||
game2 = Game()
|
game2 = Game()
|
||||||
game.printBoard()
|
game.printBoard()
|
||||||
@ -7,19 +8,13 @@ print('')
|
|||||||
game2.printBoard()
|
game2.printBoard()
|
||||||
print('')
|
print('')
|
||||||
boot_color = 'white'
|
boot_color = 'white'
|
||||||
#a=0
|
#11 21
|
||||||
ruch=None
|
a=0
|
||||||
|
ruch='noone'
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
print(ruch)
|
||||||
ruch = game.nextMove(ruch)
|
ruch = game.nextMove(ruch)
|
||||||
if(ruch==1):
|
|
||||||
game.printBoard()
|
|
||||||
exit(0)
|
|
||||||
game.printBoard()
|
game.printBoard()
|
||||||
|
print(ruch)
|
||||||
ruch = game2.nextMove(ruch)
|
ruch = game2.nextMove(ruch)
|
||||||
if (ruch == 1):
|
|
||||||
game2.printBoard()
|
|
||||||
exit(0)
|
|
||||||
game2.printBoard()
|
game2.printBoard()
|
133
move.py
133
move.py
@ -1,133 +0,0 @@
|
|||||||
from typing import Tuple
|
|
||||||
|
|
||||||
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 board import Board
|
|
||||||
from square import Square
|
|
||||||
from piece import Piece
|
|
||||||
|
|
||||||
|
|
||||||
class Move:
|
|
||||||
@staticmethod
|
|
||||||
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 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 direct in self.direction:
|
|
||||||
self.left, self.right = direct
|
|
||||||
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 pawn_moves(self, piece, row, column):
|
|
||||||
if piece.moved:
|
|
||||||
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))
|
|
||||||
|
|
||||||
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 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))
|
|
||||||
|
|
||||||
|
|
||||||
def calc_moves(self, piece, row, column):
|
|
||||||
# calculate possible moves for specific piece
|
|
||||||
if piece == None:
|
|
||||||
pass
|
|
||||||
elif piece.name == 'rook':
|
|
||||||
# piece.delete_moves()
|
|
||||||
Move.straightline_moves(self, piece, column, row, True)
|
|
||||||
elif piece.name == 'pawn':
|
|
||||||
Move.pawn_moves(self, piece, column, row)
|
|
||||||
elif piece.name == 'knight':
|
|
||||||
Move.knight_moves(self, piece, column, row)
|
|
||||||
elif piece.name == 'bishop':
|
|
||||||
Move.straightline_moves(self, piece, column, row, False)
|
|
||||||
elif piece.name == 'queen':
|
|
||||||
Move.straightline_moves(self, piece, column, row, True)
|
|
||||||
Move.straightline_moves(self, piece, column, row, False)
|
|
||||||
elif piece.name == 'king':
|
|
||||||
Move.king_moves(self, piece, column, row)
|
|
@ -1,4 +1,5 @@
|
|||||||
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
|
||||||
@ -14,13 +15,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):
|
def empty_or_rival(self, color): # sprawdz
|
||||||
if self.has_piece() is False or self.is_rival(color):
|
if self.has_piece() is False or self.is_rival(color): # 1 has piece (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):
|
if self.has_piece() is False or self.is_rival(color): # 1 has piece (color)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user