This commit is contained in:
Agata Halik 2023-06-08 16:02:55 +02:00
commit 2b7004ad34
27 changed files with 474 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,12 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="psycopg2" />
</list>
</option>
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,7 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Default" />
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (mojeszachy)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/mojeszachy.iml" filepath="$PROJECT_DIR$/.idea/mojeszachy.iml" />
</modules>
</component>
</project>

10
.idea/mojeszachy.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

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.

Binary file not shown.

Binary file not shown.

5
bishop.py Normal file
View File

@ -0,0 +1,5 @@
from piece import Piece
class Bishop(Piece):
def __init__(self, color):
super().__init__("bishop", color)

224
board.py Normal file
View File

@ -0,0 +1,224 @@
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
import random
class Board:
def __init__(self):
self.boardlist = [[0, 0, 0, 0, 0, 0, 0, 0] for x in range(8)]
#creating board
for row in range(8):
for column in range(8):
self.boardlist[row][column] = Square(row, column)
self._add_pieces('white')
self._add_pieces('black')
def _add_pieces(self, color):
if color == 'white':
self.pawn_row = 6
self.other_row = 7
else:
self.pawn_row = 1
self.other_row = 0
#pawns
#self.boardlist[5][1] = Square(5, 1, King(color)) #DO USUNIĘCIA
for column in range(8):
self.boardlist[self.pawn_row][column] = Square(self.pawn_row, column, Pawn(color)) #do kazdego square przypisuje poczatkowy pawn
#rooks
self.boardlist[self.other_row][0] = Square(self.other_row, 0, Rook(color))
self.boardlist[self.other_row][7] = Square(self.other_row, 7, Rook(color))
#knigths
self.boardlist[self.other_row][1] = Square(self.other_row, 1, Knight(color))
self.boardlist[self.other_row][6] = Square(self.other_row, 6, Knight(color))
#bishops
self.boardlist[self.other_row][2] = Square(self.other_row, 2, Bishop(color))
self.boardlist[self.other_row][5] = Square(self.other_row, 5, Bishop(color))
#queen
self.boardlist[self.other_row][3] = Square(self.other_row, 3, Queen(color))
#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
self.board.boardlist[from_row][from_column].piece = None
if (self.board.boardlist[to_row][to_column].has_piece()):
if (self.board.boardlist[to_row][to_column].piece.name == 'king'):
self.board.boardlist[to_row][to_column].piece = self.x
return 1
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.delete_moves()
return 0
def move_boot(self, color):
self.boot_piece = []
self.piece_row_column = []
for r 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.color == color:
self.boot_piece.append(self.board.boardlist[r][c])
#random piece
self.value = random.randrange(0, len(self.boot_piece))
self.initial = self.boot_piece[self.value]
while len(self.initial.piece.valid_moves) == 0:
self.value = random.randrange(0, len(self.boot_piece))
self.initial = self.boot_piece[self.value]
self.initial_row=self.initial.row
self.initial_column = self.initial.column
#print(self.initial)
#random move
self.value = random.randrange(0, len(self.initial.piece.valid_moves))
self.final = self.initial.piece.valid_moves[self.value]
self.final_row = self.final[0]
self.final_column = self.final[1]
#print(self.final)
#print(self.final_row)
#update
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
#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].piece.name== 'king'):
self.board.boardlist[self.final_row][self.final_column].piece = self.x
return 1
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.delete_moves()
#print(self.board.boardlist[self.final_row][self.final_column].piece.name)
#print(self.initial_row, self.initial_column)
#print(self.final_row, self.final_column)
return 0

91
game.py Normal file
View File

@ -0,0 +1,91 @@
from board import Board
import random
class Game:
def __init__(self):
self.board = Board()
def show_board(self):
self.b = []
for row in range(8):
if row==0: self.b.append('0')
if row == 1: self.b.append('1')
if row==2: self.b.append('2')
if row == 3: self.b.append('3')
if row==4: self.b.append('4')
if row == 5: self.b.append('5')
if row==6: self.b.append('6')
if row == 7: self.b.append('7')
for column in range(8):
if self.board.boardlist[row][column].has_piece():
self.piece = self.board.boardlist[row][column].piece.name
self.color=self.board.boardlist[row][column].piece.color
if self.piece == 'rook':
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)
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)
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)
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)
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)
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)
else:
# wyswietla puste miejsca
self.b.append(' ')
print(self.b)
self.b = []
self.b.append(' ')
self.b.append('0')
self.b.append('1')
self.b.append('2')
self.b.append('3')
self.b.append('4')
self.b.append('5')
self.b.append('6')
self.b.append('7')
print(self.b)
# sprawdzenie
# H
# self.board.calc_moves(self.board.boardlist[7][6].piece, 7, 6)
# print(self.board.boardlist[7][6].piece.valid_moves)
# 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 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)

5
king.py Normal file
View File

@ -0,0 +1,5 @@
from piece import Piece
class King(Piece):
def __init__(self, color):
super().__init__("king", color)

5
knight.py Normal file
View File

@ -0,0 +1,5 @@
from piece import Piece
from square import Square
class Knight(Piece):
def __init__(self, color):
super().__init__("knight", color)

28
main.py Normal file
View File

@ -0,0 +1,28 @@
from game import Game
#def mainloop(self):
# game = self.game
game = Game()
game.show_board()
boot_color = 'white'
#11 21
a=0
while True:
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('')

5
pawn.py Normal file
View File

@ -0,0 +1,5 @@
from piece import Piece
class Pawn(Piece):
def __init__(self, color):
super().__init__("pawn", color)

18
piece.py Normal file
View File

@ -0,0 +1,18 @@
class Piece:
def __init__(self, name, color):
self.name = name
self.color = color
self.valid_moves = []
self.moved = False
# kierunek pionkow, czarne w górę, białe w dół
if color == 'white':
self.pawn_direction = -1
else:
self.pawn_direction = 1
def add_moves(self, move):
self.valid_moves.append(move)
def delete_moves(self):
self.valid_moves = []

5
queen.py Normal file
View File

@ -0,0 +1,5 @@
from piece import Piece
class Queen(Piece):
def __init__(self, color):
super().__init__("queen", color)

5
rook.py Normal file
View File

@ -0,0 +1,5 @@
from piece import Piece
class Rook(Piece):
def __init__(self, color):
super().__init__("rook", color)

34
square.py Normal file
View File

@ -0,0 +1,34 @@
class Square:
# tworzenie pól
def __init__(self, row, column, piece=None):
self.row = row
self.column = column
self.piece = piece
def has_piece(self):
if self.piece is None:
return False
else:
return True
def is_rival(self, color):
if self.has_piece() is True and self.piece.color != color:
return True
def empty_or_rival(self, color): # sprawdz
if self.has_piece() is False or self.is_rival(color): # 1 has piece (color)
return True
else:
return False
def empty_or_rival_knight(self, color): # sprawdz
if self.has_piece() is False or self.is_rival(color): # 1 has piece (color)
return True
else:
return False
@staticmethod
def on_board(*args):
for arg in args:
if arg < 0 or arg > 7:
return False
return True