Jump to content

2D array Tic tac toe AI in java

easylifeforme

Hey,

Does anyone know how to or has made a tic tac toe AI for java. I've been trying to make the code so that it will take in a 2D array (3x3) and respond to where the other x's and o's are on the board to find the best place. If you have done this before or know somewhere that shows/teaches how to do it that would be amazing.

Thanks,

Adam

Link to comment
Share on other sites

Link to post
Share on other sites

Also a more general overview...just a quick logic behind tic-tac-toe drawing.

 

http://en.wikipedia.org/wiki/Tic-tac-toe

 

 

To guarantee a draw for O, however:

  • If X does not play center opening move (playing a corner is the best opening move), take center, and then a side middle. This will stop any forks from happening. If O plays a corner, a perfect X player has already played the corner opposite their first and proceeds to play a 3rd corner, stopping O's 3-in-a-row and making their own fork.
  • If X plays center opening move, O should pay attention and not allow a fork. X should play a corner first.
    • If O takes center (best move for them), X should take the corner opposite the original, and proceed as detailed above.
    • If O plays a corner or side-middle first, X is guaranteed to win:
      • If corner, X simply takes any of the other 2 corners, and then the last, a fork.
      • If O plays a side-middle, X takes the only corner that O's blocking won't make 2 in a row. O will block, but the best of the other two will be seen by X, and O is forked. The only way that X must lose is if O plays middle and then a side-middle.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

i'll add my 2 cents:

you can blow your mind trying to wrap your head around someone else's implementation of a heuristic-based AI, or you can spend some time building up one by yourself, making it a little thing to be proud of

 

tic-tac-toe is a simple game which does not have a lot of options, so a simple concept on which you could work is that you can "cheat"

you can simulate all the possible moves (less than 9), and see which one will bring you to the win or loss, so you will know which move to do, or which move to avoid

that could be a start to write a not-so-ridiculously-stupid AI

still a better AI than Thief

 

implementing it isn't easy, but it can be a nice exercise to practise java

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×