Strategic TIC TAC TOE

Skip to first unread message

Scott Ferguson

Aug 8, 2015, 11:47:44 PM8/8/15


This is the classic game of X's and O's where each player tries to get three of their marks in a row, column or diagonally in a grid of nine squares.

For this example, the player (you) plays against the app using your mark (X).

While you can use whatever strategy you wish during game play, the app must follow the one it is programmed to follow. I programmed it to use the following strategy:

In this particular version of the game, the player (you) allways goes first and uses the X mark to play. If the player has three X's in a row, column or diagonally then he wins. End of game.

If the player's play did not result in a win then it is the app's turn.

The app checks if all of the squares have been filled before attempting to play. If no plays are available then the game is a tie. End of game.

Otherwise, the app can make a play so...

The app will look for all winning plays and if any are available, select one at random to win the game. End of game.

If no winning play is available then continue...

The app will look to see if the player is one mark away from any winning plays and if so, block one of those plays at random by putting the app mark (O) there instead.

If the player has no winning plays to be blocked by the app then continue...

If the app finds any rows, columns or diagonals which don't contain the Player's mark then it picks an empty button from one of these to make a play with preference for the center button first, followed by any corner button then any other empty button.

What is left for App to make a play is one or more rows, columns or diagonals that have a Button containing Player's mark and either one empty button and one containing App's mark or two empty buttons.
The App chooses one of the empty buttons with a preference for the center button first, followed by any corner button then any other button.

Now play goes back to the player.

The App's strategy is shown in the procedure block below:

Most major blocks contain comments that describe their function in the app.
Project aia file is attached.


girish vinjam

Aug 10, 2015, 4:45:01 AM8/10/15
nice to meeting you

On 9 August 2015 at 09:17, Scott Ferguson <> wrote:

(you have received this message from the App Inventor Developers Library)
You received this message because you are subscribed to the Google Groups "App Inventor Developers Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Dr.Bhavani Rao Reddi

Apr 8, 2017, 2:19:33 PM4/8/17
to App Inventor Developers Library

Really terrific.

How you did not miss anything from those many complicated blocks.
App is running fine but getting defeated by the user.

Ghica van Emde Boas

Apr 21, 2017, 9:32:32 AM4/21/17
to App Inventor Developers Library
If the app maken optimaal moven, the outcome should always be a draw, unless the player is really stupid.
Cheers, Ghica.
Reply all
Reply to author
0 new messages