

The game of course should end in a draw if both players play optimally.
#Machinarium tic tac toe how to
This answer assumes you understand implementing the perfect algorithm for P1 and discusses how to achieve a win in conditions against ordinary human players, who will make some mistakes more commonly than others. Note: When you have double and forks, check if your double gives the opponent a double.if it gives, check if that your new mandatory point is included in your fork list.

if not search for double and fork(ultimate win).if not only blocking points(not to lose).if not search forks in blocking points(which gives the opponent the most losing possibilities ).search in blocking points for possible double and fork(ultimate win).if not, do you already have a fork(have a double double).Turn = 8 if Posswin(O) is not 0 then Go(Posswin(O)), else if Posswin(X) is not 0, then Go(Posswin(X)), else go anywhere that is blank. Turn = 7 If Posswin(X) is not 0 then Go(Posswin(X)), else if Posswin(X) is not 0, then Go(Posswin(O)) else go anywhere that is blank. Turn = 6 If Posswin(O) is not 0 then Go(Posswin(O)), else if Posswin(X) is not 0, then Go(Posswin(X)), else Go(Make2). Turn = 5 if Posswin(X) is not 0 then Go(Posswin(X)), else if Posswin(O) is not 0, then Go(Posswin(O)), else if Board is blank, then Go(7), else Go(3). Turn = 4 If Posswin(X) is not 0, then Go(Posswin(X)) i.e. Turn = 3 If Board is blank, Go(9), else Go(3). Turn = 2 If Board is blank, Go(5), else Go(1). Move if it plays X, the even-numbered move if it plays O. The algorithm has a built-in strategy for each move. this procedure sets board to 3 if Turn is odd, or 5 if Turn is even. If a winning row (column or diagonal) is found, the blank square in it can be determined and the number of that square is returned by this function. If the product is 50 ( 5 x 5 x 2), then O can win. If the product is 18 ( 3 x 3 x 2), then X can win. By multiplying the values of each square together for an entire row (or column or diagonal), the possibility of a win can be checked. This function operates by checking each of the rows, columns, and diagonals. This function will enable the program both to win and to block opponents win.

Posswin(p): Returns 0 if player p can’t win on his next move otherwise, it returns the number of the square that constitutes a winning move. Otherwise, this function returns any non-corner square (2, 4, 6 or 8). Make2: returns 5 if the center square of the board is blank i.e. The 1st move will be indicated by 1, last by 9. Turn: An integer indicating which move of the game about to be played. We store 2 (indicatingīlank), 3 (indicating X), or 5 (indicating O). You could, however, alter the priorities above to give characteristic weaknesses to opponent personalities.Ī typical algo for tic-tac-toe should look like this:īoard : A nine-element vector representing the board. Note: A "perfect" opponent is a nice exercise but ultimately not worth 'playing' against. Recognizing what a "fork" situation looks like could be done in a brute-force manner as suggested.

Opposite Corner: If the opponent is in the corner, play the opposite Win: If you have two in a row, play the third to get three in a row.īlock: If the opponent has two in a row, play the third to block them.įork: Create an opportunity where you can win in two ways. Quote from Wikipedia (Tic Tac Toe#Strategy)Ī player can play a perfect game of Tic-tac-toe (to win or, at least, draw) if they choose the first available move from the following list, each turn, as used in Newell and Simon's 1972 tic-tac-toe program. The strategy from Wikipedia for playing a perfect game (win or tie every time) seems like straightforward pseudo-code:
