• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

TicTacToe minimax algorithm bug

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently I made a TicTacToe game with an AI using minimax alrgorithm, but I seem to have misunderstood something and sometimes it doesn't choose the best move, so if someone knows where I made a mistake, help would be appreciated.



These are the two important methods that show how ai makes decisions.
It does well in most of the times, but sometimes there are chances of actually defeating it and that should not be possible.
I appreciate the help. Thank you!
 
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might help people understand your intent by explaining what you mean by "minimax algorithm". Then they'll have a basis for analyzing your code to see what might be out of alignment with that algorithm.
 
Jebiga StaMozed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that is exactly the problem. I think that I might have misunderstood something about the algorithm and am looking for somebody who has already worked with that algorithm to tell me what i see wrong.
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And my point is that not everyone who looks at this thread will know offhand what that algorithm is. Giving people a link to more details about it or giving a short description of what it's supposed to do would be helpful and increase your chances of getting help. It would be the courteous thing to do.
 
Jebiga StaMozed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:And my point is that not everyone who looks at this thread will know offhand what that algorithm is. Giving people a link to more details about it or giving a short description of what it's supposed to do would be helpful and increase your chances of getting help. It would be the courteous thing to do.



Alright, here is a link explaining it: https://www.freecodecamp.org/news/how-to-make-your-tic-tac-toe-game-unbeatable-by-using-the-minimax-algorithm-9d690bad4b37/
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link, that is very helpful.

This minimax algorithm basically constructs what's called a game tree and is supposed to pick the branch that will lead to the best outcome. Unfortunately, the code you shared is difficult to follow and, frankly, quite a mess.

At this point, it might be helpful if you gave an example of where your code fails to find the optimal set of moves to play. The failure could come at any time so it will be most helpful to see it go through an entire game so it's easier to see the point where it goes wrong.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!