Please help me reduce its complexity and maybe optimize it a little bit more.
#include <stdio.h>
#include <string.h>
#include <time.h>
#define size 3
void Clear_board(char (*)[size]);
void Player_turn(char (*)[size],char);
void PC_turn(char (*)[size],char,char,int*);
void Print_board(char (*)[size]);
void Scan_corners(char (*)[size],char,char);
int Check_board(char (*)[size],char,int*);
int Action(char (*)[size],char,char,int);
int Scan_diag(char (*)[size],char,char,int);
int Scan_rowscols(char (*)[size],char,char,int);
int main()
{
//-------------------------
char xo[size][size]={'1','2','3','4','5','6','7','8','9'};
char Fig_PC='X';
char Fig_Player='O';
int PC_move=0;
//-------------------------
printf("Welcom to Tic Tac Toe \n");
Print_board(xo);
Clear_board(xo);
//PC plays first and increments PC_move
PC_turn(xo,Fig_PC,Fig_Player,&PC_move);
//While nobody won, or PC reaches max move game is on
while ((Check_board(xo,Fig_PC,&PC_move)))
{
Player_turn(xo,Fig_Player);
PC_turn(xo,Fig_PC,Fig_Player,&PC_move);
}
return 0;
}
void PC_turn(char (*xo)[size],char Fig_PC,char Fig_Player,int *PC_move)
{
printf("Computer's Turn:\n");
/*
First try to win by completing an X X X
Second priority is to defend
Lastly try to take postion of corners
*/
if (!Action(xo,Fig_PC,Fig_Player,1))
{
if (!Action(xo,Fig_Player,Fig_PC,0))
{
Scan_corners(xo,Fig_PC,Fig_Player);
}
}
Print_board(xo);
++*PC_move;
}
void Print_board(char (*xo)[size])
{
int i=0;
printf("\n\n | | \n");
printf(" %c | %c | %c\n", xo[2][0], xo[2][1], xo[2][2]);
printf("___|___|___ \n");
printf(" | | \n");
printf(" %c | %c | %c\n", xo[1][0], xo[1][1], xo[1][2]);
printf("___|___|___ \n");
printf(" | | \n");
printf(" %c | %c | %c\n", xo[0][0], xo[0][1], xo[0][2]);
printf(" | | \n\n\n");
usleep(1000000);
}
void Clear_board(char (*xo)[size])
{
int i=0;
int j=0;
for (i=0;i<size;++i)
{
for (j=0;j<size;++j)
{
xo[i][j]=' ';
}
}
}
void Player_turn(char (*xo)[size], char Fig_Player)
{
// Put O where player wishes, unless illegal entry or taken cell.
int choice=0;
printf("player's Turn:\n");
printf("choose %c position\n", Fig_Player);
while ((choice<1) || (choice>9))
{
scanf("%d",&choice);
if ((choice>0) && (choice<10) && (xo[(choice-1)/size][(choice-1)%size]==' '))
{
xo[(choice-1)/size][(choice-1)%size]=Fig_Player;
Print_board(xo);
}
else
{
printf("Please try again\n");
}
}
}
int Action(char (*xo)[size],char Fig_PC,char Fig_Player, int Action)
{
// If any action taken in rows/cols/diags pass 1 so that subsequent actions won't execute.
// Try to win, or defend based on Action variable passed from below.
int diags=0;
int rowscols=0;
diags = Scan_diag(xo,Fig_PC,Fig_Player,Action);
rowscols = Scan_rowscols(xo,Fig_PC,Fig_Player,Action);
if (rowscols || diags)
{
return 1;
}
return 0;
}
int Scan_diag(char (*xo)[size],char Attk_fig,char Def_fig,int Action)
{
int i=0;
int index=0;
int count_diag=0;
for (i=0;i<size;++i)
{
if (xo[i][i]==Attk_fig)
{
++count_diag;
}
else if (xo[i][i]==' ')
{
count_diag+=100;
index=i;
}
}
if (count_diag==102)
{
if (Action)
{
xo[index][index]=Attk_fig;
return 1;
}
xo[index][index]=Def_fig;
return 1;
}
return 0;
}
int Scan_rowscols(char (*xo)[size],char Attk_fig,char Def_fig,int Action)
{
int i=0;
int j=0;
int index1=0;
int index2=0;
int count_row=0;
int count_col=0;
for (i=0;i<size;++i)
{
for (j=0;j<size;++j)
{
// count in rows
if (xo[i][j]==Attk_fig)
{
++count_row;
}
else if (xo[i][j]==' ')
{
count_row+=100;
index1=j;
}
// count in cols
if (xo[j][i]==Attk_fig)
{
++count_col;
}
else if (xo[j][i]==' ')
{
count_col+=100;
index2=j;
}
}
if (count_row==102)
{
if (Action)
{
xo[i][index1]=Attk_fig;
return 1;
}
else
{
xo[i][index1]=Def_fig;
return 1;
}
}
else
{
count_row=0;
}
if (count_col==102)
{
if (Action)
{
xo[index2][i]=Attk_fig;
return 1;
}
else
{
xo[index2][i]=Def_fig;
return 1;
}
}
else
{
count_col=0;
}
}
return 0;
}
void Scan_corners(char (*xo)[size],char Fig_PC,char Fig_Player)
{
int i=0;
int j=0;
int ii=0;
int jj=0;
int count=0;
for (i=0;i<size;i+=(size-1))
{
for (j=0;j<size;j+=(size-1))
{
if (xo[i][j]==' ')
{
++count;
ii=i;
jj=j;
}
}
}
switch (count)
{
case 1:
xo[ii][jj]=Fig_PC;
break;
case 2:
if ((xo[0][size-1]==Fig_PC) || (xo[0][size-1]==Fig_Player))
{
xo[0][0]=Fig_PC;
}
else
{
xo[0][size-1]=Fig_PC;
}
break;
case 3:
if (xo[((size-1)/2)][((size-1)/2)]==Fig_Player)
{
xo[0][size-1]=Fig_PC;
}
else if (xo[size-2][0]==Fig_Player)
{
xo[size-1][size-1]=Fig_PC;
}
else
{
xo[0][0]=Fig_PC;
}
break;
case 4:
xo[size-1][0]=Fig_PC;
break;
}
}
int Check_board(char (*xo)[size],char Fig_PC, int *PC_move)
{
int i=0;
int j=0;
int count_row=0;
int count_col=0;
int count_diag1=0;
int count_diag2=0;
if (*PC_move==5)
{
printf("It's a draw\n\n");
return 0;
}
for (i=0;i<size;++i)
{
if (xo[i][i]==Fig_PC)
{
++count_diag1;
}
if (xo[i][(size-1)-i]==Fig_PC)
{
++count_diag2;
}
for (j=0;j<size;++j)
{
if (xo[i][j]==Fig_PC)
{
++count_row;
}
if (xo[j][i]==Fig_PC)
{
++count_col;
}
}
if ((count_row==size) || (count_col==size))
{
printf("PC wins\n\n");
return 0;
}
else
{
count_row=0;
count_col=0;
}
}
if ((count_diag1==size) || (count_diag2==size))
{
printf("PC wins\n\n");
return 0;
}
return 1;
}