patternpythonMajor
Chess game in Python
Viewed 0 times
chessgamepython
Problem
I have programmed for 2 months, and I began writing a Chess game. I am a beginner programmer in Python, so please assess my code.
```
class Chess_Board:
def __init__(self):
self.board = self.create_board()
def create_board(self):
board_x=[]
for x in range(8):
board_y =[]
for y in range(8):
board_y.append('.')
board_x.append(board_y)
board_x[7][4] = 'K'
board_x[7][3] = 'Q'
board_x[7][2] = 'B'
board_x[7][1] = 'N'
board_x[7][0] = 'R'
return board_x
class WHITE_KING(Chess_Board):
def __init__(self):
Chess_Board.__init__(self)
self.position_x_WK = 7
self.position_y_WK = 4
self.symbol_WK = 'K'
def move(self):
while True:
try:
print ('give a x and y coordinate for WHITE KING')
destination_x_WK = int(input())
destination_y_WK = int(input())
if self.board[destination_x_WK][destination_y_WK] == '.' :
if ( abs(self.position_x_WK-destination_x_WK) <2 and abs(self.position_y_WK-destination_y_WK) < 2 ):
self.board[self.position_x_WK][self.position_y_WK] = '.'
self.position_x_WK = destination_x_WK
self.position_y_WK = destination_y_WK
self.board[self.position_x_WK][self.position_y_WK] = self.symbol_WK
return self.board
break
else:
print ('your move is invalid, please choose cooridnates again')
continue
except:
pass
class WHITE_QUEEN(Chess_Board):
def __init__(self):
Chess_Board.__init__(self)
self.position_x_WQ = 7
self.position_y_WQ = 3
self.symbol_WQ = 'Q'
def move(self):
while True:
try:
```
class Chess_Board:
def __init__(self):
self.board = self.create_board()
def create_board(self):
board_x=[]
for x in range(8):
board_y =[]
for y in range(8):
board_y.append('.')
board_x.append(board_y)
board_x[7][4] = 'K'
board_x[7][3] = 'Q'
board_x[7][2] = 'B'
board_x[7][1] = 'N'
board_x[7][0] = 'R'
return board_x
class WHITE_KING(Chess_Board):
def __init__(self):
Chess_Board.__init__(self)
self.position_x_WK = 7
self.position_y_WK = 4
self.symbol_WK = 'K'
def move(self):
while True:
try:
print ('give a x and y coordinate for WHITE KING')
destination_x_WK = int(input())
destination_y_WK = int(input())
if self.board[destination_x_WK][destination_y_WK] == '.' :
if ( abs(self.position_x_WK-destination_x_WK) <2 and abs(self.position_y_WK-destination_y_WK) < 2 ):
self.board[self.position_x_WK][self.position_y_WK] = '.'
self.position_x_WK = destination_x_WK
self.position_y_WK = destination_y_WK
self.board[self.position_x_WK][self.position_y_WK] = self.symbol_WK
return self.board
break
else:
print ('your move is invalid, please choose cooridnates again')
continue
except:
pass
class WHITE_QUEEN(Chess_Board):
def __init__(self):
Chess_Board.__init__(self)
self.position_x_WQ = 7
self.position_y_WQ = 3
self.symbol_WQ = 'Q'
def move(self):
while True:
try:
Solution
There are a few traps you're getting yourself into a bit.
-
By definition your white rook starts at a specific spot. What are you going to do for the other rook?
-
Also you're using class inheritance
-
You're limiting yourself to a class which specifies color. You should just have a knight class so that all 4 knights (on each side) can use that same class.
-
When I tell it my move I shouldn't have to tell it "Move white knight" because it should already know its my turn, so the white is unnecessary. And the same time that doesn't tell us WHICH white knight to even move, so it is too little and too much information at the same time. One way to have people input moves would be using standard chess notation, so you can read up more on that and see if you want to use it.
-
Consider using inputs to your classes. Suppose you had a class like knight. You could initialize it with something like
-
By definition your white rook starts at a specific spot. What are you going to do for the other rook?
-
Also you're using class inheritance
White_Knight(chess_board) in a funny way. Please read this quick chapter. In short, your implying that your white knight IS a chess board. But your white knight IS a piece and so perhaps you should have a piece class and have white knight inherit from that. Your chess board HAS pieces, so your structure should reflect that.-
You're limiting yourself to a class which specifies color. You should just have a knight class so that all 4 knights (on each side) can use that same class.
-
When I tell it my move I shouldn't have to tell it "Move white knight" because it should already know its my turn, so the white is unnecessary. And the same time that doesn't tell us WHICH white knight to even move, so it is too little and too much information at the same time. One way to have people input moves would be using standard chess notation, so you can read up more on that and see if you want to use it.
-
Consider using inputs to your classes. Suppose you had a class like knight. You could initialize it with something like
board_x[7][1] = Knight(pos_y=7, pos_x=1, team='white') that way white and position are just properties of a broader knight class and you have a class that can be re-used instead of making a class specific to that one specific knight on the board.Context
StackExchange Code Review Q#101574, answer score: 20
Revisions (0)
No revisions yet.