Help With arduino Tic tac toe

//This program is a 2 human player tic tsc toe
//Created on August 26, 2008

int val;

int O;
int val2;
int playerTurn = 1;
int spotOne = 01; // These are the varibles for each possible spot on the grid
int spotTwo = 02;
int spotThree = 03;
int spotFour = 04;
int spotFive = 05;
int spotSix = 06;
int spotSeven = 07;
int spotEight = 8;
int spotNine = 9;

int Player1 = 00; // these will be used to have the players choose their spot on the grid
int Player2 = 11;

void setup () {
Serial.begin(9600);
(playerTurn == 1);
Serial.println ("Welcome to the two player tic tac toe game.");
Serial.println ("Players will take turns putting their choices into the terminal by number.");
Serial.println ("Each free space on the grid will have a number on it from 1 to 9.");
Serial.println ("Player one will be an 0 and player two will be an 11");
Serial.println ("The first person you get 3 of theor symbols in a row wins");
delay (5000);

}
void loop ()
{
Serial.println(val);

Serial.print (spotOne);
Serial.print (" !");
Serial.print (spotTwo);
Serial.print (" !");
Serial.println (spotThree); //First Line
Serial.print (spotFour);
Serial.print (" !");
Serial.print (spotFive);
Serial.print (" !");
Serial.println (spotSix); //line 2
Serial.print (spotSeven);
Serial.print (" !");
Serial.print (spotEight);
Serial.print (" !");
Serial.println (spotNine);
Serial.print ("It is player ");
Serial.print (playerTurn);
Serial.println ("'s turn, enter your selection");
delay (500);

if (playerTurn = 1){
if (Serial.available()) {
(val = Serial.read());
if (val == 49){
if (spotOne != 11 || 0){
(spotOne == 11);
(playerTurn = 2);
}
if (val == 50){
if (spotTwo != 11 || 0){
(spotTwo == Player1);
(playerTurn = 2);
}
if (val == 51){
if (spotThree != 11 || 0){
(spotThree == Player1);
(playerTurn = 2);
}
if (val == 52){
if (spotFour != 11 || 0){
(spotFour == Player1);
(playerTurn = 2);
}
if (val == 53){
if (spotFive != 11 || 0){
(spotFive == 11 );
(playerTurn = 2);
}
if (val == 54){
if (spotSix != 11 || 0){
(spotSix = Player1);
(playerTurn = 2);
}
if (val == 55){
if (spotSeven != 11 || 0){
(spotSeven = Player1);
(playerTurn = 2);
}
if (val == 56){
if (spotEight != 11 || 0){
(spotEight = Player1);
(playerTurn = 2);
}
if (val == 57){
if (spotNine != 11 || 0){
(spotNine = Player1);
(playerTurn = 2);
}}}}}}}}}} }
}
if (playerTurn = 2){
if (Serial.available()) {
(val = Serial.read());
if (val == 49){

(spotOne == 0);
(playerTurn = 1);
}
if (val == 50){
if (spotTwo != 11 || 0){
(spotTwo = Player2);
(playerTurn = 1);
}
if (val == 51){
if (spotThree != 11 || 0){
(spotThree = Player2);
(playerTurn = 1);
}
if (val == 52){
if (spotFour != 11 || 0){
(spotFour = Player2);
(playerTurn = 1);
}
if (val == 53){
if (spotFive != 11 || 0){
(spotFive = Player2);
(playerTurn = 1);
}
if (val == 54){
if (spotSix != 11 || 0){
(spotSix = Player2);
(playerTurn = 1);
}
if (val == 55){
if (spotSeven != 11 || 0){
(spotSeven = Player2);
(playerTurn = 1);
}
if (val == 56){
if (spotEight != 11 || 0){
(spotEight = Player2);
(playerTurn = 1);
}
if (val == 57){
if (spotNine != 11 || 0){
(spotNine = Player2);
(playerTurn = 1);
}}}}}}}}} }
}
}
Can anyone help me with this program. I cant quite get it to work properly. You can make changes anywhere.

sort by: active | newest | oldest
1-10 of 11Next »
gmoon9 years ago
OK, I'm not really an Arduino guy, but I am a C guy. Wow, you don't give much information, so I'm making a LOT of assumptions (serial I/O, no routine to determine the winner, etc.)

Here are a few comments, and some code. This is NOT a definitive TTT program, just a starting point:

