Coding a Complete Chess Game AI With Python (Part 1) | PVP Game Mode

Sdílet
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

Komentáře • 255

  • @wiemsaadi2358
    @wiemsaadi2358 Před 2 lety +18

    This has been extremely helpful! Thank you for your time and effort!
    Cannot wait for part 2!!

  • @Noobslayer90
    @Noobslayer90 Před rokem +3

    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!

  • @FreedomWired
    @FreedomWired Před rokem +1

    Subscribed! What a fantastic tutorial! I just can't wait for the AI portion!

  • @rique5434
    @rique5434 Před rokem +1

    great video brother, cant wait for part 2!

  • @saravanajogan1221
    @saravanajogan1221 Před 11 měsíci +36

    Bro Part 2 is much needed so we can understand actual AI. Thanks a lot👏

  • @johnsally8320
    @johnsally8320 Před rokem +3

    @coding Spot, thanks so much for making a great tutorial. Looking forward to part 2!!

  • @kennethgonawan6458
    @kennethgonawan6458 Před rokem +1

    I love this tutorial, it follows the rule of chess and I am looking forward to part 2!

  • @jugujogurt4300
    @jugujogurt4300 Před 2 lety +7

    I just love your tutorials!! They are just amazing

    • @codingspot
      @codingspot  Před 2 lety

      Thank you! Happy you like the channel

  • @yasu_6th
    @yasu_6th Před rokem

    I love this! can't wait for the second part!!!!

  • @bradyvanbibber4556
    @bradyvanbibber4556 Před rokem

    Great video! Eager for pt 2!

  • @andreadallago3773
    @andreadallago3773 Před rokem +1

    Thank you for the content! I enjoyed it! Hopefully we will see the AI part soon ;)

  • @user-ds9ey8pp5h
    @user-ds9ey8pp5h Před 11 měsíci

    This channel is awesome, keep going man 🙏🏼❤️

  • @namphan9281
    @namphan9281 Před 8 měsíci

    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

  • @tomerhertz8502
    @tomerhertz8502 Před rokem +1

    Absolutely great tutorial
    waiting for part 2 pls

  • @protibadimon4169
    @protibadimon4169 Před 2 lety +1

    Finally i have got this, thank you, please make this complete

  • @yeager4269
    @yeager4269 Před rokem +1

    It's very useful, I hope there will be a part 2 soon

  • @tanishqkavlekar3272
    @tanishqkavlekar3272 Před rokem

    Best video!! Waiting for part 2

  • @anonymuz9575
    @anonymuz9575 Před rokem +13

    This is amazing!
    You deserve more subs bro. Any news on Part 2?

  • @manindrasammanathegreat8253

    Pt 2 needed 😍❤

  • @blitzymango9910
    @blitzymango9910 Před rokem

    Really looking forward to part 2

  • @vasubhrdyana3059
    @vasubhrdyana3059 Před rokem +4

    Wow! Such amazing content. I followed throughout and eagerly waiting for part 2. Any leads when will it come?

  • @tripl_t8709
    @tripl_t8709 Před 8 měsíci +2

    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.

  • @blacbrd3235
    @blacbrd3235 Před rokem

    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!

  • @Maite1970
    @Maite1970 Před 2 lety +1

    Felicitaciones! Esta nueva publicación es asombrosa…

  • @m3mzh
    @m3mzh Před rokem +11

    How is this so unpopular? One of the most underrated channels😢
    Hope you will be doing part 2❤

  • @andrebarcellosferreira3917

    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.

  • @Maite1970
    @Maite1970 Před 2 lety +1

    Me gusta mucho la descripción. Facilita el aprendizaje!

  • @marcmailly8176
    @marcmailly8176 Před 11 měsíci +5

    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

  • @Jheter-Pisowifi
    @Jheter-Pisowifi Před 2 lety +2

    Wow, keep it up your tutorials. This chess game is the best for beginner in python like me.this is the best tutorials.

  • @Kalammari7
    @Kalammari7 Před 10 měsíci

    this was so helpful thanks a lot ❤, Please do part 2

  • @asjadsiddiqui7409
    @asjadsiddiqui7409 Před rokem +1

    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.

    • @user-wr9ul
      @user-wr9ul Před rokem

      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??

  • @prakhars962
    @prakhars962 Před měsícem

    excellent video mate

  • @zazobege
    @zazobege Před 9 měsíci +1

    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.

  • @danprecious6313
    @danprecious6313 Před rokem

    This is the best chess game tutorial ever

  • @rankrausz5055
    @rankrausz5055 Před 2 lety +1

    Great stuff, thank you so much!! when will part 2 be out?

  • @anikettandle
    @anikettandle Před rokem +45

    Where is part 2???? Sirrrrr

    • @Lukas-ft6te
      @Lukas-ft6te Před 15 dny

      it was too hard for bro

    • @Turnpost2552
      @Turnpost2552 Před 12 dny

      The other guy he was copying from hasnt publish his video yet. Lol

  • @MagicQBoBo
    @MagicQBoBo Před rokem

    Thanks your video! It helps me a lot! Hope to see your next video!😁

  • @HkLearnsAI
    @HkLearnsAI Před 4 měsíci

    I respect you hard work thanks for this video keep uploading more contents soon!

  • @brotherjoecarden7308
    @brotherjoecarden7308 Před 2 lety +13

    cant wait to give it a go i've been coding python for 6 months and this project will look good in the portfolio!

    • @codingspot
      @codingspot  Před 2 lety +4

      This will look sweet on your portfolio!

    • @SHOTOOOOO357
      @SHOTOOOOO357 Před 9 měsíci

      ​@@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

  • @DistinctInstinct
    @DistinctInstinct Před 2 měsíci +1

    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

  • @zamzamxiv
    @zamzamxiv Před 11 měsíci

    this was great, thank you!

  • @kristopherarmknecht5956

    This is amazing I am hyped for pt2
    Do you know when ita gonna come out

  • @rahulgaikwad6669
    @rahulgaikwad6669 Před rokem +7

    Bro your explaination is best pls upload part 2 soon waiting for it eagerly🙌❤

  • @pitwolff456
    @pitwolff456 Před rokem +62

    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 !

    • @-_Nuke_-
      @-_Nuke_- Před rokem +3

      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.

    • @pranavratra843
      @pranavratra843 Před 11 měsíci +3

      @@-_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!

    • @pranavratra843
      @pranavratra843 Před 11 měsíci +1

      @@-_Nuke_- Awesome!! Glad you figured it out!

    • @sandmanscanga4654
      @sandmanscanga4654 Před 8 měsíci +1

      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_-

    • @revenge6738
      @revenge6738 Před 5 měsíci

      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?

  • @arya.z7410
    @arya.z7410 Před 7 měsíci

    thankyou alejandro for this contribution

  • @autoCHADsoftware
    @autoCHADsoftware Před rokem +9

    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".

    • @imanzainudin9584
      @imanzainudin9584 Před rokem +1

      bless you for this

    • @JK-ws6qr
      @JK-ws6qr Před rokem +1

      I had to put the assets folder inside the src, your way might have been better 😂

    • @revenge6738
      @revenge6738 Před 6 měsíci

      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?

    • @TnagerSugus
      @TnagerSugus Před 6 měsíci

      @@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

    • @revenge6738
      @revenge6738 Před 6 měsíci

      ye fixed it but i dont remember how@@TnagerSugus

  • @kunalghosalkar5429
    @kunalghosalkar5429 Před rokem

    great video..learned a lot

  • @jaredvv86
    @jaredvv86 Před 11 měsíci

    I just found this, hope you do the part two.

  • @theanonymousghoul6296
    @theanonymousghoul6296 Před 2 měsíci +1

    pls bro give part 2... part 1 very gud and helpful tysm!!!

  • @aritraranjanchowdhury1412

    Thank this is one of the best tutorial. But I am waiting for the second part that is the AI part

  • @WaveTreader
    @WaveTreader Před rokem +3

    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.

    • @codingspot
      @codingspot  Před rokem

      Great solution, thanks for that!

    • @WaveTreader
      @WaveTreader Před rokem

      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.

  • @yainattapon2521
    @yainattapon2521 Před rokem

    That #piece = piece is brilliant!

  • @EvanSthapit
    @EvanSthapit Před rokem

    Awesome vid. Is part 2 coming soon?

  • @manuels.5382
    @manuels.5382 Před rokem

    Hola, muy buen video, muchas gracias por compartir. Para cuando la segunda parte?
    When will you publish the second part?

  • @user-gj4me2st2l
    @user-gj4me2st2l Před rokem

    Great video! A request, could make a checkers this time?

  • @a.jshawon3923
    @a.jshawon3923 Před rokem

    Thank you man.....
    its really healpfull ..

  • @TomMathews
    @TomMathews Před rokem

    @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.

  • @roadto1m306
    @roadto1m306 Před měsícem

    Ty this was funn (:

  • @rishytpro
    @rishytpro Před rokem

    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....

  • @cybrd230
    @cybrd230 Před 11 měsíci +2

    We need part 2.

  • @user-hr7rb1zw3v
    @user-hr7rb1zw3v Před rokem

    Still no part 2. I love your work so much and have been waited for 6 months :(

  • @gouthxms
    @gouthxms Před rokem

    @codingspot when is the PART 2 is coming cant wait to see it

  • @arshamcem8880
    @arshamcem8880 Před rokem +1

    Hi mi amigo .Will be part 2 for this project??
    I'm waiting for coding an AI for this wonderful project.

  • @lukedamkoehler1899
    @lukedamkoehler1899 Před 2 lety

    Thank you for this!

    • @lukedamkoehler1899
      @lukedamkoehler1899 Před 2 lety

      I am sharing this link with friends! It was a great learning experience.

    • @codingspot
      @codingspot  Před 2 lety

      Great! Thanks for your support

  • @tomatenheatderechtetomaten2379

    we need part 2 !!!!

  • @doktermaiskorn833
    @doktermaiskorn833 Před rokem

    Idea for a additional update to this: You could add the function to show when a piece is threatened

  • @fadyhanyelmasry8272
    @fadyhanyelmasry8272 Před rokem

    Please I need part2 so much 🙏

  • @spadedarkyt4410
    @spadedarkyt4410 Před rokem +1

    you should be using nega max alpha beta pureing with tranpostion tables and openning book, and easy endgame ideas

  • @kollas8066
    @kollas8066 Před rokem

    Nice video

  • @khankaka7632
    @khankaka7632 Před rokem +1

    waiting for part 2

  • @sauravspcreations
    @sauravspcreations Před 3 měsíci +1

    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.

  • @vto9631
    @vto9631 Před rokem

    Man this is so cool im a brazilian viwer and THAT video catch me and obviously i will pass this video to my friends 😁

  • @qwerty-st9hh
    @qwerty-st9hh Před 11 měsíci

    i want part 2 !!!!!!💖

  • @alphazentauri273
    @alphazentauri273 Před rokem

    Brooo I need the second part!!!

  • @sudhishbishtxii8113
    @sudhishbishtxii8113 Před 2 měsíci

    We need Part 2

  • @suncake_xd
    @suncake_xd Před rokem

    Still waiting for part 2. Also wondering what theme for VSCode you're using

  • @FanZy0127
    @FanZy0127 Před 2 lety

    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 !

    • @FanZy0127
      @FanZy0127 Před rokem +1

      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 ^^

    • @gabrielrodriguez795
      @gabrielrodriguez795 Před 9 měsíci

      @@FanZy0127 have you fixed this issue

  • @sanderdejong66
    @sanderdejong66 Před rokem

    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.

  • @LingoTest
    @LingoTest Před rokem

    Part 2 please.

  • @manhtiennguyen9571
    @manhtiennguyen9571 Před 6 měsíci

    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.

  • @protibadimon4169
    @protibadimon4169 Před 8 měsíci

    Please, part2

  • @johncirincione9234
    @johncirincione9234 Před 16 dny

    Lookin good, only thing I saw was that you can promote a pawn to any piece, but your code only allows for queen promotion.

  • @userlex8390
    @userlex8390 Před rokem +1

    Awesome video, thanks a lot, can you tell me the theme that you use on vs code by any chance ?

  • @sanderdejong66
    @sanderdejong66 Před rokem

    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.

  • @nechtan24
    @nechtan24 Před 2 měsíci

    It's been two years, are we getting a part two?

  • @fumano2679
    @fumano2679 Před rokem

    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)

  • @omer7895
    @omer7895 Před rokem

    When are you making part 2?

  • @HeangBorin
    @HeangBorin Před rokem

    Will you add part 2?

  • @openp
    @openp Před rokem

    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!

  • @sanderdejong66
    @sanderdejong66 Před rokem

    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.

  • @tanukaur7900
    @tanukaur7900 Před rokem

    Please share part 2

  • @hophopgogo
    @hophopgogo Před 5 měsíci

    Is Part 2, the AI part, available?

  • @danielcjohn7016
    @danielcjohn7016 Před měsícem +1

    how to implement Checkmate and Stalemate

  • @manekane-uw3dn
    @manekane-uw3dn Před rokem +1

    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

    • @vincenzorapisardi6372
      @vincenzorapisardi6372 Před 4 měsíci

      Where should I actually code this else statement? Because I've also found this bug and I've been unable to fix it

  • @zeegy99
    @zeegy99 Před rokem

    I had a question about Sqsize, I can't seem to find anything about it and it doesn't work for me

  • @joshuajoel231
    @joshuajoel231 Před 5 měsíci

    When will part 2 be released

  • @toniocartonio6261
    @toniocartonio6261 Před rokem

    when does second part arrive?

  • @alexandrutopai2229
    @alexandrutopai2229 Před rokem

    what extensions do you use for vsc ?

  • @gurugaming8398
    @gurugaming8398 Před rokem

    Part2 please fast

  • @hosiminh8152
    @hosiminh8152 Před rokem

    please video part 2 chess AI