isvertical()

This commit is contained in:
Agata Halik 2023-06-19 20:11:22 +02:00
parent 5c9d3fd678
commit 0ff179d9ff
13 changed files with 67 additions and 125 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.

View File

@ -2,14 +2,7 @@ from piece import Piece
from board import Board from board import Board
from square import Square from square import Square
class Bishop(Piece): class Bishop(Piece):
def straightline_moves(self, piece, row, column, isvertical): def straightline_moves(self, piece, row, column):
if isvertical is True:
self.direction = [
(0, 1),
(0, -1),
(1, 0),
(-1, 0)]
else:
self.direction = [ self.direction = [
(1, 1), (1, 1),
(-1, -1), (-1, -1),

View File

@ -1,7 +1,5 @@
from square import Square from square import Square
from piece import Piece from piece import Piece
import random
class Board: class Board:
def __init__(self): def __init__(self):
self.boardlist = [[0, 0, 0, 0, 0, 0, 0, 0] for x in range(8)] self.boardlist = [[0, 0, 0, 0, 0, 0, 0, 0] for x in range(8)]
@ -11,7 +9,6 @@ class Board:
self.boardlist[row][column] = Square(row, column) self.boardlist[row][column] = Square(row, column)
self._add_pieces('white') self._add_pieces('white')
self._add_pieces('black') self._add_pieces('black')
def _add_pieces(self, color): def _add_pieces(self, color):
if color == 'white': if color == 'white':
self.pawn_row = 6 self.pawn_row = 6
@ -19,11 +16,6 @@ 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))
@ -40,4 +32,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))
"""

25
game.py
View File

@ -1,52 +1,49 @@
from board import Board from board import Board
from move import Move from move import Move
import random
class Game: class Game:
def __init__(self): def __init__(self):
self.board = Board() self.board = Board()
def printBoard(self): def printBoard(self):
self.b = [] self.b = []
for row in range(8): for row in range(8):
if row==0: self.b.append('0') if row == 0: self.b.append('0')
if row == 1: self.b.append('1') if row == 1: self.b.append('1')
if row==2: self.b.append('2') if row == 2: self.b.append('2')
if row == 3: self.b.append('3') if row == 3: self.b.append('3')
if row==4: self.b.append('4') if row == 4: self.b.append('4')
if row == 5: self.b.append('5') if row == 5: self.b.append('5')
if row==6: self.b.append('6') if row == 6: self.b.append('6')
if row == 7: self.b.append('7') if row == 7: self.b.append('7')
for column in range(8): for column in range(8):
if self.board.boardlist[row][column].has_piece(): if self.board.boardlist[row][column].has_piece():
self.piece = self.board.boardlist[row][column].piece.name self.piece = self.board.boardlist[row][column].piece.name
self.color=self.board.boardlist[row][column].piece.color self.color=self.board.boardlist[row][column].piece.color
if self.piece == 'rook': if self.piece == 'rook':
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')
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')
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')
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')
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')
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')
@ -65,7 +62,6 @@ class Game:
self.b.append('H') self.b.append('H')
print(self.b) print(self.b)
print('') print('')
def currently_available_moves(self): def currently_available_moves(self):
for row in range(8): for row in range(8):
for column in range(8): for column in range(8):
@ -80,4 +76,3 @@ class Game:
self.color = Move.move_rival(self, ruch) self.color = Move.move_rival(self, ruch)
self.currently_available_moves() self.currently_available_moves()
return Move.move_boot(self, self.color) return Move.move_boot(self, self.color)

13
main.py
View File

@ -3,13 +3,12 @@ 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:
print("gra bialy") time.sleep(1)
time.sleep(2) ruch=game.nextMove(ruch)
ruch=input("Podaj:c")
game.nextMove(ruch)
game.printBoard() game.printBoard()
time.sleep(1)
ruch=game2.nextMove(ruch)
game2.printBoard()

90
move.py
View File

@ -14,34 +14,30 @@ class Move:
self.from_row = int(ruch[1]) self.from_row = int(ruch[1])
self.to_column = Move.letters_to__numbers(ruch[3]) self.to_column = Move.letters_to__numbers(ruch[3])
self.to_row = int(ruch[4]) self.to_row = int(ruch[4])
#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)
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) 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
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 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.x=self.board.boardlist[self.initial_row][self.initial_column].piece
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) Move.pawn_promotion(self, self.x, self.final_row)
# update piece # 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.ruch = str(self.initial_column) + str(self.initial_row) + ' ' + str(self.final_column) + str(
self.final_row) self.final_row)
return self.ruch return self.ruch
def random_valid_move(self, color): def random_valid_move(self, color):
#usuwanie ruchów z valid_moves, które mogłyby doprowadzić do szachowania króla
self.boot_piece = [] self.boot_piece = []
self.piece_row_column = [] self.piece_row_column = []
for r in range(0, 8): for r in range(0, 8):
@ -53,37 +49,31 @@ class Move:
if Move.check_szach(self, color, r, c, move[0], move[1]) is False: if Move.check_szach(self, color, r, c, move[0], move[1]) is False:
self.kopia.append(move) self.kopia.append(move)
self.board.boardlist[r][c].piece.valid_moves=self.kopia self.board.boardlist[r][c].piece.valid_moves=self.kopia
for xddr in range(8): #spis pól z figurami, które mają możliwość ruchu
for xddc in range(8): for r in range(8):
if self.board.boardlist[xddr][xddc].has_piece() and self.board.boardlist[xddr][xddc].piece.color!=color: for c in range(8):
if (len(self.board.boardlist[xddr][xddc].piece.valid_moves)>0): if self.board.boardlist[r][c].has_piece() and self.board.boardlist[r][c].piece.color != color:
self.boot_piece.append(self.board.boardlist[xddr][xddc]) if (len(self.board.boardlist[r][c].piece.valid_moves)>0):
self.boot_piece.append(self.board.boardlist[r][c])
if (len(self.boot_piece)==0): if (len(self.boot_piece)==0):
print("MAT") print("MAT")
exit(0) exit(0)
#losowanie figury
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_square = self.boot_piece[self.value]
while len(self.initial_piece.piece.valid_moves) == 0: #losowanie ruchu figury
self.value = random.randrange(0, len(self.boot_piece)) self.move_initial_row = self.initial_square.row
self.initial_piece = self.boot_piece[self.value] self.move_initial_column = self.initial_square.column
print(self.initial_piece.row, self.initial_piece.column) self.value = random.randrange(0, len(self.initial_square.piece.valid_moves))
self.move_initial_row = self.initial_piece.row self.move_final_square = self.initial_square.piece.valid_moves[self.value]
self.move_initial_column = self.initial_piece.column self.move_final_row = self.move_final_square[0]
#random move for piece self.move_final_column = self.move_final_square[1]
self.value = random.randrange(0, len(self.initial_piece.piece.valid_moves)) return self.move_initial_row, self.move_initial_column, self. move_final_row, self.move_final_column #, self.x
self.move_final = self.initial_piece.piece.valid_moves[self.value]
self.move_final_row = self.move_final[0]
self.move_final_column = self.move_final[1]
self.x = self.board.boardlist[self.move_initial_row][self.move_initial_column].piece
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)
pass pass
elif piece.name == 'pawn': elif piece.name == 'pawn':
Pawn.pawn_moves(self, piece, row, column) Pawn.pawn_moves(self, piece, row, column)
@ -92,7 +82,7 @@ class Move:
Knight.knight_moves(self, piece, row, column) Knight.knight_moves(self, piece, row, column)
elif piece.name == 'bishop': elif piece.name == 'bishop':
pass pass
Bishop.straightline_moves(self, piece, row, column,False) Bishop.straightline_moves(self, piece, row, column)
elif piece.name == 'queen': elif piece.name == 'queen':
pass pass
Queen.straightline_moves(self, piece, row, column,True) Queen.straightline_moves(self, piece, row, column,True)
@ -100,36 +90,28 @@ class Move:
elif piece.name == 'king': elif piece.name == 'king':
King.king_moves(self, piece, row, column) King.king_moves(self, piece, row, column)
pass pass
def pawn_promotion(self, x, final_row): 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)
pass pass
def check_szach(self, color, initial_row, initial_column, final_row, final_column):
def check_szach(self, color, initial_row, initial_column, final_row, final_column):#kolor przeciwnika #zapisanie poprzedniego ustawienia tablicy i wykonanie ruchu
self.piece = self.board.boardlist[initial_row][initial_column].piece self.piece = self.board.boardlist[initial_row][initial_column].piece
self.previous_piece = self.board.boardlist[final_row][final_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[initial_row][initial_column].piece = None
self.board.boardlist[final_row][final_column].piece = self.piece self.board.boardlist[final_row][final_column].piece = self.piece
for rowxd in range(8): for r in range(8):
for columnxd in range(8): for c in range(8):
if self.board.boardlist[rowxd][columnxd].has_piece(): if self.board.boardlist[r][c].has_piece():
if self.board.boardlist[rowxd][columnxd].piece.color == color: if self.board.boardlist[r][c].piece.color == color:
Move.calc_moves(self, self.board.boardlist[rowxd][columnxd].piece, rowxd, columnxd) Move.calc_moves(self, self.board.boardlist[r][c].piece, r, c)
for r1 in range(8): elif self.board.boardlist[r][c].piece.name=='king':
for c1 in range(8): self.king_position = (r, c)
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 r2 in range(8):
for c2 in range(8): for c2 in range(8):
if self.board.boardlist[r2][c2].has_piece(): if self.board.boardlist[r2][c2].has_piece():
@ -137,16 +119,12 @@ class Move:
if self.king_position in self.board.boardlist[r2][c2].piece.valid_moves: 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[final_row][final_column].piece=self.previous_piece
self.board.boardlist[initial_row][initial_column].piece= self.piece self.board.boardlist[initial_row][initial_column].piece= self.piece
#print("XXXXXXXXXXXXXXXXXXX")
for rowxd2 in range(8): for rowxd2 in range(8):
for columnxd2 in range(8): for columnxd2 in range(8):
if self.board.boardlist[rowxd2][columnxd2].has_piece(): if self.board.boardlist[rowxd2][columnxd2].has_piece():
if self.board.boardlist[rowxd2][columnxd2].piece.color == color: if self.board.boardlist[rowxd2][columnxd2].piece.color == color:
Move.calc_moves(self, self.board.boardlist[rowxd2][columnxd2].piece, rowxd2,columnxd2) 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 return True
self.board.boardlist[final_row][final_column].piece = self.previous_piece self.board.boardlist[final_row][final_column].piece = self.previous_piece
self.board.boardlist[initial_row][initial_column].piece = self.piece self.board.boardlist[initial_row][initial_column].piece = self.piece
for rowxd in range(8): for rowxd in range(8):
@ -156,9 +134,6 @@ class Move:
if self.board.boardlist[rowxd][columnxd].piece.color == color: if self.board.boardlist[rowxd][columnxd].piece.color == color:
Move.calc_moves(self, self.board.boardlist[rowxd][columnxd].piece, rowxd, columnxd) Move.calc_moves(self, self.board.boardlist[rowxd][columnxd].piece, rowxd, columnxd)
return False return False
@staticmethod @staticmethod
def numbers_to_letters(number): def numbers_to_letters(number):
if number == 0: if number == 0:
@ -178,7 +153,6 @@ class Move:
elif number == 7: elif number == 7:
number = "H" number = "H"
return number return number
@staticmethod @staticmethod
def letters_to__numbers(letter): def letters_to__numbers(letter):
if letter == 'A': if letter == 'A':

View File

@ -5,16 +5,11 @@ 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): def change_where_king(self, move):

12
rook.py
View File

@ -2,19 +2,13 @@ from piece import Piece
from board import Board from board import Board
from square import Square from square import Square
class Rook(Piece): class Rook(Piece):
def straightline_moves(self, piece, row, column, isvertical): def straightline_moves(self, piece, row, column):
if isvertical is True:
self.direction = [ self.direction = [
(0, 1), (0, 1),
(0, -1), (0, -1),
(1, 0), (1, 0),
(-1, 0)] (-1, 0)
else:
self.direction = [
(1, 1),
(-1, -1),
(1, -1),
(-1, 1)
] ]
for dir in self.direction: for dir in self.direction:
self.left, self.right = dir self.left, self.right = dir