Szach
This commit is contained in:
parent
60b15d7996
commit
5c9d3fd678
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7
board.py
7
board.py
@ -19,6 +19,11 @@ class Board:
|
|||||||
else:
|
else:
|
||||||
self.pawn_row = 1
|
self.pawn_row = 1
|
||||||
self.other_row = 0
|
self.other_row = 0
|
||||||
|
|
||||||
|
self.boardlist[0][0] = Square(0, 0, Piece("king", 'black'))
|
||||||
|
self.boardlist[2][2] = Square(2, 2, Piece("queen", 'white'))
|
||||||
|
self.boardlist[2][2] = Square(2, 2, Piece("king", 'white'))
|
||||||
|
"""
|
||||||
#pawns
|
#pawns
|
||||||
for column in range(8):
|
for column in range(8):
|
||||||
self.boardlist[self.pawn_row][column] = Square(self.pawn_row, column, Piece("pawn", color))
|
self.boardlist[self.pawn_row][column] = Square(self.pawn_row, column, Piece("pawn", color))
|
||||||
@ -35,4 +40,4 @@ class Board:
|
|||||||
self.boardlist[self.other_row][3] = Square(self.other_row, 3, Piece("queen", color))
|
self.boardlist[self.other_row][3] = Square(self.other_row, 3, Piece("queen", color))
|
||||||
#king
|
#king
|
||||||
self.boardlist[self.other_row][4] = Square(self.other_row, 4, Piece("king", color))
|
self.boardlist[self.other_row][4] = Square(self.other_row, 4, Piece("king", color))
|
||||||
|
"""
|
||||||
|
1
game.py
1
game.py
@ -78,5 +78,6 @@ class Game:
|
|||||||
return Move.move_boot(self, 'black')
|
return Move.move_boot(self, 'black')
|
||||||
else:
|
else:
|
||||||
self.color = Move.move_rival(self, ruch)
|
self.color = Move.move_rival(self, ruch)
|
||||||
|
self.currently_available_moves()
|
||||||
return Move.move_boot(self, self.color)
|
return Move.move_boot(self, self.color)
|
||||||
|
|
||||||
|
1
king.py
1
king.py
@ -2,6 +2,7 @@ from piece import Piece
|
|||||||
from board import Board
|
from board import Board
|
||||||
from square import Square
|
from square import Square
|
||||||
class King(Piece):
|
class King(Piece):
|
||||||
|
@staticmethod
|
||||||
def king_moves(self, piece, row, column):
|
def king_moves(self, piece, row, column):
|
||||||
self.direction = [
|
self.direction = [
|
||||||
(0, 1),
|
(0, 1),
|
||||||
|
14
main.py
14
main.py
@ -1,11 +1,15 @@
|
|||||||
from game import Game
|
from game import Game
|
||||||
|
import time
|
||||||
game = Game()
|
game = Game()
|
||||||
game2 = Game()
|
game2 = Game()
|
||||||
#game.printBoard()
|
game.printBoard()
|
||||||
#game2.printBoard()
|
#game2.printBoard()
|
||||||
ruch=None
|
ruch=None
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
ruch = game.nextMove(ruch)
|
print("gra bialy")
|
||||||
game.printBoard()
|
time.sleep(2)
|
||||||
ruch = game2.nextMove(ruch)
|
ruch=input("Podaj:c")
|
||||||
game2.printBoard()
|
game.nextMove(ruch)
|
||||||
|
game.printBoard()
|
100
move.py
100
move.py
@ -9,8 +9,6 @@ from king import King
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
class Move:
|
class Move:
|
||||||
def szach(self):
|
|
||||||
pass
|
|
||||||
def move_rival(self, ruch):
|
def move_rival(self, ruch):
|
||||||
self.from_column = Move.letters_to__numbers(ruch[0])
|
self.from_column = Move.letters_to__numbers(ruch[0])
|
||||||
self.from_row = int(ruch[1])
|
self.from_row = int(ruch[1])
|
||||||
@ -20,27 +18,27 @@ class Move:
|
|||||||
#update board
|
#update board
|
||||||
self.x = self.board.boardlist[self.from_row][self.from_column].piece
|
self.x = self.board.boardlist[self.from_row][self.from_column].piece
|
||||||
self.board.boardlist[self.from_row][self.from_column].piece = None
|
self.board.boardlist[self.from_row][self.from_column].piece = None
|
||||||
Move.mat(self, self.x, self.to_row, self.to_column)
|
#Move.mat(self, self.x, self.to_row, self.to_column)
|
||||||
self.board.boardlist[self.to_row][self.to_column].piece = self.x
|
self.board.boardlist[self.to_row][self.to_column].piece = self.x
|
||||||
Move.pawn_promotion(self, self.x, self.to_row, self.to_column)
|
Move.pawn_promotion(self, self.x, self.to_row)
|
||||||
self.board.boardlist[self.to_row][self.to_column].piece.moved = True
|
self.board.boardlist[self.to_row][self.to_column].piece.moved = True
|
||||||
self.board.boardlist[self.to_row][self.to_column].piece.delete_moves()
|
|
||||||
return self.board.boardlist[self.to_row][self.to_column].piece.color
|
return self.board.boardlist[self.to_row][self.to_column].piece.color
|
||||||
|
|
||||||
def move_boot(self, color):
|
def move_boot(self, color): #kolor przeciwnika
|
||||||
#move.szach(self, color)
|
|
||||||
self.x, self.initial_row, self.initial_column, self.final_row, self.final_column = Move.random_valid_move(self, color)
|
self.x, self.initial_row, self.initial_column, self.final_row, self.final_column = Move.random_valid_move(self, color)
|
||||||
self.board.boardlist[self.initial_row][self.initial_column].piece = None
|
self.board.boardlist[self.initial_row][self.initial_column].piece = None
|
||||||
Move.mat(self, self.x, self.final_row, self.final_column)
|
Move.mat(self, self.x, self.final_row, self.final_column)
|
||||||
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
||||||
Move.pawn_promotion(self, self.x, self.final_row, self.final_column)
|
|
||||||
#update piece
|
Move.pawn_promotion(self, self.x, self.final_row)
|
||||||
|
# update piece
|
||||||
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()
|
||||||
|
|
||||||
self.initial_column=Move.numbers_to_letters(self.initial_column)
|
self.initial_column = Move.numbers_to_letters(self.initial_column)
|
||||||
self.final_column=Move.numbers_to_letters(self.final_column)
|
self.final_column = Move.numbers_to_letters(self.final_column)
|
||||||
self.ruch = str(self.initial_column) + str(self.initial_row) + ' ' + str(self.final_column) + str(self.final_row)
|
self.ruch = str(self.initial_column) + str(self.initial_row) + ' ' + str(self.final_column) + str(
|
||||||
|
self.final_row)
|
||||||
return self.ruch
|
return self.ruch
|
||||||
|
|
||||||
def random_valid_move(self, color):
|
def random_valid_move(self, color):
|
||||||
@ -48,20 +46,33 @@ class Move:
|
|||||||
self.piece_row_column = []
|
self.piece_row_column = []
|
||||||
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].has_piece():
|
||||||
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.kopia = []
|
||||||
#random piece with valid_moves>0
|
for move in self.board.boardlist[r][c].piece.valid_moves:
|
||||||
|
if Move.check_szach(self, color, r, c, move[0], move[1]) is False:
|
||||||
|
self.kopia.append(move)
|
||||||
|
self.board.boardlist[r][c].piece.valid_moves=self.kopia
|
||||||
|
for xddr in range(8):
|
||||||
|
for xddc in range(8):
|
||||||
|
if self.board.boardlist[xddr][xddc].has_piece() and self.board.boardlist[xddr][xddc].piece.color!=color:
|
||||||
|
if (len(self.board.boardlist[xddr][xddc].piece.valid_moves)>0):
|
||||||
|
self.boot_piece.append(self.board.boardlist[xddr][xddc])
|
||||||
|
if (len(self.boot_piece)==0):
|
||||||
|
print("MAT")
|
||||||
|
exit(0)
|
||||||
self.value = random.randrange(0, len(self.boot_piece))
|
self.value = random.randrange(0, len(self.boot_piece))
|
||||||
self.initial_piece = self.boot_piece[self.value]
|
self.initial_piece = self.boot_piece[self.value]
|
||||||
while len(self.initial_piece.piece.valid_moves) == 0:
|
while len(self.initial_piece.piece.valid_moves) == 0:
|
||||||
self.value = random.randrange(0, len(self.boot_piece))
|
self.value = random.randrange(0, len(self.boot_piece))
|
||||||
self.initial_piece = self.boot_piece[self.value]
|
self.initial_piece = self.boot_piece[self.value]
|
||||||
|
print(self.initial_piece.row, self.initial_piece.column)
|
||||||
self.move_initial_row = self.initial_piece.row
|
self.move_initial_row = self.initial_piece.row
|
||||||
self.move_initial_column = self.initial_piece.column
|
self.move_initial_column = self.initial_piece.column
|
||||||
#random move for piece
|
#random move for piece
|
||||||
self.value = random.randrange(0, len(self.initial_piece.piece.valid_moves))
|
self.value = random.randrange(0, len(self.initial_piece.piece.valid_moves))
|
||||||
self.move_final = self.initial_piece.piece.valid_moves[self.value]
|
self.move_final = self.initial_piece.piece.valid_moves[self.value]
|
||||||
|
|
||||||
self.move_final_row = self.move_final[0]
|
self.move_final_row = self.move_final[0]
|
||||||
self.move_final_column = self.move_final[1]
|
self.move_final_column = self.move_final[1]
|
||||||
self.x = self.board.boardlist[self.move_initial_row][self.move_initial_column].piece
|
self.x = self.board.boardlist[self.move_initial_row][self.move_initial_column].piece
|
||||||
@ -69,8 +80,8 @@ class Move:
|
|||||||
return self.x, self.move_initial_row, self.move_initial_column, self. move_final_row, self.move_final_column
|
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):
|
def calc_moves(self, piece, row, column):
|
||||||
piece.delete_moves()
|
#piece.delete_moves()
|
||||||
#piece.valid_moves=[]
|
piece.valid_moves=[]
|
||||||
if piece.name == 'rook':
|
if piece.name == 'rook':
|
||||||
Rook.straightline_moves(self, piece, row, column,True)
|
Rook.straightline_moves(self, piece, row, column,True)
|
||||||
pass
|
pass
|
||||||
@ -90,16 +101,63 @@ class Move:
|
|||||||
King.king_moves(self, piece, row, column)
|
King.king_moves(self, piece, row, column)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def pawn_promotion(self, x, final_row, final_column):
|
def pawn_promotion(self, x, final_row):
|
||||||
if x.name == "pawn":
|
if x.name == "pawn":
|
||||||
if (final_row==0) or (final_row==7):
|
if (final_row == 0) or (final_row == 7):
|
||||||
x.name == "queen"
|
x.name == "queen"
|
||||||
def mat(self, x, final_row, final_column):
|
def mat(self, x, final_row, final_column):
|
||||||
if self.board.boardlist[final_row][final_column].has_piece():
|
if self.board.boardlist[final_row][final_column].has_piece():
|
||||||
if self.board.boardlist[final_row][final_column].piece.name == 'king':
|
if self.board.boardlist[final_row][final_column].piece.name == 'king':
|
||||||
|
|
||||||
|
print("MAT")
|
||||||
exit(0)
|
exit(0)
|
||||||
#def szach(self,color):
|
pass
|
||||||
# for
|
|
||||||
|
def check_szach(self, color, initial_row, initial_column, final_row, final_column):#kolor przeciwnika
|
||||||
|
|
||||||
|
self.piece = self.board.boardlist[initial_row][initial_column].piece
|
||||||
|
self.previous_piece = self.board.boardlist[final_row][final_column].piece
|
||||||
|
self.board.boardlist[initial_row][initial_column].piece = None
|
||||||
|
self.board.boardlist[final_row][final_column].piece = self.piece
|
||||||
|
for rowxd in range(8):
|
||||||
|
for columnxd in range(8):
|
||||||
|
if self.board.boardlist[rowxd][columnxd].has_piece():
|
||||||
|
if self.board.boardlist[rowxd][columnxd].piece.color == color:
|
||||||
|
Move.calc_moves(self, self.board.boardlist[rowxd][columnxd].piece, rowxd, columnxd)
|
||||||
|
for r1 in range(8):
|
||||||
|
for c1 in range(8):
|
||||||
|
if self.board.boardlist[r1][c1].has_piece():
|
||||||
|
if self.board.boardlist[r1][c1].piece.name=='king' and self.board.boardlist[r1][c1].piece.color != color:
|
||||||
|
self.king_position= (r1,c1)
|
||||||
|
#tu skonczylas
|
||||||
|
for r2 in range(8):
|
||||||
|
for c2 in range(8):
|
||||||
|
if self.board.boardlist[r2][c2].has_piece():
|
||||||
|
if self.board.boardlist[r2][c2].piece.color == color:
|
||||||
|
if self.king_position in self.board.boardlist[r2][c2].piece.valid_moves:
|
||||||
|
self.board.boardlist[final_row][final_column].piece=self.previous_piece
|
||||||
|
self.board.boardlist[initial_row][initial_column].piece= self.piece
|
||||||
|
#print("XXXXXXXXXXXXXXXXXXX")
|
||||||
|
for rowxd2 in range(8):
|
||||||
|
for columnxd2 in range(8):
|
||||||
|
if self.board.boardlist[rowxd2][columnxd2].has_piece():
|
||||||
|
if self.board.boardlist[rowxd2][columnxd2].piece.color == color:
|
||||||
|
Move.calc_moves(self, self.board.boardlist[rowxd2][columnxd2].piece, rowxd2,columnxd2)
|
||||||
|
#print("valid moves przeciwnika")
|
||||||
|
#print(self.board.boardlist[r2][c2].piece.valid_moves)
|
||||||
|
return True
|
||||||
|
|
||||||
|
self.board.boardlist[final_row][final_column].piece = self.previous_piece
|
||||||
|
self.board.boardlist[initial_row][initial_column].piece = self.piece
|
||||||
|
for rowxd in range(8):
|
||||||
|
for columnxd in range(8):
|
||||||
|
if self.board.boardlist[rowxd][columnxd].has_piece():
|
||||||
|
if self.board.boardlist[rowxd][columnxd].has_piece():
|
||||||
|
if self.board.boardlist[rowxd][columnxd].piece.color == color:
|
||||||
|
Move.calc_moves(self, self.board.boardlist[rowxd][columnxd].piece, rowxd, columnxd)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def numbers_to_letters(number):
|
def numbers_to_letters(number):
|
||||||
|
9
piece.py
9
piece.py
@ -5,14 +5,19 @@ class Piece:
|
|||||||
self.valid_moves = []
|
self.valid_moves = []
|
||||||
self.moved = False
|
self.moved = False
|
||||||
if color == 'white':
|
if color == 'white':
|
||||||
|
if (self.name=='king'):
|
||||||
|
self.where_king=(3,7)
|
||||||
self.pawn_direction = -1
|
self.pawn_direction = -1
|
||||||
else:
|
else:
|
||||||
self.pawn_direction = 1
|
self.pawn_direction = 1
|
||||||
|
if (self.name=='king'):
|
||||||
|
self.where_king=(3,7)
|
||||||
def add_moves(self, move):
|
def add_moves(self, move):
|
||||||
self.valid_moves.append(move)
|
self.valid_moves.append(move)
|
||||||
|
|
||||||
def delete_moves(self):
|
def delete_moves(self):
|
||||||
self.valid_moves = []
|
self.valid_moves = []
|
||||||
|
def change_where_king(self, move):
|
||||||
|
self.where_king = move
|
||||||
|
|
||||||
|
|
||||||
#def czy_szachuje(self, row, column, color):
|
|
||||||
# self.ruch=[]
|
|
||||||
|
Loading…
Reference in New Issue
Block a user