1
\$\begingroup\$

I'm working on a little project. The aim is to play Tic-Tac-Toe against an AI, but I require that the game always results in a draw against the AI opponent. How can I implement the AI to ensure that?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ So it also has to end in a draw when the player tries to lose intentionally? \$\endgroup\$
    – Philipp
    Nov 6, 2014 at 15:19
  • \$\begingroup\$ Yes, exactly! No win or lose situation \$\endgroup\$
    – rakeden
    Nov 6, 2014 at 15:46
  • 2
    \$\begingroup\$ Just build an AI that can read XKCD: xkcd.com/832 \$\endgroup\$
    – Elva
    Nov 6, 2014 at 15:55

5 Answers 5

1
\$\begingroup\$

It is possible. Probably the best way to program an AI for tic-tac-toe that could do this is to use a minmax algorithm.

Normally, you would give a winning move a very high score so that the AI would make that move. In your case however, you would want to give a winning move a very low score and instead favor moves that prevent the opponent from winning. This means that moves that either let the player win or the AI win would be very low scoring moves and every other move would be high scoring.

Now, this still doesn't guarantee a draw 100% of the time. If the player puts the AI into a position where no matter what move it makes it will win, the AI will of course win. However, this should help the AI force a draw the majority of the time.

\$\endgroup\$
0
1
\$\begingroup\$

Don't be confused by calling this game tic-tac-toe, it is its own game, and might or might not be as easily solvable as tic-tac-toe. Your game is "player1 wins by a tic-tac-toe draw, player 2 wins by either a tic-tac-toe win or loss." (Or the converse).

If you think about it, either this game is a win for the first player, or it's a win for the second player, so if you have to play both sides, your task is impossible. Your instructor is playing a little trick on you.

\$\endgroup\$
1
  • \$\begingroup\$ Keen insight! My brain thanks you. I think the question is still valid, though, with the addition of, "if possible". Winning may be contingent on the player making a mistake, in some cases. \$\endgroup\$ Nov 7, 2014 at 19:01
0
\$\begingroup\$

I have done a project similar to this about a year ago. I researched strategies on how to always win/tie the game, and simply coded the strategies - though it does take some time - into my game (NOTE: make sure to plan out an efficient way for the computer to analyse the "board" and then make its next move, or else you will end up with a giant mess of if statements). I would recommend reading this article on wiki how. It clearly explains what to do to win/tie a game regardless of what your opponent does.

This project is possible and solvable! Good luck with it!

\$\endgroup\$
0
0
\$\begingroup\$

Well... There's only 9! or 362880 possible games. Less, since you stop playing after someone wins. And less, conceptually, once you reach a state where neither player can win. Times 2 if you want to treat X and O starting as different cases.

At any moment, the number of possible board states is 3^9 or 729. Each of nine cells has one of three marks in it (blank, X, O). Less, since many combinations can't happen in a game.

At any rate, these are very small and tractable numbers. One could, in a pleasant afternoon's coding, perhaps in node.js or python, iterate over all those games, and produce a table showing for each of the 729 board states, which move to make next.

Print it out as one long line of javascript. It could be to initialize an array, or print out a giant switch statement or mess of if-statements. Doesn't matter, computers don't mind. Paste that into your function nextMove(){}, and, done!

\$\endgroup\$
0
\$\begingroup\$

What you have to do to get a draw is easy. But you're going to have to go on impossible mode.

First, you're gonna put X/O in the bottom right corner. The AI should put the X/O in the middle. Then you are going to put an X/O beside it going sideways. AI should put one to stop you at the end of the bottom line. You are then going to put an X/O in the top right corner. Then one at the Middle row left side (the one on the farthest left side). Then finally after the AI goes you should put the final X/O in its place for a draw.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ What should the AI do if the human player does not make this specific set of moves? To answer this question, we need not just one sequence of plays, but an algorithm to determine the appropriate next move for the AI no matter where the human plays. \$\endgroup\$
    – DMGregory
    Feb 22, 2021 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .