Hi I am a beginner and l would love to hear your opinion on my first C project, how can it be improved in any way, for example dynamic memory allocation isn't used in the program because I didn't quite understand where to allocate it, also I couldn't figure how to solve the problem that if input isn't an integer the program goes into an endless loop
Thanks for anyone who helps
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<ctype.h>
void print();
void change(int num, int a);
void restart();
bool draw();
bool win();
bool check(char a[]);
bool CheckInput(int num);
char a[9] = {49,50,51,52,53,54,55,56,57};
void main(){
int b[9] = {1,2,1,2,1,2,1,2,1};
int num;
restart();
for(int i = 0; i < 9; i++)
{
system("cls");
print();
printf("player %d enter a number option ",b[i]);
scanf("%d",&num);
change(num,b[i]);
if(i == 8)
{
system("cls");
print();
}
if(win() == true)
{
print();
printf("payer %d wins",b[i]);
return;
}
}
printf("its a draw");
return;
}
bool CheckInput(int num)
{
char (*str)[9] = &a;
if(num > 9 || num < 1)
return false;
else if(*(*str+(num-1)) == 79 || *(*str+(num-1)) == 88)
return false;
return true;
}
void change(int num, int p){
char (*str)[9] = &a;
int n = 0;
if(CheckInput(num) == true)
{
if(p == 1)
*(*str+(num-1)) = 88;
else
*(*str+(num-1)) = 79;
return;
}
else
printf("\nincorrect enter a new number ");
scanf("%d",&n);
change(n,p);
}
void print()
{
printf("\n");
int j = 0;
for (int i = 0; i < 2; i++)
{
if(i == 1)
j = 3;
printf(" %c | %c | %c\n",a[j],a[j+1],a[j+2]);
printf("_____|_____|_____\n");
printf(" | |\n");
}
j = 6;
printf(" %c | %c | %c\n",a[j],a[j+1],a[j+2]);
}
void restart()
{
char (*str)[9] = &a;
static int j = 49;
for(int i = 0; i < 9; i++)
{
*(*str + i) = j;
j++;
}
print();
}
bool win(){
char (*str)[9] = &a;
char b[3];
char c[3];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
b[j] = *(*str + i * 3 + j);//columns
c[j] = *(*str + j * 3 + i);//rows
}
if(check(b) == true || check(c) == true)
return true;
}
int j =0;
for(int i =0; i<3; i++)
{
b[i] = *(*str + i * 3 + j);//left to right across
j++;
}
int k = 2;
for(int i =0; i < 3; i++)
{
c[i] = *(*str + i*3 + k);//right to left across
k--;
}
if(check(b) == true || check(c) == true)
return true;
return false;
}
bool check(char a[3]){
int count = 0;
int count2 = 0;
for(int i =0; i<3; i++)
{
if(a[i] == 88)
count++;
else if(a[i] == 79)
count2++;
}
if(count != 3 && count2 !=3)
return false;
return true;
}