[Music] hey guys welcome to codingspot and welcome to a new video where i'm gonna be showing you how to code this beautiful chess game from scratch we're gonna be coding two different game modes on the first part of this tutorial we're gonna focus on the player versus player game mode and on the second part of this tutorial we're gonna code our own artificial intelligence to play against now this is gonna be super cool however it's gonna be a little bit complex but don't worry i'm gonna try my best as always to make you understand each line of code that we type um so yeah this is the final result of the game that we're gonna code as you can see we can drag our pieces around the board we're also gonna be able to change our background or our themes um so yeah for for for this example i coded four themes so we have the green one the brown one the blue one and the gray one but we can create as many themes as we want also we're gonna be implementing sounds so as you can see uh each time i capture a piece a sound is displayed now also when we move our piece so let's say i move right here we're gonna get a different sound also we're gonna be coding a research function so we are going to restart our game whenever we want and we can start playing all over again and finally on the second part of this tutorial as i told you we're gonna be coding an ai to play against so i'm gonna change the game mode to ai for you to see but before that i want to open the console i want to show you something okay so i'm gonna press a key in order to change the game mode and we're gonna start playing against the artificial intelligence i'm gonna go e4 and as you can see ai answers with c5 the sicilian defense i'm gonna go with knight f3 and then i'm gonna develop my other knight now as you can see finding best move is printed on console now these first two moves were book moves so we didn't really use the minimax algorithm which is the algorithm that the aei is going to use they were just pretty fine moves now on the third move we actually use the minimax algorithm so we start finding the best move um so yeah as you can see we printed initial eval final level and number of ports explored now we're gonna go into detail of what an evaluation is when we code the all the ai but i want you to see the number of boards that the a i explored in order to make this move so in other words from almost 8 000 words this was the best move according to our evaluation function probably this is not the best move in general but according to our evaluation function the evaluation function that we are gonna code this was the best move now as you can see the the number of boards well it's obviously variable and and it is directly proportional to the time it takes the ai to make a decision um so yeah it's pretty cool we're gonna go into detail uh of these of these things when we go the ai but but yeah this is the final result so as i show you a moment ago we can play against another person that would be the player versus player game mode but we can also play against our artificial intelligence so so yeah this is going to be a really long tutorial but but i think the final result deserves all the time and effort that we are gonna put um on this project so yeah i really hope you like this tutorial i hope i can i can do my best for you to understand each line of code um so yeah now that you know what we are gonna code let's start coding now before we start adding files or coding i wanna actually show you the structure of our game now you can skip this part but i strongly recommend you at least to see the classes that we're gonna use and have an idea of how these classes are going to connect between them in order to achieve the fully functional game so yeah feel free to skip this part if you want but yeah i'm gonna start explaining how are we going to structure our game okay so this is a class diagram so if you haven't seen one of these before it's pretty simple each box represents a class of our application in this case of our game now each arrow represents a reference from one class to another one so for example a board has multiple squares and a square has zero or one piece so that's how we're gonna connect basically all of our application so yeah we're gonna start with this box right here this is actually not a class this is this is going to be just a file pretty simple file and we are gonna we're gonna save the constants like the main constants that we need for the game so in this file we're gonna save the width and the height of our screen we're gonna save the rows and the number of calls of the board this is obviously gonna be equal to eight and we're gonna have a constant called square size which is going to be equal to either width divided by cos or height divided by rows so pretty simple file now we're going to start with our main route which is our main class now the main class is gonna be long but it's pretty simple we're gonna have an attribute which is the screen and we're gonna have a method called main loop so this method is going to be huge from this method we're going to call basically all the application so this method is it's quite complex now the main the main class is going to have a reference to our game class now game class is a little bit long also we're gonna have two attributes we're gonna have next player so either white or black we're gonna have a game mode so pvp or ai and we're gonna have these methods so inside the game class we're gonna have the render methods like show background show pieces show moves show last move show hover we have a reset function a change theme a sound effect next turn change game mode set hover select piece and unselect piece so these methods are actually pretty simple from here let me show you from here to here so from reset to unselect piece those methods are gonna be pretty simple now the methods that are gonna be more complex here are gonna be the ones that we need that we have to render things so all these show methods okay let's continue our game class is going to have a reference uh with every single class pretty much so i'm gonna start with the board so a game has one board obviously we're gonna have two private methods create which creates the board and add pieces we're gonna have move this method is going to be quite complex because it's responsible of moving a piece check promotion valid move and calc moves this method is going to be huge also okay so what else a board has also some references it has one or zero last move so we're gonna save a variable called last move inside of our board and this is going to be important because we wanna we'll render our last move uh we're also going to have multiple squares so we're gonna have a reference to a lot of squares uh if we want to be precise we're going to have 64 squares so a board has 64 squares and the the square class is pretty simple uh its properties are gonna be row column so the row one column of the of the square an alpha call alpha call is basically the column but represent it as a char um okay we have some methods here inside of our square class so we have has peace has team peace has rival peace is empty is empty or rival so these methods basically return true or false if they meet some conditions okay so from square we're gonna have a reference to to a piece so a square can have uh zero or one piece and the the the piece class is gonna be pretty simple we're gonna have some attributes so we're going to have the name of the piece the color of the piece the value of the piece really important attribute the moved this is a boolean attribute the texture of our piece the texture rect of our piece this is a pie game rectangle and we're going to have two methods add a move and set texture so pretty simple now from this class there are gonna be some classes that are gonna uh will inherit so obviously we're gonna have the pawn class which inherits from peace if you if you don't know what inheritance is it's basically that the pawn class is gonna have the same as the piece class uh but we're gonna we're gonna add some things some other things to it so so in the case of the pawn we're gonna have an attribute called direction uh we have the night class the bishop class the rook the queen and the king class um so yeah it's it's pretty simple we're gonna put all these classes in one file okay what else a piece has a reference to a move because well a piece has a lot of moves and by moves i mean valid moves so i'm gonna put valid moves so a piece has a lot of valid moves this class it's responsible of saving a move so where is the initial position of the move and where's the final position of the move uh so as you can see we have a reference to a square which is a initial and we have a reference to a square another reference which is final with the initial and the final squares we have a move okay let's continue game class it's also going to have a reference to our square to to our square class we're going to have also a reference to our piece piece class and that's it now let's go to this side of our class diagram okay so the game class is going to have uh well a reference to the ai class a game is going to have one ai and the ai class is going to have the following attributes we're going to have an engine attribute which is going to be equal to either book or minimax so it's basically we're using the book or we're using the algorithm to determine the best move we have a depth or the level of the ai we have the color of the pieces of the ai and we have an attribute boards explored which is the attribute that i previously showed to you that was being printed on console now we have some methods these methods are going to be complex um are going to be hard because this is where we are going to implement the artificial intelligence so we have an eval method we have a minimax which is the algorithm huge method we're gonna have a heat map which is responsible of telling us the best positions of any specific piece we're gonna have a threads method and we're gonna have a book move and get moves now bookmove is going to call the class book so ai has a reference to book and book it's basically a book literally where we're gonna pre-define or we're gonna have predefined moves so we're gonna create the book and we're gonna have one method called next move now inside of next move we're gonna have this class note which i don't really know i don't think we're gonna code this like i'm not even sure we're gonna use a book like that's the problem i think we're gonna just use the minimax i don't think we're gonna use book moves but we'll see but however a note is basically one move of the of the book moves so yeah i know it's a little bit confusing but as i told you i don't i'm not sure if we're going to code these two classes okay let's continue our game class it's also going to have a reference to our dragger class the dragon class is going to be responsible of dragging the pieces so if you remember when when i showed you the gameplay we were able to drag a piece around the board this is the class responsible for that it's actually pretty easy it's not it's not so complicated we're gonna have some attributes dragging mousex mouse y initial row initial call and we're gonna have some methods update bleed update mouse save initial position drag piece and then drag piece so yeah and lastly we have all the configuration classes so the game is going to have a reference to a class called config config is going to have an index we're gonna see what what this is when we're coding it we're gonna have a font the font of the game uh we're gonna have a private method add themes and we have a method change theme which is connected to this method change theme as you can see okay from config we have multiple themes and a theme has three reference to color why because a theme has a trace color a background color and a moves color we're gonna see what's that when we code it now the color class pretty simple just has a light color and a dark color now the last reference is from config to sound so it has two references move sound and capture sound um and the sound class it's pretty simple we're gonna have the path of the sound uh we're gonna have the actual sound object which is a python sound and we're gonna have a method called play which is basically responsible of playing this sound so yeah this is the structure of our game i think it was pretty important for me to show you first how are we going to organize our code now we're not gonna follow this structure exactly as it appears here because obviously we're gonna we'll start changing some things and we're gonna have problems and as i told you i think we're not gonna use the the book and node class we're just going to code the minimax function um because this is not this is not a i this is not ai and well the idea of implementing the artificial intelligence is to use ai this is predefined moves so yeah make sure to pause the video see all the classes and more or less understand this structure understand the connections between the classes and now we're ready to try coding our game okay so the first thing is to make sure you have python pie game and any text editor installed on your computer if you don't have any of these three things installed on your computer make sure to see my videos where i explain how to do that for mac and windows users now once you're ready make sure to create a new folder i called mine chess ai i'm going to use bs code as my editor but you can use whatever editor you want so we're going to start by creating our constants file so go ahead and type cons dot py and we have our first file okay before we start coding it i want to create a folder i'm gonna call source this is where we are gonna store all of our classes so i'm going to put our const file right here and inside this file we want to create the following so we're going to create the screen dimensions so screen dimensions we're gonna have a width i'm gonna use 800 pixels but you can well you can play with these values and i'm gonna have a height of 800 pixels make sure these two values are the same because we want to have a square now we're going to have also our board dimensions right here we want to have the columns which is going to be equal to 8 and the rows which are going to be equal to 8. and as i told you we're going to have a square size constant which is basically width width divided with double slash because we want it to be an integer divided calls you can also divide height by rows it will give you the same result okay and that's all that's our first file so now we're going to create our main file and we're going to import these constants that we just created so go ahead and type main.py inside of this file we're going to have our main class so in order to create our main class go ahead and type class main right here we are gonna create our init method i'm gonna explain what this is in a moment we're gonna receive self as parameter and we're gonna pass it now we're also going to create a method called main loop so this is the method like pretty much responsible of calling all other classes we're going to receive also self as parameter and we have our main class now at the bottom of the file right here we want to create an instance of this main file so go ahead and type main equals main so main is the object main is the instance of the main class and now that we have an instance we can call our main loop so go ahead and type main dot main loop now this init method is always called when we create an object so when we created this main instance the first method that's being called is the init method and then we're calling the main loop as you can see so if i print something so let's say hello and right here i print world and i hit run i run my file you can see we have hello world hello being printed first on console so this this init method is always going to be called first now once that we have this we can go ahead and start importing the models that we are going to use so go ahead and import pygame uh yeah the pie game model we're also importing the sys model this is gonna help us quit the application and we're gonna import um our constant so gonna type from const import all now we have access to the constants that we previously created now inside of the main class we're going to create our screen that's the first thing we want to do our pi game screen so in the init method go ahead and type pygm.init we are initializing our pie game model so we we always have to do this when we are working with pygm we want to initialize it now we're going to create the the screen attribute so go ahead and type self self.screen so each time we want to create an attribute or a property of our class you have to create it with a self keyword so self.screen and this is gonna be equal to pygame dot display dot set mode and inside here we want to pass a tuple with the width and the height of our screen so these are the constants that we previously created and this line of code is basically creating a screen apply game screen and we are saving it on a variable self.screen now we're gonna set up the caption so go ahead and type pygame dot display dot set caption so set caption and we're gonna call it chess this is the caption of the application and that's all for now now inside of the main loop we're gonna create the following so these first lines of code as i always say on my tutorials are always the same so don't worry if you don't understand these things this is basically how we are going to set up a pygame file so go ahead and type well true so this is our main loop and right here we're gonna loop uh we're gonna loop through all the events through all pie game events so for event in pygame.events pi game dot events.get we're gonna check if the event dot type is actually equal to pygm.quit in order to call the pygmy.quit method and also the sys.exit method so as i told you you don't have to understand this but we're basically looping through all the events so click events key press events mouse motion events and we're checking if the user is actually quitting the application so if it's clicking on the x button so so yeah pretty simple now at the end of the main loop so right here we want to type the following pygame.display.update this is going to update our screen so make sure this is the last line of code inside your main loop and also make sure this line of code is outside of the for loop so it has to be inside the while true but outside the for loop now we can run the file you can save and run the file and we should see a screen being displayed okay so we have an error events yeah my bad it's not events is event so for event imp.event.get now if you save we should see our screen so as you can see we have a screen of 800 by 800 pixels but obviously we have it empty so let's start doing some things with that screen so the first thing that we're gonna do is to render the background so we're gonna have the the chess board being displayed on screen so for that as i told you we're gonna create a game class which is responsible of all the rendering methods so go ahead and create the class the game class so class game and inside we're gonna have the init method we're going to receive self as parameter we're going to pass it for now and we're going to have a show background method we're going to receive self as parameter and we're also going to receive a surface and we're going to pass it for now i'm going to put a comment right here telling that these are going to be our show methods now this surface that we are going to receive as a parameter is going to be the self.screen that we have created right here so inside of this method we're gonna we're gonna draw the board basically so first of all we're gonna import pygame again we're gonna import the constants so from const import all and now we can do the following we can do a double for loop so we're gonna type for row in range rows so we're gonna loop all the rows of our board and inside of that we're gonna loop all the columns of our board and right here we want to well we want to draw the pattern of the the chess board so as you know a chess board has one dark square and one light square then one dark square then one light square and so on so on so in order to do that we're gonna use an if statement so go ahead and type if the row plus the column model two is actually equal to zero we are going to create a color variable and this is going to be equal to 234 comma 235 comma 200. um so this is a green color this is the light green color you'll see in a moment but if that's not true so if row plus call and by the way this is row and call model 2 is not equal to 0 then we want to set our color to be equal to 119 154 comma 88. um so here is model two okay so this is the dark green so i'm gonna put it right here light green and dark green we're then going to save these colors in our theme class so don't worry for these uh but for now i want you to show that this actually works okay so once we have this color variable we're gonna go ahead and create the rectangle that we are gonna actually draw so a pygmy rectangle by the way i'm going to call it rect so a pygmy rectangle has four params it's a tuple with four parameters the first one is going to be where we are going to start on the x so we want to start in the column times the square size the second parameter is going to be where we want to sort on the y-axis so that would be row times square size square size and the last two params are going to be the width and the height so that's basically square size comma square size so like this this is how you should have the rectangle so pretty simple now once we create the rectangle we can actually draw it on screen so go ahead and type pygame.draw.rect we are going to pass three parameters so let me show you this method so as you can see it receives a surface right here it receives a color so a surface a color and a rectangle so we're going to has the surface the color and the rectangle and that's it that's our method for now so inside of the main file we can go ahead and import from game import game which is the class and inside of the init method we're going to create a new attribute called game so we're making the reference from our main class to our game class and inside of the main loop we want to type the following right here we want to type game i'm sorry self.game dot show background so right here so each time we loop remember this is an infinite loop so it it's always like looping through this we are going to show the background so i'm gonna run the file and i'm gonna see what happens okay oh yeah we're missing the surface so as you know our method show background receives a surface so we're gonna send ourselves the screen and now it should work and as you can see we have an awesome a beautiful chess board being displayed on screen so we have our board we have our graphic board we need to create the console board but we now have the graphic one so pretty awesome now i want to do something before we continue uh so let me explain to you inside of this while loop we're gonna use a lot our attributes our main attributes so the game and the screen now i don't wanna type self.game each time we wanna we wanna call the game and sell that screen each time we wanna call the screen i just wanna type game or screen so outside the the while loop we're gonna create two variables one is gonna be game and this is gonna be equal to self.game and the other one is going to be screen and this is going to be equal to self.screen so now we can use game and screen instead of self.screen and self.game this is gonna make our code look much more organized now if i run the file you'll see it works um so yeah pretty cool we have the the graphic board okay so the next thing we're gonna do is actually add some pieces to our board so i want this empty board to have some pieces rendered on top of it so let's go ahead and do that for that we're gonna create the piece class the pawn the knight the bishop the rook the queen and the king class we're going to also create the the square class because remember if you see our structure um let me see right here the square is the one that has some pieces so we're gonna basically let me show you we're gonna uh i think we're gonna create these three classes all the basic things of these three classes in order to render the pieces correctly so okay let's start let's start creating our square class we're then going to create our board class and we're then going to link the our game class to our board class so let's start creating our square so go ahead and create a new file square.pie we're gonna create the class square we're gonna have an init method which receives self as parameter row call and piece with a default value of none so let me explain to you what's this so we have our self keyword we have the row and the column of that specific square and we have an optional parameter which is ps because remember if you see the square class right here it's gonna have zero or one pieces so not all squares are going to have a piece obviously that's why we set it a default value of none so inside of our init method we're going to type self dot row equals row self.call equals call and self.peace equals peace and that's all for now so i'm gonna leave this class right here and now we can create our board class so go ahead and create a new file word.i we're gonna create the class so class board we're going to create the init method and we're going to pass it for now and we want to import from const import import all so we're going gonna import all constants now inside of the board class we're gonna have um we're gonna start by creating two methods the first one is the create method we're gonna receive self as a parameter i'm going to pass it for now and we're also going to create an add pieces which is going to receive self as parameter and a color and we're going gonna pass it for now so if you're asking yourself why did i put these underscore before the before the name of the method is because i wanna somehow show that these methods are private methods so we're going to call these methods just inside of our board class so inside of our init method i want to first create a new attribute for the board which is going to be squares and this is going to be equal to an empty list so remember our board has a lot of squares right here so inside of our create method i want to do the following so go ahead and type solve that squares we're gonna initialize this list so let me tell you how we're gonna do this we're gonna i'm gonna first code it and then i'm gonna explain it to you so i'm gonna add eight zeros four four yeah so i'm gonna add a list of eight zeroes i'm gonna type four coal in range calls so let me explain you what this does and actually i want to print these for you to see self.squares and i'm going to call it right here so i'm going to create a board instance and i'm going to call the create method so create method i'm going to run the file and you should see our console board now let me explain these lines of code so we're basically creating a list of eight zeros for each column so try to visualize the the board i'm actually going to run it for you to see so for each column we're going to add a list of eight zeros and that's gonna be our column as you can see so for each column we're adding a list of eight zeros so i hope that's a little bit more clear so yeah once that we have created our board our console board we can loop it and we can start adding our squares our squares object because we don't want to have zeros inside of our board squares we want to have actually squares so go ahead and type four row in range rows and four call in range calls we're gonna type self.squares in the position row in the position column so we're accessing each one of our squares that we created right here we're accessing these zeros now instead of having a zero we're gonna set a square a new square so first of all we're going to import from square we're going to import square the square class and now we can do the following square is equal so remember we have to pass a row a column and a piece so we're gonna pass row and co now the pieces we're gonna do that inside of this method so for now we're just creating a board full of square objects and actually i wanna i wanna get these this line of code instead of having it here i wanna have it on our init method so here we are creating basically a two-dimensional array and here we are looping that two-dimensional array and we are adding a square object instead of a zero so hope that's clear now inside of our init method make sure to call that create function or create method that we just created now we're gonna add the the pieces so for this we actually need to create our piece dot py file um because remember let me show you a board is going to have is going to have multiple squares and each of those squares is going to have zero or one pieces we already did the part way where they don't have pieces right here but now we want to add some pieces to the squares that actually have pieces so those will be the first two rows and the last two rows so before we actually add the pieces let's create our piece class so go ahead and type class piece as always create your init method with a self with a name so this is the name of the piece the color of the piece the value of the piece uh the texture of the piece and the texture rect we'll see what these two things are in a moment and this is gonna have a default value of none and we're gonna pass it for now and down here we're gonna create our all the classes that inherit from peace so we're gonna create the pawn class the night class the bishop class the rook the queen and the king class so it's pretty simple don't worry we're going to create our pawn and in python in order to tell that this class inherits from this one we want to put brackets and inside we want to put the mother class so pawn inherits from peace so right here we want to create also our init method but if you see our pawn class is going to have a direction why because pawns different from other pieces have a direction only move in one direction so right here we're gonna receive the color uh because with the color we know in what direction the piece or the pawn is moving or should move so we're gonna do something like this if the color is white the self dot direction so the direction of that pawn is gonna be equal to minus one now remember in pi game let me show you um coordinates work a little bit different because the x-axis is it's okay it it increases to the right but the y-axis increases going downwards now the white pieces we're gonna have the white pieces on this section of the board right here so we want to tell the pawns that their direction is gonna be minus one because we wanna go up so the same thing with the black pieces the black pawns are going to have a direction of plus one because we want them to go down so hope that's clear i'm gonna go right here so if the color is wide our self direction is minus one else our self direction is plus one now this if statement we can actually code it the following way so go ahead and type self.dear equals minus one if color equals equals white else one so this line of code and this if statement are pretty much the same i'm gonna use that one because it's shorter now we're gonna we're gonna call our piece class init method from here because we wanna initialize our values so go ahead and type super dot init and let me actually explain so we're calling the super init so super will be our piece and this is the init method so remember we need to pass a name a color a value and a texture now by the way the texture is also going to have a default value of none we'll see what this is in a moment so okay for now we're gonna pass the name so the name we can hard code the name pawn we can pass the color so that will be the color that we receive as a parameter and we are going to pass a value so the pawn is going to have a value of one this is a really important attribute for our ai because our ai is going to evaluate the board based on these these values uh we're gonna has a value of one now we're going to do pretty much the same for each other class the problem by the way the pawn class is is ready so i told you it was gonna be pretty simple now we're going to create our knight class which inherits from peace and we want to do pretty much the same but we don't want to have this direction can because knights can move in any direction right um so we're gonna call the super init with knight and we're gonna pass a value of three by the way these are real values like if you search for the [Music] the chess pieces values you'll see that pawn is one night is three now we're gonna create another class i'm actually going to copy this and we're going to create the bishop class which inherits from peace and the same thing but right here we want to change this for bishop now bishop has also a value of 3 however i want to make it a little bit higher than our knight than our knight value you can leave this as three but i'm gonna use 3.001 so i'm just telling like the ai when we code the ai i'm telling the ai okay bishops are a little bit more important than knights um okay i'm gonna copy these a couple of times so let me see right here we're gonna have the rook the root class and right here we're gonna change the name to rook and the value of a rook is gonna be five um the bishop we want to change this to queen also this to queen and the value of a queen is gonna be nine points and we have the king inherits from peace name is king and the value of a king is going to be a super high value so you can use math dot infinity but i'm gonna use something like 10 000. so this is going to tell our ai like okay the king is the most important piece of the board you cannot lose it because you will lose the game uh so okay i think we're ready we have our classes and we have our main class which is the piece now inside the piece class i want to do the following i want to set the name the self.name equals to name the self.color equals to color so for the value i'm going to do the following i'm going to check if the color but i'm actually going to use these these if statements that we can write it on one line of code so we're gonna create a variable called value sign and this is gonna be equal to to one if the color if color is equal to white else minus one why because we want to have um you'll see you'll see this is this is going to make sense when we code the ai but black pieces are going to have negative values and white pieces positive values so right here we can assign our self.value and it's going to be equal to the value that we're sending so this number times the value sign that we just created so hope it makes sense um okay let's continue right here we're gonna create our texture so we're gonna have a method for that we're gonna call it set texture we're gonna create it in a moment and right here we want to put self.tex direct texture rect equal text erect um what oh texter i missed the x we're also going to put the self.texter equal to texture and then we're going to call this method so let's create it set texture i'm gonna pass it for now okay so i hope you have understand what we've made so far you'll see it will make sense once we have all finished these adding pieces method so remember we're doing this because we want to add the pieces to our board um so okay the texture is going to be the path where we can find the actual image of the piece so texture you can call it image it's gonna be pretty much the same or url image url so as you can see we don't have any folder where we can find the images so we're gonna find those images um on the repository on the github repository so i'm gonna leave a link on the description below and you should see something like this now if you see the final this is the final project uh and the final project has a folder called assets so we're gonna download these assets folder so right here make sure you see the assets folder and right where it says code go ahead and download the sip so you're basically downloading the final project we're not gonna use it we're just going to use the the assets folder so go to your downloads or wherever you download the the zip file and double click on it and then you have access to these assets folder so as you can see assets folder has images and sounds and as you can see inside of the images folder we have two other folders so go ahead and take this assets folder and just put it on your on your project uh so let's say copy folder yeah so copy folder and as you can see we have access to our assets so as i told you we have some images we have some sounds um so yeah pretty cool now in the set texture method we can actually access our images folder so right here go ahead and type self.texter it's going to be equal to and we need a model for this so go ahead and import us so right here go ahead and type us dot path dot join and right here we wanna pass the path where we are gonna find data specific texture so the path is going to be the following put an f and inside of these quotation marks go ahead and type assets slash images slash images like this hyphen open these brackets we're basically telling python that we are going to add a variable right here oh yeah by the way the set texture is going to have a default uh default param which is going to be size equal to 80. so right here we're gonna pass size then pixels like this slash open brackets self.color underscore self.name i'm going to explain what we are doing dot png so make sure your file your url looks something like this um so let me explain you now we're accessing assets so right here then we're accessing images so right here then we're accessing either images 80 pixels right or 128 pixels that we'll see uh we'll see later why we need those uh so in this case it will be these ones and then as you can see we have our images named the following way we have the color underscore the name.png so as you can see it's pretty much the same the color underscore the name.png so that's why the the url is like this the path is like this okay so we pretty much finish these this class like we're missing one method which is i'm gonna actually create it add moves we're gonna receive self and a move as a parameter but it's pretty simple we're going to append this move i'm sorry we're gonna append the move to our moves attribute that we haven't created yet so append move and that's all so pretty simple method this is literally all this file we don't need i think we don't need anything else now let's create these where is it this attribute so right here i'm going to type self.moves this is going to be equal to an empty list and we're going to have a boolean value called moved and this is going to be default to false now you can call moves also valid moves so by moves i mean valid moves of that specific piece okay so we have our piece class ready now we can start adding the pieces to our board so this this is the cool part you'll see that this board let me show you this empty board is gonna start having some pieces so pretty cool let's go to our board class and right here we want to do the following so the first thing we want to check is the color now remember i told you let me actually show you something remember i told you we are gonna use we're gonna put the black pieces on on the first two rows and the white pieces on the last two rows so we can actually save those those rows into variables so we're gonna we're gonna we're gonna do something like this so if color is equal to white we're gonna have a row pun variable comma a row other variable equal to a tuple which is six comma seven so let me explain row pawn is going to be equal to six and row other is going to be equal to 7 when the color is white why because let me show you this is the row 7 so here we have we want to have the white pieces and this is the row six so here we want to have the palm pieces so that's why we do that if statement um so right here else we want to have row pawn comma row other equal to you say it one comma zero now try to try to put this if statement as as this because we're gonna we're gonna use this this kind of is if else because they they they just need one line of code so try to convert this if else statement to to this structure and it will be something like this color i'm sorry row comma row pawn comma row order is going to be equal to six comma seven six comma seven if the color is white else there are going to be equal to one comma zero so we can delete these effects and we're gonna say with this one so pretty cool pretty simple now we're gonna do the following we're gonna start adding the puns so for call in range calls so we're gonna loop through each column we're gonna put self.squares in the row pawn so we're gonna use here the pawn uh then in the column remember this has to be like this because we have a two dimensional array and we're gonna send the row pawn so row pawn comma remember our square class has three params row call and peace then we're gonna send the column and then we're gonna send a new piece so we're gonna create a piece inside of the of this query class but first we need to import it so from ps import all we need to import all because we want to have also uh well what happened what we want to have also the pawn the knight the bishop the rook the queen and the king class so we need to import all so right here we want to create a new pawn now a pawn needs a color pretty simple we're going to pass the color that we have here and that's all we have created all puns so i'm going to put a comment these are all puns so puns awesome now let's create the knights knights this is gonna be even simpler than the than the pawns so we're gonna have self that squares in row other so knights are gonna be in the column one and six so actually let me copy these we're gonna have the squares on line one and six filled with um with knights so gonna pass the same right here we're gonna create a new knight and we're gonna pass the color um we wanna copy these paste it right here and instead of one we wanna have six so yeah let me show you column one will be this one so all this column and column six will be uh zero one two three four five six this one so right here there is where we want to have our knights so okay let's continue with our bishops and i'm actually going to copy these i'm gonna paste it right here and bishops are gonna be on column two and column five i think yeah five so change also here to 5 and we want to create a new bishop there we go now let's go ahead and create the the rooks the queens and the king so let's see right here rooks so self.squares in row other uh rooks are gonna be on the columns 0 and 7. actually going to do these 0 and 7 right here and we're going to have a rook we want to create a rook here now we want to have the queens i'm sorry the queen yeah because because we're just creating like one color remember so it's one queen and one king so king queen and this the the queen is going to be always on column three and this is going to be a new queen and the king is always on the column four and this is gonna be a new king so we pretty much did this method so we have it i hope you you understood what we did it's actually not not hard but if you're a little bit confused make sure to pause the video and try to well understand what we are doing so okay now we can call this method add pieces inside of our init method so right here after we create the board we want to add the pieces now it receives a color as a parameter so we're going to add the white pieces and then we want to add the black pieces and we have it we literally have it now we're missing the rendering part so the same as we did with the background we're gonna do with the pieces so show pieces we're gonna receive a surface and that's it i'm gonna pass it for now so we're gonna do the following we're gonna do these we're going to loop our board so in order to loop our board we want to do a double for loop and inside of the double for loop we want to check if there's a piece on that specific square so we're going to ask if self.board dot squares on row comma call dot has piece we're gonna create this method we are going to do the following okay we're having a problem oh yeah we haven't created the self.board so my bad right here on the init method go ahead and type self.board equals to a new board and it doesn't need params we're going to import it so from board import the class board and we have it we don't need params so now it should what happened oh yeah i forgot the self param right here so yeah self.board.squares row comma call has ps so let's go ahead and create that has piece method inside of our square class so dev has piece it's going to be pretty simple we want to return um self.piece different from none so if we have a piece this is going to return true if the piece is none so if the square doesn't have a piece it's going to return uh well false so let's go to our where is it our game class so if we have a piece we wanna i wanna save into a variable called piece all this thing so this dot piece so instead of instead of typing all these we just type piece okay so once we have the piece saved in a variable we're going to create an image variable and this is going to be equal to piece dot i'm sorry to pie game dot image dot load uh the texture of our piece so the piece dot texture so remember our piece so right here we have a texture that texture is basically a path so what we're doing right here it's basically using pygame.image.load method to convert that texture into a well an actual image so now that we have the image we can create a new variable which is image center because we want to center our images on the board and this is going to be equal to co times square size plus square size divided by two so this will center the image on the x-axis comma rho times square size plus the square size divided by two now we can take our piece dot text erect so remember let me show you we have also an attribute here called text erect this is the rectangle of the actual image so this is gonna be equal to the image that we created dot got get wrecked so this is a pie game pie game method because image is a pie game image and inside of the get rect we're going to put center equals image center now this way we're actually centering the image and then we want to type the following surface so in this case the screen the surface that we are having as a parameter dot bleat and bleed receives um the object that we want to bleed on screen so in this case the image and the position so that will be the piece dot text red so yeah i i know this might be a little bit confusing but let me explain we are first checking if we have a piece on a specific square right we're then saving that piece into a variable we're then taking the image the actual image not only the texture so we're converting the the piece that texture to into an image then we're creating an image center so this is responsible of centering our image then we're telling that the piece that text erect is going to be the image that we created um but centered and then we are actually telling pygame okay bleat my image the image that we created inside the text erect and remember text rect already is centered so i hope it is a little bit clear of course you have pie game documentation on google if you want to google it and you can make sure you understand this code now once we have these pretty simple we just need to call it so right here after we show the background we want to show the pieces on screen and these hopefully it will start working so i'm gonna save run the file and we have it first try oh my god we have it on the first try we have our images being displayed on screen so as you can see they are centered on each square and the position of the pieces is actually well correct uh so yeah pretty pretty cool i'm actually really happy it worked um on the first try so i know we did a lot of things so make sure to pause the video and understand what is the square class doing what is the board class doing what is the piece class doing all the piece classes and finally what are we doing inside of our show pieces so so yeah pretty cool now we can continue with our game so we're gonna continue with our draggery class so the draggery class it's basically responsible of letting us drag any of these pieces all around the board right so as you can see we have just the images but what we want to do is actually drag them so let's start coding that we're gonna start by adding some pie game events so right here inside the for loop we're gonna start adding them so i want you to think the following and i'm actually going to run the file again i want you to think the order of the events in order to drag a piece so the first thing that happens or the first event that happens is the click right so if you want to drag a piece you need to click on that specific piece right the second event is going to be the mouse motion so when we are dragging the piece we are targeting the event mouse motion and the last event is actually undragging the piece so releasing our click so we're gonna code those three events and you'll see what i mean so go ahead and type if event dot type equals equals pi game dot mouse motion down we want to do something i'm going to pass it for now this is our click event so click then we're going to create the mouse motion so a leaf event dot type equals equals pi game dot mouse motion we want to pass it for now and this is going to be our mouse motion and the last one is going to be the leaf event dot type equals equals pie game dot mouse button up and we're gonna pass it for now so this is the click release so we have our three events and for the last event i'm gonna put in a leaf instead of an if okay so we have our our events i'm actually going to put a comment right here so this is quit the application and we have our three events in order to drag a piece around okay so we're gonna start with the click one but before we start coding what happens when we will click the the screen we want to create our dragger class so go ahead and type dragger or go ahead and create a new file dragger.p and go ahead and create the all the class the dragger class so class dragger we're going to create the init method and we're going to leave it empty for now and we're also going to import we're going to import pygm and we're going to import the constant so from const import all okay we're gonna leave it empty for now but okay what do we need to do so the first thing is actually well making a reference from our game so self.dragger equals dragger we need to import it so from dragger import dragger so we did the reference so as you can see game has a reference with dragger and we are now going to make the reference here so we're going to create a variable called dragger which is going to be equal to self.game.dragger awesome okay so the first thing that we want to do um is actually the click event right so when we click we wanna save the position where we click it so we're gonna be coding it and i'm gonna explain in parallel what we are coding because it it's hard for me to explain like all the things we're gonna do previously so let's start by by creating two attributes which we're gonna call mousex and this is going to be equal to none i'm sorry to zero because there are gonna be integers or float numbers um and we want to create the mouse y as i told you i'm gonna i'm gonna be explaining this uh well in parallel with the code that we're gonna do and down here we're gonna create a new method called update mouse we're going to receive self and a position as a parameter and this is going to be equal to self dot mousex mousex comma self dot mouse y equal position so position is going to be something like let me see it's going to be a tuple like x coordinate comma y coordinate so position is going to be something like this and we're going to assign the mouse x to the x coordinate and the mouse y to the y coordinate pretty simple okay so right here in the in the event in the pi gaming event inside we wanna we'll do the following so we wanna take our driver variable and we wanna put update mouse and we wanna send the event dot position so i'm gonna actually print the event dot pause for you to see what's this but it's basically the position where we are clicking so we have our board and each time i click you can see we have some positions so we're sending this information to the update mouse and we're storing that information inside of our mouse x and mouse white variables now the next thing we want to do is actually check if that position has a piece so it's not the same i want you to think it's not the same opposition um to a row so let me explain we're going to create a clicked row and that is going to be equal to the dragger dot mousex mousex divided with double slash the square size and we are going to have um oh i'm sorry this is this is mouse y because rows are affected by the the y coordinates we're gonna create also the clicked clicked call and this is gonna be equal to dragger dot mouse x double slash square size there we go and now we wanna check if our board uh oh so we don't have a board that's other thing we want to create right here so board is equal to self.self.game.board so if our board dot squares in the position click row in the position click call dot has piece has piece so remember this is a method that we already created and this is from the square class has piece so we're we're just asking uh if square has a piece if click i'm sorry if clicked square has a piece okay so if it has a piece we want to do the following but before we do that i want you i want you to see the difference between clicked row and mouse y and clicked called and mouse x so right here i'm gonna print um let's see i'm gonna print the dragger the dragger dot mouse y mouse y and i'm gonna also print the clicked row and down here i'm gonna print the dragger dot mouse x and clicked uh this will be call yeah so like this and i'm gonna run the file for you to see the difference so remember these are our printing statements i'm gonna click let's see i'm going to click this one the first square so i click it and as you can see we get 51 0 and 50 zero so what's this 51 is the dragger mouse y so it's the y coordinate 50 is the dragger mousex so we clicked in 51 comma i'm sorry in 50 comma 51 and these two zeroes are actually click row and click column um respectively so it's actually working because the rook is actually on the on the road number zero and the column number zero now if i i don't know if i click this one as you can see we click on three comma three so it's actually correct because this this square right here is located on the row number three and the column number three so it's actually working i wanted to show you that because it's not the same according to our row so we we wanna do this conversion first so i'm going to delete these print statements and right here inside the if statement we want to create a variable called piece and this is going to be equal to board.squares in the click call in the click row i'm sorry then in the click call dot piece so instead of typing all these we are going to type just piece now we're going to create other methods inside of our draggery class so we're going to have a method called safeinitial initial we're going to receive self as parameter and we're going to receive as a position also we're going to pass it for now and before we code that that method we want to create two attributes called initial row and this is going to be equal to none or actually zero also and self.initial call which is gonna be also equal to zero and right here we wanna just well basically do the same the same conversion that we did here so we wanna save our initial row and this is going to be equal to the position the the second element of that position tuple so remember a position is x-coordinate y-coordinate so we want to take the y-coordinate we want to divide it by the square size so the same thing we did on our main class and we want to have our self.initial call and this is going to be equal to the position the first element of the position array or the tuple and we wanna divide it by square size and that's all so same thing that we did here and right here we wanna take our dragger and we wanna call that a method so it will be safe initial and we're gonna send the event dot pass now it's important to do this inside the if statement because we wanna we we just wanna do this if we're actually clicking a piece right because if we click an empty square we're not going to drag like an empty piece you know so okay we save the initial and we also want to we also want to save the piece that we are dragging so inside of our draggery class we're going to create a new a new attribute which is going to be the the piece a piece attribute this is going to be equal to none as a default um and this is basically the piece that we are dragging so we're gonna create a a new method which is gonna be drag piece and we're gonna receive self and a piece as a parameter and we want to self dot piece and we want to basically set the piece equal to our new attribute so we're basically saving as we as we did right here that we save the the coordinates we want to save the piece that we are dragging and we are going to have an attribute called dragging this is going to be false as default false and right here we want to set the dragging to true you'll see why we need this but it's basically a boolean so pretty simple and we also want to have an undrag piece so it's basically the same we don't need any parameters because we just want to do self.ps equals none equals none i'm sorry and self self. dragging equals false so these are the methods of our dragon class we're missing one that we are going to code um in a moment so okay let's see we are saving our initial position but we also need to save our piece so dragger dot drag piece and we're gonna send the piece awesome that's all what we want to do inside the click event for now okay let's continue with the mouse motion so inside the mouse motion event we want to ask ourselves if we are actually dragging a piece so if dragger.dragging we want to do something and that's why it's important to have this boolean attribute because i mean mouse motion is going to be targeted each time we move the mouse but we just want to drag a piece if we're actually well dragging a piece in other words if we have saved previously a piece to drag okay so i hope that's clear so we're going to create a new method which is actually is the one that's gonna be responsible of actually dragging the piece so we're gonna call it update blit we're gonna receive self as a parameter and we're gonna receive also a surface and i'm gonna pass it for now and inside of our mouse motion event inside of the dragger dot dragging we wanna dragger.updatebleat and we're going to send our screen and that's all so let's go ahead and actually we'll code this method so remember on our piece class we have a set texture method which receives a size so and also remember that we have inside of our images folder we have 80 pixels images but also 128 pixels images why because when we are dragging a piece i want that piece to be a little bit more bigger than the ones that are not being dragged so the first thing we want to do is actually take our piece so the dragon piece remember and we want to set the texture we want to set the texture but we want to send the size equals to 128. so that's the first thing we want to do so texture i'm going to call it like that texture then we want to create the image so remember this is just the the path this is the the path to the image but if we want to create the image we want to do the following so image equals pi game pi game dot image dot load so we need a pad and the pad will be all the texture so i'm actually doing a variable here and it's going to be self.piece.texture so in other words this is the path but i call it texture and we're going to send the texture so we have the texture and we have the image the next thing that we want to do it's actually the rectangle so this is going to be the image center and if you if you see this is actually the same thing that we did when we bleeded the the pieces inside the show pieces so we take the piece we take the image which is the pizza texture which we take an image center then we take the texture rect of that piece and then we bleed it so we're basically doing the same but for our dragging piece so the image center is going to be the self.mousex and the self.mouse y because we wanna we want to put our image where our mouse is actually well located then we want to set the all the piece so the piece dot text erect text erect and this is going to be equal to the image dot get wrecked get rekt and right here we want to put center equals image center so i know it might be a little bit confusing but it's basically i'm actually going to copy it i'm going to show you i'm going to paste it right here we're doing the same so let me see we have a piece i'm sorry yeah we have a piece we have a piece we have a texture we have a texture right here we have an image we have an image we have an image center we have an image center we have the piece text erect we have the piece text erect and the last thing that we're missing is actually bleeding the image so it's pretty much the same um so right here we're gonna put bleed and it's basically going to be the surface.lead and bleed receives first the object that we want to bleed so it will be image and then the position so it will be the self dot texture rect i'm sorry self dot piece dot text erect so in other words this method is updating the position like the the visual position of our piece and that is going to make the illusion that we are actually dragging that piece we're we're also um making that piece a little bit bigger and you'll see you'll see why it looks pretty cool okay so we're actually done with this class i told you it was gonna be not that hard so i'm gonna put here uh draw methods or bleed method and right here i'm gonna put other methods because i wanna i wanna divide the bleeding section from the other method section so right here we did the same i'm gonna put methods um okay now let's go to our main class we can finish our driver from here so the last thing we did was actually updating the bleed so we we also want to do the following we want to actually before updating the bleed right we want to also update the mouse the mouse position so dragger dot update mouse update mouse um okay it needs a position so we're gonna do basically the same thing we did here um [Music] oh no we can pass just event.pass that's it why because we want to first update the mouse obviously and then we want to update the bleed because this depends on the on the mouse position if you if you if you see here the update bleed depends on the mouse positions so that's why we want to update our mouse first okay so i think that's all for the for the mouse motion event uh but we'll see okay so we're gonna code the click release event and i think we just need to [Music] call our dragger dot and drag and drag peace i think this should work we'll see but okay let's let's go again through each line that we we recently coded so um in the click event we update the mouse right we then check if a square the square that we clicked had a piece and if it had a piece we save the initial row and initial column of of that piece that we we are dragging and this is because we need this the safe initial method because in the case that we did a bad move like an invalid move we want to we want to return the piece to the initial position that's why um and we also saved the the piece that we're dragging into the variable uh well dragger.piece uh inside the mouse motion we checked if we are actually dragging a piece and if that's true we call the update mouse so we are updating our mouse position and then we are updating the bleed position and finally in the click release we just undrag the piece so pretty simple i'm gonna see if it actually works so we have a report i'm gonna try to i don't know i'm gonna try to drag this pawn okay so we're dragging it but we're having some bugs yeah we're definitely having some bucks so let's see what what is happening i'm gonna close it okay so i'm actually going to run the file again to see what box are we having um okay the first one is that our when we're dragging the piece you can see it's not actually like fluid it actually disappears so we want to fix that and we also want to fix this um okay so let's start with this one let's make our piece not disappear i i i think i know why it's because we are well showing the background then we're showing the pieces um but we're not like showing the drag piece so the background is actually like bleeding on top of our track piece so i think that's the problems so go ahead and type dragger dot dragging so if we're dragging a piece we want to show it right so dragger dot update blit and i think that should work i'm not sure let's see um okay we're missing the surface which is a screen so it should work now so let's drag a piece yeah as you can see it's working now we're having a problem i don't know if you can see it but behind our piece it makes the illusion that there is another piece so we need to fix that and we also need to fix this so the the pieces that we start dragging first are not disappearing from their original position so when i'm dragging a piece we don't want that piece to well to be here um and also the the texture is is getting big it's getting the texture of the 128 pixels so we need to fix those things also so let's start with the with the texture that would be actually pretty simple we need to go to our game class right here and as you can see inside of our show pieces method as you can see we are bleeding all pieces but we don't want it to be like this we want to bleed all pieces except the one that we are dragging so let me show you i'm gonna put a comment here so all pieces except dragger piece so we have our object remember our self.dragger and right here we want to check if peace remember peace is the well is the actual piece that we want to render so if peace is not self.dragger dot piece we wanna bleed that piece so we're basically bleeding all pieces except for the one that we are dragging right so let's see if it actually works so yeah as you can see we don't have okay we need to fix this but let me show you again we're fixing the issue that we had that the the piece stayed here and that's that's well we did it with that if statement that we recently coded um okay now we're having this problem let me show you again when we undrag the piece the piece stays with the 128 pixels dimensions so we don't want that so inside this method we wanna first of all take the piece and set the texture to the size of 80 pixels so remember set texture is this method right here um yeah we don't want we we don't have to send this param because remember the param is by default 80 pixels but i want to be explicit so i want to send it um so yeah we want to set the texture and then we want to bleed it so let's see it should work so now when we undrag the piece it should set the texture to the one of 80 pixels so i'm gonna drag it and as you can see we did it we fixed that bug now i wanna i wanna also fix this one this bug that we're having the illusion that we have another piece behind and that is because i think it is because we are not showing the background um when we are moving the piece so before we update the blade we want to actually [Music] game we want to call game dot show background with the screen as a param okay i think this should work so let's see okay we're having another bug now but i think we fixed that one right yeah we fix it now i don't know what's happening okay so we also need to draw when we also need to show the pieces so show pieces show pieces in screen i want to send the screen and now we should have the dragging movement perfectly okay it's working fine so we can drag a white piece we can drag it and it actually works so try with any piece it should work with any piece also with the black ones um yep so yeah it's working correctly we did it and this is actually a a big part of the code because because dragging a piece is actually pretty important for our game if we can not drag a piece we cannot move it you know so so we actually did a pretty big chunk of our game our game code so so yeah pretty good we did the dragon part and we're ready to continue okay so the next thing we're gonna do is actually check though all the bullet moves that any specific piece has so let's see i'm gonna run the file again so we can drag our piece but each time i drag a piece i want the game to show me the possible moves so for example if i drag this knight i want the game to show me that the possible moves will be in this case these two squares right if i drag i don't know maybe this pawn i want the game to show me that the possible moves will be this and this one so we're gonna do that right now this is actually a complex method we have to do a lot of things but there's one there's one method the one that that checks if it is a bullet move that will be quite a complex but i'm gonna try my best to explain it so okay we're gonna start by creating let me show you the the structure of our game the class diagram uh inside of our board class we have one method called calc moves i told you this method was going to be huge and this is the method that we're going to code next now calc moves is going to be responsible and this is inside the board class so right here calc moves is going to be responsible of calculating the possible moves or the valid moves of an specific piece so let's go ahead and create it so go ahead and type def calc moves and we're going to receive self as a parameter we're going to receive peace we're going to receive a row and we're going to receive a column so let's see i'm actually going to put a comment here because this is a big this is a big method so i want to i want to actually describe what it does so this is going to calculate all the possible or valid moves of an specific piece on a specific position so this is what this method is going to do okay so let's start coding it now inside of this method we're going to have other methods so the first thing we want to do is actually check what piece are we talking about so we're going to do some if statements so if piece dot we can do these two ways so we can do we can use the is instance piece from for example pawn pawn and this will make we will calculate all moves for any specific pawn piece or we can do it this way which is if piece dot name equals pawn so it is the same but i'm actually using the one of his is instance so if his instance piece of pawn of pawn so in other words if the piece is an instance of the pawn class uh we want to i'm going to pass it for now now if the if the piece is instance of the night class we want to pass it also i'm actually going to copy these for the bishop for the rook queen and king so uh bishop we're gonna put bishop right here then we're going to put rook then we want to put a queen and then we want to put a king awesome okay so we're going to start with the puns you know i'm actually going to start with the knights because to calculate the pawn moves it's it's actually harder than though than the knight than the knight moves i want to give you an idea what we're going to do so we're going to start with something easy the easiest one is the night class so we're gonna start with it and then we're gonna do the pawn class and the ma the bishop one the rook the queen and the king so okay let's start with the knight so inside of our method we're gonna create some other methods as i told you so there is going to be one method called night night moves and we're going to pass it we're not going to receive any parameters and inside of our if statement of the of the night class if statement we want to call night moves which is a method inside the method you'll see why i mean this is not necessarily you you can create these methods outside of the calc moves but i mean i want to have all the calculating moves inside of one method so that's why i'm putting this method inside so we've checked if the piece is actually a knight we are going to do this method so i want to start by creating a variable called possible moves this is going to be a list i'm actually going to hit enter and we're going to have this variable it's actually an array so i want you to think i'm actually going to run the file i want you to think the possible moves that a knight has so let's see this knight right here it has two possible moves this one this square and this one right but let's imagine we have a knight right here and let's suppose there are no other pieces all the possible moves of a knight are these ones this square this is a square this square this square right here this square right here this square right here this square right here and this square right here so we have eight possible moves for a knight that is at the center of the board assuming that there are no more pieces so that's the first thing we want to we want to have cleared so let's go to our method so let's think about it so we know there are eight possible moves so i'm actually going to write a comment so eight possible moves right and we're going to represent the move with a tuple so we can do this i'm actually going to copy and paste eight times so let me see yeah eight times so we have eight possible moves okay so we're gonna have a tuple the first one is going to be or or the first possible move is gonna be the row minus two positions so this will be going up two positions and going right one position so i'm actually going to put them like this so this is the first move i'm gonna show just this one and then i'm gonna do the rest but this move let's suppose we have a knight here right here in this white square we wanna the the first the first move is going to be two like two rows going up from our original row and one column going right from our original column so let's go to our method this is the first possible move so we're going to do that for the eight possible moves the second one will be row minus one comma code plus two then we're gonna have a let me think about it row plus one comma call plus two if it's easier for you like to draw the possible moves on a paper i actually recommend it and it will make sense uh the next one will be row plus two we want to go down and we want to go to the left um to the right i'm sorry then we want to have a row plus two comma column minus one then we want to have a row plus one comma column minus two then we want to have a row minus one column minus two and the last possible move is going to be a row minus two and column minus one so i think we have all possible moves there make sure you have all possible combinations and as i told you if it's easier for you to draw them to draw them on a paper i actually recommend it because it will make much more sense so okay now we have the possible moves then we want to check other things we want a loop so for possible move we want to loop this array so for possible moves in i'm sorry for possible move in possible moves right we want to loop it we want to then divide the row and the column so we want to do something like possible move row i know these names are quite long but they will make more sense comma possible move call and this is going to be equal to possible move now we want to check the following the first thing we want to check is actually if the the move the possible move is actually on the range of the board right so for that we are going to create a static method and i'm going to explain what an aesthetic method is inside of our square class so right here go ahead and type at static method and down we want to type def in range we want to call it like this now in range is gonna receive a lot of arguments as a parameter so i'm gonna pass it for now i'm gonna explain it so i'm gonna explain what an aesthetic method is in a moment now this asterisk followed by by alex is basically telling python or telling this method okay you can receive as many parameters as you want so we need to loop these arguments this is like a list of arguments you can think it like that and i'm actually going to first code the all the method and then i'm going to explain the the static what an aesthetic method is so we want to loop for argument in args we want to we want to loop our our parameters and we want to check if the argument is less than zero so this is outside the board or the argument is greater than seven so this is outside the board also if this is true we want to return false if not we want to return true but outside the for loop so if we loop all and we didn't return false we can return true okay let's actually explain and let me explain to you what an aesthetic method is if you know already what an aesthetic method is go ahead and skip this part but let's say i create an instance of the square class i'm going to call it s now with s i can call has piece right the method has piece as we know now a static method is going to let me call the method inside of that class without an object i just need the class itself so i can do something like square dot in range and i can call this method without the need of creating an object so i can put something let me actually print i'm going to print a square dot in range and i'm going to pass for example eight two five and three this is going to return false because of the eight so i'm gonna run the file oh okay so i'm missing two params but it was because of this so i can delete it i'm gonna run the file again and as you can see we get a false now if i change this to five this will return true as you can see now i hope it's clear what an aesthetic method is is basically we can call the method with the class not with an instance right so this if i do this i want to call the in range with an object this is not going to be possible okay so hope that's clear now the method itself is telling us if all arguments are actually inside the board so if there are less than zero either if we have a row less than zero or a column less than zero we're outside the board and if we have a row or column greater than seven that also means that we are outside the board so that's what this method does so we can return to the board and the first thing we want to do is check if remember we're going to use our class square dot in range and we want to pass the possible move row and the possible move call to see if both of them are actually in range now if they are in range the next thing we want to check is if there is a piece right if there is a piece or the square is empty right so if it's in range we know we're good to go but we now need to check in order to be a possible move we need to check if the square is empty or if it has a rival piece to capture so we're gonna create also a method right here in the on inside the square class and we're gonna create a method that i'm gonna name is empty or rival i'm gonna receive self as a parameter i'm gonna receive a color you'll see why and i'm gonna pass it for now okay inside of this we're gonna call another method that we're gonna create right here which is going to be has rival peace we're going to receive self and we're going to receive a color we're going to pass it for now we are also going to create a new method which is is empty we're going to receive self this is going to return uh we're going to return the self dot has piece okay so i'm sorry this is not so we want to return if the if the square that we are calling this method on has actually not a piece so that would be that is empty right um okay so it's empty is ready now has rival piece we're going to return the self dot has piece it has to have a piece but also the piece so the self.piece dot color has to be different from our color which is the parameter right so i hope it's clear we're basically checking if it has a piece and also if the color of that specific piece it's actually different to our color so pretty good now i'm going to create also another method we're not going to use it i think for now but it's going to be has tin piece so it's pretty much the same so we're going to put self and color we're going to receive self and color and we want to do pretty much the same as we did here but instead of not being equal to the color we want it to be equal to the color okay so those are the methods that we want to have inside the square class for now okay the this method is empty or rival we want to return self dot is empty or let me actually raise this or self dodd has rival peace has rival peace awesome now we need to send the color to the rival piece method so we're gonna send them this color okay awesome now we can go back to our board method the night night moves method so we want to check if self dot squares on the possible move row and then on the possible move call dot is empty is empty or rival and we want to send the piece.color piece.color so if this is true we can continue so let me explain piece is the piece that we are receiving as a parameter and this is the piece that we want to calculate the all the moves right so if it's empty or rival is basically checking if that square of the possible move is actually empty because that would be a valid move or it has a rival piece which is different from our piece.color um and if it has a rival piece it will also be a valid move right so right here we can actually i'm going to put a comment we're going to create a new move so create a new move and i'm gonna pass it for now why because we need to create the move class we haven't created the move class so let me show you our our structure uh right here a move is made by an initial square and a final square that's all square class move class i'm sorry it's actually pretty easy so let's go ahead and quickly create it so go ahead and create a move.pi file and we want to create the class move we want to create the init method i'm going to pass it for now okay so as i told you we want to receive an an initial square and a final square right right here we want to put self dot initial equals initial and so the final equals to final so let me actually put a comment here initial and final are squares squares so there are not tuples there are squares that's something important to have clear and that's actually pretty much all we have to do so we're going to go back to our board class right here we want to create the new move so we want to create the squares first so the initial square is going to be a square and remember a square has three params row column and piece so the row will be our row not the possible moves row because this is the ending row right this is the initial this is the one that we're sending so row and call we don't have to pass a piece um and final final square is going to be also a square but as i told you is going to be with the possible move row comma the possible move column and for now we need to do this but for now we're not sending any piece but i'm gonna actually put up a comment piece equals piece you don't have to understand this this is for me to to remember that we have to do that but for now we're gonna leave it like this it should work and now we can create the move so i'm gonna put a comment uh move so actually this i'm gonna call this create a new move and this is create squares of the move of the new move so okay move is gonna be move it's gonna be a new move which we have to import so from move import move down and move receives an initial and a final so we're gonna send the initial square on the final square awesome and then we can call our piece.addmove method with the move so i'm actually putting a comment here so append new valid move so as you know we have inside of our piece class right here we have a add moves oh by the way this should be add move my bad so add move move and we are appending to the moves array the new move that's all so let's go back here so at move move awesome and that's pretty much all we need to do for the night for the night moves so yeah i hope it's clear we actually did a lot of things we created an static method we created all of these methods the smt has has rival piece we created also a class a new class so we actually did a lot so make sure this is clear and yeah you can pause the video if you like okay so now we can continue with the pawn the knight the bishop the rook the queen and the king uh i'm sorry the knight we already did it but we can continue with the other ones but first i want to actually render the possible moves for you to see how is this method working okay so remember we want to render or the render methods are going to be inside of our game class so blip methods so right here i'm going to create a new method called show moves and show moves receives self as a parameter and a surface and that's it we're going to pass it for now so we want to show the moves of the piece that it's being dragged right so we want to check if self dot dragger dot dragging so if we are dragging a piece we want to show the moves of that piece right so i'm gonna actually save into a variable called piece and i'm gonna take the self.dragger piece so this is the this is the piece that we wanna we wanna show the moves right now we wanna do the following so for move in peace dot i think we call it i don't know if we call it valid moves or moves i call it moves so piece that moves we're gonna loop all the possible moves so loop all valid moves and we want to draw them we want to bleed them so the first thing we want to create a color in order to bleed it then we want to create a rect and then we want to bleed it so this is actually always the same as you can see here let me show you we create a color right here then we created a rectangle and then we bleed it so same thing we're gonna do the same so let's start with the color so color is gonna be equal i'm gonna use the if statement uh in one line but you can use the if if statement the normal if statement so color is gonna be equal to c 86 46 this is a color you'll see 4 so 86 46 4 if the move dot final remember we have a final attribute final row plus move dot final call model two we're basically doing the same that we did here we're getting the model of the row plus the column because we want we want because we want to draw like squares and dark squares depending on their position of the row one column you'll see what i'm talking about so model two equals zero else we wanna have the following color so hashtag c 8464 and six this is a red color this one i think this one's the light one this one's the dark one yeah this is the light one this is the dark one again i already told you this but colors we're gonna save colors inside of our theme class um but for now i just want you to see what we are coding so we can hard code these values okay let's now create the rectangle this is gonna be pretty much the same as we did here so remember columns times square size row times square size and square size and square size so we're going to do pretty much the same move.final dot co times square size this is where we are starting on the x axis then move dot final dot row times square size this is where we're starting on the y axis then the width and the height so that would be square size comma square size pretty easy now let's go to the bleed part which is actually pretty simple also it will be just pygame dot draw dot rect and it gets it has some params but we're going to send the surface a color and a rectangle that we already created so a color i'm sorry the first one is surface yeah so surface will be screen surface i'm sorry we call it surface then the color and then direct and we have it we pretty much have the method which renders the possible moves of a specific piece now we need to call it inside of our main loop so let's go to the main class so we want to do the following the first thing is when we click it so when we click the the specific piece right so right here we wanna called our board dot calc moves the method that we created inside the board class this one we want to send a piece a row and a column so we're going to send the piece that we already created right we want to send the clicked row and the click column and that's all so this method is going to calculate the moves of any specific piece so we want to do the following i'm going to put a comment here i'm going to put show methods and we want to first well we want to show the background right then we want to show the pieces game that show pieces and then we want to show the no i'm sorry the the show pieces has to be the last method because we don't want the moves to [Music] to be on top of the pieces we want the pieces to be on top of the of the moves of the possible moves so we're going to put here game dot show moves on screen we need to do this also on top right here at the beginning of our main loop so game dot show moves and we want to pass the screen as a parameter okay what else let's see so we already did the when we click it now we want to do when we move the mouse so in here we're already showing the background so remember in between the show background and show pieces we want to show the moves on screen um what else when we release it we're gonna leave it like this for now and i think this should work let me put a comment here so i'm gonna put show methods also um show methods and right here also i'm gonna put something like show methods so i think this should work it will only work with our nights uh so i'm gonna click i don't know this night oh my god it worked so as you can see yeah we have it we are displaying we're showing the player what are the possible moves for the knights now obviously if you if you take a pawn it will not do anything if you take a queen it will only work with the knights so pretty cool let's now continue with the other pieces now before we do that i'm actually going to create a night so let me run the file again i'm actually going to create a knight on the center to well to make sure it's working with all the moves so let's go i'm going to do this pretty quickly i'm going to go to the board and i'm going to create a new knight let's see let's see i'm gonna create a new knight and it's gonna be i think five five comma five should be one of the center squares um [Music] so i'm gonna run the file to see okay it's actually not i think it's zero one two three four it's actually four my bad i'm gonna do this pretty quickly so for this is just to check if the [Music] all the code that we did is actually working so yeah it's working and i want you to see this it's actually working on these two pieces because there are rival pieces now if i put i'm actually going to do this pretty quickly i'm sorry uh if i do this on the three comma three square i'm gonna run the file as you can see it actually don't works with these pieces because those pieces are of the same color as mine so it's working perfectly so yeah pretty cool i'm going to delete this line of code that's it okay awesome we can continue with the other pieces so we have the night moves awesome this is working we can close this method now we're gonna go and create the pawn moves so right here go ahead and type def pawn moves i'm gonna pass it for now and i'm actually going to okay i'm going to call it here first upon moves but i want to do these in one line because i don't want to take so much space i'm gonna pass this one for now and i'm gonna pass the rook for now the queen for now and the king for now yeah this looks much better and yeah yeah awesome okay let's go ahead and code the pawn moves method so this is actually a lot harder than the knight moves um this is actually the the hardest one of all the pieces uh but okay let's do it let's start coding this so the first thing we want to check here is if a pawn has actually moved so let me actually show you something um right here i'm sorry right here on the board class inside the board class we have a move a move method this move method is going to be responsible of setting the the moved attribute of a piece the boolean attribute to true so each time we call the move method with a piece inside we're going to set this moved attribute to true so the first thing we want to do is to check if the piece has moved so if p is dot moved we want to do something else we want to do something now i'm actually going to um okay i'm going to code it first like this and then i'm going to pass it to a one line if statement we're going to have a variable called steps so right here i'm going to create steps and this is going to be equal so if the piece has moved the number of steps is going to be equal to 1. now if the piece has not moved the number of steps is going to be equal to two why because pawns on the first move when they haven't moved uh they can they can go to well they can advance two squares right so i'm gonna actually do the following steps equals one if if ps.moved else to awesome i can delete this we have it so i'm actually going to put a comment here this is going to be steps and down here we want to check we want to start checking the the same things that we checked on the night moves so we're going to start by coding the vertical moves because as you know we also have diagonal diagonal moves when we want to eat a piece right this is only when we want to eat an enemy piece okay so we're gonna start with the vertical moves so uh i'm gonna code three lines of code that are gonna be quite confusing and then i'm gonna explain them so for now let's save into a variable called start we're gonna save row plus the piece dot direction so remember in this case we are talking about pawns and pawns if we see inside of the class they have an attribute called direction right um so the start is going to be equal to row plus pz.right direction or there and the end something that we are going to call end is going to be equal to the row plus the piece dot deer this is actually inside brackets so piece dot dear times one plus steps i know this is confusing i'm going to explain it in a moment now we're going to do a for loop so it will be for move row a variable that we're going to call move row in range we're going to pass start then end and then we want to put ps.direction and we want to pass it for now okay let me explain i'm going to run the file and i'm going to have the code to my left so right here okay let's say we want to move this pawn which hasn't moved so okay let's start with the [Music] with the the start variable start variable is going to be equal to rho plus ps dot direction so okay the row is this one this will be the sixth row plus the piezo direction we know that for white pawns direction is minus one so we're gonna subtract one and this will give us a result of five we're in the fifth row right here now end is going to be equal to the row so this one right remember we're here this is the start and the end is going to be equal to rho plus let's do this operation one plus steps first this piece hasn't moved so it has the steps equal to two so one plus the steps is gonna give us three now piece that theory is equal to minus one so minus one times three is going to give us minus three right now if we add this row with minus three it will give us right here right here is the end so we have a start here in this dark square and we have an end here in this dark square now when we are looping we are actually looping from here from the start to the end but it's exclusive so we're gonna go up to here right and the step is gonna be piece dot direction why because in the case of the white pieces we're gonna start in the row fifth we're gonna end under row three exclusive so we're actually ending here so we want to have a step of minus one and that will give us this result so i hope this is clear so we're basically going through each row which that pawn can move right okay um let's suppose this pawn has already moved the starting position will be here and the ending position will be here but as it's exclusive it's gonna be here so i hope that's clear this is the logic behind the the pawn moves okay so we're gonna loop through each move row let's start doing that so the first thing is to check if square dot in range of our actual move row so move row and this is in range like this so if it's in range we want to check if that square is empty now we don't want to check if the square is empty or rival because we can only move vertical with the pawns if the square is empty right so we're going to put if self dot squares in the position move row then in the position call we're not changing of column dot is empty i don't know if we already coded that method let me see in square is empty yeah we have it so if it's empty we can create initial and final uh move squares and then we want to create a new move okay so initial position is going to be equal to a new square we're gonna pass the move row and we're gonna pass the column so move row comma call and then this is actually initial and then we're going to create final and this is a square oh i'm sorry this is move row this one is just row remember row and call are the initial ones this is the final one the move from comma call call is not gonna change okay now we can create a move so move equals new move with the initial and final awesome and lastly we can piece dot add move move awesome so same thing we did with the knight actually right here the difficult part about these this method is actually understanding this logic so i strongly recommend you to pause the video actually take a piece of paper that helps me a lot actually right try to write these things with numbers and you'll see it makes sense okay now what happens if the square is not empty we want to break the the loop we want to break it why because if the first square is not empty we cannot like make possible the other square even though it's empty because we are already blocked okay so this means so actually i'm going to put a comment this means we're blocked and same logic with the first if statement if we are not in range for our first square we're not going to be in range for our second square so this is not in range and we want to break we want to break the loop awesome so i think we have it like i'm actually going to put these brakes here awesome we have the vertical moves so as i told you try to understand these first three lines of code those are the hard things of this method okay um let's see if that if it actually works so i'm gonna go to main and it actually works as you can see awesome so i'm gonna try it with all pawns first the white so they work now the black ones and as you can see they also work awesome um now i'm gonna let's see i'm gonna create i'm going to run the file again i'm going to create a pawn a black pawn right here right here actually right here so in the row number zero one two three four four comma zero so in four comma zero i'm gonna create a pawn black pawn so let's go to board this is for testing uh right here i'm gonna copy these 4 comma 0 to test it so self.squares 4 column 0 4 and 0. um okay when i run the file we should have a pawn right here oh i'm gonna run the main file so yeah we have a pawn right here so now if i click this pawn we should have just this square as a valid move awesome it's working okay now i'm gonna increment the the row so let me increment the row so this four i'm gonna make it five and now we should we should not have any possible move so as you can see we we don't have possible moves this is actually working awesome so i'm gonna delete this this is for testing remember and now we can go ahead and code the diagonal moves so right here so we're going to create a variable called move row and this is going to be equal to row plus the piece that deer so remember this is the same as the start here this is giving us the start of the moving row the possible move row um i'm actually going to call this possible to be explicit oh we call it i'm actually going to change the name to possible possible move row i want to be explicit so right here i want to put possible move row and we're going to have a possible move with calls we want to have an array so we're going to have two possible um move calls the first one is gonna be call minus one so to the left and the other one is gonna be called class one so to the right okay that's why it's diagonal now we want to loop these possible move calls so for possible move call impossible move calls we want to as always we want to check if the square dot in range and this is without space so like this we want to check the possible possible move row and the possible move call move call so if those possible move coordinates are in range we want to now check if the dot squares in the position possible move row and then possible move call dot has rival piece has rival piece um [Music] i don't know if we created this method inside the square class as has rival piece yeah we did it so we want to send our piece.color i'm actually going to change these for for enemy it's actually more clear so let me let's quickly do that so instead of rival instead of rival we want to put enemy enemy and we want to change also inside the world class instead of rival so rival i'm gonna command d with command d you can search all the rival things the rival words uh so i'm gonna put enemy so we're changing also the the knight moves okay i just changed the name you you didn't have to do that um but okay so if it has an enemy piece um we're gonna actually move right because we cannot move if the diagonal is empty or if it has a piece of the same color as ours so if it has a rival and an enemy piece i'm sorry we can actually create a new move so we're going to copy these comments and copy these two comments and we can create initial and final move squares so this will be initial this is gonna be square from row comma call so this is the initial position and the final one is gonna be a new square and we have the possible move row possible move row comma possible move call and that's it then we want to create the move so this is a mu funny move with initial on final and then we want to append it so this is another comment actually append new move so right here append new move and this is going to be ps dot at move move awesome so yeah we're pretty much doing the same lines of code uh we actually can create a method for this but maybe at the end of the of the video we can do that for now this is working correct so we're gonna leave it like this um now let's see if it actually works i'm gonna explain it pretty quickly what we did was so we get the possible move row if we're gonna move diagonal we just move by one step so we don't need steps here so it will be our initial row plus the direction so plus one or minus one depending on the color uh then we create the possible move calls which are call minus one to the left or call plus to the right then we loop this uh this list and we're basically checking if that's a possible move with the possible move row and the possible move call so for each for each possible move call um okay hope that's clear i told you it was gonna be a little bit more harder than the night moves um but yeah let's test it so we need to create some puns let's create some some black pawns um so the first one is gonna be let's see the position i think it was row five yeah it was row five um column is going to be zero there we go and let's create i think we can create just one to test it so let's see i want to run the file we should have a pawn right here so the possible moves here uh will be this square right and it's working awesome i'm gonna i'm gonna move actually i'm gonna move this pawn right here to see if it works both directions so i'm going to put these to one the first column or the column with index number one okay awesome it's working pretty pretty cool it's working um so when we code the move method this is going to be a valid move all the red squares are valid moves so we could eat this pawn right here awesome we have the pawn logic the pawn moves logic um we're missing also some things of the pawns we're missing the the promotion of the pawns but that will be in a different method so as you know if you reach the final row so if i reach here i i can make that pawn to be a queen a new queen we're gonna do that later and we're also we're also missing something called ampersand uh i don't know if we are gonna code ampersand but we're missing that so i'm actually going to put a comment oh no you know what instead of a comment i'm actually going to use these this thing that i have here i'm going to create some to-do's so i'm going to create first upon promotion so we need to do this and we are also going to do the pawn on percent this is for me to well not forget that we have to do that um so yeah pretty cool so yeah we have the pawn moves for now we can close this method we can close the knight moves method and we can continue with the bishop the rook and the queen these methods or well these pieces are gonna use the same method so we're gonna create a new method called straight line moves we're going to pass it for now and we're going to receive something called increase which stands for increments uh you'll see what i'm talking about so in these three cases bishop case rook case and queen case we're gonna call the straight line moves but we're gonna pass different increments uh i'm actually going to hit enter here it's actually more clear yeah um okay why because bishops move in straight line rooks move in straight line and queen the queens move in straight line the only thing are the increments the only thing that change are the increments we'll see what an increment is so um let's start with the bishop one the bishop increments i'm gonna hit enter here the bishop increments so i'm gonna run the file for for you to know what i mean with increments so let's take this bishop uh okay yeah uh let me actually comment these when i comment this comment that for now i'm gonna go to my main class run the file so let's say we have this bishop or any bishop increments what i mean with increments is how are you gonna move in straight line so for this bishop or for any bishop i mean increments will be either this diagonal and this is uh row minus one but also called plus one rho minus one called plus one rho minus one called plus one so as you can see that is what i mean with increments the increments in that case will be row minus one and cold plus one but we can also move in this direction so we can move in the direction of row minus one but call minus one but we also move in that direction like downwards so let's suppose we're in here we can move downwards so that would be row plus one column plus one or we can move row plus one column minus one so those are increments in the case of the rooks an increment is well basically um row minus one and we don't even care about the column in this case so just row minus one or just call plus one or just row plus one or just column minus one so those are the increments now the queen is actually not hard to code the queen because it's the combination of the bishop and the rook that's a queen so pretty simple we're gonna code first the increments or the logic behind let me go to the board class the logic behind the bishop and the rook and then the logic behind the queen will be basically the combination of these two classes so i know it might be confusing let's start coding it might help you understand clearly what we want to do so okay let's start with the increments of the bishop so increments of the bishop this is gonna be a list by the way um so there are gonna be the following there are gonna be two balls and the first one is gonna be um let's see minus one for the row comma plus one for the column so this is a um going to the upper right diagonal yeah so upper right i'm actually going to put a comment upright we also have the increment going down right so that will be one comma i'm actually going to start with the the other one so minus one minus one and this is the this is the up left corner then we have the one comma one and this is gonna be an increment for the down right i hope this is making sense and the last one should be going down left so that is one one for the row and minus one for the left so down left so these are the increments for the bishop we're gonna do the increments for the for the rook so it's gonna be minus one comma zero comma zero this is up basically then we have the one comma zero i'm sorry the zero comma one this is left i'm gonna go clockwise this actually doesn't matter the order you put this but i'm gonna go clockwise so left now i'm gonna go down that will be -1 i'm sorry one because we're gonna going down one comma zero this is down and the last one is going to be to the left so we have the minus one comma zero i'm sorry zero comma minus one it's a little bit confusing i know left so yeah we have the rook uh increments and we have the bishop increments now as i told you queen increments pretty simple just copy uh let's see we're gonna copy just copy all the bishop ones and let's copy all the rook ones and that's a queen and that's it and this is pretty cool this method is pretty cool because once we code this method we're gonna be able to move the bishop the rook and the queen we're gonna kill three birds with one shot so yeah pretty cool um let's see okay let's start coding actually the [Music] the straight line moves method okay so we're gonna start by looping so for ink in inks so for increment in increments we're gonna create something called co row ink uh comma call inc this is gonna be equal to the ink so to the increment it's funny how i say ink um what else we're gonna create as always our possible move row and this is going to be equal to d row plus row increment now by the way if you have another way to to check the possible moves of the bishop the rook and the queen go ahead and do it you don't have to follow this because this was my logic to solve that problem right and i know it might be a little bit hard to understand um someone else code so yeah make sure to think about it and if you if you if you want to try it well go ahead and create your own method so okay i'm going to do the same with the possible move call it's going to be equal to the call plus but plus the call increment now we can start checking so we're gonna check i'm gonna put a comment here of while true i'm gonna just comment this just comment this you don't have to understand it i'm gonna go to this once we code this this part of the code so okay we're gonna have or we wanna check if the square as always dotting range and we're going to send the possible move row and the possible move call so if they are in range we want to we want to check also if the square dot i'm sorry we need the self dot squares we need the instance here so squares in the possible possible move row and possible move call dot is empty is empty we want to do something i'm going to pass it for now so empty for this case and you'll see why i know this is confusing but just let me we'll code it first and then we we're going to try to understand it so we want to divide the empty and the the other one though has enemy piece why why because if the square is empty we want to continue checking the increments but if it has an enemy piece well we're gonna add that to our move but we don't want to break this while true that i'm commenting for now so hope that's clear but right here we want to check also if the self.square's impossible move row possible move code dot has enemy piece and we want to send the piece.color we want to pass it for now now if it's empty we want to create a new move right create a new move but if it has an enemy piece we also want to create a new move so i'm just putting the comment create a new move now if we are going to create a new move on both if statements we can do it outside so right here so we're going to call we're going to create the the variable initial i'm actually going to copy the comments right here so create squares of the possible because this is possible new move so initial is going to be equal to a new square on the row one column final is going to be equal to the square but impossible move row and the possible move call and the actual move so create a possible new move this is going to be equal to any move initial and final awesome now here we don't want to create it we want to append it so append new move over here append new move so if it's empty we want to have our piece that move move and if it has an enemy piece we want to do the piece dot add move move but then we wanna oh okay we're gonna leave it like this now i wanna go to this to the wall true so as you can see we're doing this for each increment but what we want to do i'm actually going to run the file for you to see so let's say we have this bishop we're doing it for each increment but we want to continue doing it you know like all the all the straight line of that increments then all the straight line of the other increments so these ones then the other ones so the down left and then the other ones the down i'm sorry the down right here and the down left here so we want to like continue doing it until when i run the file again uh what happened oh my god oh so right here i'm going to put the board the board class so i want to continue doing it until we reach either a piece so this will be an immediate break of the of the wild true or we have an enemy so let's suppose i have this bishop here so i'm going to increment this is valid increment this is valid because it's empty this is valid because it's empty but then this is also valid because well it has an enemy piece but i want to break i want to break the code right here because i don't want to continue right so hope that's clear and that's why we need a while true so well true and we're going to put all these inside the wall through so well true we're going to do this and at the end of the walkthrough we want to change the increments so we're going to do something like move row actually possible move row comma possible move call it's going to be equal to the possible move row plus the row increment and then comma the possible move call plus the call the call increment the call increment i'm actually going to do this in two lines of code so i'm actually going to copy these so the possible move row it's gonna be equal to these and then the possible move call is gonna be equal to this yep so this has to be uh outside so right here like inside the while loop but outside this if statement now let's start we're missing some things so i'm going to put a comment here first these are increments we're incrementing the increments it sounds funny but it's what we are doing so incrementing inks or increments yeah um okay let's see as i told you if we're not in range so we're going to put a nails here we want to break so that's obvious if we are not in range we want to break it immediately break not in range now if it's empty we can continue so empty equals continue looping why because at one point we're gonna be here not in range or at one point we're gonna be here enemy piece right um okay so we don't have to do anything here but if it has an enemy piece we want to actually add the move first but then we want to break it and this is the example that i show you here so let me show you again let's suppose we have a bishop here oh my god it's actually well it's not working but we're actually getting the squares okay this is funny because i i wasn't expecting this to work but okay we have the bishop here let's suppose we have it here when we reach this point so this enemy piece we this is valid move this square is a valid move but we want to break it because we don't want to continue right so yeah pretty funny it's working um we're missing uh one if statement which is our own piece so has uh team peace team piece so i'm actually going to copy all these i'll paste it here so we're going to put it has team piece uh i don't know if we send the param for that yeah we send it uh let's go to the board so pieces are the color uh we don't wanna append that as a move we just wanna break it so has seen peace equals break and this is gonna be equals add move plus break so hope that's clear and i think this should work this should work so yeah awesome bishops are not working obviously uh little tube is not called what happened okay we're gonna see that bug in a moment but um but let's create let's create a bishop let's create a bishop to test our increments on the bishop so we're gonna do it under row i'm gonna put it five here five and the column let's see i'm gonna do it actually on the seventh maybe so yeah okay so we create a bishop here if you're asking yourself why is it creating only black pieces it's because we're calling the add pieces with the black param after we call the add pieces with the white part that's why we only see the black piece um so it's actually working as you can see this is what i this is what i what i meant so if it's empty just continue incrementing now if it's your your your team piece break immediately break it but if it's the enemy piece okay you can add it but then you need to break it so i think it's working let's put it on the center just to see so five comma let's put four comma four let's put a four comma four if it works with black it should work with white i'm not gonna test it but it should work okay awesome it's working pretty good now we had a problem when we tested the queen one uh but we'll see we'll see okay when i delete this line of code um what else okay we're calling these straight line moves with the bishop uh let's see if this is correct minus one zero this is going up yep this is going left yep this is going down yep left yep i mean this is right my bad okay so something was happening let's let's check the the rooks okay we're having a problem here so we're not having problems with bishops let me actually grab or drag bishops yeah we're not having problems but we are having problems with the rooks and queens okay let's see type error straight line moves takes one position but four were given um okay what's that oh okay this error is because we didn't put the increments inside the list so let's see if it actually works now see we we have a comment here perhaps you missed a comma uh it's not callable okay we'll see we'll see what's there let's okay we can drag the we can drag the rooks we're going to test it in a moment but when i touch when i want to drag a queen something is happening so it says tuple object is not callable so it should be something it should be an error here uh yeah i was missing a a comma yeah that was the error that was the error okay now it should work also with the queens yeah nice okay we're not having problems let's test it i'm gonna put both a queen and a rook on the middle of the board so let's do the rook here it's gonna be on four comma four and we're gonna have a queen on d let's do three comma three let's see okay now run the file let's hope this work because this is a lot of code and it's pretty good it works first try so it's working i think it's working yeah okay let's see the queen can move diagonal it breaks here it breaks automatically here because we have our piece it breaks here awesome so diagonals are working perfectly now horizontal moves they're working also it breaks when it touches an enemy piece awesome it it works so now we should see the rook also working yeah it's working okay nice we have it we have the visual valid moves coded we are we have a pretty good chunk of our code awesome we can actually close straight line moves so as i told you we killed three birds in one shot the bishop won the rook one and the queen one are ready to go we're missing king and we're done so let's start coding our king moves so i'm going to create a new method inside of our calc moves method i told you this method it's probably the biggest method we're going to code so if we are able to code this we're able to code anything so we're gonna create a new method king moves uh we're not gonna receive any params and we're gonna pass it for now okay i'm not gonna call the castling so for those of you who don't know what a castle is it's basically when you have let's suppose this bishop and this knight are not here so we have an empty connection between the the the king and the rook and this could be also with the other rook i'm going to make the example with this castle link will be to move the rook here and then the king here and and you can do that in one move um as long as you haven't moved before your king or your rook so that's castling now castling is gonna be part of the king moves but i think we're not gonna code it yet um so i'm gonna actually add it to the toudus king castling and we wanna code two castlings the king castling and the queen castling there are two castlings so we're gonna leave that on the two deuce um okay we're just going to to code the like the moves that the the normal moves of the king which is actually easy so we're going to create a new variable we're going to call it adjacent adjacent so like this and we're gonna do pretty much the same as we did with the knight moves if you remember we just put all the possible moves and then we check for those possible moves which of those were valid so we're gonna do pretty much the same but with the king possible moves so king possible moves are the following are basically the adjacent squares so this one this one this one this one this one the one downwards and the one downwards here and down the one downwards here so all the adjacent squares um so that's gonna be pretty easy we have we have a total of of eight possible moves so let's start with the the adjacent which is app so we row minus one and zero now it's not zero but clock call plus zero because these are not increments so it's different uh this is app i'm gonna put a comment here so up then we have the one to the upright so that would be row minus one co plus one comma and this is up right then we're gonna have to the right that is gonna be equal to rho plus zero comma code plus one comma this is right i'm gonna put some spaces here then we have the let me think that will be downright so that will be row plus one comma call plus one comma and this is down right then we have down i'm going clockwise so this will be row plus one and call plus zero this is down uh then we have down left so it will be row plus one comma call minus one so this is down left then we're gonna have left so this is row plus zero plus zero um call minus one so this is the left one and then we have the up left so that will be row minus one yeah minus one comma column minus one so i think that's correct and we have the um what was this it was app left so yeah i think we have the eight possible moves yeah okay awesome and we're going to do basically the same thing we did with the with the night moves so we're gonna loop and we're gonna check and that's all we did it so let's start the loop so for edge this is adjacent in adjacent or actually possible move i'll call it possible move for possible moving at in adjacent we're gonna have the possible as always this is this is repetition of code possible move row comma possible move call this is going to be equal to the possible move and we want to start checking okay so if as always this is always the same in range and we want to send the possible move row and the possible move call now if it's in range we want to check if it's empty or enemy piece so if the self dot squares on the position possible move row on the position possible move call dot has no is empty it's empty or enemy piece that color um we want to add our comments so i'm actually going to copy these i think it's going to be the same we can actually create as i told you we can actually create a method for this because we're doing the same things i think we can actually merge king moves and night moves but we're gonna do that later if it's necessary because we're repeating a lot of code but i think it's clear for you to understand it this way so okay created squares of the new move initially is going to be square row and call final is going to be square of possible move row possible move call um oh yeah we have these which i commented on the on the night moves uh yeah we need to do this later also i'm gonna come back to these later and then we want to create the new move and then we want to append it and that's all that's all so i want to actually put a comment here these are the normal moves because we can also have the castling moves and inside the castling moves we can have queen castling and king castling we're gonna come back to this later but for now i think the king should work so i'm gonna close it and i'm gonna call it here so king moves that's it um [Music] let's try it i'm actually because i mean the the kings are not able to move on the first move so obviously we can't test it so i'm going to create a new king let's put it on d let's see let's see i'm gonna put it on the row number five and the column number i don't know maybe three and this should work yeah we have a king right here so the possible moves are basically all adjacent awesome awesome yeah because we can eat these pieces um but we can also move to empty squares now let's see i'm gonna try to put it on the row number uh let's see i think it's row number two so i'm gonna test it with this row okay now valid moves should only be these ones now these three are not valid books awesome we have it so happy we have it oh my god so we actually have the biggest method of all the game can you believe it pretty cool we have it calc moves is ready um so yeah we have some to do's we have the palm promotion pawn on percent and the king castling we're gonna come back to those later but for now we have the moves the visual moves we're gonna go ahead and code the the console moves so we're literally moving the piece on our console board okay that's our next step okay so this is gonna be actually much easier than drawing the moves because we already did the logic if you think about it we already did the the part where we get all the valid moves of a specific piece so um this part is actually not hard so let's go ahead and create a new method it's gonna be move we're gonna receive self as parameter we're gonna receive a piece and we're gonna receive a move we're gonna pass it for now um what else i'm gonna create a another method called valid move so valid move it's gonna receive self as parameter is going to receive a piece and it's going to receive a move also this is pretty simple we can just code it now we're going to return move in self dot i'm sorry in peace darmups that's all pretty simple method um okay now we're gonna code the move one which is actually not hard um so yeah let's code it so we're gonna have an initial position this is gonna be equal to well to the move dot initial and we have the final which is move that final now we wanna i'm gonna put a comment here so we're gonna put console board move update so we're gonna update the the board the console board as i told you so we're gonna get the squares of that squares in the position initial dot row and in the position final dot i'm sorry initial dot column initial initial dot co so this is basically the square in other words this is the square where where our piece is is located at at this point of of the code of the game so we're gonna set this equal to none i'm sorry not this but the piece so the piece in here is gonna be equal to none because we don't wanna have the piece on the initial square we want to have it on the final one so we're going to put it equal to none now we want to do the same with the final so solve that squares and instead of initial we're going to put final but in this case the piece is going to be equal to piece and that's all so pretty simple also as i told you inside of this method we need to move like virtually move the piece so we're going to put like piece that moved equal to so remember we use this attribute uh for the pawn pawn moves right here so it's actually important then we're gonna get we're gonna like clear valid moves why because we change position so we need to clear so it will be ps.moves it's gonna be equal to this we're gonna actually create a method right here so def clear moves self as a parameter and it will be self that moves equal to an empty array and we're going to call it so clear moves clear moves there we go so we clear the valid moves because yeah we're changing position and the last thing we're gonna do is we wanna create a new attribute so actually let's go to our init init method we're gonna create a new attribute and we're gonna call it last move you'll see why and this is gonna be equal to none as default value but here last move is gonna be equal to to the move where is it to this one so set last move so each time we move a piece we want to like render the move so yeah we're going to save that and i think we're done so pretty simple pretty simple method as i told you this was the method the hard method the complex method because it has five big methods inside so it's actually huge this method is huge so okay we have the move the move thing ready now we need to connect the move with the actual game not with the actual game but with the with the main class so let's see what do we have to do in order for this work so let's think about it um we want to make a move when we undrag a piece right when we have the click release so right here we want to do the following so this is the last line of code that we want to have inside the [Music] event statement if statement uh because yeah we want to drag it at the end so right here we want to do an if statement so if dragger.dragging if we're actually dragging a piece it means that if we made a click release we we did a move or we wanted to do a move so the first thing we want to do is actually take our dragger we want to update the drager dot update mouse with the event position and then that we have updated the mouse of the dragger well the the mouse position of the dragger um we want to do the following we want to create two variables released row which is going to be equal to dragger dot mouse x i'm sorry mouse y is mouse y divided by square size we already did this this is the conversion between the position and the rows or will be the board position so released call is going gonna be equal to dragger dot mouse x double slash square size we have it now we wanna check if it's valid if that move is valid right so we wanna create it so create move possible move because we don't know if it's valid remember so we're going to do initial as always it's going to be a new square and it will be dragger dot initial row initial row that's why we have those attributes and we have dragger dot initial call okay square is not defined we need to import it so from square import square we have a final which is equal to square release row and release code right and we have a move which is a new move we need to import move class initial comma final so also from move import move um okay now that we have the move we can actually make or call we can call this these valid move method so if board valid move no valid date did i call it validate no it's valid move so valid move so it asks for a piece so it will be the dragger dart piece and it asks for a move so right here we're asking if this is a valid move and if it is we want to call our board.move method with the dragger.piece comma it asks for a move so we're going to send the move so this is going to move the piece inside the console board and why do we need that because the game dot show pieces is actually accessing to the console board that's why you'll see why you'll see what i'm talking about in a moment once we move it we want to draw so draw or show methods show methods so we want to show the background on screen we want i think we don't yeah we don't need to we don't need to show the we don't need to update the blade so the the dragger the dragon piece and we also don't need to show the possible moves obviously because we already release it so game dot show pieces that's actually important screen if you don't show the pieces after you show the background pieces are gonna disappear so yeah i think we have the because yeah we make a move only when we make a click release yeah i think we don't need to edit click or mouse motion um because we're calculating the moves inside the click event and that's it so yeah i think this should work so i'm gonna run the file and we should see the following we should see oh by the way i'm gonna we have a king here so i'm gonna actually delete it uh add pieces right here okay so we should see the following when we when when i move a piece so let's say if i move this pawn right here we should see the pawn here we should see this square empty and that's it so i'm gonna try it it doesn't work i don't know why oh my god i don't know why it doesn't work with any piece why is that okay we need we need to fix something so let's start debugging we should enter here so i'm gonna print valid move so we should see printed a valid move on console no we don't see it why is that okay so we're not entering this if statement board valid move um so let's check this method return moving piece that moves so yeah this method is correct let's see the move one we are taking initial up but we haven't moved yeah so this method shouldn't be shouldn't be the problem i don't know what's the problem actually um okay i'm gonna do the following i'm gonna try to debug it myself and once i have it i'm gonna well i'm gonna tell you what's the problem okay guys so i know the error it was actually hard to find and i'm gonna show you how to fix it okay so what's happening i wanna explain what's happening so let's go to our board class right here we're returning if a move is in the piece that moves so what's python doing behind the scenes here it's actually looping right through each piece moves and it's comparing it's comparing the move to each of the piece valid moves right and in case the equality is actually well true um he's going to return true that's what python is doing behind the scenes now the problem is the following python doesn't know how to compare a move to another move because this is a self-made object like we did moves so python needs to know somehow if this move is actually equal to another move so we need to tell him what's what's like what's the condition for a move to be equal to another one so we're gonna do that so that's the problem now the solution is the following it's pretty it's pretty simple so we're going to create another dunder method and dunder methods are just special methods that a python has so we are going to create the dunder method eq which stands for equality and and we're going to type this so we're going to receive self and other as a parameter i'm going to explain why so what we're going to do here we're going to tell python when does a move is actually equal to another move so that's what we're going to do so we want to return if the self dot initial so if the the initial square is equal to the other dot initial square and the self.final square is actually equal to the other dot final squared now we have a problem python doesn't know when a square is actually equal to another one so we want to do the same for our square class so right here go ahead and type the same we're gonna we're gonna literally type the same but instead of having initial and final we're gonna use row one call so we wanna return self.row is equal to the other dot row and self.call is equal to the other.com and this will fix the bug so let's go to our move class so yeah now python is going to loop so right here it's going gonna loop through all of our piece moves piece valid moves and it's gonna compare if that move is actually equal to one of those now how is it gonna compare that move well it's gonna use this equality now if initial is equal to other initial and as we know initial is actually a square so we he needs to compare the square so he's gonna go into this method and once he compared the the initial square he's gonna do the same with the final one and then he can return if a move is actually equal to another move so that's behind the scenes hope that's clear and now we don't need to change anything from here and it should work correctly so when i run the file it was actually hard to see that bug but now try to move any piece and it will work so as you can see now what happens if i capture i don't know it actually disappears okay um so yeah it's actually working let's move our queens try to play with these well with with what we have and try to see bugs because i don't find any bug try to move all your pieces so knight rooks knight of all the recorder move the king of the king move the bishop king i'm gonna eat these so yeah i think it's working correct so make sure to play with it and uh yeah we did it we actually did it we actually coded the move um but on the console board so this is a huge huge part of the code we have a huge part of the code believe me we're just missing some small things but we actually have a lot we actually have a lot so if you continue watching this video feel proud of yourself because we we have done a lot believe me we have done all that um okay let's see we're gonna continue with with with our game coding our game so [Music] the next thing we're gonna code it's actually something really easy to code so as you can see on the structure of our game on the class diagram we have right here the game class we have a next player attribute that we haven't created why do we need that because as you can see we can start with white but then we can continue playing with white we don't want that we want that just when white plays the next string will be black then white then black and so on so on right um so we're gonna do that right now so go ahead and create a new a new attribute i'm sorry right here we're gonna call it next player and default is going to be equal to white the default value is going to be equal to white now we're also going to create a new method and this is not going to be a render method so i'm going to put other methods it's gonna be next torn we're gonna receive self as parameter and we're gonna code the following so if the we're gonna do the the if else in one line so self dot next story is equal to white if self dot next turn is equal to black else is black if it's quite confusing for you well just do the if else classic statement and it should work correct okay so we have the let me see we have our next run method we also have our attribute now we want to check inside of the main loop um we want to check that we want to check the next player so it's pretty simple we just need to check the following so inside the click event we want to check uh let's see i want to check if it's a valid piece valid piece color so we want to check if the piece dot color it's actually equal to the game dot next player and if that's true we can do this i think that's all now remember we have an attribute here color and the piece in the piece class and color is basically white or black and inside of our game this attribute is actually the same it's going to be white or black when we change it so it should work it should work so let's try it i'm gonna save the file and run it and we should we should move the we should be able to move the white pieces but we shouldn't be able to move the black pieces so let's see yeah as you can see i i cannot drag it i cannot drag it so try it yourself i'm gonna make a move um okay we're missing that my bad we're missing that we need to change of player when we do a valid move so right here so right here i'm gonna put next torn and it will be a game dot next torn we should have it should work so i'm able to move the white pieces now i'm able why i'm able to what is happening what what is going on okay game dot next turn let's see game that next turn self dot next turn oh yeah that's the problem this is not next turn this is actually next player oh my god so this is next player now this should work pretty pretty short it should work now i can move the white pieces and now i can move the black ones and i cannot move the white pieces so yeah we have implemented torns on our game pretty cool pretty cool we're almost done actually okay so let me tell you what we are gonna do next we already did the hard part which was this method the calc moves um now we're going to do the following the first thing we want to do is actually [Music] remember i told you that we're going to have but we have a reference to a last move we have an attribute last move inside of our board class that's because we want to draw this last move so that's the first thing we want to do then let me see then we want to then we want to create a new method called show hover you'll see what i'm talking about but that's the method that we want to create pretty simple don't worry then we want to create the theme class so if you remember at the beginning of the video i show you that i was able to change a theme from blue to brown and green and gray those were my possible well themes so we're going to create those and we're also going to add some sounds so we're going gonna do that and we we should have by the end of of this section we should have a pretty decent game we we we're gonna be missing some things like the the ambassador the castling we're gonna be missing checks so okay let's start by by creating not creating but by showing this last move so remember last move um is set to the when we move a piece so we already did that and now we want to show it so right here in the show methods when i create a new method we're going to call it show moves show last move i'm sorry show last move we're gonna receive self as parameter and we're gonna ask if the board if the self dot board actually has a last move um so if it has a last move we want to do the following we want to save in a variable initial the self dot board dot last move dot initial i'm going to copy this and here we want to do the same with final with final so we the first thing we want to do is save the initial square and the final square because we're gonna be rendering the last move from the squares right so we're gonna do a for loop so go ahead and type for position or pause in and i'm gonna put a list an explicit list here initial comma final so we're going to loop initial and final and right here we want to do the following so we want to save a color we want to do the same as we did here i'm going to actually paste it i'm going to delete these lines of code but the structure is the same we want to save a color then a rect and then a bleed so the color is going to be equal to 244 247 this is rgb format and 116 if the pass dot row plus the post dot co model two is equal to zero else the color is gonna be 172 comma 195 comma 51. so this is the same that we've done on previous renders um and yeah remember position is either initial or final and we know initial and final have some attributes called row and call so we can do this now we have the color um by the way these colors we're gonna save them on our theme class but for now we're going to hard code these values okay so now direct so rect is going to be equal to pause dot co times square size so remember x x-axis where are we going to start on the x-axis then plus dot rho times square size where are we going to start on the y-axis then the width and then the height so that's direct on the bleed will be we need to pass surface we want to receive surface as a parameter and we want to type the following so surface i'm sorry pie game pie gameplaygame.draw.rect and we want to pass surface color and rect and that's it we have it now each time we do a move we're going to show it so let's see we need to we need to call this from the main class so let's see we do a move when we release the click so here so we actually want to show it right here in between the show background and the show pieces because we want to render the move on top of the background but below of the pieces that's why so game dot show last move on screen and this should work so let's see whoop it's not working um let's see why see the game yeah it's game show last move i think i know the error and it's because we're not showing it right here so let's see we want to show it on top of the background but do we want to show it on top of the moves no so we're gonna put it here game that show last move on screen this should work it should work yeah awesome this looks pretty nice so make sure you play with it oh okay we're having a bug here yeah when we when we mouse when we are doing the mouse motion it's actually not not showing so we're gonna fix that right now so in mouse motion um let's see right here we wanna do game dot show last move on the screen i think that's that's all we need to do uh oh this wasn't mouse motion this is actually this is actually click yeah we're missing mouse motion so right here it will be game dot show last move on screen and i think we have it yeah so let's see i have the the last move being drawn being bladed on the screen now as you can see if i hover it works now it changes now it changes this is the last move now this one awesome this is pretty cool pretty pretty cool i like it so try to try to play with it like we have a pretty big chunk of the code now so yeah play with it let's see what happens if i capture a piece then i capture this one okay it's working awesome okay so let's now create the method that i told you about the show hover so go ahead and type def show hover we're gonna receive self as parameter and we're gonna pass it for now now these methods are are mostly designed methods so if you if you don't care about design well you i think you can skip this but but yeah i think it's pretty cool to have to have these these methods like show last move show however you'll see what i'm talking about uh show moves um all that it's actually pretty cool so okay for show hover we want to create a new attribute and we're going to call it hovered hovered squared this is going to be equal to none as default now we inside of the other methods section we're going to create a new method called set hover we're going to receive self as parameter and we're going to receive a row and a call we want to set the harvard square to be equal to the self dot squares um i'm sorry so to to the self.board dot squares in the position row in the position column there we go and that's it uh okay what else let's actually code the show hover method so this is going to be pretty simple so if self dot hovered hovered square so if we have a hovered square we want to do pretty much the same as we did here so we're going to have a color we're going to have a rect and we're going to have a bleed so the color is going to be always 180 180 180 the rectangle it's gonna be instead of pause go ahead and change this to self dot hovered square here it will be dot call and here it will be dot row and when we bleed it okay we need to receive surface and that's it we have it now we're missing some things we're missing the set hover call so we're going to call the set hover from the main class so we want to set a hover inside the mouse motion let me show you so right here we want to set it outside the dragger because we don't care if we're dragging a piece we want to hover a square when we have a mouse motion on it so we're gonna type game.set hover and it receives a row and a call so let me see we want to create them right here so the motion row is gonna be equal as always this is always the same so we then pause in the position one double slash square size we are converting from a coordinate to a board position and then we have a motion row a call i'm sorry this is going to be equal to the event dot pause at the position zero uh divided by square size and now we can set the over to that motion row and motion call that's it now we need to show it so we want to show it let's think about it we want to show it i think we want to show it right here so we game that show hover on screen this is like the order we want to show things so first the background which is the the first thing we want to we want to show on top of the background we want to show the last move then we want to show the moves um then we want to show the pieces then we want to show the hover and then finally we want to show the the dragger piece the piece that we are dragging um okay so in mouse motion and i think that's it we also need to call it right here let me see right here we're gonna put a game dot show hover and you'll see what hover is because i don't i don't know if you if you understand what show hover does so i think i didn't explain it but okay um okay this is show hover but it's not gonna be like this we don't want the direct to have to be filled so it's pretty simple right here on the blit part of the of show hover we can actually send another pram an optional param which is let me see border radius this one no i'm sorry width yeah it will be width it will be width so if no width is specified the the rectangle is going to be filled but if we specified so width equals three pixels um the square is not going to be filled so now as you can see i don't know if you if you can see it but we're hovering the square and it actually looks so cool so that will help us well now where is our mouse you know pretty cool you can change the the color if you if you like but but yeah okay we did show however i told you it was going to be pretty simple and now we're going to start creating some more classes so if we go to our class diagram we're going to be coding all these we're going to be coding these classes um so we're gonna be coding a configuration class which is gonna have two reference one to a theme one to a sound class thin class is gonna have three reference to the color class and sound class it's gonna be pretty simple actually these four classes are pretty simple so let's go ahead and code them so we're gonna start by creating the config dot py the configuration class so class config we're gonna create our init method pass it for now we're gonna have a method called change theme we're gonna receive self as parameter we're gonna pass it for now and we're gonna have a private method called add themes going to receive self as parameter and we're going to pass it so this is the config the config class we're also going to create the the theme class so theme.py class theme we're going to create an init method pass it for now and yeah we we actually don't need methods inside of this class uh we also are going to create a new class called color dot py so gotta create it class caller we're gonna have an init method we're gonna pass it for now we're not gonna have methods in here and we're missing the last one sound so sound.py we're gonna be coding those classes in a moment they are pretty simple believe me so class sound same the init method pass it for now so yeah they are pretty simple you'll see so okay we're gonna start with the configuration class now actually we're gonna start from the bottom so from the those simple simple classes so color and sound then we're going to code the theme and then we're going to code the config so let's go ahead and start with the color pretty simple file we're just going to receive light and dark that's it and we're gonna put self.light equals light and self.dart equals arc pretty simple we have the color class ready like literally this is the file we can actually close it now let's go ahead and edit our sound class now by the way this this class is going to have one method which is play which is gonna it's gonna be responsible of playing the sound so i'm going to pass it for now we need to import python inside of this file and we want to receive some params which is not some just one which is going to be the path of the sound so self.path equals path and we're going to have a sound attribute which is going to be equal to pygame pygm dot mixer dot sound so this is going to create a pie game sound okay so that's why we need to import my game and the the code is the following pie game.mixer.sound and sound receives a a path a path also so we're gonna send our path and inside the play the play method we wanna do the following so pygame.mixer dot sound dot play and we wanna play the self dot sound um so yeah here we are creating a pie game sound and here we are playing the pie game sound so yeah pretty simple file also we already finished and we're gonna go ahead and code theme class so the theme class is going to receive a lot of params so we're gonna receive a light bg or light background also a dark background we're gonna you're gonna see why now right here we're gonna receive a light trace a dark trace let me see i'm actually going to put this comma right here so right here like this and then we wanna also receive light moves and light i'm sorry dark moves and we'll see what um this will start to make sense when when we when we actually apply it okay so we're gonna we're gonna do the following we're gonna create a new a new background attribute this is gonna be equal to a new color so we need to import colors so from color import color there we go so remember a color has two properties light and dark so we're gonna send the light background and the dark background and that's it we want to do the same for a self.trace it's going to be a new color we are going to pass light trace and dark trays and we also want to do the same for the moves these are our three references to the color class as you can see so color and it will be light moves and dark moves and that's it we have it we can close this file now config is a little bit more long but it's actually pretty simple so we're gonna start by importing game pygame we're also going to import os the operating system model and we want to import from sound import sound and from theme import theme and that's all okay so right here we want to call the add themes we're going to be coding this method in a moment then we're going to have an attribute called index this is going to this is going to represent the index of the themes themes is a list um so actually let me create it here some of the themes will be default and empty an empty list but we're also going to have a theme so these are the themes but this is the active theme so it will be the self.themes uh on the position of self.index there we go uh what else we need a font which we're gonna create later font we're gonna leave it there uh we have um we have a move sound so we're gonna have two different sounds one for the movement and one for the capture so this is actually a new sound and the sound receives a path so the path is going to be us the the operating system model dot path dot join and we want to go to assets slash let me see slash sounds sounds slash move.what wab um okay so we have created our move sound now we're gonna create our capture sound so this is a new sound and it's gonna be the same so os dot pad dot join acid sounds capture what there we go okay we have our sounds now we're gonna call the the [Music] the play method inside of our main class so we're finished with the sounds now change theme so it's gonna be the following self.index equals one because we're changing the theme so we're incrementing the index then we want to put our index to be equal to the model of this value of the index value model the length of the self.themes because we can hard code this so for example we're going to have i think four themes as default but if you want to add more well this code will let you add as many themes as you want and this will always like choose the next one and if you if you if you reach the final like theme this line of code will make themes restart like you're gonna choose the the first theme i'm not i'm not i don't know if i'm being clear so so let's suppose we have a t1 t2 t3 and t4 these are themes right so if we increment the index we're gonna pass from this theme to this one and so on so on but if we're in on the last theme well we cannot increment the the the index so this line of code is gonna make the theme for next theme to be the theme one this is what this line of code is doing so hope that's clear and then we can actually set the theme to be equal to the self.themes in the self.index nice okay now we're going to create the themes so we're going to have a green theme we're going to have this is the default theme so we already have this one we're gonna have a brown brown theme we're gonna have a blue theme you can you can create as many themes as you want and we're gonna have a gray one you can create more if you want i'm gonna go with these four now here we're gonna add those themes so self.themes equals equals green green brown actually going to do this in one line yeah so brown uh blue and gray there we go now we want to create them so let's see for example the the green theme we already have these colors if you think about it so it starts with the with the background so light bg and dark grey g we already have that inside the game class right here remember so i'm going to copy these where is it right here so this is the first param uh without the color this is the first param then we want to have the dark background so this one which is the dark green uh where is it here i'm gonna paste it then we want to have a light trace so this is gonna be the last move the last move um color so it this is the light one i'm gonna copy it and i'm gonna paste it right here then the dark one so it will be this one and i'm gonna paste it then we we want the light moves so that would be the red the the red red color that we use for showing the value moves where is it is this one so this is the light one so like this and then the dark one which is gonna be this one there we go so that's the green theme now let's go ahead and create the brown the blue and the gray one you can skip this if you don't care about well creating some themes but okay the the light background is going to be 2 3 5 comma 209 comma 166 the dark one is going to be 165 comma 117 comma 80. then the traces so 245 234 comma 100 then we have 209 comma 185 comma 59 we're gonna use the red for all so same red there we go now we have the blue theme let's go ahead and create the blue theme so it will be 229 comma 228 comma 200 then the dark background is going to be 60 comma 95 comma 135. light trace is gonna be one two three comma one eight seven comma two two seven dark trace it's gonna be 43 119 191 and we're gonna have the same moves so red moves there we go and last we have the gray one so it will be 120 comma 119 118 the dark background is going to be 86 85 by the way i'm copying these values that i had on another file but you you can use whatever values you want this will be 99 126 comma 143 then the dark trace is going to be 82 comma 102 comma 128 and then we are going to have the same red color okay so we have our themes uh now the next thing we want to do is actually instead of hard coding those colors we're gonna make them variable depending on the theme we have so we're gonna create a new variable this is gonna be the same for all basically so theme equals self.config we need to create that attribute dot theme so right here go ahead and type self.config equals config we want to import it so from config import config now we have the theme right here so self.config.theme and now the color so i'm actually going to change that if statement so color color is going to be equal to to the theme dot background background light light if the row plus the call model uh where is it model two equals zero else theme dot background dot dark and this line of code is going to replace all these let me actually put a comment here so wrecked and right here delete there we go so we have it here now the show pieces we don't need to change it now show moves so same thing we're going to create an uh variable theme which is going to be equal to the config.theme and the color is going to be equal to the theme dot moves dot light if this happens else else is gonna be the theme dot moves dot dark i think we call it we call it moves i don't really know i mean let me actually check uh yeah moves okay and we are ready with this one now the last move also needs some editing so self i'm sorry theme equal self.config.theme and same thing it's gonna be theme dot is this the trace yeah this is a trace dot light and then we wanna put theme theme.trace.dark and we have it so instead of our coding the the colors we are basically making them variable so if we change the theme all colors are going to change now we wanna we want to we want to um actually change the theme so how we're gonna do that with our keys our keyboard keys so right here i'm gonna put key press this is another type of event so go ahead and type alif event.type equals equals pygame dot key down and now here so this depends on on you but i'm going to do the following so if the vendor key if the vendor key is equal to pi game dot k underscore t this is gonna be for changing themes we want to take our game dot i think we have encoded that method okay we're gonna have a change theme change theme method inside of our game and that's all we're gonna be coding some other keypress events but for now we have the theme one and inside of the game we want to create the def change theme self as parameter and we want to call the self.config dot change theme so remember config where is it here change theme is doing this so now it should work when we when we press the the t key it should work so i'm gonna run the file to see event object has no attribute key um okay my bad this is not a bent kit this is a vent type um okay so we have the the green well the green theme this is the default one now if i press t look what happens we have the brown theme awesome this is pretty cool now if i move when i'll show you also this we have this last move this is the trace um if i change of theme this will be the blue one and look what happens with the trace it it also changes so this is variable this is actually pretty cool and we have the gray one awesome so so yeah now we have some themes to play with pretty nice and we have finished this section like this design section okay i actually forgot to to also code the sounds so we're gonna do that pretty quickly let's go to our game class and right here we want to create a new method it's going to be called sound effect we're going to receive self as parameter and we are going to receive one boolean parameter that is going to be captured and we're going to set a default value to false so if captured so if we capture a piece then we want to call ourself.config dot capture sound dot play and that's it else so if we if we don't capture a piece we want to call the config dot move sound and call the play method that's it so we have the sound effect now we need to call it so let's see where do we want to call it we want to call it when we release a piece right when we do a move so right here so if it's valid move we want to play a sound right so right here we want to check the following we want to check captured it's going to be equal to board dot squares in the position released row comma release call um dot has piece that has p so this is going to return a boolean value if this square has piece so if it has piece we actually capture a piece right so we have the boolean value now we move the piece and then we want to play the sound so it will be something like game dar play sound uh sound effect actually gonna call it play sound play sound and we're gonna send the captured um so let's change the name so instead of sound effect we're gonna use play sound now it should work so each time we either move a piece or capture a piece we're gonna hear a sound awesome nice nice nice it's working i'm gonna i'm gonna now capture a piece awesome it changes nice so it's working pretty good when i ate my king okay pretty good hey guys coding spot from the theater here i already finished the tutorial you are watching and i just realized that i forgot to code this font so as you can see we left a comment here a font comment here so we're going to be doing that and you can continue with the video now you're going to be seeing a lot of methods that you probably don't have till this point because remember i'm from the future i already finished this tutorial so don't worry if you see some things different i just came back to show you what are we gonna do with this font so let's go ahead and create a new font this will be self dot font and this is going to be equal to pi game dot font dot cease font and we're going to send a name as you can see we're going to send a size and we're going to send bold equals to true so the name of our font is going to be monospace like this monospace the size is going to be 18 pixels and we're going to send the bold equal to true now that we have the font we can go ahead and use it so let's go to our game class remember don't worry if you see some things different now we're gonna modify our show background so right here inside of our double for loop we want to do the following i'm going to put a comment here this is going to be row coordinates coordinates so we want to check if call equals to zero so we're gonna put the coordinates on the first column and if that's true i wanna create a new color and this is gonna be color equals to theme dot bg dot dark if the row model two equals zero else theme dot dot and we have it so this is going to be the color of our font now if you're asking yourself like what the heck am i doing this is going to be responsible of marking the squares marking the coordinates of the squares you'll see now we're going to create a label so label label and to create a label we're going to create a new variable called label and this is going to be equal to the self.config dot font the font that we previously created dot render and we're going to receive a text so what do we want to render an anti-alias and a color so the text is gonna be the string we're gonna cast this to a string and it's gonna be the total rows minus the row you'll see anti-alias is gonna be equal to one and the color is the color that we just created now i'm going to create also the position so the label position where are we going to render this label and this is going to be equal to a tuple and it will be pretty simple it will be 5 comma 5 plus the row times the square size now if this is quite confusing for you because i know there are a lot of variables here make sure to grab a piece of paper and literally just replace these values with numbers and it will make sense now the last thing we want to do is actually bleed our label so as always surface dot blit the first param is what we want to bleed so that will be the label and now we want to pass the position where are we going to delete it so now if i run the file we should see an error why is this so it says invalid destination position for bleat uh oh did i send a color let me see whoa i'm sorry didn't see this now this should work and as you can see we have some beautiful numbers being displayed on screen now we're gonna do the same but with the letters so let's go ahead and continue with our code now we wanna put the column coordinates and this is gonna be if the row equals equals seven we are going to do pretty much the same so i'm gonna copy this i'm gonna paste it now the color is gonna be the theme background dark if row plus column so if row plus co model two equals zero else the light background theme now the label is gonna be the config font render but instead of rendering string rows minus row we are gonna render the letter that is going to represent our columns now if you remember inside the square class oh we actually didn't create it let me show you the class diagram so right here square we have something called alpha code so we are going to create it right now so right here on top of our init method i'm going to create a new a new dictionary this is going to be equal to alphacall alpha call like this actually alpha calls and this is going to be equal to a set the keys are going to be the numbers and the values are going to be like the letters so 0 is going to be a 1 is gonna be b and so on so on two is gonna be c three is gonna be d four is gonna be e then we need the f five f six g and seven h we're gonna do it until the seventh column now right here we're gonna put cell dot alpha call and this is gonna be equal to d self dot alpha calls so the set this set in the position of our column so if we have a column of 2 the square is going to be the c the column c now we're going to create a new static method so remember static methods are accessed with the class not the object we're going to call it get alphacall alphacall you'll see why we need this we're going to receive a column as a parameter and we don't need the self obviously because we are accessing through the class and now go ahead and copy the the set that we previously created we can't access it as we don't have the self keyword so this is gonna be pretty much the same alpha calls are going to be equal to that and we are going to return the alpha calls in the position of the column and that's all so now inside of our game class we can render the alpha code so we're going to call the static method so we're gonna use our square class for it and this is gonna be equal square dot get alpha call alfa co and we're gonna send the column now we need to import the squirt so from square import square and now this should work we're gonna we're gonna render it on a different position so go ahead and clear this and this is going to be equal to co times the square size plus the square size minus 20 comma height so the height of the screen minus 20. so again if this is a little bit confusing for you make sure to use real numbers and you'll see it makes sense um okay and now we're bleeding the label on the label position and now we should see when i run the file we should see the coordinates being rendered on screen okay i had a problem square has no attribute get alphacall uh let me see square dot get alpha cool why is this how did i call it get alpaca oh my god so yeah get alpha call i'm gonna close these and now we should see these working yeah now i'm gonna run the file and we should see the columns being rendered and as you can see we have it so this looks pretty nice we have the a1 square b2 c3 so now we can talk in coordinates um so yeah we can continue with the video now remember i'm coming from the future so you're not gonna be seeing these things being rendered on the tutorial but these shouldn't affect what we are going to be doing next so don't worry you can obviously leave these coordinates on your board and this looks just so nice now by the way if i change the the theme look what happens so these numbers also change of color this is so so beautiful so so yeah guys let's continue with the video so yeah pretty nice pretty nice what are we missing we we actually have a lot you've made it so far like look look the lines of code that we've we've typed like this is a lot of code so we're gonna start adding some some small things like the restart function so right here that will be a keypress event so i'm going to copy this i'm going to paste it here i'm going to put if vendor keep i in underscore r this will be for restarting our game we're gonna call the game.reset that we need to create that method so right here go ahead and create a new method called reset we're gonna call the self we're gonna we're gonna receive self as parameter and this is gonna be pretty simple we wanna do the self.init we're gonna call the init method so in other words we are creating a new game right now we need to do some other things because if you remember we have these variables here so we're gonna copy them and we're actually going to paste them right here why because we are restarting the game but we are not restarting the values the variables so screen i think the screen we don't need it game is going to be the new self.game board is going to be the new self.game.board and so on so on so we need to do this this is important in order to restart our game so let's run the file to see if it works i'm gonna play i'm gonna play here play here i don't know i'm gonna put my pawns like these and then i'm gonna press the r key and as you can see it works now let's check here and it actually works i'm going to try to move the black pieces actually works so it's working correctly okay so let's actually start doing the tasks that we have on our to-do list we're gonna start with the pump promotion then we're gonna code the king castling so both the queen side and the king side castling and finally we're gonna call the ampersand move so we're gonna start with the pawn promotion uh it's actually pretty simple you'll see we need to go to our board class and inside of our board class we're gonna modify our move method so right here in between these lines of code after we update our console board but before we set the pc.move to true we're gonna do the following i'm gonna actually put a comment this is gonna be the palm promotion so i want you to understand what i'm gonna do next so once we have moved the piece on the console board we're gonna check if that piece was actually upon so that's the first thing so go ahead and type if it's instance piece of pawn we are going to call a method that we need to create which is going to be called check promotion and we're going to send us a parameter our piece so that will be the pawn and we're also going to send the final the final square so the square word this piece where this pawn ended okay so let's go ahead and create this method check promotion i'm gonna do it let's see i'm gonna do it right here so def check promotion we're gonna receive self as parameter we're gonna receive the piece and a final square okay now inside of this method is gonna be pretty simple we wanna check if the final dot row so the final row of our piece is equal to zero or the final dot row of our piece is equal to seven so i'm going to explain this so this means that a pawn our piece is now ready to be promoted why because let me let me put you an example white white pawns are gonna promote on the row zero right now black pawns are never never ever going to touch the row zero because they can't go backwards so we don't need to check the color actually we just need to check if the row is equal to zero or if the row is equal to seven so if the row is equal to zero we're gonna make a queen a white queen and if the final row is equal to seven we're gonna do a black queen so we don't need to check the color actually you'll see so inside of this if statement we're going to do the following go ahead and type self.squares in the position final.row and in the positionfinal.com dot piece so we're gonna change the pawn because remember this is the pawn this is the same piece as we have here and this is gonna be equal to a new queen and remember queen or any any of our peace classes receives a color so we're going to send the same color so that will be the piece.color and this is pretty much all we need to do so so yeah pretty much that's all we need to do i'm gonna i'm gonna actually check it i'm gonna see if it works so i'm gonna run the file and let's see i'm gonna start moving let's say this pawn also this one so i'm gonna push them to the final rows i'm gonna capture this rook and we should see how this pawn promotes into a queen awesome awesome awesome awesome nice pretty nice so it's working for the white pieces so let's see for the black pieces also working nice so we have our two queens so let's see if they can actually move as queen's move so i'm gonna drag it and as you can see awesome this is working as you can see we have the movement of the of the of the queen we can move diagonal horizontal and vertical so i'm gonna eat this also here so yeah it's working awesome so i told you it was not gonna be hard at all it's actually pretty simple to implement the palm promotion so we're gonna mark these as doan and we are gonna start working with our king castling so let's go ahead and implement the king castling so that will be also inside the board class um and we're gonna do it right here so king castling before we check it i wanna i wanna actually create a new method so let me see right here below the chat promotion i want to create a new method called castling we're going to receive self an initial square a final square and that's it i'm actually not going to code it yet because i just remember that we need to we need to first modify this method the the king moves method inside of our calc moves method so this one we need to modify this one first let's start adding the the castling as a valid move so this is gonna be a little bit long so listen to me i know this code that i'm gonna do next could be optimized i know it's possible to do it shorter but i want to do it this way because i think it's easier for me to explain you so we're going to start first by checking so by the way let's let's remember that we are inside the king's move moves king moves um so we're the the piece is actually um a king so if not p is that moved so if we haven't moved if the king hasn't moved we can actually will apply the both queen castling and king castling so okay right here we're gonna start with the with the queen castling we're gonna start with this one so i'm actually going to show you right here oh i have an error i'm going to pass it for now and let me show you so queen castling will be this one so like this castling to the left of our king so we're gonna we're gonna code that that castle in first um so okay we're gonna create a new variable called left rook and this is going to be equal to self.squares in the position row because the rook is going to be on our same row obviously and the column is going to be the zero one if we're gonna castle we we are assuming that the left rook hasn't moved also so it's gonna be on this on the on the column number zero right now we wanna check if his instance left rook from rook so if it's actually the rook the one that is there we want to now check if that left rook um dot moved so if it hasn't moved so if not left rook got moved now if all of these statements are true then we can do the queen castling so queen castling will be a valid move for for both the rook and and the king now i'm gonna explain you the algorithm that i'm gonna use so we're gonna we're gonna loop through each column so from here from where we have our knight to where we have our queen and we're going to check that these squares are actually empty and we want to actually add the all the castling move the queen the queen said castling so let's see we're gonna do a for loop so four i'm gonna use c as column because we're already using call so i can use call in range zero now i'm sorry one two four so these are the columns that i show you a moment ago now four is excluded so we're gonna check until the third column um and we want to check the following so if self dot squares on the row and on the c dot has piece we want to break we want to break the loop and this is going to pretty much exit all the if statements or in other words um castling is not possible because there are pieces in between that's what this if statement does so i'm going to copy this comment i'm going to put it here castling is not possible because there are pieces in between okay so we break now if that's not true we want to now check if we are on the column the c number three because that means that we we finish with the for loop and that means that we can that we can add the the castling as a valid move okay i'm gonna pass it for now because for this i wanna i wanna create a new attribute um inside of our king class so go ahead and open the the piece file and inside of our king class i'm gonna create a new two new attributes first one is gonna be left rook this is gonna be equal to none as default and also right rook and this is going to be equal to none as default now we need these attributes because we want to we want to have a reference from our working to the rook so you can think this that instead of having one move like the king is having a valid move it's it's it's two moves in one so we have to move the king but also the rook that's why we we're making a reference okay so now if we manage to reach inside this if statement we can actually add the the left rook to the to our king so ps.left rook it's going to be equal to left rook remember this piece is actually our king so i'm going to put a comment here adds left rook to king okay next thing we're gonna create the move for our rook so rook move and then we're gonna create the king move so rook move is going to be the following so we're going to create a an initial square as usual this is going to start at row and at the column equal to zero and a final square which is gonna be the same row but the column number three so if you if you see our board we want this rook to finish right here and this is this is column number three so so let's continue this is the final move and then we can the final square i'm sorry and then we can create a new move and this is going to be initial comma final and finally we can append or we can add to our left rook this new move so add a move move and that's all so we did the the rook move now we're gonna do the king move which is pretty much the same so we're gonna actually going to copy these because we we're going to have an initial a final a move and we didn't want to we'll append that move so initial is going to be row comma call final is going to be row comma 2 the move is initial on final and then we want to put our piece the rat move the the new move and we did it so we have the queen castling i'm going to close this now this is the part that i that i was talking about this is actually not optimized because we're going to literally copy all these and paste it for the king castle we're going to do the same now again i'm repeating this code because i think it's clear for you to understand what are we doing on the queen castling and also what are we doing on the king castle but i want you to know that you can merge both blocks of code so i'm gonna paste all these and now instead of left rook go ahead and replace all the left rook for right rook then instead of having the zeroth column we want to have the the column number seven now by the way i'm missing something here so left rook is actually equal to this but dot piece i missed the piece um the same here so dot piece now we write rook is equal is instance of rook this is okay this is okay this is the same now the for loop we need to change the for loop because now it's gonna be from five to seven and this is gonna be the same and then we can check if c equals six and then we're going to add the right rook right rook to our king and then we have the root move so this is going to change so it's going to be row comma 7 for the initial and row comma 5 for the final initial final this is the same and we're going to change this one because final is going to be now the column of the final square is going to be now the sixth sixth column and that's it so yeah we finished the we finish adding the the the castling as a valid move now let's see let's go ahead and code this this method which is actually going to be pretty easy you'll see it's going to literally be one line of code so we want to return the absolute value of the initial dot co minus the final dot co equals equals to 2. now let me explain in other words we're returning if the king moved by two squares and that means that we are castling the only way that the king moves by two squares is because we are castle there is no other possible way that our king is going to move two squares without castling so um now right here we want to check if this instance piece from king so if it's a king we want to check if the move was a castling so if self.castling this is the method that we just created we're going to send initial and final so if this is true if the absolute value of the difference is actually equal to 2 we want to take the difference but this time without the absolute value so final dot co minus initial dot co we without the absolute value because we need the sign for this and then we're going to put rook is going to be equal to peace so our king dot left rook if the difference is less than zero so in other words if the if the king moved to the left else we want to set this variable this rook variable to be equal to the piece.right rook why because we want to move our rook so we're gonna we're gonna do something called recursion here we're gonna call again our move method but this time with the rook and with the final move of our rook so that would be rook.moves in the final position so i know this might be a little bit confusing but let me explain so the first thing that we're doing we're actually moving the kings so the king is actually moving two squares now we're checking if the king actually moved two squares so that means that we have castled now if we castled we need to know if it was a queen side castling or a kingside castle that's why we need this if statement checking if the king was going to the left or to the right and finally we're calling again our move method but this time with the new rook that we get from the if statement and we're sending us a move because remember we have a parameter of move uh we're sending the last move and the last move is actually let me show you uh where is it right here it's actually this one that we added inside of the king moves method so hope that's clear and i think this should work so i'm gonna go ahead and run the file okay so i'm going to do the following i'm going to try the kings the king castling with the white pieces and the queen castle with the black pieces so let's start developing our pieces i'll put this here so we should see okay it's working we we are seeing this as a bullet move so two squares as a valid move and now as you can see i can castle awesome now i'm gonna try the king the queen side castling and it also works awesome now i want to try what happens if i move my king this we shouldn't be possible to to castle so let's see pretty quickly i develop my pieces and as you can see it's not possible so pretty good i'm going to put it here and it's also not possible for the black pieces now i want to move my rook let's see i'm going to develop the pieces um [Music] i'm going to move this this rook also this one this one this one and we should see that the castling is not possible so yeah as you can see it's not possible i'm gonna move this and this one is also not possible so although we haven't moved the king we move the rook and that's why the castling well it's not possible um so pretty good i think we did it it's all working correctly i'm gonna put this i'm gonna mark this as dawn and now we can start coding the pawn ampersand now before we do the pawn unprecedent we're gonna be coding the checks because i think this is a really important thing of our game and i want to start coding this before we code the pawn ampersand i want to have checks first so let me show you what i mean by checks let me run the file so let's suppose we are on the following situation let's say i move my bishop right here and the next player to move is the one with the black pieces now as you can see this pawn right here is pinned right because if i move it obviously my bishop could capture the king now that is not possible so i want to somehow let's do again that situation i want to somehow make this pawn to have no valid moves so the idea what we are going to be coding next in a moment the idea is that when we have this situation or any other situation similar to the one you can see on screen we shouldn't be able to move a pinned piece so in this case this pawn so when i when i drag this pawn we should not see these two squares as valid moves now let's suppose we have now this situation so let's say uh let me think let's say we have this situation so black pieces black king is in check now i want that on this situation the only valid moves for black are the following ones so the knight right here blocking the check or right here blocking the check or this pawn right here blocking the check or the bishop or the queen right here so i want those to be the only valid moves for black so yeah we're gonna start coding that as you may see it's gonna be complex it's a complex code you can try and code it by yourself um but yeah it's quite complex but it's definitely something that we wanna have on our game and once we have finished this we're gonna continue with the pawn amps and moves and we're ready to start coding our ai after we do these two things so okay let's start coding the checks for this we will need to create a new method inside of our board class so i'm going to create it let me think right here i'm going to call it in check so go ahead and type def in check we're going to receive self we're gonna receive a piece and we're gonna receive a move i'm gonna pass it for now so let me actually show you what i wanna do and then we're gonna start coding it so let's say we are on this situation again right here i want to do the following when i start dragging a piece so in this case this pawn when i start dragging this pawn i want to check if after any of these two moves so for example let's say i'm gonna move it right here so i want to check if when i move the pawn to to this square i'm going to check all the white pieces possible moves and if any of the white pieces has its final move with the enemy king as a piece i wanna invalidate this move so let me explain again each time i start dragging a piece i want to check for the other color pieces i want to check if if any of those pieces final move is actually having the king as a final target as a final piece because that will mean that we are in check and i want that specific move to be obviously invalid so i hope it's more clear now we're gonna be coding that right now now in order to do that we need to change some things so if you see for example i'm gonna go here if you see the straight line moves uh we're adding a new move but as you can see the final the final square we're not adding a piece although we can right because we have the param but we're not adding that piece so we're gonna go ahead and add some pieces to the final moves so we're gonna start with a pawn now vertical moves we don't need the like the final piece because we cannot capture on vertical moves so we're gonna do it on diagonal moves so right here go ahead and type final piece and we need to create this variable i'm going to create it right here and final piece is going to be equal to the self dot squares impossible move row impossible move call dot piece that will be the piece so in other words we're taking either none as a piece here or we're actually taking a piece and then with this final piece we want to check if that is actually the king of the other color so that would be a check so hope that's clear i'm actually going to copy this line of code because we're going to paste it a lot of times so in night moves we're going to do the same create a final piece variable and just put it here final piece and actually do you remember i put this comment some time ago that's what we're doing right now so uh final piece so okay knight moves now we're gonna go inside the straight line moves so right here i want to create the final piece the final piece variable and i want to send it here also by the way i don't know why we did this with if statements only but this should be a leaf there is one bug the other time i was playing alone and i and i got a bug that was because of this because of the if statements because this should be a leaf instead of if uh i don't remember the bug actually but it it had a bug so make sure to put the leaf keyword instead of the if one um okay now let's go to the king moves uh okay we actually don't need to add for the king moves because a king cannot give a check obviously [Music] so yeah i think we're ready to code the in check method so right here the first thing we want to do is to create a temp board now we want to create a 10 board because we're going to start moving pieces and we don't want to actually move pieces on our original board that's why we're going to use a temporary board now to do this we need to import a new model which is going to be the copy model and we're going to type copy dot deep copy and we're going to copy our self now remember self is the actual board because we're inside the board class so let's go ahead and import copy import copy and by doing this we're actually copying all the properties of our board so we're literally cloning our board but now we can start working and applying the methods that we need to apply for this algorithm to our temporary board and not to the original one so okay we're gonna do that and also let me create a new variable here we're going to create a temporary piece which is going to be the same the copy dot deep copy our piece because as i told you we want to move the piece inside a board but we actually want to move the temporary piece on the temporary board we don't want to touch our original objects okay once we have these the next thing we want to do is actually move the piece so we're going to type temp board dot move so remember this is a board so we can actually access any of our methods so we're accessing this one the move one and we're gonna move our 10 piece or let me see the params yeah we receive a piece and a move so we're going to receive the 10 piece and the move we're going to send these two params okay so why do we want to move the piece let me actually show you again because i think because i know this is this this might be a little bit confusing we want to move the piece because we want to know if after we move the piece there is a check available that's why we want to move it so yeah in this case in this case let me do it again in this case the piece that we're moving will be the the black pawn that's the piece that we're talking about so we move it and then we want to check if there's actually a check so how do we do that we're gonna do a double for loop as always so for rowing range rows in range rows we're gonna loop our board basically so for calling range calls and we to check if the temporary board remember we are not using the original one dot squares on the position row on the position column dot has enemy piece and we're going to send the piece.color so remember in the in in on our example the piece will be the black pawn so this will be the the black color and we're checking for the white pieces because we want to know if any of the white pieces has a final move with the king on it so we're going to save on a variable p which stands for ps but we can use ps again because we already have it we're going to save inside the p the temp word dot squares in the position row in the position call dot piece so on our example this will be for example the bishop the the piece that's threatening a a check now on our temp board we want a calc let me temp board we want to calculate the moves for that specific piece so the the new piece the bishop on our example and we want to send the p which is the piece the row and the call so we're again calculating all the valid moves for our enemy piece now we can loop those moves so for m we can't use move as we're using it here so for m in p dot moves we want to check if his instance so his instance is instance the move.final dot piece from king i'm gonna explain it on a moment and we're gonna return true if this is true and outside of both of the for loops we wanna return false and that's all we need to do we're missing some things but this is like the algorithm now let me explain you this part this code so as i told you we're calculating the moves for our enemy piece and then we're looping each move and we're seeing if the final square remember a move is an initial and a final square if the final square has a piece which is the king so in other words there is a check so in other words we don't want that move that we're sending to be valid and we're gonna do that in a moment but yeah so hope that's clear i'm gonna show you the last time what we're doing so i'm moving right here uh yeah that should work i'm moving my bishop right here now when i drag these i want to check for each valid move i want to check if after i move it remember we're moving it on our temporary board i want to check for each of our enemy pieces so let's say i'm checking the bishop one if any of the bullet moves so any of the red squares has a king on it and that will be invalid so i think i think it's clear now so okay we have this method now inside of calc moves we need to start doing some things so let's start with our pawn moves now before we add the move we want to check if that move actually leads to a check and if that's true we don't want that move to be to be valid so we want to check let me put a comment check uh potential potential checks so we're gonna do an if statement so if not in check in check with the piece that we are we are analyzing the moves and we need to send a move so the one that we previously created if not in check we can actually add the move now this is not gonna work yet but this is the idea now why would this not work let me show you um so actually i'm gonna show you the following where is it right here so inside in check we're calling calc modes as you can see but also inside calc moves we're calling in check so this will lead to an infinite loop and this will damage our program obviously so we need to do the following let me actually explain it i'm going to create a new param for our calc moves i'm going to call it bull this is gonna have a default value of true so remember calc moves is is being called from two different parts the first one is inside this method so the in-check method but also we're calling the [Music] the calc moves right here when we click a piece let me show you right here we're calling also the calc moves now right here when we are going to move a piece we actually need to check if moving that piece leads to a check so here we're going to have our boot as true we're going to leave it as default now inside of our in check method however we don't want to actually move this piece like i mean think the following right here we're just checking we just want to know if the final piece of of a final move is a king we don't want to actually move that piece so we don't want to so we don't want these these method called moves to call the in-check method again so we're going to send the bull as false i know this is confusing but but it might start to make sense when we code it so now we just want to call our check method if bull so if bull is true and we can do this now if bull is not true we can actually just add the move like literally copy this and paste it right here remember bull equals to true when we're calling the calc moves from the main class but bull is equal to false when we're calling it inside the in check method so in other words when we call it here we don't want to actually check again if there is a check for this piece i know it's confusing but it's it's the way that i thought about it and and as i told you this this is a complex code so so yeah this is literally the line of code that we need and we can actually paste it inside of the other inside the other methods so inside the night moves straight line moves and king moves now i wanna show you let me see if this actually works so i'm gonna show you i'm gonna run the file right here and we should see that it's not possible moving this pawn right here so i'm going to set up the same situation that we had now as soon as i drag this piece or try to drag this piece we should see not no valid moves because this piece is pinned and as you can see it's working awesome it's working as you can see awesome now let me show you uh let me show you another case scenario so let's say we have the bishop right here and now i'm gonna move this pawn here now i'm going to move this bishop here now i'm going to move this pawn right here and now i want to move this bishop here now this pawn shouldn't be able to move also and as you can see it's not possible it's working correctly however the other pawns are able to move as you can see awesome so pretty good now we just need to add that same line of code so this one inside the the other methods so for the diagonal moves we're gonna do the same so instead of appending the new move we're pasting our code and it should work also with diagonal moves and we're ready with our pawn moves now inside the knight moves we're gonna do exactly the same so go ahead and erase this paste the if statement now inside the knight moves however we need to add one small thing and it is the following right here we're going to add a nails and we're going to break it why do we want to do this because we're inside a for loop and basically basically we're telling we're telling this method like okay if the night that we're the specific knight that we are talking about is actually in check so this is what these cells represents if it's actually in check so if we move the knight the our king will be pretty much dead we want to break the loop like we don't want to add any more moves because it's pin we don't want to add moves that's why we need to to put this else break um we're going to do pretty much the same inside the straight line moves so instead of having these we're gonna paste our if else i know there is a lot of repeated code you can actually refactor these methods but i think i'm good with it i don't want to over complicate myself so so yeah we're going to paste it also here leave this break keyword because remember this represents that the that it has an enemy piece so we need to add the move but also we need to break it and right here we want to break it so we are not adding the files and i think we are ready with the straight line moves and the final is the king moves so right here we are going to replace it with this now these are the normal moves for our king and uh we are inside the for loop so we need to do the same so else break now castling moves this is also a different case because we're moving two to two pieces but it's pretty much the same so we're gonna paste the our if evals right here and instead of checking just one piece we want to check two so let me think i'm gonna i'm gonna rename this variable i'm gonna call it move r for move rook so go ahead and rename it and i'm gonna call the move k for move king and inside we can do the following so if not self in check peace which is our king and not self dotting check now instead of p's we're gonna use left rook oh by the way this is move k and this is move r so if by moving both of those pieces we are not in check so if it's all good to go we can append the move to our king so we move k so open new move to king um [Music] we can delete this line of code and also we are going to append append the new move so append new move to rook so yeah left rook add move move r um yeah i think i think this should work now else we want to add it to the king a name of king but also to the left rook again i know this is a lot of repeating code but yeah you can try to refactor it yourself let's see we need to do the same with the this is the coin castling we need to do the same with the king castling so i'm actually copying these i'm erasing these two lines of code and i'm pasting the code right here so it will be let's change the name for move r and move k again um let me see so if not in check the king and if nothing check the left this will be right right rook move r we're going to take the right rook and add the move r and we're gonna add the move k to the king and this should be right brook add move move r and this should be move k so we need to change that also right here i think this should work so let me think if we're missing something i think we're good to go i'm not sure but okay let's see so yeah hope it's clear we what we just coded i know it's confusing this is actually one of the most complex things about this game checking the checks so let's see i'm gonna have a new a new setup so it's working we are able to move the pieces so try to test it try to test it it should work as normal so try to test it i think it's working now i'm gonna put in check the the black king now we should be able to only move this pawn to this square this knight to this square or this one this bishop to this square or the queen to this square there are no more possible moves so i'm gonna start with the pawn awesome we can't move it nice we can move the knight we can obviously not move the bishop we can't move this pawn we can't move this one okay i think it's working now i'm gonna i'm gonna drag the the knight awesome this is awesome guys it's working as you can see this is not a valid move because we're in check so we need to block it this is valid awesome this is also valid this is also valid and this is also valid king obviously can't move nice so this is working i'm gonna put the knight here now if i move my bishop this knight is not going to have any possible moves because it's pinned however if i protect it and i move my i don't know let's say i move my bishop here i can start moving my knight so it's working correctly guys we did it we basically did the hardest part of this tutorial and we were just missing the upon ampersand which is actually not that hard so if you're confused about it as i always recommend grab a piece of paper and try to like literally draw it and try to think it your way if you're thinking on another solution remember this is not the only solution this is how i thought about it but you can think your own solution try to do it i think we can mark this as tone this is a big step big big step awesome now we're just missing the pawn amp which is actually not that hard so super cool guys we're almost finishing the player versus player game mode and now we can start coding the final thing of our game which is the pawn on person so i'm gonna actually explain for those of you who don't know what is the ampersand move so let's say let me think how can we do this okay let's say i move this pawn right here this one right here this one right here this one then i move my knight whatever now i want you to see this side of the board this is called the ampersand when i move oh i actually move my knight i had to move my palm okay whatever um okay as you can see we have two valid moves obviously now ampersand is applied when our pawn the pawn that we are moving advanced two squares so if we put it right here now if there's any pawn on his row i'm sorry not row but on the adjacent squares of the rose so either right here or right here and by pawn i mean enemy pawn this pawn the enemy pawn should be able to capture like this so should be able to advance diagonally and actually capture these these this pawn so something like this um so yeah we we're gonna start coding that and it shouldn't be that hard so hope it's clear what emphasis on these if if it's not clear make sure to search it okay before before we start coding it let me actually show you what we're gonna like what's the idea what how how did i thought about it so again let's say we're in this situation um so yeah let's say i move this pawn right here so what i want to do is the following i want to add a new property to our pawn class called ampersand now each time we move upon two squares so for example this one i want to set the property ampersand to true now by the way i didn't talk about this but to capture ampersand you need to do it on the same turn that the pawn actually advanced two squares so okay let's go back to what i was talking about so i want to add a new property now each time a pawn moves two squares i want to set that property to true so ampersand to true and now when we are moving upon so this is going to be inside of our pawn moves method i want to check the following for example let's say i do the following move i want to check for this pawn that we're dragging if there's any pawns adjacent to me on my same row so in this case there are two pawns now i want to check if their property ampersand is equal to true and if that's true i want to add the row below them in the case of the black pieces the white pieces will be different the row below them as a valid move so right here now remember these these would have a an ampersand equal to false because we are not on the same round however this pawn we are on the same round just just see that the last move was this one so the the property on percent of this pawn is going to be equal to 2 so i will be able to move it right here now for the case of the white ones the white pieces is going to be pretty much the same we're going to check our adjacent pawns of our row i want to check if any of those pawns have a property of ampersand equal to true and we can capture subtracting a row from from our white pawn row so hope that's clear also and we can start coding it so let me actually start by creating the property so right here on the pawn class and and you may start seeing why inheritance is important because i mean pawn has some properties that we don't want the other classes to to have like direction and amplitude so i'm going to call it on poisson i think it's spelled like these actually don't know but i think this is going to be equal to false as default uh now let me see let me see inside of our board class inside of our pawn moves so inside this method let me actually close all other methods um create at pieces i'm going to close all in check castling check promotion bullet move okay whatever i'm going to open the pawn moves okay so inside of here i want to check if the oh my bad we're not gonna be coding that in here we're gonna be coding it inside the move the move method so do you remember what we did with the with the castling thing like we checked if a king moved had an absolute value of the move greater than than one which is in other words castling we're gonna do pretty much the same for our pawn so right here we're gonna put upon ampersand and um let me see we want to check if this instance oh actually we can do this inside here so we can have a palm promotion and also we can have the pawn ampersand yeah so then the pawn amp sound will be if self dot amposant we're going to create this method so percent we're gonna send initial initial and final we're gonna do pretty much the same as we did with the castling thing so if self dot ampersand i want to pass it now else i want to do the i want to check the palm promotion so this will be inside here okay so amplisant we can code it right below the the castling it's pretty much the same it's actually the same you can actually call this method the same like you can name this method like castling or or ampersand because it's gonna be the same so ampersand we're going to receive self as parameter an initial value and a final and we want to return the absolute value of the initial in this case will be row so be careful initial row minus the final row yeah because castling is is is for the column but ampersand will happen on the row now if this is equal to 2 we want to change the property of our pawn so right here we want to do the following we want to piece dot amposant equal to true in other words it moved to a square so so we want to set the property to true i'm actually going to print well right here sorry right here i want to print something like pawn moved to squares i'm gonna i'm gonna see if it actually works so i'm gonna run the file and we should see something printed on console uh when we moved upon by two squares so if i move it by one it says pawn move to squares okay something's going on yeah i know what's happening i know what's happening the thing is that we're calling let me put the class we're calling the the move method inside of our inside of our in check method do you remember so so yeah it's normal we see we we see a printing statement for like for every pawn but this should be printed twice when we move it two rows so if i move it one it's printed one time but if i move it twice i move it twice you can see it's printed twice so it's actually working uh yeah it's actually working i'm gonna move one for this yeah so let's say we have we have five printing statements now we have seven as you can see so yeah i think it's working so we're setting the property to true now this property we need to set it to false at the end of the of the torn so okay i'm gonna i'm gonna delete this and now we can go to our pawn moves method right here and let me see we have the vertical moves so yeah right here yeah i think okay so we have the vertical moves right here and we have the diagonal ones i actually want to create a new section for the ampersand so ampersand amazon moves so let's see i'm going to create a row so r because we're already using rows so r is going to be equal to uh let me see so the row that we want to check for the ampersands in the case of the white pieces is this one so this will be those zero one two three the third one and the fourth for the black ones so three and four so r is gonna be equal to three if the piece that color is equal to white else four for the black ones now we wanna check if we're going to call our static method in range so in range and we're going to send let me see let me see we're going to send the column so we're going to send the piece dot column plus one we're gonna we're gonna check the if there is any point to the right or any pawn so ps dot co to the left oh but we actually want to divide this so we're going to do it first for the left one so left ampersand this will be like left ampersand so if squaring range but also if the piece dot row is equal to r so if we are on the route for ampersand now if this is true remember this is left ampersand we want to check if those self dot squares on d let me think about it those this will be r or actually row we can use row for these and here we're gonna put coal minus one if this dot has piece has piece has enemy piece actually because we want it to be an enemy piece has enemy piece and we're gonna send our piece that color now if it has an enemy piece we want to check if that piece so if naturally i'm going to create a new piece which is going to be equal to self that squares on the row on the column minus one dot piece now we want to check if is instance p of pawn and now we want to check if the p dot um how do we call it ampersand the ampersand ampersand attribute on percent do we call it percent or passion i don't know how how do you spell these i'm sorry so let me see i think i call it percent with a yeah sound so if ampersand we can add a valid move which is going to be the following one and i'm actually going to copy these and we also need to check the checks so i'm actually going to copy all these now remember you can you can actually refactor a lot of code right here so i'm going to paste it and i'm going to hit some tabs here okay let's think about it the initial as always it's going to be row and call now the final final piece why do we need final piece yeah we need the final piece it's gonna be the the self that squares not on possible row it's gonna be actually on on our same row but column minus one this will be the final piece and the final is going to be equal to a new square but this is going to be and this depends this depends if we're talking about white or black so we need to create a new variable i'm gonna call it final so f r for final r this is gonna be equal to let me actually think this is quite confusing this is going to be equal to 2 if the piece.color piece.color equals white else i think five so we're gonna put a final row right here hope it's clear final row and move call this will be the call minus one um we're gonna send the final piece awesome so okay we have the move we create a new move now potential checks if bull if not self in check piece and move uh is it a piece or p now it has to be ps because p is the one that we're gonna capture oh by the way the final piece is just p what's going on yeah this is gonna be p that's it we don't need to create a new any variable okay so we're gonna we're gonna call the in-check for our piece so our current pawn the one that's capturing the move uh so if not in check we wanna add that move else we wanna add that move awesome uh we're missing some things but this should work so this is left ampersand uh let me actually test it let's hope this works okay pawn object has no attribute call line 142 board 142. so right here yeah my bad this is not pizza this is actually just call minus one and row um yeah i'm gonna run the file again okay it's working so we should see let me see this okay this is working i'm gonna put this pawn right here now we should see we should see as a valid move in this case we we should see this as a ballot move but we are not we haven't coded yet the the well capturing that piece okay so it's not working i'm going to test the black ones it's also not working um yeah we're definitely having a problem so when i check the code because this might be a difficult bug to see and i'm gonna be right back okay guys i'm back and you're definitely not gonna believe my bug it was oh my god it was so stupid i mean you should not have any error it was my fault i'm to show you what is it but let me show you oh i screw it um let me show you so right here as you can see it's working correctly our left ampersand now as you can see obviously we're not capturing that piece but we're adding the bullet move so that's a big a big step so i'm gonna show you the bug it was so stupid i i put here on percent with an e instead of an a and that's why it wasn't working so make sure you spell it correctly and you shouldn't have any problems so let's go i'm actually going to close some of these classes we're pretty much finished now we're gonna implement the right one so this is the left one oh my god it's too much code you can refactor these i know uh i'm gonna copy these so i'm gonna paste it here we're gonna change some names so right ampersand will be plus one so first of all if it's in range and row equals to r this this shouldn't change yeah this is not going to change uh let's see we need we need to change all coal minus one to call plus one so this will be called plus one has enemy piece piece that caller p equals self that scores yep if his instance yup yup i'm actually deleting these print statements this was for debugging now we create the new one yup and i think this is pretty much the same so this should work also with the with the with the right ampersand so i'm moving this one now moving this one and as you can see it's working correctly awesome now we're having the problem that if i move these this should be the one with the ampersand now as you can see i have both ample sense and this shouldn't be possible because we should have only the ampersand for the the last move so we're gonna fix that and we we're also going to implement the the capture thing which is actually pretty easy so yeah we have we have implemented the move the valid move and we can actually start capturing so right here where it says console board move update remember we're putting the initial equal to none and the final equal to b we're gonna do pretty much the same when we have an ampersand an ampersand move so we're going to create a new method called let's see we can call it ampersand and i actually spelled also this one incorrectly oh my god let me change that it's on percent so okay if self dot amposant we're gonna receive we're gonna send the self yeah we need to call it different so it will be self.ampersandcapture so if the capture if if there is an ampersand capture we can console board update so this will be let me put a comment ampousant ampersand capture capture we can update it and it should be um not the initial call but the initial call minus or plus a difference depending if it was a left or right ampersand because so we're gonna create a variable here i'm gonna call it beef and this is gonna be equal let me think it will be initial now final final dot column minus initial dot column or yeah so if this is negative so if diff now i'm actually going to put the like d row like the delta row this is delta rho and this is going to be equal to well i think actually we don't need this we can just put initial plus plus difference i'm not sure i want you to know i have encoded this so i don't know i don't know if it's correct so okay initial call plus div this should work uh dot p is equals to none and the final row final called psp's equals to the piece okay now we need to we need to code these ones so ampersand capture on person capture but then we code it didn't we code it okay we need to code it so def ampersand ampersand capture and you know i'm so sorry but this is much easier if we do it inside the the move method so we just want to check if we moved our pawn diagonally and also if the final square is empty that's all we we want to do so we actually going to create this diff variable outside and we want to check if div is different from zero because you know if we're moving straight forward this is gonna be equal to zero so we wanna check if we are diagonal if we move diagonal now if it's different from zero and the final dot not not actually the final the self dot squares on the final row and the final call that is empty that is empty and that's all we need to do so this should work this should work let's hope it works i think we're amazing yeah we're missing also the the changing the property of of the pawns let's see i'm gonna test it for both left and right so i'm gonna start with the left one right here we should see okay we need to fix this but we should see when we when we move diagonal we should see this pawn disappear let's hope okay it's not working oh my god why is this again i'm i'm gonna quickly debug it myself and and i'm gonna i'm gonna tell you the well what was the bug because i don't wanna i don't wanna waste your time like searching for the bug so i'll be right back okay guys i'm back it was actually not that hard so i'm gonna i'm gonna show you what i did uh i'm actually going to to to show you from the beginning so let me undo all the things i did and that's it okay so we had this code the first thing i i noticed is that we need to do this just if we are upon like if we're moving upon so i pretty much just copy and paste those lines of code inside the if statement that verifies if we're talking about a pawn now the second thing i noticed and this was the the main bug is that we were asking if the final row and final call is empty now it will obviously not be empty because we're setting it right here you know like look we're setting the final the final row and final call square to be equal to our own piece so this will never enter the if statement now what i did was to create a new variable which i called ampersand empty and this is going to be equal to the final square dot is empty but we need to do this before we set it equal to our own piece obviously and now we want to check if ampersand empty so if difference is different from zero and ampersand empty and this should work now we're missing what i told you about the property and we're also missing the sound like we're not displaying the the the sound the capture sound okay okay so as you can see we have our three options this one as you know is incorrect um and as you can see i captured the piece awesome and we don't have any piece here we set it equal to none so pretty cool we're gonna do the sound thing right now so we're actually doing we're we're actually playing the sound inside of our main class right here it is capture equals board squares release row release call dot has piece so this is like a normal capture normal normal capture as ampersand is a little bit different i'm gonna actually play the sound inside of our move method it's actually easy so right here so this this if statement is the one confirming that that there was an actual amplison capture so we can we can easily do the following we can create a new sound so i'm going to call it sound equals a new sound uh we need to import sound so from sound import sound sound this receives a path so path is gonna be oh we need the os model also so right here import os uh let's see let's see so right here i want to do os dot path dot join i'm actually going to put this here and i want to put this here and the pad will be the following will be our our assets slash sounds i think we call it capture yep so sounds slash capture dot what and i think this should work okay we need to play it also so sound dark play and this should work so i'm going to test it pretty quickly uh no file okay so i had a problem with my path let me actually confirm the path where did we create a the previous sound so in config right here oh okay we need to access the the assets directly so my bad i need to access like this as it sounds capture what so i'm gonna test it um what okay it's actually oh my god it's actually playing before the before we do the the ampersand so and i know why why is that it's because we're calling it from the in check do you remember so i'm going to create a new param so i don't want to i don't want to waste more time in this so i'm going to add a new param like testing this is equal to false and when we send it so right here we're gonna send the move testing this is gonna be equal to true testing and now we just wanna play the sound so right here if not testing i know this is not actually it's not actually good code but but it will solve our problem um okay if not testing we want to create and play it and this should work so pretty quickly i'm gonna move my pawns right here amazon we got it awesome okay um i'm going to fix the property thing and then we can test all the possible scenarios for our ampersand and um and we finish we pretty much finished so okay let's think about it we have a last move we can use that one i think we're gonna use that for for setting the property back to false so let's see let's see where where was it that we were setting the property to true i don't remember right here so i think we want to create we're going to create a new method right here let's call it set false on percent we're gonna receive self we're going to basically loop for rolling range rows and four calling range calls we want to check if the self.scores on the row comma call dot has piece actually we can we can check directly if this instance if it's instance this is a square piece from pawn and if this is true we want to check if the remember we have a self.last move so last move should have a final but before that we want to actually check if we have a last move so if self dot last move then if self dot last move dot final dot piece is not equal to this one we can take that one so actually let me create a piece variable it's gonna be dad actually pawn let's call it pawn and if this is not the pawn we are gonna set the pawn dot ampersand to be equal to false so yeah we're basically checking if it's not though like the final move if this is true we can set that upon to false okay so we now need to call this method inside of our let me think i think it's the main function yeah so we're gonna call it when we have a valid move so right here we can do something this has to be after we move the piece because we want the board to have the the well the last move so we can call the board dot set false ampersand and this should work this should work uh let's see let's do this one this one this one okay it's actually deleting both ampersands so let me actually debug it and i'm gonna i'm gonna be right back okay guys so i'm back um actually fix it but we need to change some things so go ahead and delete the the method that we previously coded now i called this one set true ampersand and we're gonna receive the piece or the pawn that we're going to set well the outputs are equal to true now this inside of our main class is going to look something like this so board that said true ampersand with the dragger piece this is inside the click release so you should send it to this and we want to do the following uh it was actually pretty close so if it's instance the piece of pawn if it's not instance we we want to check first that so if it's not a pawn we can return because we don't want to do anything remember this is the this is the piece that we previously moved uh then we want to loop so we want to do a for loop a double for loop we want to check if the cell that scores at the row comma call ah dot p's so if that piece is an instance of pawn want to set its um its ambassador to false so we are piece equals false here we are setting we are literally setting all ampersands to false so at the end we want to just take our piece the one that that we moved and we know it's already a pawn because we already checked it so please dot ampersand equals to true and that's it now we can actually delete the [Music] the thing that we had right here so we can delete this one actually and go ahead and delete also the ampersand so this one we don't need it anymore because we're setting true the opposite on the right here now by the way this is not this is not a piece but a piece that ampersand so make sure you are setting the on position not the piece obviously um okay and this should work we have we should have our game ready our pvp ready the final version so okay it's working now we should see the amp sound just for this side awesome now if i try to move it here obviously i can't but if i move it here i can capture that piece with ampersand nice so yeah guys we have finished the ampersand and we can mark this as done and now we have finally finished our player versus player game mode now i i found a bug really really small bug and it happens when i try to castle so i'm gonna do it quickly uh i was in check i couldn't move um so i'm gonna move my knight here and i'm gonna try to castle and as you can see i have an error this is pretty simple we need to do the same thing that we did here we want to put not testing inside of this if so we want to put if self cancelling and not testing and i'm gonna run the file and you'll see this is going to work now there there there is gonna be a lot of bugs i'm sure there are a lot of bugs but it's quite difficult for me to to like to test all all possible scenarios all possible chess scenarios in order to find those bugs so if you find a bug make sure to leave it on the comments below and i'll try to help you but also you can try yourself to all to solve it now as you can see the the castling thing is working so we literally now finished our player versus player game mode so in the next video we're gonna be coding the ai we're gonna be coding from scratch our own ai to play against and we should have by the end of the other video we should have both player versus player and ai game modes ready so we're literally having our game ready and as i told you we should have some bugs i'm sure we have some bugs but i mean general in general the game is working so i'm pretty i'm pretty fine with it so yeah i hope you liked this video i hope you will learn something new and i'll see you on the next part of this tutorial to start coding our own artificial intelligence to play against thanks for watching and i'll see you in the next video you