Tic-Tac-Toe - Can we create a impossible to win algorithm?
Hope everyone had came across this classic game of Tic Tac Toe, Yes?
If you are one of the rare species who says I have no idea.. Don't worry I also didn't have any idea that such a thing existed before I got to know about it from a friend.
Just type Tic Tac Toe in Google and you can play it instantly in single player mode against an algorithm in different levels from Easy to Impossible...
Fun fact here is, the impossible isn't actually impossible as my friend was able to beat it in 1/7 th of the games.. That's when I thought, "Why not create an impossible version?"
When I decided to create it, within few moments of looking at how my friend played, the logic behind it seemed evident. We just have to fill along a row, column or the diagonal with X, while at the same time opposing our opponents moves.
The strategy of logic I came up with was just few functions -> check_risk(), next_win(), next_move(),win_move()..
(The link for my version of Tic Tac Toe website and also it's source code is made available at the end of the page.)
The game logic is simple, if a user places his 3 moves straight along the row, column or in any one of the two diagonals of the 3x3 grid he wins.. So we basically have to counter that to prevent the win..
Mathematically we shouldn't allow the sum of user's (X) to be 3 in any of the row, column or the diagonal. And also we have to maximise our win when user doesn't have a win move in immediate step. So the logic boils down to these functions..
next_win( ) -> Checks whether we have any win_move in immediate move (ie..Sum of any path is currently 2)
win_move( ) -> If previous function returns true, it finds that move to win (ie..Position to place the move to make the sum 3)
check_risk( ) -> If we don't have any win move possible, It checks whether the user can make any win move in next move.
next_move( ) -> If check_risk returns true, we have to prevent the win of the user by placing our 'O' to counter/spoil the user's win chance.. If user doesn't have any win move in next step we should make a move that maximises our win in next iteration.
That's basically the logic to make a classical strategy..So we made an impossible to win Tic-Tac-Toe? (Just by not allowing the user move to have a sum of 3 in any direction)
The answer is not quite yet...it seems as we had, but if we start playing it for a quite long we can find that by placing along the diagonals in such a way we get 2 win moves along (diagonal & row) or (diagonal & column) we can trick the algorithm as it can only counteract one win move so it will be helpless when two win move arises at the same time.
This is what makes the game playable, because there's always a possibility of winning in maybe 1 of 5 games after observing its initial moves (irrespective of any random initial moves).
Though we can't make it impossible but we can make it harder by randomising the initial moves so that user can't learn the pattern and to advanced level we can employ a ML to learn user's moves over time and make good initial moves (We say initial moves, because its the first 2-3 moves that define the win)..
Catch you up soon with an another interesting topic..Stay tuned :)
My version -> https://ssrtest1.000webhostapp.com/
Source code -> https://drive.google.com/drive/folders/1e6lu4Zvx8_eVYMINq7SOBcTLCMhRSfjS
Comments
Post a Comment