Problem Running Tic Tac Toe game

Hello everyone! I have written a very simple code regarding the creation of a basic Tic Tac Toe game.

The program runs fine up until it is the 3rd turn of the player, in which the program keeps placing O's instead of alternating the turns between X's and O's. Could someone please help me find where the problem in the code lies?

Thank you!

(The code is done on a mac and using Xcode)


//
// main.cpp
// Example1
//
// Created by Francesca Petrini on 23/05/2018.
// Copyright © 2018 Francesca Petrini. All rights reserved.
//

#include <stdio.h>

void printboard (char a1, char a2, char a3, char a4, char a5, char a6, char a7, char a8, char a9)
{
printf(" %c | %c | %c\n", a1,a2,a3);
printf(" ------------\n");
printf(" %c | %c | %c\n",a4,a5,a6);
printf(" ------------\n");
printf(" %c | %c | %c\n",a7,a8,a9);
}

int main (void)
{
int i=1;
char b1= '1', b2='2', b3='3', b4='4', b5='5', b6='6', b7='7', b8='8', b9='9';
char input;
printboard(b1,b2,b3,b4,b5,b6,b7,b8,b9);

while (i<=9)

{
printf("give me the location:");
scanf("%c",&input);

if (input=='1' && b1=='1')
{if (i%2==1)
{
b1='X';
i++;
}
else
{
b1= 'O';
}
}

if (input=='2' && b2=='2')
{if (i%2==1)
{
b2='X';
i++;
}
else
{
b2='O';
}
}

if (input=='3' && b3=='3')
{if (i%2==1)
{
b3='X';
i++;
}
else
{
b3= 'O';
}
}

if (input=='4' && b4=='4')
{if (i%2==1)
{
b4='X';
i++;
}
else
{
b4= 'O';
}
}

if (input=='5' && b5=='5')
{if (i%2==1)
{
b5='X';
i++;
}
else
{
b5='O';
}
}

if (input=='6' && b6=='6')
{if (i%2==1)
{
b6='X';
i++;
}
else
{
b6= 'O';
}
}

if (input=='7' && b7=='7')
{if (i%2==1)
{
b7='X';
i++;
}
else
{
b7= 'O';
}
}

if (input=='8' && b8=='8')
{if (i%2==1)
{
b8='X';
i++;
}
else
{
b8= 'O';
}
}

if (input=='9' && b9=='9')
{if (i%2==1)
{
b9='X';
i++;
}
else
{
b9= 'O';
}
}
printboard (b1,b2,b3,b4,b5,b6,b7,b8,b9);

void winboard (char a1,char a2,char a3,char a4,char a5,char a6,char a7,char a8,char a9);
{ if (b1==b2 && b2==b3)
{printf("You Won!!\n");
}
}

{ if (b4==b5 && b5==b6)
{printf("You Won!!\n");
}
}

{ if (b7==b8 && b8==b9)
{printf("You Won!!\n");
}
}

{ if (b1==b5 && b5==b9)
{printf("You Won!!\n");
}
}

{ if (b3==b5 && b5==7)
{printf("You Won!!\n");
}
}

printboard (b1,b2,b3,b4,b5,b6,b7,b8,b9);

}

}



The turn, X or O, is controlled by the value of i.

When the program draws an X, it increments i.
When the program draws an O, it does not increments i.
Start i at 0. Then at the very beginning of your while loop, increment i by 1. Each time you start the loop again it will increment i by 1.
Topic archived. No new replies allowed.