# I created a PERFECT minesweeper AI

## In this AI video ...

With all my AI creations, I’ve never actually claimed that any of the Moreny good never mind perfect. Most of the time they’re borderline useless, but this time is different because I actually believe that the AI I have created is the best mine sweeper player ever. Of course other AI’s might have been created which are as good as my AI, but no player can surpass my child because he is mathematically and absolutely perfect. I think probably not, but I think it’s close. Ok, let me show you the beast, prepare yourself. This is him playing mine sweeper at an expert level. Shit. Ok, well I never claimed it would always win, but I am serious about the perfect thing. The problem with mine sweeper is that at heart it’s a game of luck. Sure there’s some problem solving, but even the best player can get stuck with a 50% chance of losing. So yes, sometimes my baby fails, but it always performs the best moves that it can. It’s worth pointing out that this AI is different to any previous AI’s. This guy is hard coded, that means no Nate, no neural networks, no genetic algorithm, just plain old coding. If you don’t care about the technical side, you can skip forward to watch this AI kick some ass. The first few moves of random, because well there’s not much else we can do. After that I started by programming in some logic. The first logical rule was, if a tile has the same amount of hidden squares around it, as unflagged bombs remaining around it, then all the hidden tiles are bombs. This really is some pretty basic stuff. Consider this example. The two has only one bomb flag near it, and only one hidden space remaining, so this hidden space must be a bomb. The second rule is, if a tile has the same amount of flags around it as the number on the square, then all remaining hidden tiles around it are safe. The square where the one has one bomb flagged around it, so there cannot be any more bombs near it, thus the rest are safe. Now with these two rules in place, the AI can perform pretty well, but has to guess way too much, so let’s add some more complex logic. Consider the following situation. With our current logical rules, the AI cannot solve it, but it can be easily solved. Note that the three only needs one more flag around it, that means that the two squares near it can’t both be flagged, because then the total would be real up to 4 and that’s just not how mind-swip it works. So now the two spaces are what I like to call linked. Between them, they contain exactly one bomb, so you can consider them as one square. Now this means when we look at the two, if those two spaces contain exactly one bomb, it means that the remaining square near two must also be a bomb, to bring the total up to two. Now with our old rules, we can finish this example. This is much harder to implement than all the other rules, but with that in place, the AI can handle many more situations. We could keep hard coding logical rules for ages, and then we still couldn’t be sure that we got them all. What we need to do is combine all possible rules into a single rule. Is such a rule possible, well obviously otherwise I wouldn’t bring it up. Anyway, what you need to remember is mind-swip, but is a game of luck, and what that means is we’re going to have to use a little bit of math and probability. Wait, don’t click away. We can make this interesting. Math and probability. Oh yeah. If we can calculate the probability of each square being a bomb, and then clicking the one with the smallest chance of exploding in our faces, then we would be set. We can use this method to simulate all possible logical rules, because any spaces that are safe have a 0% chance of a bomb, and any spaces which need to be flagged have a 100% chance of being a bomb. So we just need to calculate the probability of each square being a bomb, and then she will achieve perfection. Now how can we do this? Well it’s simple really. All we need to do is generate all possible arrangements of bombs in the remaining spaces, and then count the number of arrangements in which a bomb is placed on that certain space. Though we divide that number by the total number of arrangements possible, and voila, we get the probability of a bomb being in this space. And with that we have created the perfect mind-sweeper player. This can replicate any logical method for finding bombs and safe spaces, as well as tell us which squares are the best odds of not being a bomb. By date breath, that’s the end of the math-heavy side of the video. Now you can just sit back, relax, and watch as we crush some mind-sweeper. Okay this is a hardly a challenge, let’s see how big a game this AI can take. I hope you enjoyed watching my AI, like if you did, subscribe if you did, and I’ll see you next time.