tic tac toe ai This is a topic that many people are looking for. bluevelvetrestaurant.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, bluevelvetrestaurant.com would like to introduce to you Coding Challenge 154: Tic Tac Toe AI with Minimax Algorithm. Following along are instructions in the video below:
In this challenge I take the Tic Tac Toe game from coding challenge #149 and add an AI opponent for a human player by implementing the Minimax algorithm.
? Coding Challenge #149: Tic Tac Toe: https://youtu.be/GTWrWM1UsnA
Links discussed in this video:
? Minimax: https://en.wikipedia.org/wiki/Minimax
? Minimax Algorithm in Game Theory: https://www.geeksforgeeks.org/minimax…
? Algorithms Explained – minimax and alpha-beta pruning: https://youtu.be/l-hh51ncgDI
? Code of Conduct: https://github.com/CodingTrain/Code-o…
?Help us caption and translate: http://www.youtube.com/timedtext_cs_p…
?Suggest Topics: https://github.com/CodingTrain/Rainbo…
?Share your contribution: https://thecodingtrain.com/Guides/com…
?Coding Challenges: https://www.youtube.com/playlist?list…
?Intro to Programming: https://www.youtube.com/playlist?list…
? p5.js: https://p5js.org
? Processing: https://processing.org
Video editing by Mathieu Blanchette.
#ai #minimax #game
Hello and welcome to a coding challenge tic tac toe. Oh. With the minimax algorithm algorithm that’s why i’m here because i made this other coding challenge tic toe.
Where i created a kind of a big. It was kind of a mess if i’m being perfectly honest. But i made a working version of the tic tac toe game that just played with two players picking random spots.
So since then you can check one of my live streams. If you want to find where i did this i made some adjustments to it so that i could as a human being play the game so right now. I’m going to play the random computer picker.
I’m going to go here. And then i’m going to block x. And then i’m going to go here.
And ha ha. I win o wins. So what i have the adjustment that i made is that i added a mouse pressed function where i find where did i click and i put my human variable.
Which is the letter o onto. The board and then i called next turn. Where next turn picks a random spot in the board.
And makes that the ai spot or x’s spot. So. The whole point of this video is for me to implement something called minimax.
Which is an algorithm a search algorithm. If you will to find the optimal next move for the ai. I mean it could find it for myself.
And then i could implement it. But the idea is i want this player. The computer player to beat me at the game or at least tie to play a perfect game.
If you want to learn more about the minimax algorithm. I would suggest two resources that you can look at that i actually looked at before beginning this coding challenge. I haven’t programmed this before.
But i watched this video by sebastian lague that explains the minimax algorithm also something called alpha beta pruning. Which i’m not going to implement. But could be a good exercise next step for you and i also found this article on the geeksforgeeksorg website.
Which is three part series about the minimax algorithm and how to apply it to tic tac toe. So those resources are probably your best bet for learning about the theory of the minimax algorithm and how it can be applied to a wide variety of scenarios. But i’m going to look at the specifics of tic tac toe.
Which is a great starting example because it’s a simple enough game that i can actually diagram out all of the possibilities. Which even if that’s computationally possible. It’s very hard to diagram and there’s lots of scenarios.
Where you couldn’t even compute. All of the possible outcomes and chess is an example of that so minimax is typically visualized as a tree so the root of the tree is the current state of the game board music playing so i’ve drawn a configuration here of the tic tac toe board midway through the game. Because i don’t want to start at the beginning.
Where there’s so many possibilities that i couldn’t diagram so right now. It’s x’s turn and x has three possible moves. So i could express that in a tree diagram as move one move two and move three so let’s take a look at what i mean by that music playing i’m going to use a blue marker.
So you can see the new moves. So let’s say one possible move is x going. Here.
Let me diagram out the next. Two possible moves music playing another possible. Move is x.
Going. Here. Music.
Playing 03 07. 03. 10.
03. 12. 03.
16. 03. 19.
03. 24. 03.
26. 03. 28.
03. 30. 03.
32. 03. 34.
03. 38. O.
Could go. Here or o. Could go.
Here. Oh could go here or o. Could go here and look at these in this case.
And this case. O has won remember we’re trying to solve for the optimal move for x x. Is making the first turn so this means.
I can mark these were o wins as red as like those are bad outcomes. So while these are not terminal states. There’s only one move possible for x.
So i could draw an arrow and put that down here. But ultimately i can just consider this as x has to go here. So these you could consider as terminal and in which case x will win.
Now you see that i visualized. All the possible outcomes of the tic tac toe game based on this starting configuration. So how does x pick.
Which path to go down. Where we can see here that the goal is x to win the game to get to here here or there how does it do that and the way that it does that by the way spoiler alert. This is the move it should pick it should just win instantly.
But the idea of the minimax algorithm is if we could score every possible outcome those scores can ripple back up the tree and help x decide which way to go to find the highest possible score the nice thing about tic tac. Toe is there’s not a range of scores. The endpoints are either you won you lost or you tied i.
Could say if x wins it.’s a score of plus 1 . If o wins.
It’s a score of negative. 1. And if it’s a tie.
It’s a score of 0. So in other words. We can give this a score of plus 1 this.
A score of plus 1 . This a score of negative. 1.
This a score of negative. 1. And this a score of plus 1.
But we’ve got an issue. I’m trying to pick between this option this option and this option these two don’t have a score. How do i pick their score.
Well. I can pick their score based on which one of these would be picked. But who’s picking these this is why the algorithm is called minimax because we have two players each trying to make an optimal score.
X. Is what’s known as the maximizing player so to go from here to here. It’s x’s turn and x is maximizing.
But here. It’s now o’s turn o wants to win x can assume that o is going to play their optimal. Move now you could have a situation.
Where your player doesn’t play optimally and that’s something you might have to consider in terms of game theory. But in this case of the algorithm. We can assume that o is going to make the best possible move.
I mean if it makes a worse move that’s only better for us anyway. So it’s all going to work out so o is what’s referred to as the minimizing player. The minimizing player so o has the option of picking negative.
1 or plus 1 negative. 1 or plus. 1.
Which one is o going to pick as the minimizing player. The option to win. So we can then take the best outcome for o.
The minimizing outcome for o and pass that back up here’s the score. And the same thing here now x. Is the maximizing player.
Which one of these is it going to pick negative 1 plus 1 or negative 1 these are negative. 1. Because even though it could win here if x goes this way o is going to win if x goes this way o.
Is going to win if o plays optimally. So this is the way to go this scenario that i picked might not be the best demonstration of the idea in fact there’s no ties here. There’s only two turns being played so maybe what you might want to do is pause this video and do the same exact diagram.
But have x and o. With two starting positions and see if you can diagram that whole thing out yourself. So how do we implement this in code.
So that your first clue should be the fact that this is a tree diagram anytime you see a tree diagram typically speaking a recursive algorithm a function that calls itself is something that will apply and this case is no different i want to call minimax on this board position try. And then loop through all the possible moves and call minimax on each one of these board positions loop through all the possible moves. If i ever get to an end point return.
The score and have that score backtrack up or ripple back up the chain. So let’s see if we can write the code to do this so. This here is the place where i want to write the new code currently a move is chosen by picking a random available spot.
And i no longer want to do that i’m actually going to change the name of this function to best move and i don’t need to worry about this. What’s available. I just want to actually look at all the possible spots.
So basically i just want to know is the spot available. If the spot is available. I want to try going there so i’m going to say board ij is the ai player and then i want to call minimax on the board.
Why do i want to call minimax because i want to get what is the score. I want minimax with return to me the score for this particular move now. It’s going to have to recursively check these two spots.
But we’ll get there we’re getting there ultimately though i need to figure out which one was the best so i need to keep track by saying like the best score is negative infinity. If score is greater than the best score. Then that’s the new best score.
And the best move is this particular ij and let me declare best move 09 43 09 45 09 48 09 51 09 55 09 57 10 00 10 01 10 02 10 05 10 07 10 10. 10. 11.
10. 14. 10.
15. 10. 19.
10. 22. 10.
23. 10. 26.
10. 27. 10.
29. 10. 31.
10. 33. 10.
36. 10. 38.
10. 40. 10.
41. 10. 44.
10. 50. 10.
52. Ok. Where it went there watch.
It’s going to go to the next available spot to the next available spot to the next available spot. So you can see this is working. It’s working in a sense that the algorithm is making a choice.
But no matter what it’s always just going to go in order of the board and oh x. Won. There’s also a big issue here which thankfully no major problems happened.
But i named this function best move and then i created a variable inside of the function with the same name which can always run into trouble so let me actually just change this to move sort of by definition. It is the best. Move.
11. 34. 11.
36. 11. 37.
11. 39. 11.
41. 11. 43.
11. 45. 11.
47. 11. 49.
11. 53. 11.
56. 11. 58.
12. 01. 12.
02. 12. 03.
So. I add. An argument.
Depth. Here. And then give it a 0 at first oh you know what i need another really important argument.
This is a turn based game. And the algorithm is going to behave differently. Whether it is the maximizing player’s turn x or the minimizing players turn o in this case.
And again you could have more than two players or there could be a lot of weird stuff going on. But that’s ultimately what i need here so. Let.
Me also add is maximizing is maximizing and this here would be true all right so i want to perform minimax on this board with a starting depth to 0. And the first move is the maximizing player so let’s add maximizing what do i want to do actually what i want to do even before this is check to see if somebody won music playing 13 01. 13.
03. 13. 06.
13. 09. 13.
10. 13. 12.
13. 14. 13.
18. 13. 20.
13. 24. 13.
26. 13. 27.
13. 29. 13.
30. 13. 33.
All right. So. This is my lookup table for what the score.
Then and again. It’s a very simple scenario. There’s only three possible scores.
If the result is not equal to null the score is the number associated with this particular o it got rid of those i didn’t need those quotes visual studio code just clean that up for me thank you very much the score is based on whoever. Won and then i can return that so this would be the terminal condition. If i am calling minimax on this particular board configuration at this particular depth.
And it’s an end state. If it’s a terminal state just return the score. That’s what it’s supposed to do ringing.
14. 19. 14.
22. 14. 25.
14. 26. 14.
31. 14. 33.
14. 36. 14.
37. 14. 40.
14. 43. 14.
45. 14. 50.
14. 53. 14.
55. 14. 56.
14. 58. 15.
01. 15. 03.
15. 04. 15.
07. 15. 09.
15. 11. I need to find the best move.
I’m kind of doing when i did again is there a way for me to reduce the amount of code. I’m writing. I’m not going to worry about this i want to once again find the best score.
Which will be in this case. Negative infinity. And i want to say the score is the minimax algorithm of the board at depth plus.
1. And now the next player’s turn is false because i’m the maximizing player and then i’m going to undo this just like i did before why do i have to write this code twice. I’ll think about it later if i can refactor this only do it once if score is greater than best score best score is equal to score.
And then after all of this for loop for loop. If it’s an empty spot boom. Boom.
Return. The best score. 16.
11. 16. 14.
16. 18. 16.
24. 16. 26.
16. 30. 16.
32. 16. 33.
16. 34. 16.
35. 16. 38.
16. 40. 16.
41. 16. 44.
16. 47. 16.
51. 16. 54.
16. 58. 17.
01. 17. 05.
17. 10. 17.
13. 17. 14.
17. 17. 17.
23. 17. 28.
17. 31. 17.
33. 17. 36.
17. 39. 17.
41. 17. 43.
17. 45. 17.
46. 17. 48.
17. 49. 17.
50. 17. 52.
17. 54. 17.
57. 18. 00.
18 03 18 05. 18. 09.
18. 11. 18.
15 18. 16. 18.
18. 18. 20.
18. 23. 18.
24. 18. 25.
18. 27. 18.
29. 18. 31.
18. 35. 18.
37. 18. 41.
18. 42. 18.
45. 18. 46.
18. 48. 18.
50. 18. 51.
18. 56. 19.
02. 19. 03.
19 03. 19 04. 19.
05. 19. 07 19.
09. 19. 11.
19. 12. 19.
15. 19. 17.
19. 20. 19.
22. 19. 23.
19. 26. 19.
29. 19. 32.
19. 34. 19.
38. 19. 38.
19. 40. 19.
43. 19. 45.
19. 48. 19.
50. 19. 54.
19. 57. 19.
59. 20. 03.
Music. Playing. Why.
Ca. N’t you figure. Out to go.
There. X. Oh.
Oh buzz ok this i’m. Finding this. Score.
Is the new score. And the best score should be the bigger one the maximum between score and best score not score this has to be this because i’m returning best score. And this has to be this ok ok whistling let’s see i really should not continue to play this drum sound drum roll come on x figure out where to go ok.
I’m going to go. Here. Oh shit.
Come on oh. No no no no no no no no no no no no no no no no buzz that’s in the wrong place. It’s in the wrong part.
I knew look my brackets bad brackets. I need to return the best score hello. After i checked all of the possibilities meaning both for loops the i and the j.
I’ve completed. I got that right up here. But not down here.
Oh ok. I really think we’ve got it this. Time.
Tympani. Sound. Drum.
Roll. 21. 17.
21. 18. 21.
20. 21. 21.
21. 23. 21.
24. 21. 25.
21. 28. 21.
32. 21. 34.
21. 37. 21.
39. 21. 40.
21. 42. 21.
45. 21. 47.
21. 48. 21.
51. 21. 53.
21. 55. 21.
55. 21. 57.
21. 57. 21.
59. 22. 01.
22. 03. 22.
05. 22. 07.
22. 09. 22.
11. 22. 13.
22. 14. 22.
15. 22. 16.
22. 16. 22.
18. 22. 20.
22. 24. 22.
26. 22. 27.
22. 28. 22.
28. No i lost. I don’t believe.
There’s any way for me to beat the ai player it’s a solved game because it’s always going to go to the center. So i could try going here. I could go here go here the best.
I could do is a tie. So you’ve watched this coding challenge. Maybe you have a creative idea about how to make your own version of this.
But i just quickly whipped up thanks to the chat in the livestream also some suggestions of things you could try so first of all what happens. If you just make two ai. Players that just play against each other over and over again well in that case.
It’s going to be a tie every single time. It’s a solved game. But that might lead to some interesting possibilities.
One thing that i’m not using is using the depth in the score. So for example. If i go back to the code.
And i change these to say 10 and negative 10. I could account for the depth meaning. I could have a higher score.
If i win more quickly than if it takes longer to win right now. I’m not accounting for that so where in the code would you subtract out depth. That’s something you could try certainly it would be interesting to make a larger board.
So can you play tic tac toe in a 5 by 5 or a 7 by 7. The winning conditions have changed. The optimal play would change that would be exciting to try i hope somebody does that you could try more players like how would you have a tic tac toe game with three players.
That’s something you try with a larger board you could try another game maybe. Connect four would work might be able to apply minimax. Too.
Just thinking about the interface animation like right now whenever. I go the next turn. Happens immediately you could think about timing.
And that sort of thing. But then i would say high degree of difficulty and maybe worth me coming back to in a future video. At some point.
Would be these. Two last items. One is known as alpha beta pruning alpha.
Beta. Pruning refers to an aspect of the minimax algorithm. Where you find an optimal path.
And you know that all the other possibilities are going to be worse. And you don’t need to go forth and check every possibility so you could research how that works and add it to this algorithm. It’s explained in the sebastian lague video.
Then there’s a possibility of a game where the number of moves is so vast you couldn’t possibly compute the entire tree chess is the classic example of that so you need some heuristic or a way of an estimated. Guess of what the score could be with any given state. So one way of doing this which i’m not saying is a good strategy.
But a simple thing you could do with chess is you could add up the total number of white pieces versus black pieces. And you could say like well the score is higher. If the maximizing player has more pieces than the opponent.
So that’s one way of approaching it so you don’t actually stop at the end of the tree. But you just go some predetermined depth and then calculate an estimate and have that ripple back up the tree. So maybe give that a try with a more complex game certainly other types of ai.
Algorithms and neural networks could play a role at some point with how you make that estimate. But ultimately that is something that would be interesting to try as well one more idea that just came from the chat. So this is quite similar to the idea of a larger board but instead of just a larger two dimensional board.
You could create a three dimensional board so tic tac toe that happens in a cube. I should just do that as a coding challenge separately myself. So hopefully you’ve learned something.
And maybe. This algorithm looked a little terrifying to you at one point. Now seems quite.
Accessible and doable. Maybe your creative idea is not on this list please make your own version of this. Share it with me at the codingtraincom.
They’ll be a link in this video’s description with instructions for how to share it if you don’t know how to share. It just write a comment file an issue. We the coding train community are here to help you whistle.
And i can’t wait to see you in a future video goodbye music playing .
Thank you for watching all the articles on the topic Coding Challenge 154: Tic Tac Toe AI with Minimax Algorithm. All shares of bluevelvetrestaurant.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.