I started learning Python on Codecademy about a week ago, and when I try to learn languages I try to think of a project to attempt to accomplish in that language to measure my progress.
Chess seems like it will be a very difficult project that will take a lot of thinking, and will force me to find new ways to deal with problems that I haven’t thought of before.
The goal of this post is to look for resources and/or tips anyone could have for starting to evaluate the legality of a “move” in chess. I don’t want you to hand the answers to me, I’m just looking for ideas.
Here is my initial ideas on the project, I’m about 50% through the Python course, so forgive me if I’m missing some blatant issues.
player = "nothing" coinflip = random.random() if coinflip == 1: player = "white" else: player = "black" #Evaluate Algebraic Notation. Longform only. Qh4e1 or Pa2xa4 #Determines the row and column of the piece you are moving and where it is moving to. #Determines if you are taking a piece #Determines which piece you are moving playerMove = input("What move would you like to make?") playerMovePiece = playerMove playerMovePieceLoc = playerMove[1:3] playerMoveLoc = playerMove[-2:] playerMovePieceLocRow = playerMovePieceLoc playerMoveLocRow = playerMoveLoc playerMovePieceLocCol = playerMovePieceLoc playerMoveLocCol = playerMoveLoc if playerMovePiece == "Q": playerMovePiece = "Queen" if playerMovePiece == "R": playerMovePiece = "Rook" if playerMovePiece == "P": playerMovePiece = "Pawn" if playerMovePiece == "K": playerMovePiece = "King" if playerMovePiece == "B": playerMovePiece = "Bishop" if playerMovePiece == "N": playerMovePiece = "Knight" str1 = "x" if str1 in playerMove: takes = True else: takes = False print("Your move: " + playerMove) print("Your piece: " + playerMovePiece) print("Your piece location: " + playerMovePieceLoc) print("Where you are moving it: " + playerMoveLoc) print("The row it is currently in: " + playerMovePieceLocRow) print("The column it is currently in: " + playerMovePieceLocCol) print("The row you are moving it to: " + playerMoveLocRow) print("The column you are moving it to: " + playerMoveLocCol) print("Are you taking a piece? " + str(takes)) if playerMovePiece == "Pawn" and playerMovePieceLoc != playerMoveLoc: print("Illegal Move!") if abs(int(playerMovePieceLocRow) - int(playerMoveLocRow)) > 2: print("Illegal Move!") #En passant detection. If you move the pawn, change the value, so we can check for double moves in future. This has a flaw at the moment where it doesn't care which pawn is moving into the column, it just decides to turn off that column, that should be easily fixable if/when I find a way to map to my list format that I've planned. pawna = True pawnb = True pawnc = True pawnd = True pawne = True pawnf = True pawng = True pawnh = True if playerMovePiece == "Pawn" and playerMovePieceLocCol == "a": pawna = False if playerMovePiece == "Pawn" and playerMovePieceLocCol == "b": pawnb = False if playerMovePiece == "Pawn" and playerMovePieceLocCol == "c": pawnc = False if playerMovePiece == "Pawn" and playerMovePieceLocCol == "d": pawnd = False if playerMovePiece == "Pawn" and playerMovePieceLocCol == "e": pawne = False if playerMovePiece == "Pawn" and playerMovePieceLocCol == "f": pawnf = False if playerMovePiece == "Pawn" and playerMovePieceLocCol == "g": pawng = False if playerMovePiece == "Pawn" and playerMovePieceLocCol == "h": pawnh = False # # # evaluate playerMove(Is checkmate? Is taking piece? Is stalemate? Is check? Is legal?) # perform playerMove(Yikes) # switch players # repeat # # Ra1a3 # is the move legal for Rook? # is there a Rook on a1? # is a2 occupied? # is a3 occupied? # if yes, is it occupied by the opponent? # if yes, take the piece. # if no, move the piece. # # Pawn = 1 # Rook = 2 # Knight = 3 # Bishop = 4 # Queen = 5 # King = 6 # # row8 = [2,3,4,5,6,4,3,2] # row7 = [1,1,1,1,1,1,1,1] # row6 = [0,0,0,0,0,0,0,0] # row5 = [0,0,0,0,0,0,0,0] # row4 = [0,0,0,0,0,0,0,0] # row3 = [0,0,0,0,0,0,0,0] # row2 = [1,1,1,1,1,1,1,1] # row1 = [2,3,4,5,6,4,3,2] # a,b,c,d,e,f,g,h
So far the only “working” code I have takes a user input in longform Algebraic Notation, something like Qa3xa4 and splices that into it’s individual pieces to determine what the goal of the input is.
Q would indicate that they are trying to move their Queen
A3 would indicate that the piece they are trying to move is in column A and row 3
X would indicate that they are taking a piece, so there is a piece already on the destination
A4 would indicate that the destination is column A and row 4
I have a basic idea of how to make pawn legality checking function, I can have a trigger for “if the pawn in question has been moved yet” and if so, it’s only legal move is to take on a diagonal in front of it or to move 1 forward. Otherwise it’s legal moves consist of a double move, a single move, a normal diagonal take, or an en passant.
Any input anyone has, feel free to provide, this is a learning project for me, I have plans in the future to give it actual graphics as well rather. than it all just be backend, but I’d like to get the function worked out before I delve into trying to figure out how something like tkinter would work.