-- Don't use integers (int) when something smaller (char, byte) will do.

-- Don't use individual variables when an array is much more effective. An array of 9 chars is:
char board[9];
-- Use loops ("for", etc.), rather than many individual statements whenever possible.

-- Convert the ascii input directly to numerical values by subtracting 48... now 49 (char "1") becomes 1, 50 (char "2") becomes 2, etc. This is an OLD trick.... You'll have to check for illegal input, though.

-- If the cell is empty, it's a legal move.

-- The player is 0 or 1, and is toggled (XOR'd) when the move is legal. A 0 or a 1 is placed in the board array cell ONLY if the cell is empty.

Code follows: don't expect this to be error free, I haven't run it through any thing to check for syntax errors. I think the 'C' constructs used here should work with the Arduino....

I'm taking your code for the serial connection directly...if that doesn't work, don't blame me. ;-)

I didn't write ANY of the display routines--I.E., the prompts or the drawing the board. Use a "for" statement to loop through the board array...

// constants#define CELL_X  0#define CELL_O  1#define CELL_EMPTY  2char userinput;char board[9];byte player;bolean status_incomplete;int moves;void setup () {moves = 0;// init the board// make this a function, so you can replay without powering downfor (byte i=0; i <= 9; i++){   board[i] = CELL_EMPTY;} player = 0x01;Serial.begin(9600);}void loop(){// insert a routine for board displaydo {    status_incomplete = true;    if (Serial.available()) {    userinput = Serial.read() - 48;   // convert ASCII to 1-9    // Doesn't check for illegal input! Do that yourself...    If (board[userinput] == CELL_EMPTY)  // legal move if empty cell        {        board[userinput] = player;  // player is 0 or 1        moves++;   // increm # of moves        player = player ^ 0x01;     // toggle player if move is legal        status_incomplete = false;        }        else        Serial.print ("oops! "); // bad input                // if terminal "bell" is available, try that..    }   } while (status_incomplete)}
sotsirh194 (author)  gmoon9 years ago
Can I see an example of the char board statement and how it can help me in my program?
It's in the example above:

This statement declares a array of 9 char:
char board[9];
"userinput" holds the move (the cell or box #), so:
board[userinput] = player;
Sets that particular box to the player number ( 0 or 1)

gmoon gmoon9 years ago
Errors in my code: -- misspelled "boolean" -- Arrays are numbered from zero, so a 9-element array is 0-thru-8. You should subtract 49 (not 48) from the ascii value (converting ascii 1-9 to numerical 0-8, or you'll get an out-of-bounds error. Is any of this making sense?
sotsirh194 (author)  gmoon9 years ago
It's starting to, but I am still somewhat confused how would i display this visually.
First (and I eluted to this in my original comment), what are you using as input / output devices? We need to know this to proceed. And which Arduino you have would help, too.

As it's originally written, it's serial I/O. Essentially wiring the Arduino to the serial port (or USB port) of your computer, then connected to the port with a text terminal on the PC... If you've already got that working, then cool.

If instead you're using an LCD as output, and maybe a PS2 keyboard directly, then you need more supporting code.... Look to the Contributed Libraries for that.

Or an LCD output, and a key matrix (like 9 buttons in a 3x3 matrix)?

-- The number of pins for the output depends on what type of LCD. A standard HD44780-compatible LCD needs 6 pins. A TTL serial LCD needs fewer pins.

-- An input 3x3 key matrix needs at least 6 pins, and you must "scan" them to get the 9 separate button presses. Here's a keypad tutorial, but you can use separate pushbuttons rather than a pre-made pad.

-- An LED output matrix (3x3) is possible, too. But you'd probably need supportive components, since each "cell" of a Tic tac toe has three states-- empty, O and X.
sotsirh194 (author)  gmoon9 years ago
I was just planning to use the terminal. I am using the usb arduino decimila. i would display the board in the terminal and have each available space numbed 1-9. Whichever box you wanted all you would do is type in the number if the box. The program would then check if the box is empty/unused by a player and assign the varible there a new value. Originally this was meant to be an x or an o, but I did not know about the caracter varible. Can that hold a number(1-9) or letters. I am still confused on the board command and how it would be used in my program.
That makes it much easier.

"board" isn't a command-- it's a char array variable named "board". It's made up of 9 different characters, and each one is accessed by the index in the brackets.

Want to access the 4th variable? It's
X = board[3];
(arrays are always numbered from zero. So a 9 element array is indexed 0 -to - 8. The first element is index 0, the second is 1, etc.)

So the single board array completely replaces all of your "spot....." variables (spotone through spotnine.)

And this is important----we can access the elements of an array by using variables for the index number:
X = 3;Y = board[X];
So now we can use loops instead of referring to each element explicitly.

Make it simple on yourself (avoid any unneeded conditional statements):

Don't use characters ( O and X ) to represent the players in the array, use numbers ( 0 and 1 )--
So the statement:
player = player ^ 0x01;     // toggle player if move is legal

XOR's the current player. Everytime the statement is called, 0 switches to 1, or 1 switches to 0. No conditionals ( if() statements ), just quick and dirty...

How to display?
Here's the C shell version.

void drawboard() {printf("\n");for(i=0; i < 3; i++)   {   printf ("%c ",convert[board[i*3]]);   printf ("%c ", convert[board[i*3 +1]]);   printf ("%c\n",convert[board[i*3 +2]]);   }printf("\nplayer %c: ", convert[player]);}

How does it work? Most importantly, I've defined another array, this is a fixed 3-element array called "convert":
char convert[] = "XO_";

Convert holds the characters we want to display when the values in the "board" array are 0, 1 and 2.
convert[0] would return an "X" characterconvert[1] would return an "O" characterconvert[2] would return an "_" character 

Since the players are defined as a 0 or a 1, we get an "X" for player 0, and a "O" for player 1...

..................................
I know the drawboard() function looks complicated, but it's simple:

--the statements are inside a for() loop, that just counts from 0 to 2 (so three "interations.")
--each iteration of the loop prints one line, something like this:
O _ X
--the first printf() statement prints the first char, the second statement prints the second char, etc.

So for the first iteration (i = 0):
board[i*3] returns element i*3, or element 0 

So the array within the array....
convert[board[i*3]] 

....returns whatever character corresponds to the value (0-2) that's contained in the element 0 of the "board" array.

To make the output work with the arduino, you need to replace the printf() statements with Serial statements. They won't be nearly as flexible as printf(), but you can do it...

So the C printf() statement:
printf ("%c ",convert[board[i*3]]);

Can be written something like:
Serial.print(convert[board[i*3]], BYTE);

....but it will not print the second character (space) included in the printf() string. That's OK--you can use other Serial.print() statements, like:

Serial.print(convert[board[i*3]], BYTE);Serial.print(" ");



Just for fun, I wrote it as a standard C shell program. I corrected any errors, and I've attached the source. It works fine. Here's the output of one game:
$ ./tttTTT gameGrid is numbered 1-9Press number, then return to make a moveplayer O: 5_ _ __ O __ _ _player X: 3_ _ X_ O __ _ _player O: 1O _ X_ O __ _ _player X: 9O _ X_ O __ _ Xplayer O: 4O _ XO O __ _ Xplayer X: 7O _ XO O _X _ Xplayer O: 6O _ XO O OX _ Xplayer X:

If you want me to post the executable, I will, but it's a Cygwin program and it may not work unless you have Cygwin installed....

Sorry for the long response....
ttt.c1 KB
sotsirh194 (author)  gmoon9 years ago
How can I get Cygwin. Is it free?
Yes, it's free. Here's the Cygwin site. You download the installer, then choose the programs/tools you need. If I recall correctly, there's a "development" tree, and you should install it all (the C compiler is gcc, but you should get Make and a lot of other stuff.)

It's essentially a Unix / Linux -style environment for the PC. And like Linux, the C/C++ compiler is part of every distribution.

You could also use a Linux distribution or a Linux "live" CD/DVD, which would include the same tools, and more....

Compiling a simple "C" program

The Cygwin "start" menu should include a way to start the shell...something like "Cygwin Bash Shell"; start it, and you're in an MS-DOS style environment (that's the "shell".)

The ttt.c program is really simple, so it doesn't need a Make file. You can compile it (from the Cygwin shell) by typing:

gcc -o ttt ttt.c

This will create a cmdline (shell) executable named "ttt" from the source file "ttt.c" (of course, the ttt.c file must be in the current dir / path to do so. You can move around in the dir tree by using the CD cmd (Current Directory.)

To execute the program type (also in the current dir):

./ttt

(usually the current directory isn't searched for executables, so the prefix ("./") tell the shell to look for "ttt" in the current dir.)
1-10 of 11Next »