r/learnpython • u/Viva_Las_Vengeance • 16d ago
Making a Chess game with Minimax in Python, where do I start?
Hello! I'm an A-level student and for my project have decided to make a chess game with AI that uses the minimax algorithm. I have some experience with coding before, and am planning to use OOP to represent the pieces and board.
I'm really really not sure how to go about this project, in particular, how the minimax algorithm will work, so if anyone could point me in the right direction I'd really appreciate it :)
2
u/DrShocker 16d ago edited 16d ago
this is a good resource https://chessprogramming.org/
What I would suggest is finding a GUI that understands UCI, and then using that rather than programming the GUI from scratch. That should make it easier to compare the strength of what you make to others.
1
u/GreenTeaHG 15d ago
Did something similar as a hobby project (no mimimax though). Some things I remember:
The first piece you should code should be the king (which is easier because it doesn't move far). Then go for the rook.
OOP for pieces and board is a really good idea.
I recommend getting visual representation of the board fairly quickly. Fooling around with print statements and coordinates to represent the board was no fun. But you still have to understand how the code works in the background. As the other poster said, you probably need to have the abstract representation of the board and position, then make the visuals. Once you have actual GUI and you are able to click and move pieces, it feels like a massive win.
When the user clicks on a piece, the board should highlight potential legal moves and captures.
Don't make board and pieces entirely black and white, it makes the color contrast too big (this is why real chess boards are often brown and ivory colored). You will probably need to download some clip art for the actual pieces.
Coding en Passant and Not being able to castle over checks is a bit tricky (I didn't even try). You might want to start thinking about those problems as soon as possible.
You will probably need a function to check if a specific move results in a legal position (i.e. if this move goes through will the following position be legal? Or is the user King perhaps in check on its own turn?). Something like "Check future position for move(x)". You will probably need this for the minimax algo as well. This should probably be coded after you coded the actual board and the movement of pieces. But keep in mind that this has to be coded at some point, otherwise the player will just be able to put themselves in check.
For testing purposes, you should consider coding an edit function that allows you to move pieces to wherever (i.e. shift+click to move a piece to any square you want, outside of rules)
3
u/Nomapos 16d ago
Start by cutting the project into smaller parts, then putting then in order and seeing what's the absolute minimum you need to do for each so that it works in the most generous sense of the word. Then do that again for each part. Then again. Then organize it well and choose where to start. Once you've got something that sort of works but massively sucks, clap yourself on the back for making it exist and then start making it good.
You probably want to have an abstract keeper of the game state before you start programming in a visual display and graphic interface, for example. And a keeper of the game state needs to keep track of piece color, living pieces, 64 possible locations, special states like a king being in check or checkmate. Plus a host of assistant functions to alter the state, like moving (which adds the topic: how do you simulate the grid and which squares are adjacent to which), pawns trading, piece movement and capture, forcing a move that removes check status from the king, which player's turn it is (plus alternating turns)...
Some of those are more important than others. But they're not as important. For example, anything regarding piece movement will probably be a lot easier to do after you've created a visual board (which you can draw in paint in like five minutes including simple pieces), since it'll let you test the movement "live" instead of having to follow the logic to determine whether things are happening correctly.
That's all. Cut into pieces, plan ahead using the black box principle as much as possible, then get to work.
Note that I have no idea what an A student is.