Coding a Complete Chess Game AI With Python (Part 1) | PVP Game Mode
Vložit
- čas přidán 16. 06. 2024
- In this tutorial I will show how to code a fully functional chess game from scratch, with both player vs player and artificial intelligence game modes, using object-oriented programming, the minimax algorithm, Python and its Pygame module.
LINKS:
😺 GitHub Repo: github.com/AlejoG10/python-ch...
- Part 2 (AI Game mode): Soon...
- Windows Setup: • How to Install Python ...
- Mac Setup: • How to Install Python ...
FOLLOW FOR MORE:
💥 GITHUB: github.com/AlejoG10
💥 INSTAGRAM: / coding_sp0t
⏱ TIMESTAMPS ⏱
00:00 Introduction
4:34 Game Structure (UML)
17:13 Initial Setup
24:42 Rendering Board
31:31 Console Board
39:02 Creating Pieces
1:07:50 Dragger
1:37:07 Checking Valid Moves
3:01:36 Moving Pieces
3:37:36 Game Design Config
4:10:21 Restart Function
4:12:14 Pawn Promotion
4:17:09 Castling
4:33:18 Game Checks
4:58:32 En Passant
5:35:05 Castling Bug
5:36:23 End - Věda a technologie
This has been extremely helpful! Thank you for your time and effort!
Cannot wait for part 2!!
Awesome work! Im sure you put a lot of effort in it! I am a passionate chess player and data scientist and I always wanted to code my own Chess game and Chess AI. This is perfect for me!
Subscribed! What a fantastic tutorial! I just can't wait for the AI portion!
great video brother, cant wait for part 2!
Bro Part 2 is much needed so we can understand actual AI. Thanks a lot👏
True.
@coding Spot, thanks so much for making a great tutorial. Looking forward to part 2!!
I love this tutorial, it follows the rule of chess and I am looking forward to part 2!
I just love your tutorials!! They are just amazing
Thank you! Happy you like the channel
I love this! can't wait for the second part!!!!
Great video! Eager for pt 2!
Thank you for the content! I enjoyed it! Hopefully we will see the AI part soon ;)
This channel is awesome, keep going man 🙏🏼❤️
great video, looking foward to your next part. One thing i want to talk about your chess code, you forgot at the castling method that when the castling "road" is threatened by some enemy pieces, the king should not able to castling. But doesn't matter, thank you for the awesome video. Last but not least, when you get excited each time your code works made me get passion
Absolutely great tutorial
waiting for part 2 pls
Finally i have got this, thank you, please make this complete
It's very useful, I hope there will be a part 2 soon
Best video!! Waiting for part 2
This is amazing!
You deserve more subs bro. Any news on Part 2?
Pt 2 needed 😍❤
Really looking forward to part 2
Wow! Such amazing content. I followed throughout and eagerly waiting for part 2. Any leads when will it come?
Still waiting for part 2... this is so sad. Whatever you're doing coding spot, I hope you're well. Loved the video, this was my first real experience with coding a game and you made this first experience so joyful; so thanks a lot.
Thank you for this incredible video! It taught me a lot of concepts that can be applied else where in different projects and was very well explained!
Felicitaciones! Esta nueva publicación es asombrosa…
Gracias! :)
How is this so unpopular? One of the most underrated channels😢
Hope you will be doing part 2❤
Totally true
Hi. Congratulations on your video. It has helped me a lot. I've just started my doctorate in architecture, studying artificial intelligence applied to it. This was my first real contact with Python, and your video helped me a lot. Can you say when you are going to make the second part available? Thanks.
Me gusta mucho la descripción. Facilita el aprendizaje!
If the 2nd part comes, I want to translate the IA to Eddie Sharick's project.
Eddie Sharick don't have a Piece class, but his board can recognize his pieces colors (letter 'w' for White and 'b' for Black). Sharick's moving methods is ranged in a move fonctions dictionary, and he have self.pins and self.checks listes to check pins and checks, a class CastleRights of 4 booleans for kingside castle and queenside castle. His class GameState is a complete controller for valid moves
Wow, keep it up your tutorials. This chess game is the best for beginner in python like me.this is the best tutorials.
this was so helpful thanks a lot ❤, Please do part 2
Such a beautiful and useful video, I can't thank you enough for this video, it's like intermediate level python, and its also sort of a project twhich makes following along the video much more rewarding and enjoyable.
Hey, does the video cover the AI part and if not do you think we can integrate the AI part using any other chess AI example easily??
excellent video mate
Hope you upload the code and show a part 2 soon, I'm trying to make my own version of chess and will need to consider adding AI, until then I will learn from this tutorial and try to carry on from there.
This is the best chess game tutorial ever
Great stuff, thank you so much!! when will part 2 be out?
Where is part 2???? Sirrrrr
it was too hard for bro
The other guy he was copying from hasnt publish his video yet. Lol
Thanks your video! It helps me a lot! Hope to see your next video!😁
I respect you hard work thanks for this video keep uploading more contents soon!
cant wait to give it a go i've been coding python for 6 months and this project will look good in the portfolio!
This will look sweet on your portfolio!
@@codingspothi I know it's been a year and I'm not sure if you'll see this but what if the 800 800 is to big for my screen how do I change it and still get the whole chess board visible
Hey man! Absolutely amazing video, I learnt so much. Are you going to release part 2 with an Ai version and also fixing all the bugs listed in the comments? Would be amazing if you could!! Thank you
this was great, thank you!
This is amazing I am hyped for pt2
Do you know when ita gonna come out
Bro your explaination is best pls upload part 2 soon waiting for it eagerly🙌❤
It's already 10 months part 2 won't come.
@@alvynabranches1 it has been 1 year
Great video ! There are some bugs though. The king can't make certain moves when he is in check and he can castle when he is in check or by crossing forbidden squares. Also missing is the ability to promote a pawn to a piece other than the queen. Finally, it is necessary to detect the different ways to make a draw (repetition, 50 moves rule, stalemate...). An animation to indicate the end of the game would also be nice. Good luck to solve all these problems and to code the AI. I hope the second part will be released soon and I'm looking forward to it !
Does anyone know how I should code this, I *explicitly* don't want to have and or use valid moves?
For example in tabletop simulator, one can play chess just like he would in real life, by picking up the pieces and placing them onto the board.
I have just finished this lesson and it was very good, but I would want to try to make a game where the game doesn't check if the move that you are making is legal or nor.
@@-_Nuke_- I actually happened to code exactly what you seem to be looking for: drew in the board and the pieces, but didn't check for legal moves. The way I did this was with the Turtle module. I generated a board and pieces, and with each mouse click checked which square's central coordinates were nearest to the click, to indicate which square was selected. Every piece move was designed to give the piece the same coordinates as the central coordinates of the square, so it was simple to check if the clicked square had a piece on it. If it found that a piece was indeed there, you could then click on a different square to move the piece to that location, but it never checked which piece (or which colour) was selected, so it had no concept of legal moves. It was a purely geometrical construction, creating the board and pieces and assigning squares and pieces particular coordinates for their locations on the screen. By only checking the coordinates of the mouse click, and comparing it with the coordinates of the board squares and the pieces, you can generate a chess game that has no concept of valid moves. Sorry this answer is kinda all over the place, but hope it helps!
@@-_Nuke_- Awesome!! Glad you figured it out!
If the board is a mutable object, you don't actually need to save a copy of the entire instance. You can just save the position and temporarily selected piece, then do whatever logic you need to do pending the user did something valid... Then just redraw the board right over the existing board with new positions. Think of it like painting over the old canvas instead of getting an entirely new canvas every time something happens.@@-_Nuke_-
hello, can you give me an idea on how to check if the game is over, because idk how to get the count of all valid posititons, beucase if its 0 then its checkmate but isnt it gonna be way too slow to check it on every iteration?
thankyou alejandro for this contribution
Since I don't see this mentioned, I needed to add "../" to the beginning of the path pointing to the assets folder, in order for python to find the chess piece images. It may have something to do with my interpreter's default "working directory", but anyways, "../" is how you get one more step up a file path tree structure. It has to go up one step from "src" in order to find "assets".
bless you for this
I had to put the assets folder inside the src, your way might have been better 😂
File "c:\Users
ikoa\OneDrive\Desktop\testP\src\piece.py", line 29, in __init__
super().__init__('pawn', color, 1.0)
TypeError: __init__() missing 1 required positional argument: 'texture'
Im getting and i did what u said but it still doesnt work. Could you give me some advice?
@@revenge6738 I had this problem too. In the Piece-class I set Texture=None, after that I had a different error but unrelated to yours I think
ye fixed it but i dont remember how@@TnagerSugus
great video..learned a lot
I just found this, hope you do the part two.
pls bro give part 2... part 1 very gud and helpful tysm!!!
Thank this is one of the best tutorial. But I am waiting for the second part that is the AI part
There is actually a nicer way of avoiding putting the king in check. This method does not need any copy or simulating all possible moves, it does not need to check for all opponent's possible moves. It works like this.... you must have tracked the kings position every move and save it. Make the move even if it puts the king in check, from there check from the king's position if any piece attacks it by looking at capture moves from the king's position. If it results in a capture, just undo the move. it happens really fast you wont see the invalid move being executed. The idea is that you make a function that assumes the king can move like a queen, bishop, rook, knight or pawn capture.
It is much simpler coz you are only checking from the king's position to move like any other attacking pieces, if they are present from these attacking positions, then that means the last move is invalid so undo the move.
Great solution, thanks for that!
No worries bro, I really enjoyed this tutorial. I am learning a lot. Sometimes ideas pop out through your tutorials. But I have to learn how to implement it.
That #piece = piece is brilliant!
Awesome vid. Is part 2 coming soon?
Hola, muy buen video, muchas gracias por compartir. Para cuando la segunda parte?
When will you publish the second part?
Great video! A request, could make a checkers this time?
Thank you man.....
its really healpfull ..
@codingspot This has been really impressive, and awesome. Thanks a ton. Is there any update on the Part 2 of this video? Looking forward to it.
Ty this was funn (:
It is a nice video. Everything is very beginner friendly. I am eagerly waiting for the 2nd part. Can you please upload it quick? It's been an year to the first part....
We need part 2.
Still no part 2. I love your work so much and have been waited for 6 months :(
@codingspot when is the PART 2 is coming cant wait to see it
Hi mi amigo .Will be part 2 for this project??
I'm waiting for coding an AI for this wonderful project.
Thank you for this!
I am sharing this link with friends! It was a great learning experience.
Great! Thanks for your support
we need part 2 !!!!
Idea for a additional update to this: You could add the function to show when a piece is threatened
Please I need part2 so much 🙏
you should be using nega max alpha beta pureing with tranpostion tables and openning book, and easy endgame ideas
Nice video
waiting for part 2
Very Very Excellent, i just complete your course, and I found some bugs, I'll try to solve, if I can't then how to connect with you regarding problem.
Man this is so cool im a brazilian viwer and THAT video catch me and obviously i will pass this video to my friends 😁
i want part 2 !!!!!!💖
Brooo I need the second part!!!
We need Part 2
Still waiting for part 2. Also wondering what theme for VSCode you're using
Hello there,
Fantastic work. I've been inspired by your video and decided to go for a chess AI project to validate my school year. :)
Refactoring as much as possible, trying some things (for example I implemented the fact that you can choose your own pawn promotion thanks to easyGUI).
I have a question : do you also have a problem with the fact that the castling is still allowed while in check sometimes ?
By the way, I'm trying to figure out how to handle the pat in the code, and also have something hapenning when in check mate ?
I can't wait for you to show how you deal with AI !
To know if you'll use Keras, Tensorflow, using models, etc ^^
Keep going !
I don't know if this is just me or something, but I also noticed that when an enemy piece is attacking our King, and it is next to it, the King cannot eat that piece. I'm trying to figure out why ^^
@@FanZy0127 have you fixed this issue
12:00 I have also been thinking about a chess program. Have you considered a class Player with subclasses HumanPlayer and ComputerPlayer? With a method next_move, which will ask either a human or a computer/algorithm/AI for the next move.
Part 2 please.
I've seen cases where the king is behind, the rook is in check, has a way to run but can't move the king, or can move the king but can't use another piece to block it.
Please, part2
Lookin good, only thing I saw was that you can promote a pawn to any piece, but your code only allows for queen promotion.
Awesome video, thanks a lot, can you tell me the theme that you use on vs code by any chance ?
i think it is Dracula Official
@@echedangelo5593 thx
1:40:00 Any reason why you don’t use the class structure here, in which each Piece subclass does its own calculation? In that way this calc move can simply call the calc move method of the Piece. No need for a long if-else statement.
It's been two years, are we getting a part two?
1:00:35 i dont understand why we assign new squares to the square list. i thought we would just asign the piece to the current square -> self.squares[row][col].piece = Pawn(color)
When are you making part 2?
Will you add part 2?
Great video, but I feel like some aspects of the code were taken from Eddie Sharick's Python Chess Engine series. I just thought I should make you aware, maybe put a link to his series in the description? Sorry if this content is 100% original, it just looked similar!
4:47:50 I strongly recommend to rename bool to something meaningful. Now you have to look through the code each time, to find out what it is used for. If you give it a proper name, you will save yourself a lot of time.
Please share part 2
Is Part 2, the AI part, available?
how to implement Checkmate and Stalemate
the bug is that if u click a piece and don't move it then that piece will remember all the valid_moves. To fix this just add else statement clear_moves() when its an invalid move
Where should I actually code this else statement? Because I've also found this bug and I've been unable to fix it
I had a question about Sqsize, I can't seem to find anything about it and it doesn't work for me
When will part 2 be released
when does second part arrive?
what extensions do you use for vsc ?
Part2 please fast
please video part 2 chess AI