class piece_move
This commit is contained in:
parent
bf069ba0fe
commit
918a7f7b8f
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.
BIN
__pycache__/piece_moves.cpython-310.pyc
Normal file
BIN
__pycache__/piece_moves.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
__pycache__/valid_move.cpython-310.pyc
Normal file
BIN
__pycache__/valid_move.cpython-310.pyc
Normal file
Binary file not shown.
110
board.py
110
board.py
@ -42,113 +42,3 @@ class Board:
|
||||
#king
|
||||
self.boardlist[self.other_row][4] = Square(self.other_row, 4, King(color))
|
||||
|
||||
def calc_moves(self, piece, row, column):
|
||||
def knight_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():
|
||||
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))
|
||||
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
|
||||
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():
|
||||
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':
|
||||
straightline_moves(True)
|
||||
elif piece.name == 'pawn':
|
||||
pawn_moves()
|
||||
elif piece.name == 'knight':
|
||||
knight_moves()
|
||||
elif piece.name == 'bishop':
|
||||
straightline_moves(False)
|
||||
elif piece.name == 'queen':
|
||||
straightline_moves(True)
|
||||
straightline_moves(False)
|
||||
elif piece.name == 'king':
|
||||
king_moves()
|
||||
|
4
game.py
4
game.py
@ -1,5 +1,6 @@
|
||||
from board import Board
|
||||
from move import Move
|
||||
from piece_moves import Piece_moves
|
||||
import random
|
||||
|
||||
class Game:
|
||||
@ -71,7 +72,8 @@ class Game:
|
||||
for column in range(8):
|
||||
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)
|
||||
#self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||
Piece_moves.calc_moves(self, self.piece, row, column)
|
||||
def nextMove(self, ruch):
|
||||
self.currently_available_moves()
|
||||
if ruch == None:
|
||||
|
1
king.py
1
king.py
@ -1,5 +1,4 @@
|
||||
from piece import Piece
|
||||
|
||||
class King(Piece):
|
||||
def __init__(self, color):
|
||||
super().__init__("king", color)
|
4
main.py
4
main.py
@ -1,8 +1,8 @@
|
||||
from game import Game
|
||||
game = Game()
|
||||
game2 = Game()
|
||||
game.printBoard()
|
||||
game2.printBoard()
|
||||
#game.printBoard()
|
||||
#game2.printBoard()
|
||||
ruch=None
|
||||
while True:
|
||||
#print(ruch)
|
||||
|
6
move.py
6
move.py
@ -76,8 +76,10 @@ 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)
|
||||
#self.board.boardlist[final_row][final_column].piece = x
|
||||
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
|
||||
def numbers_to_letters(number):
|
||||
|
132
piece_moves.py
Normal file
132
piece_moves.py
Normal file
@ -0,0 +1,132 @@
|
||||
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))
|
||||
|
@ -12,10 +12,11 @@ class Square:
|
||||
|
||||
def is_rival(self, color):
|
||||
if self.has_piece() is True and self.piece.color != color:
|
||||
#print(self.piece.color, color, self.piece.name)
|
||||
return True
|
||||
|
||||
def empty_or_rival(self, color):
|
||||
if self.has_piece() is False or self.is_rival(color):
|
||||
if self.is_rival(color) is True or self.has_piece() is False:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
Loading…
Reference in New Issue
Block a user