4

I'm a junior programmer and I know the basics of pascal and c++. I made a Tic Tac Toe game with Player-Computer and the game is all finished.

The computer generates a random place where the Os go on the table and that's not good.

I thought that i should multiple procedures that check every winning position and the computer should else try to block the player's Xs or to make a winning position, BUT this would have been lots of time lost cause all of the if's.

Then I thought of a simpler version with some kind of ifs but it would still have been taking lots of time to do.

Then i thought deeper: What about a find-four game? How in the earth someone would manage to check every space available and how it would've been possible that someone could make a function that checks absolutely any winning or progress of player/computer position, Oh and wait, that's not ALL, what if the player is doing some tricks so he blocks the computer? How would the computer know that?!? For sure, that would take ages to program. And I am not talking about something that seems more impossible: Chess.

So here I am, asking myself that there SHOULD be a way more simpler way the computer should search and solve some problems than tons of ifs.

In this case, if any of you know any way of solving this, how can i manage to make the simplest procedure to block and beat the player in a TicTacToe game?

If someone wants to check my code or use it: http://pastebin.com/jhyUn7d1

Samuel Cota
  • 57
  • 1
  • 2
  • 6
  • There are *many* techniques. For simple games like TTT, you should search for "Depth-First Search." For more complex games, you could start researching "alpha-beta pruning." And for *really* complex games, you can read about, let's say, "Monte-Carlo Tree Search." – Larry OBrien May 10 '13 at 18:54
  • 2
    http://imgs.xkcd.com/comics/tic_tac_toe.png :P – BlackBear May 10 '13 at 18:59
  • possible duplicate of [Simple tic-tac-toe AI](http://stackoverflow.com/questions/15753572/simple-tic-tac-toe-ai) – Raymond Chen May 10 '13 at 19:29

6 Answers6

3

What you're looking for is Minimax.

Using this algorithm the computer will win every Tic Tac Toe game or you could adjust the depth that the computer analyzes the moves in order to achieve some kind of medium difficulty.

It's not hard to implement, you should be familiar with recursivity and you're set, of course the implementation differs according to your code, but the wikipedia page offers a pretty good starting point.

Paul
  • 20,883
  • 7
  • 57
  • 74
  • 1
    This is exactly right. The minimax will work for finding the optimal move in any 2 player, deterministic game with perfect knowledge of the game state. So it would also work for connect 4, checkers, and even chess. (Although for extremely complex games in terms of state-space such as chess, the algorithm is intractable in practice.) Also, extensions for the algorithm exist for n-player games, games with hidden information, games with random elements, etc. – Nicu Stiurca May 10 '13 at 19:28
1

Tic tac toe algorithm is something like:

  1. Take spot if going to win
  2. Take spot if going to lose
  3. Take corner
  4. Take non-corner non-center
  5. Take center
RandyGaul
  • 1,915
  • 14
  • 21
0

I recently dealt with this, although my code was in C#.

I came up with a way of scoring each candidate move. The approach I took creates a score based on the number of moves that would then be required for a win (less moves needed results in a higher score).

My algorithm also considers the combined number of moves for multiple squares. As a result, the algorithm favors moves that would produce multiple potential wins (the only real tactic I know about for Tic Tac Toe). For example, it is possible sometimes to make a move that produces two potential wins that must be blocked. Since the opponent can only block one, it produces a win.

I posted my entire code and a description of it in the article A Tic-Tac-Toe Game Engine.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

The short answer is "try all the different moves until the game is won, and record which ones lead to computer winning".

Long answerÖ

For a limited size TTT game, the number of possible moves before the game is won, isn't that much, so simply try a each possible move, then recursively try all possible opponent moves, and keep going until the game ends. Give each move a "score" of how well it went (e.g. how many different solutions did you get that went successful for the computer, and how many went success to the opponent, and pick the one with the "best" result). Beware that you will probably end up with something that is near on impossible to win against if you do well.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

I did this once, a long time ago. I don't know if I still have the code lying around...

Anyway, I created a function, return type int, which was the square in which the computer should place its piece (assuming 0 is top left and 8 is bottom right square). Yours uses a 2D array, so would be a little different.

Anyway, for each row, column and diagonal, check to see if any two pieces on that row belong to the player. If they don't, check for the same but belonging to the computer. On the first row that this is true, check the remaining piece - if it's available, put the piece there for the win. If you have a player-dominated row, check that you don't already have a piece there and stick it in to block.

const int PlayerPiece = 1;
const int CPiece = 2;
const int Empty = 0; 

int board[3][3];
if(board[0][0] == PlayerPiece && board[0][1] == PlayerPiece && board [0][2] == Empty)
{
    //Put_Your_Piece_In_[0][2]
}

You could then go on to changing it so that it could check each row i.e.

int numRows = 3;

for(int i = 0; i < numRows; i++)
{
if(board[i][0] == PlayerPiece && board[i][1] == PlayerPiece && board[i][2] == Empty)
    {
    //Put_Piece_In_[i][2]
    }
}

Then, do the same for rows.

You could always consider that Tic-Tac-Toe is essentially just a magic square, described fairly well here: http://www.sciforums.com/showthread.php?134281-An-isomorphism-Tic-Tac-Toe-on-Magic-Square

Joe
  • 398
  • 4
  • 15
0

There is a perfect strategy for Tic-Tac-Toe available on wikipedia. It is really simple. Due to the small size of the grid, the number of cases you need to test (eg test if there are 2 blocks in a row), are very small.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90