# Tic Tac Toe Game using a microcontroller

Status
Not open for further replies.

#### lmtalsoul

##### Junior Member level 3
I was wondering if the tic tac toe game can be realised using a microcontroller. Can someone please tell me how to start off? Please Help...

#### mrniceguy

##### Newbie level 6
microcontroller tic tac toe

A microcontroller, FPGA, or even discrete logic can be set up to solve your problem.

First think about the rules and what types of ins and outs your system needs.
For a very basic version:

I invision a 9 bi-color LED grid to represent the moves and indicate which player has selcted each square. You may want a 10th bi-color LED to indicate which players turn it is.

I also invision some sort of input system. 9 Momentary switches, or a DTMF like switch keypad can be used to allow the human player(s) to select the square they want.

One of those switches can also be configured for setup functions (and the led bank can display preset patterns for the various modes, like play the computer easy, play the computer hard, play another person, and your own modified rules (like 3 in an L shape wins, etc..)

So the way I see it you need 10 trimode outputs (sink for one color, source for the other, High-Z prior to being selected so LED is off, or you could use a solid LED for x and a flashing LED for O but it is much more processor intensive. Another solution would use 20outputs sink for off, set for on, two sets for each color)
and at least 9 Inputs that connect to a sitch array for each of the 9 possible moves.
I'll assume you are up to coding a microcontroller in assembley. You could do it in C or C++ but lets make this lean and mean.

To make the most basic game(2 human Players) your code must address the following: Whos turn is it (Indicate on turn LED) Which blocks are X's and which are O's and has anyone won or are we out of moves.

Keeping track of whose turn it is is easy. A single bit can be used. If its set, its player X, source current to the turn LED. If its not set then its O's turn, sink current from the turn LED.

Keeping track of the previous moves is pretty easy too. Establish two 3x3 Arrays and based on which loop you are in (X's turn or O's turn) write the value back to the appropriate array (X's array tracks all X moves, O's array tracks O's moves) Noting that a check should be run to ensure that X is not trying to move to a space O already has taken and vice versa)

At the end of each loop a check should be run to see if either player has won (check the winning combinations in each array if any one is completely set then trigger all the output LED's to flash that color or something and watch for an input to reset the game)

The logic for a Human vs Microcontroller game is a little harder so I'll leave you to build a human vs human solution first. Then we can address how to make the chip "think" and even how to make different difficulty levels. (Heres a preview.. c source code for an unbeatable AI on a PIC16f628 hxxp://picant.com/c2c/examples/tactic/tactic.c.html)

Here's my pseudo code for the controller:
{Initialize}
Establish 10 output lines a1 a2 a3 b1 b2 b3 c1 c2 c3 t1
Establish 9 input lines ai1 ai2 ai3 bi1 bi2 bi3 ci1 ci2 ci3
Set all outputs to High Z state
Intiliaze X array,
Initiliaze O array,
Set turn indicator for X,
{Game play loop}
Call X loop
Call Y loop
{winner detected loop}
Set all outputs to match winners color
check input ai1 to reset game if found goto initialize
clear all ouputs after n fractions of a second delay (makes them flash)
delay for n fractions of a second(1/2 or so is good)repeat
{end winner loop}
{X loop}
Monitor inputs when input recieved
Check Y array at position corresponding to input,
if not set check X array at position,
if set call flash setx loop
if not set set value and
check X array for winning combinations if none found return if one found call
winner loop pass variable X
if set call FLASH sety routine
LOOP as NEEDED until RETURN
{Y loop}
Monitor inputs, when recived
Check X array at position corresponding to input
if not set check Y array at position
if set call flash sety
if not set, set value and
check Y array for winning combinations return if none call Winner passing Y is
found
if set call flash setx routine
LOOP AS NEEDED UNTIL RETURN

SETY Routine
Flash selected output in Y color for n seconds
Return

SETX Routine
Flash Selected output in X color for n seconds
Return

END

That should cover the basics of a two human player game. Once you've got that working and understand how/why it works you can work on human VS chip. you just substitute in some AI for one player or the others loop. (Could be as simple as looking for certain preset combinations ie X has top corner all others free O wants another corner or X has top and bottom of a Row Y wants to block. Etc its not too tough)

Good luck

#### lmtalsoul

##### Junior Member level 3
game new tic tac toe two player one player

I was expecting a reply but oh god.... your reply is awesome... Thank you very much. It'll be of great use to me. I do have one thing to say. Will there be any difficulty if I try to do this project in 8051? I mean, AT89C51 or AT89S51... I'm a little bit familiar with those architectures rather than PICs. I am not that confident in writing code using Assembly but i'll try my hand at C. Can you suggest me in this regard? I'm really serious about bringing this to life...So i'd be grateful to you if you could advice me.

##### Full Member level 3
tic tac toe using 8051

You could probably use an 8051 to play chess if you worked at it hard enough. Tic Tac Toe will not be a problem. Tic Tac Toe requires no actual 'thought' all the moves can be done using lookup charts if need be.

Status
Not open for further replies.