patternpythonMinor
Chess game that stores moves and pieces as data
Viewed 0 times
piecesmovesstoreschessgamethatanddata
Problem
I started to try my hand at making a chess game. It works, but I know this isn't the most efficient way of writing code.
I'm just looking for advice and critique on how to get better. Please be tough!
`MOVEMAP = {'a':0 , 'b': 1, 'c': 2, 'd': 3,'e': 4,'f': 5, 'g': 6, 'h': 7,
'1': 7, '2': 6, '3' : 5, '4': 4, '5': 3, '6':2, '7':1, '8':0}
PIECEMAP = {'P' :[(1,0),(-1,0)],
'R': [(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),
(-1,0),(-2,0),(-3,0),(-4,0),(-5,0),(-6,0),(-7,0),
(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),(0,7),
(0,-1),(0,-2),(0,-3),(0,-4),(0,-5),(0,-6),(0,-7)],
'N' : [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(-1,2),(1,-2),(-1,-2)],
'B' : [(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),
(1,-1),(2,-2),(3,-3),(4,-4),(5,-5),(6,-6),(7,-7),
(-1,1),(-2,2),(-3,3),(-4,4),(-5,5),(-6,6),(-7,7),
(-1,-1),(-2,-2),(-3,-3),(-4,-4),(-5,-5),(-6,-6),(-7,-7)],
'Q' : [(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),
(1,-1),(2,-2),(3,-3),(4,-4),(5,-5),(6,-6),(7,-7),
(-1,1),(-2,2),(-3,3),(-4,4),(-5,5),(-6,6),(-7,7),
(-1,-1),(-2,-2),(-3,-3),(-4,-4),(-5,-5),(-6,-6),(-7,-7),
(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),
(-1,0),(-2,0),(-3,0),(-4,0),(-5,0),(-6,0),(-7,0),
(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),(0,7),
(0,-1),(0,-2),(0,-3),(0,-4),(0,-5),(0,-6),(0,-7)],
'K' : [(1,0),(-1,0),(0,1),(0,-1),(-1,1),(1,-1),(-1,-1),(1,1)]}
CHECK = False
CHECKMATE = False
hostiles = []
class Board:
def __init__(self,board = None):
if board == None:
self.board = [['WR','WN','WB','WQ','WK','WB','WN','WR'],
['WP','WP','WP','WP','WP','WP','WP','WP'],
['0 ','0 ','0 ','0 ','0 ','0 ','0 ','0 '],
['0 ','0
I'm just looking for advice and critique on how to get better. Please be tough!
`MOVEMAP = {'a':0 , 'b': 1, 'c': 2, 'd': 3,'e': 4,'f': 5, 'g': 6, 'h': 7,
'1': 7, '2': 6, '3' : 5, '4': 4, '5': 3, '6':2, '7':1, '8':0}
PIECEMAP = {'P' :[(1,0),(-1,0)],
'R': [(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),
(-1,0),(-2,0),(-3,0),(-4,0),(-5,0),(-6,0),(-7,0),
(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),(0,7),
(0,-1),(0,-2),(0,-3),(0,-4),(0,-5),(0,-6),(0,-7)],
'N' : [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(-1,2),(1,-2),(-1,-2)],
'B' : [(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),
(1,-1),(2,-2),(3,-3),(4,-4),(5,-5),(6,-6),(7,-7),
(-1,1),(-2,2),(-3,3),(-4,4),(-5,5),(-6,6),(-7,7),
(-1,-1),(-2,-2),(-3,-3),(-4,-4),(-5,-5),(-6,-6),(-7,-7)],
'Q' : [(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),
(1,-1),(2,-2),(3,-3),(4,-4),(5,-5),(6,-6),(7,-7),
(-1,1),(-2,2),(-3,3),(-4,4),(-5,5),(-6,6),(-7,7),
(-1,-1),(-2,-2),(-3,-3),(-4,-4),(-5,-5),(-6,-6),(-7,-7),
(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),
(-1,0),(-2,0),(-3,0),(-4,0),(-5,0),(-6,0),(-7,0),
(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),(0,7),
(0,-1),(0,-2),(0,-3),(0,-4),(0,-5),(0,-6),(0,-7)],
'K' : [(1,0),(-1,0),(0,1),(0,-1),(-1,1),(1,-1),(-1,-1),(1,1)]}
CHECK = False
CHECKMATE = False
hostiles = []
class Board:
def __init__(self,board = None):
if board == None:
self.board = [['WR','WN','WB','WQ','WK','WB','WN','WR'],
['WP','WP','WP','WP','WP','WP','WP','WP'],
['0 ','0 ','0 ','0 ','0 ','0 ','0 ','0 '],
['0 ','0
Solution
Note: You've posted a large chunk of code, so I might miss something during my review. Please excuse me for any mistake. Better documentation is appreciated.
I'm a C++ chess engine programmer, so I'll share my experience with you.
not very important unless you want to code a chess engine (there're only 64 squares in chess). Professional softwares use bitboard.
Overall, I don't think it works correctly for the chess rules. You should check your board generator against Perft.
Too much two-dimensional looping. It's okay for chess because we have only 64 squares, but try to be smarter in your next project. This is an opportunity for you to learn hashing and dictionary.
On a 1-10 scale, if a commercial quality GUI (such as Chessbase's Fritz) is 10/10. I'd rate this attempt as 1/10.
I'm a C++ chess engine programmer, so I'll share my experience with you.
- The Python coding is fairly standard so this is ok
- Your board representation is too slow for a chess engine, but acceptable for a chess GUI. If you would like to write your own chess engine, everything would have be rewritten.
- You forgot to check whether the castling squares are attacked by the enemy pieces.
- You forgot to implement promotions and under-promotions
- You forgot to check for pinned pieces. For example, your program would move a knight away from it's king when it can't.
- You only need to know whether the rooks and king have moved or not. You don't care your queen, knight and bishop. You only need rook and king because for castling. Therefore, the function
has_movedis wrong.
- How you check whether or not a piece has moved is too complex and unnecessary. Professional softwares do it with Zobrist hashing. Consider to hash your position. If you don't want to do it, use a simple dictionary. Stop looping and then looping.
- How you find a piece is slow because it is a linear search. Fortunately, it's
not very important unless you want to code a chess engine (there're only 64 squares in chess). Professional softwares use bitboard.
in_dangeris wrong. You check whether your piece is being attacked by the enemy piece. But what if your piece is protected? A piece is really in danger if it's being attacked and unprotected.
- Furthermore, a protected piece is in danger if it's being attacked by a less valuable piece. For example, your protected queen is being attacked by your opponent's pawn. You might want to check static exchange evaluation.
Overall, I don't think it works correctly for the chess rules. You should check your board generator against Perft.
Too much two-dimensional looping. It's okay for chess because we have only 64 squares, but try to be smarter in your next project. This is an opportunity for you to learn hashing and dictionary.
On a 1-10 scale, if a commercial quality GUI (such as Chessbase's Fritz) is 10/10. I'd rate this attempt as 1/10.
Context
StackExchange Code Review Q#154416, answer score: 5
Revisions (0)
No revisions yet.