Please review my current try of creating a class for chess pieces. I not only want to be able to play a game of chess with this but also solve chess puzzles, thats why I want a flexible board size and pieces that can placed and created by hand.
I put many comments in the code and put the modules that were needed together so you can read it easier. It should still work, print the files of the boardmatrix
after the initialization of all pieces and give some information of the black kingside rook. Things that I want to change:
1) reset
function gets a start
option which initializes all pieces.
2) I dont know how to place the pieces while creating them, thats why the placement is in the __init__
and as a seperate place function to move them.
3) I already have functions like kingMove
, queenMove
, … . Where do I put them? In the elif
parts where I decide which kind of piece the new one is? I feel like this would make the class too big and hard to keep track of.
# I put all modules in one file for the review and changed the variables from modulename.name to name # board.py # the board and game settings are supposed to be here from string import ascii_uppercase # I want the dimensions of the board to be able to be set up to 15*15 width = 8 height = 8 # the leading zeros are there to calculate more intuitive: files[2] = B, not C. files = "0" + ascii_uppercase[:width] ranks = range(0, height + 1) # status.py # contains the boardmatrix which holds the information of how the board currently looks boardmatrix = [[" " for x in range(1, height + 1)] for y in range(1, width + 1)] def reset(setting): # function to control the boardmatrix. i had to use the "global" to be able to change # the matrix with the function global boardmatrix if setting == "empty": boardmatrix = [[" " for x in range(1, height + 1)] for x in range(1, width + 1)] # elif setting == "start": # initialize all pieces else: print("Something failed.") # classpiece.py # class that holds all information about a piece: color, piece, position, its short handle, # whether rook or king can be used to castle etc. # class piece: # Summons piece of given color and position def __init__(self, color, piece, position): # color if color == "w": self.color = "white" elif color == "b": self.color = "black" else: self.color = "none" # piece if piece == "p": self.piece = "pawn" self.short = color + "P" elif piece == "n": self.piece = "knight" self.short = color + "N" elif piece == "b": self.piece = "bishop" self.short = color + "B" elif piece == "r": self.piece = "rook" self.short = color + "R" self.canCastle = True elif piece == "q": self.piece = "queen" self.short = color + "Q" elif piece == "k": self.piece = "king" self.short = color + "K" self.canCastle = True # position and place self.position = position.upper() file = files.index(((self.position[0]).upper())) rank = self.position[1:] boardmatrix[int(file) - 1][int(rank) - 1] = self.short def place(self, position=None): # remove old oldfile = files.index(((self.position[0]).upper())) oldrank = self.position[1:] boardmatrix[int(oldfile) - 1][int(oldrank) - 1] = " " # place new position = position or self.position file = files.index(((position[0]).upper())) rank = position[1:] boardmatrix[int(file) - 1][int(rank) - 1] = self.short # update position self.position = position # i gave each piece a unique name, i dont know how else to initialize and later do stuff with it # w for white, b for black wApawn = piece("w", "p", "a2") wBpawn = piece("w", "p", "b2") wCpawn = piece("w", "p", "c2") wDpawn = piece("w", "p", "d2") wEpawn = piece("w", "p", "e2") wFpawn = piece("w", "p", "f2") wGpawn = piece("w", "p", "g2") wHpawn = piece("w", "p", "h2") bApawn = piece("b", "p", "a7") bBpawn = piece("b", "p", "b7") bCpawn = piece("b", "p", "c7") bDpawn = piece("b", "p", "d7") bEpawn = piece("b", "p", "e7") bFpawn = piece("b", "p", "f7") bGpawn = piece("b", "p", "g7") bHpawn = piece("b", "p", "h7") # Q for queenside, K for Kingside, W for white square, B for black squre wQrook = piece("w", "r", "a1") wQknight = piece("w", "n", "b1") wBbishop = piece("w", "b", "c1") wQueen = piece("w", "q", "d1") wKing = piece("w", "k", "e1") wWbishop = piece("w", "b", "f1") wKknight = piece("w", "n", "g1") wKrook = piece("w", "r", "h1") bQrook = piece("b", "r", "a8") bQknight = piece("b", "n", "b8") bBbishop = piece("b", "b", "c8") bQueen = piece("b", "q", "d8") bKing = piece("b", "k", "e8") bWbishop = piece("b", "b", "f8") bKknight = piece("b", "n", "g8") bKrook = piece("b", "r", "h8") for line in boardmatrix: print(line) print(bKrook.position) print(bKrook.color) print(bKrook.canCastle)