![]() (I'm not clear why the pawn can move up-right, and down-right, instead of up-right and up-left - probably the game is going left/right on the board, I was assuming it was up/down, if that matters. If x = 1: # if the pawn is in the second rank (has not moved) If the board width and height go 0-7 then replace them with inline safety checks. The try / except / pass blocks add a lot of length and not a lot of features. (Yeah, I've been replacing the nested lists of lists with tuples). Probably people who have written chess games before have much better ideas, but here are my thoughts. Thinking about it, I bet a lot of it could be cut right down if the function actually didn't care about moves going off-board, like you say, and that was tidied up later. Moves = append_strip_negatives(potential_moves) # if the pawn is in the second rank (has not moved) Potential_moves = # moves before clean up function The list before the cleanup for a knight which has not moved at the beginning of the game would look like this:, ],, ],, ],, ],, ],, ],, ],, ]]Īnd after clean up, looks like this:, ],, ]]Īnd here is the function: def enumerate_moves(x, y): ![]() It assumes there are no other pieces on the board, and that all moves are possible (see example list of moves below) and a separate function 'cleans' the list and removes any moves that aren't possible (say if the space is taken, or if the value would put the piece off of the board) My code works, but it is large and redundant, so I was wondering if anybody had any idea how to better accomplish this? Assuming the computer will always be playing black. I have a function that when passed the x and y coordinates on a 2D list can enumerate all possible moves for a piece. I'm just at the beginning stages of making a simple chess engine.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |