# Python Checkers AI Tutorial Part 2 – Implementation &amp; Visualization (Minimax)

we’re trying to maximize if min players are is max player’s fault we’re trying to minimize again that means we technically could use this ai to have the ai play against each other white and red which maybe we’ll show later next we have game game is the actual game object that we’re going to get right from here in this main.pi file we’re going to pass that to the algorithm and what that will do is then strip out anything it needs from that and potentially draw some stuff on the screen if we want to do some visualization all right so the first thing to do inside of the mini max algorithm is to determine what our depth is now remember that we only evaluate a position when we reach the end of the tree so in our previous example we had the root node and then we had two side nodes right and then or two or three nodes after that and then after all those nodes we had other nodes right so it went like the red move or it went the white move and then the red move and we evaluated at the red move and bubbled up those evaluations upwards so we’re only going to actually make an evaluation if the depth of our tree is at zero so if we’re looking at the last node essentially in the branch that we’re on so i’m going to say if depth equals equal 0 or position dot winner now remember that if we look at board we have this winner method that i put somewhere maybe at the top i think i had winner yeah winner is right here so we can check if someone has won the game so essentially if uh position.winner we can just say does not equal none then what we’re going to do is return position dot evaluate and position now i’ll talk about why we’re also going to return position afterwards but the point of this is essentially to say okay if the depth is zero so we’re at the last node in the tree meaning it doesn’t we’re not going any further than this let’s get the actual evaluation of that position and then let’s return this current position alongside of its evaluation so we know what evaluation goes with what position now when i say or position.winner does not equal none what i’m trying to say is well if we’ve won the game then the game is over there’s no need to continue evaluating right so that’s what this is saying all right so now i’m going to say if max underscore player and else so the reason for this you can kind of guess is because well if it is the max player then we’re going to do something different maximize the score whereas if it’s the min player we’re going to minimize so these two blocks here the if and else will be similar we can actually copy and paste them and change some things but it’s just because max and min so the first thing i’m going to say is the max evaluation that i’ve seen so far is negative infinity reason for this is because whenever we check a new position we’re going to determine which one is the best and well if we don’t haven’t checked anything yet then currently the best that we’ve seen is negative infinity right meaning if we see anything else then as a score higher than negative infinity which they all will we’ll consider that the current best until something else comes along and beats it next i’m going to say best underscore path equals none you can guess what or actually instead of best path let’s say best move equals none you can guess what that means that’s going to store the best move that we could make and then we’re going to say 4 move in get all moves which i’m going to write in a second this is going to be a function what this function will do is get all of the possible moves for our current position so we’ll pass it the position we’ll pass it actually the color that we want to consider which in this case is going to be white because the maximizing player is going to be the white player we’re just going to hard code that in there and we will pass it the game in case again we want to draw anything for visualization purposes so actually rather than waiting before we write this i’m going to write this now just so that the get all moves function just that it’s clear and that we don’t get too confused and then go back to it so i’m going to say define get underscore all underscore moves what this is going to take is a position it’s going to take a color and it’s going to take the game now the point of this function is to get all of the possible moves that we could make from the current position so that is why again within this board class we just wrote that method that got all moves i forget where i put it but i think it was like yeah uh sorry get all pieces so that in inside of here we can get all of the pieces and then we can check all of the moves for those pieces and add those into a moves list so i’m going to say moves equals a blank list moves what this list is going to store is it’s going to store the new board so it’s going to look like this board and piece essentially it’s going to say okay if we move this piece this is what the new board will look like if we move it to a certain position so this is the piece and this is the new board and then we’ll have a bunch of these that’ll say okay this is you know another new board so new board and this is a piece that we

move to get that new board so moves equals a blank list i’m going to say for piece in board dot get all moves or not get all moves sorry get all pieces and we just need to pass it the color so we’ll simply pass it color like that and what this will do is get all of the pieces of that certain color now i’m going to say valid underscore moves equals board dot get all valid moves or get valid underscore moves and we’ll just pass it the piece so hopefully this is making sense loop through all the pieces in the board there the certain color get all of the valid moves for that piece on the board and then say for move comma skip which i’ll explain in a second invalid moves dot items like that now we’re going to do something so validmoves.items is saying okay i want to loop through since this is a dictionary all of the items now the items is a key value pair right and the key value pair is going to say row column colon and then it’s going to have a list of all of the row call or sorry of all of the pieces that we would skip if we move to this row column so the way that i’ve kind of done this is that the keys are going to say okay if we move this piece to this position to this row column position we will skip over and need to remove these pieces that say if i jumped over another piece then there would be pieces or a piece in this list to tell me which one i need to remove if i take this move most of these won’t have anything because most of the moves we make won’t be jumping over someone else but they potentially could be right so that’s what we need to consider alright so four moves skip invalid move.items move again is that row column tuple skip is a list of the pieces we would skip what i’m going to say is temporary board equals deep copy board so i need to do this and sorry um instead of we’re going to call instead of position i’m going to call this board it’s really the same thing but board i think is just more clear here so we’ll say board but i’m going to deep copy the board because i’m going to be modifying this board in order to determine when i move to this position what the new board will look like so i don’t want to modify the same board i want to make a copy of it every single time so that the next piece that i consider has a fresh board that it could potentially move to um like hopefully that makes sense but we just can’t have it be the same board otherwise every time we move a piece it will move on the same board which means when we’re running this algorithm but board will permanently move to all of the things that we’re just trying out right which obviously we don’t want so now i’m going to say new board equals and this is going to be a new method which we’ll write in a second simulate underscore move of peace move and then one second here we need to pass the temp board game and skip now i’ll write this method in just a second but imagine that what this is going to do is or we haven’t written this yet but this function sorry imagine what this is going to do is take the piece take the move that we want to make take the temporary board and then make that move on it and return to us the new board that’s what it’s doing it’s telling us okay if you make this move the new board will look like this and then we’re going to store that and we’re going to put that in our moves list so we’re going to say all right moves dot append the new board like that and then comma the piece so we’re gonna say all right if this piece moves to whatever move it is then the new board will look like this and then we’ll score that board from inside of here and we’ll see which board is the is the best and we’ll return that and that will be the board that we then set and like technically the move that we make so i hope that makes sense but here i’m going to say return moves and now i’m going to write the function for simulate move so to find define simulate underscore move like that this is going to take a piece it’s going to take a move it’s going to take a board it’s going to take game and it’s going to take skip all right so what we’re going to do in here is simply just say if i can look at here board dot move so what i’m going to do in here is now move the piece now if i go to the board method here and i have a look at move it takes a piece and a row and column we want to move it to so what i’m going to say is board dot move piece and we’re going to move that to the move 0 move 1. now the reason i have to do this is just because this is a tuple so i just need to split it up and make it 0 and 1. so this means we’ll get the row and then we’ll get the column so we’ll move this piece to that row and column and then i’ll say if skip