3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino and Touchpad Tic Tac Toe

Step 7Programming Tic Tac Toe

Attached is the code for the game.

Let's first break down the tic tac toe game into its various steps, in the two player mode:

Step 1: Player A picks an unfilled cell by touching an intersection. 
Step 2: The LED for that cell lights up with the colour A.
Step 3: Check to see if Player A has won.
Step 4: Player B picks an unfilled cell.
Step 5: The LED for that cell lights up with colour B.
Step 6: Check to see if Player B has won.
Step 7: Repeat 1-6 until there's a win condition, or if all the cells are filled.

Reading the cells:
The program loops between reading the grid and displaying the LED matrix.  As long as the grid sensor does not register a non-zero value, this loop will continue.  When an intersection is pressed, the Pressed variable stores the position of the pressed cell.

Checking if the cell is unfilled:
When a position reading is obtained (variable Pressed), it is compared against the current cell status (stored in the variable GridOnOff) using a bitwise addition.  If the Pressed cell is unfilled, then proceed to light up the LED, otherwise return to reading the cells.

Toggling the colours:
A boolean variable, Turn, is used to record whose turn it is.  The LED colour chosen when a cell is picked is determined by this variable, which alternates each time a cell is chosen.

Checking for a win condition:
There are only 8 possible win conditions, and these are stored as word variables in an array (winArray).  Two bitwise additions are used to compare a player's filled cell positions to the win conditions.  If there's a match, then the program displays a win routine, after which it starts a new game.

Checking for a draw condition:
When nine turns have been recorded and there is still no win condition, then the game is a draw.  The LEDs are then faded out and a new game is started.

Switching to one player mode:
If the switch is in the on position, the program goes into one player mode, with the human player starting first.  At the end of the human player's turn, the program simply picks a random cell.  Obviously, this isn't the smartest strategy!

« Previous StepDownload PDFView All StepsNext Step »
4 comments
Aug 18, 2010. 10:28 PMkristiansmic says:
This will be my first project with arduino. It don't looks hard. Can i don't use ''touchpad'' but just buttons in matrix?
Dec 13, 2009. 11:08 AMDemonSpawn says:
 i cant download eather of these sets of code it allways comes out as a .tmp could you email them to me please. my email is mickynicky1@hotmail.com

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
8
Followers
3
Author:origamiwolf