Tic-Tac-Toe Arduino

I have recently been taking some arduino classes and the project I worked on was a 2 player Tic-Tac-Toe game that you play over serial. The schematic for it is attached and the image is here:
I have the code attached also but I will paste it here too.

int val = 0;int X = 0;
int Y = 0;
char val1 = ' ';
char val2 = ' ';
char val3 = ' ';
char val4 = ' ';
char val5 = ' ';
char val6 = ' ';
char val7 = ' ';
char val8 = ' ';
char val9 = ' ';
int player1X = 0;

int player2O = 0;
void setup(){
  Serial.begin(9600);
 Serial.setTimeout(100);
pinMode(0, OUTPUT); 
 pinMode(1, OUTPUT);
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, OUTPUT);
 pinMode(13, OUTPUT);
 pinMode(A0, OUTPUT);
 pinMode(A1, OUTPUT);
 pinMode(A2, OUTPUT);
 pinMode(A3, OUTPUT);
 pinMode(A4, OUTPUT);
 digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  analogWrite(A0, 0);
  analogWrite(A1, 0);
  analogWrite(A2, 0);
  analogWrite(A3, 0);
  analogWrite(A4, 0);
  analogWrite(A5, 0);
}
void loop(){
  Serial.print("X score is: ");
  Serial.println(X);
  Serial.println("");
  Serial.print("Y score is: ");
  Serial.println(Y); 
  Serial.println("");
Serial.println("  TIC-TAC-TOE  ");
Serial.println("0  1   2   3 Y");
Serial.println("1    ");Serial.print(val1);Serial.print(" | ");Serial.print(val2);Serial.print(" | ");Serial.println(val3);
Serial.println("___________ ");
Serial.println("2    ");Serial.print(val4);Serial.print(" | ");Serial.print(val5);Serial.print(" | ");Serial.println(val6);
Serial.println("___________ ");
Serial.println("3    ");Serial.print(val7);Serial.print(" | ");Serial.print(val8);Serial.print(" | ");Serial.println(val9);
Serial.println("             ");
Serial.println(" Please enter Player 1's selection");

while(Serial.available() == 0){
  
}

player1X = Serial.parseInt();

switch(player1X){
case 1:
if(val1 == ' '){val1 = 'X';analogWrite(A4, 255);analogWrite(A5, 0); }
break;
case 2:
if(val2 == ' '){val2 = 'X';digitalWrite(2, HIGH);digitalWrite(3, LOW);}
break;
case 3:
if(val3 == ' '){val3 = 'X';digitalWrite(4, HIGH);digitalWrite(5, LOW);}
break;
case 4:
if(val4 == ' '){val4 = 'X';digitalWrite(6, HIGH);digitalWrite(7, LOW);}
break;
case 5:
if(val5 == ' '){val5 = 'X';digitalWrite(8, HIGH);digitalWrite(9, LOW);}
break;
case 6:
if(val6 == ' '){val6 = 'X';digitalWrite(10, HIGH);digitalWrite(11, LOW);}
break;
case 7:
if(val7 == ' '){val7 = 'X';digitalWrite(12, HIGH);digitalWrite(13, LOW);}
break;
case 8:
if(val8 == ' '){val8 = 'X';analogWrite(A0, 255);analogWrite(A1, 0);}
break;
case 9:
if(val9 == ' '){val9 = 'X';analogWrite(A2, 255);analogWrite(A3, 0);}
  break;
default:
break;}

Serial.println(" I received a");
Serial.print(player1X);
if(val1 == 'X' && val4 == 'X' && val7 == 'X'){
val = 1;Serial.println("Player 1 Won via way 1!");X++;restart(); 
}
if(val2 == 'X' && val5 == 'X' && val8 == 'X'){
val = 1;Serial.println("Player 1 Won via way 2!");X++;restart(); 
}

if(val3 == 'X' && val6 == 'X' && val9 == 'X'){
val = 1;Serial.println("Player 1 Won via way 3!"); X++;restart();
}

if(val1 == 'X' && val2 == 'X' && val3 == 'X'){
val = 1;Serial.println("Player 1 Won via way 4!"); X++;restart();
}

if(val4 == 'X' && val5 == 'X' && val6 == 'X'){
val = 1;Serial.println("Player 1 Won via way 5!"); X++;restart();
}

if(val7 == 'X' && val8 == 'X' && val9 == 'X'){
val = 1;Serial.println("Player 1 Won via way 6!"); X++;restart();
}

if(val1 == 'X' && val5 == 'X' && val9 == 'X'){
val = 1;Serial.println("Player 1 Won via way 7!"); X++;restart();
}

if(val3 == 'O' && val5 == 'O' && val7 == 'O'){
val = 1;Serial.println("Player 1 Won via way 8!"); X++;restart();}

 if(val == 0){
 Serial.print("X score is: ");
  Serial.println(X);
  Serial.println("");
  Serial.print("Y score is: ");
  Serial.println(Y);
  Serial.println("");
Serial.println("  TIC-TAC-TOE  ");
Serial.println("0  1   2   3 Y");
Serial.println("1    ");Serial.print(val1);Serial.print(" | ");Serial.print(val2);Serial.print(" | ");Serial.println(val3);
Serial.println("___________ ");
Serial.println("2    ");Serial.print(val4);Serial.print(" | ");Serial.print(val5);Serial.print(" | ");Serial.println(val6);
Serial.println("___________ ");
Serial.println("3    ");Serial.print(val7);Serial.print(" | ");Serial.print(val8);Serial.print(" | ");Serial.println(val9);
Serial.println("             ");

Serial.println(" Please enter Player 2's selection");
while(Serial.available() == 0){
  
}

  player2O = Serial.parseInt();

switch(player2O){
case 1:
if(val1 == ' '){val1 = 'O';analogWrite(A5, 255);analogWrite(A4, 0); }
break;
case 2:
if(val2 == ' '){val2 = 'O';digitalWrite(3, HIGH);digitalWrite(2, LOW);}
break;
case 3:
if(val3 == ' '){val3 = 'O';digitalWrite(5, HIGH);digitalWrite(4, LOW);}
break;
case 4:
if(val4 == ' '){val4 = 'O';digitalWrite(7, HIGH);digitalWrite(6, LOW);}
break;
case 5:
if(val5 == ' '){val5 = 'O';digitalWrite(9, HIGH);digitalWrite(8, LOW);}
break;
case 6:
if(val6 == ' '){val6 = 'O';digitalWrite(11, HIGH);digitalWrite(10, LOW);}
break;
case 7:
if(val7 = ' '){val7 = 'O';digitalWrite(13, HIGH);digitalWrite(12, LOW);}
break;
case 8:
if(val8 == ' '){val8 = 'O';analogWrite(A1, 255);analogWrite(A0, 0);}
break;
case 9:
if(val9 == ' '){val9 = 'O';analogWrite(A3, 255);analogWrite(A2, 0);
break;
default:
break;}


if(val1 == 'O' && val4 == 'O' && val7 == 'O'){
Serial.println("Player 2 Won via way 1!");Y++;restart();
}
if(val2 == 'O' && val5 == 'O' && val8 == 'O'){
Serial.println("Player 2 Won via way 2!"); Y++;restart();
}

if(val3 == 'O' && val6 == 'O' && val9 == 'O'){
Serial.println("Player 2 Won via way 3!"); Y++;restart();
}

if(val1 == 'O' && val2 == 'O' && val3 == 'O'){
Serial.println("Player 2 Won via way 4!"); Y++;restart();
}

if(val4 == 'O' && val5 == 'O' && val6 == 'O'){
Serial.println("Player 2 Won via way 5!");Y++;restart(); 
}

if(val7 == 'O' && val8 == 'O' && val9 == 'O'){
Serial.println("Player 2 Won via way 6!"); Y++;restart();
}

if(val1 == 'O' && val5 == 'O' && val9 == 'O'){
Serial.println("Player 2 Won via way 7!"); Y++;restart();
}

if(val3 == 'O' && val5 == 'O' && val7 == 'O'){
Serial.println("Player 2 Won via way 8!");Y++;restart(); 
}}}else{}}
void declare(){ val1 = ' ';
 val = 0; val2 = ' ';
 val3 = ' ';
 val4 = ' ';
 val5 = ' ';
 val6 = ' ';
 val7 = ' ';
 val8 = ' ';
 val9 = ' ';
 player1X = 0;

 player2O = 0;
};

void Setup(){Serial.begin(9600);
pinMode(0, OUTPUT); 
 pinMode(1, OUTPUT);
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, OUTPUT);
 pinMode(13, OUTPUT);
 pinMode(A0, OUTPUT);
 pinMode(A1, OUTPUT);
 pinMode(A2, OUTPUT);
 pinMode(A3, OUTPUT);
 pinMode(A4, OUTPUT);
 digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  analogWrite(A0, 0);
  analogWrite(A1, 0);
  analogWrite(A2, 0);
  analogWrite(A3, 0);
  analogWrite(A4, 0);
  analogWrite(A5, 0);};

void restart(){
  declare();
  Setup();
};

I know it seems like a lot of code but it is pretty simple to understand just reading values and calculating numbers. Oh and a note about playing it. Download it to your arduino then open the serial monitor. The first player to go is X and the second player is O. to enter your selection enter the number by counting from the top left examples: the middle one is 5 the bottom left spot is 7 and the top right is 3. Please post any questions you have in the comments and I will reply to you as soon as I can. 

Comments

  1. how do i play this game in an arduino 2560 with push butons (one or two hardware butons with red green bicolor leds?)

    ReplyDelete
    Replies
    1. Thanks for reading my Blog! This program works by inputting your selection over serial and selecting the spot 1 through 9. So if you wanted to use pushbuttons, you would have to have at least 9. As for the bicolor LEDs, depending on how they work, you should be able to just wire the signal for both the "O" and the "X" for that spot into the LED. If you need to know how to wire the LED, I would check its datasheet.
      Thanks,

      Delete
  2. How does this work?? I might use this for a project. But how do you control the game like, where are the buttons??

    ReplyDelete

Post a Comment