The exception is that on their first move they can move two squares, but only on their first move!Ī pawn is weak on its own, but it has a unique strength. Pawns can only move forward up the board. It is suitable for beginners wanting to learn to play chess. This tutorial explains the basics of all chess piece moves and includes a print-friendly diagram (pdf) to download and use during your games. Each chess piece has its own rules and you should understand them all if you want to win at chess. Although that move is already in the valid move list anyway.Ever wondered “How does each chess piece move?” In order to play chess online or face to face over a chessboard you need to learn how chess pieces move. But you don't seem to do anything to indicate that the bishop can take a piece and move to x+2,y+2 or similar, so it doesn't seem to be checking for a piece. I can't tell what's happening in your Bishops code with the check for squares starting with "w", whether it's supposed to indicate a white square or a white piece on the square. Potential_moves += ur + dr + ul + dl #ur = up/right, etc. All the moves are mirrored vertically and horizontally. The longest distance is from one of the corners to the other, so from the bottom left 0,0 to x+7, y+7. So the Bishop moves diagonally - x+1 and y+1. You do 50+ lines of code to handle each direction from the center, all with two counters and all with safety checks for going off-board. Right, foothills navigated, what about Bishops? They travel in a big X shape. your Knight code has a bug duplicating the pair (x+1, y+2) instead of the second one being (x+1, y-2)). Make use of list.extend() / + overload for lists: # resolve knight moves. I'm also not clear why it checks the square to see if the square is white (or has a white piece?) but I'm leaving those as is).Ĭode just looks heavy and redundant. (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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |