Tic Tac Toe program error

Hi there,
I am trying to write a program that plays tic tac toe with you but the serial is not doing what I want it to…
This is the code for it

char s1grid = 'Q';
int s1 = 0;
char s2grid = 'Q';
int s2 = 0;
char s3grid = 'Q';
int s3 = 0;
char s4grid = 'Q';
int s4 = 0;
char s5grid = 'Q';
int s5 = 0;
char s6grid = 'Q';
int s6 = 0;
char s7grid = 'Q';
int s7 = 0;
char s8grid = 'Q';
int s8 = 0;
char s9grid = 'Q';
int s9 = 0;
void setup(){
  Serial.begin(9600);
}
void loop(){
  
  
  Serial.println("   Tic Tac Toe");
  Serial.println(" ");Serial.print(s1grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s2grid);Serial.print(" ");Serial.print("|");Serial.println(" ");Serial.print(s3grid);Serial.print("      ");
  Serial.println("--------------");
  Serial.println("          ");
  Serial.println(" ");Serial.print(s4grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s5grid);Serial.print(" ");Serial.print("|");Serial.println(" ");Serial.print(s6grid);Serial.print("       ");
  Serial.println("--------------");
  Serial.println("          ");
  Serial.println(" ");Serial.print(s7grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s8grid);Serial.print(" ");Serial.print("|");Serial.println(" ");Serial.print(s9grid);Serial.print("        ");
  delay(3000);
  
  
}

This is what it shows on the serial monitor
Tic Tac Toe

Q | Q |
Q --------------

Q | Q |
Q --------------

Q | Q |
Q

It should be:

Tic Tac Toe
Q | Q | Q

Q | Q | Q

Q | Q | Q

( the q’s take up the space where an X or an O will be so that is why it is all Q’s)

Serial.println(" ");Serial.print(s3grid)

The Serial.println before you print s3grid should be a Serial.print. Same error copied and pasted for the other two lines.

Looking at arrays would be a good idea.

thx

ok well here's my other problem. I'm trying to get it to read whatever number the user puts in (1-9) but when I put in the number 5 it gave me this: Tic Tac Toe

Player 1 please enter your selection. The available sections are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 53 Tic Tac Toe

| |

| X | X

X | X | X Player 1 please enter your selection. The available sections are: 1, 2, 3, 4,

53 is the number 5 in ascii and it printed it so that I could see which number was received. It should put an X only in the center square( number 5). This is the code:

char s1grid = ' ';
int s1 = 0;
char s2grid = ' ';
int s2 = 0;
char s3grid = ' ';
int s3 = 0;
char s4grid = ' ';
int s4 = 0;
char s5grid = ' ';
int s5 = 0;
char s6grid = ' ';
int s6 = 0;
char s7grid = ' ';
int s7 = 0;
char s8grid = ' ';
int s8 = 0;
char s9grid = ' ';
int s9 = 0;
int selection = 0;
void setup(){
  Serial.begin(9600);
 printgrid();
 }
void loop(){
  Serial.println();
  Serial.println("Player 1 please enter your selection."); Serial.println(" The available sections are: ");
  switch(s1grid){
  case ' ':
  Serial.print("1, ");
break;}
 switch(s2grid){
  case ' ':
  Serial.print("2, ");
break;}
switch(s3grid){
  case ' ':
  Serial.print("3, ");
break;}
switch(s4grid){
  case ' ':
  Serial.print("4, ");
break;}
switch(s5grid){
  case ' ':
  Serial.print("5, ");
break;}
switch(s6grid){
  case ' ':
  Serial.print("6, ");
break;}
switch(s7grid){
  case ' ':
  Serial.print("7, ");
break;}
switch(s8grid){
  case ' ':
  Serial.print("8, ");
break;}
switch(s9grid){
  case ' ':
  Serial.print("9, ");
break;}
  delay(5000);
  if(Serial.available() > 0){
    selection = Serial.read();
  }Serial.println(selection);
  switch(selection){
    case 49:
    if(s1grid == ' '){
      s1grid = 'X';}
    case 50:
    if(s2grid == ' '){
      s2grid = 'X';}
     case 51:
    if(s3grid == ' '){
      s3grid = 'X';}
     case 52:
    if(s4grid == ' '){
      s4grid = 'X';}
     case 53:
    if(s5grid == ' '){
      s5grid = 'X';}
     case 54:
    if(s6grid == ' '){
      s6grid = 'X';}
     case 55:
    if(s7grid == ' '){
      s7grid = 'X';}
     case 56:
    if(s8grid == ' '){
      s8grid = 'X';}
     case 57:
    if(s9grid == ' '){
      s9grid = 'X';} 
  break;}
  printgrid();
  
}

int printgrid(){
  
  Serial.println("   Tic Tac Toe");
  Serial.println("  ");Serial.print(s1grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s2grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s3grid);Serial.print(" ");
  Serial.println();
  Serial.println("--------------");
  Serial.println(" ");Serial.print(s4grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s5grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s6grid);Serial.print(" ");
  Serial.println();
  Serial.println("--------------");
  Serial.println("  ");Serial.print(s7grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s8grid);Serial.print(" ");Serial.print("|");Serial.print(" ");Serial.print(s9grid);Serial.print(" ");
}

Look up how the switch statement is used, paying particular attention to the use of break.

There are a bunch of problems in the code, but let's look at one important one first. Your code has:

 if(Serial.available() > 0){
    selection = Serial.read();
  } Serial.println(selection);
  switch(selection){
    case 49:
    if(s1grid == ' '){
      s1grid = 'X';}
    case 50:
    if(s2grid == ' '){
      s2grid = 'X';}

Which says that, even if there is no serial data to read, pretend there is and fall into the switch statement. This needs to be changed so nothing happens until Serial.available() is greater than 0. Next, switch statements are of the form:

switch (selection)
{
   case 49:
      // Whatever code you want associated with 49
     break;
   case 50:
     // Whatever code you want associated with 50
     break;
   default:
      // Do this if no case value is matched
      break;
}

Your code has a lot of dangling parentheses plus you don't use any break statements, which means the code falls through and executes all of the case statements. Fix those up and then see if you can use arrays to track the moves.

   case 49:

and

   case '1':

do exactly the same thing.

One is obvious. One is not. I'll leave it to you to determine which is which.