new class move
This commit is contained in:
parent
010e4a626c
commit
b4cfdd77fb
120
board.py
120
board.py
@ -42,126 +42,6 @@ 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():
|
||||
#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):
|
||||
#update
|
||||
self.x = self.board.boardlist[from_row][from_column].piece
|
||||
|
14
game.py
14
game.py
@ -1,4 +1,5 @@
|
||||
from board import Board
|
||||
from move import Move
|
||||
import random
|
||||
|
||||
class Game:
|
||||
@ -24,37 +25,38 @@ class Game:
|
||||
if self.color=='white':
|
||||
self.b.append('r')
|
||||
else: self.b.append('R')
|
||||
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||
|
||||
Move.calc_moves(self, self.board.boardlist[row][column].piece, row, column)
|
||||
elif self.piece == 'pawn':
|
||||
if self.color=='white':
|
||||
self.b.append('p')
|
||||
else:
|
||||
self.b.append('P')
|
||||
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||
Move.calc_moves(self, self.board.boardlist[row][column].piece, row, column)
|
||||
elif self.piece == 'knight':
|
||||
if self.color=='white':
|
||||
self.b.append('h')
|
||||
else:
|
||||
self.b.append('H')
|
||||
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||
Move.calc_moves(self, self.board.boardlist[row][column].piece, row, column)
|
||||
elif self.piece == 'bishop':
|
||||
if self.color=='white':
|
||||
self.b.append('b')
|
||||
else:
|
||||
self.b.append('B')
|
||||
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||
Move.calc_moves(self, self.board.boardlist[row][column].piece, row, column)
|
||||
elif self.piece == 'queen':
|
||||
if self.color=='white':
|
||||
self.b.append('q')
|
||||
else:
|
||||
self.b.append('Q')
|
||||
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||
Move.calc_moves(self, self.board.boardlist[row][column].piece, row, column)
|
||||
elif self.piece == 'king':
|
||||
if self.color=='white':
|
||||
self.b.append('k')
|
||||
else:
|
||||
self.b.append('K')
|
||||
self.board.calc_moves(self.board.boardlist[row][column].piece, row, column)
|
||||
Move.calc_moves(self, self.board.boardlist[row][column].piece, row, column)
|
||||
else:
|
||||
# wyswietla puste miejsca
|
||||
self.b.append(' ')
|
||||
|
31
main.py
31
main.py
@ -1,6 +1,5 @@
|
||||
from game import Game
|
||||
#def mainloop(self):
|
||||
# game = self.game
|
||||
|
||||
game = Game()
|
||||
game2 = Game()
|
||||
game.printBoard()
|
||||
@ -8,7 +7,6 @@ print('')
|
||||
game2.printBoard()
|
||||
print('')
|
||||
boot_color = 'white'
|
||||
#11 21
|
||||
a=0
|
||||
ruch='noone'
|
||||
while True:
|
||||
@ -18,7 +16,7 @@ while True:
|
||||
ruch = game.nextMove(ruch)
|
||||
if(ruch==1):
|
||||
game.printBoard()
|
||||
print(game)
|
||||
#print(game)
|
||||
exit(0)
|
||||
game.printBoard()
|
||||
print('')
|
||||
@ -27,27 +25,6 @@ while True:
|
||||
ruch = game2.nextMove(ruch)
|
||||
if (ruch == 1):
|
||||
game2.printBoard()
|
||||
print(game2)
|
||||
#print(game2)
|
||||
exit(0)
|
||||
game2.printBoard()
|
||||
|
||||
|
||||
# a = a + 1
|
||||
#print(a)
|
||||
#if game.make_move_boot('white') == 1:
|
||||
# print("Przegrana")
|
||||
# game.show_board()
|
||||
# break
|
||||
#if game.make_move_rival()==1:
|
||||
# print("Przegrana")
|
||||
# break
|
||||
#game.show_board()
|
||||
#print('')
|
||||
#if game.make_move_boot('black') == 1:
|
||||
# print("Przegrana")
|
||||
# game.show_board()
|
||||
# break
|
||||
#game.show_board()
|
||||
#print('')
|
||||
|
||||
|
||||
game2.printBoard()
|
143
move.py
Normal file
143
move.py
Normal file
@ -0,0 +1,143 @@
|
||||
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):
|
||||
# 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.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):
|
||||
# piece.delete_moves()
|
||||
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)
|
||||
# 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][
|
||||
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.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):
|
||||
# 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.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.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)
|
||||
#knight_moves()
|
||||
elif piece.name == 'bishop':
|
||||
# piece.delete_moves()
|
||||
Move.straightline_moves(self, piece, column, row, False)
|
||||
elif piece.name == 'queen':
|
||||
# piece.delete_moves()
|
||||
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)
|
Loading…
Reference in New Issue
Block a user