Making Tic Tac Toe Smarter
I've googled for tic tac toe AI and I know what I have to do but I'm not sure how I'm going to....... code it :rolleyes:
All I have to do is give each empty box a 'rank' on the basis of the current set up of the game and then hit the box with the highest rank. I can't think of how I am going to achieve this probably because of the bad design of the game I've made so far.
- I have used 1D arrays instead of 2D arrays. Now I can't think of a way to access each box , evaluate other boxes in the same row and column and rank that particular box. (right now all it does is picks a random empty box and marks it)
- I used 'extern' excessively and I've heard that's bad (I don't know why but I find pointers more difficult to use :( ).
Anyway, I'm posting my code, let me know what you think and what I should change.
main.c
Code:
#include<stdio.h>
#include "myheader.h"
char box[9]={'1','2','3','4','5','6','7','8','9'};
char human,ai;
int empty[9]={TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE};
int gameover=FALSE,num;
int main()
{
char ch;
/*WELCOME SCREEN*/
printf("\n\n\n\n*******Tic Tac Toe*******\n");
/*SELECT SYMBOL*/
printf("\n1) X \n2) O\n");
printf("\n\nPlease select your symbol(Enter 1 or 2):");
do
{
scanf("%d",&num);
if(num!=1&&num!=2)
printf("\nInvalid choice, try again:");
while( (ch = getchar()) != '\n' && ch != EOF);
}while(num!=1&&num!=2);
if(num==1)
{
human='X';
ai='O';
}
else
{
human='O';
ai='X';
}
/*END SELECT SYMBOL*/
printf("\n OK, you will play with %c, and my super dumb AI will fill random boxes with %c",human,ai);
printf("\n\nThis is the playing area:\n");
while(!gameover)
{
gui();
getinput();
gameover=check();
if(!gameover)
playai();
}
printf("\nThanks for playing\n");
return 0;
}
ai.c
Code:
#include<stdlib.h>
#include<time.h>
#include "myheader.h"
void playai()
{
extern char box[9];
extern int empty[9];
extern char ai;
int r;
do
{
srand(time(NULL));
r=rand()%9;
}
while(empty[r]!=TRUE);
empty[r]=FALSE;
box[r]=ai;
}
check.c
Code:
#include<stdio.h>
#include "myheader.h"
int check()
{
int x;
extern char human;
extern int empty[9];
extern char box[9];
for(x=0;x<=6;x+=3)
{
if((box[x]==box[x+1])&&(box[x+2]==box[x])&&(box[x]=='X'||box[x]=='O'))
{
gui();
if(box[x]==human)
printf("\nYou won!");
else
printf("\nAI won!");
return TRUE;
}
}
for(x=0;x<=2;x++)
{
if((box[x]==box[x+3])&&(box[x+3]==box[x+6])&&(box[x]=='X'||box[x]=='O'))
{
gui();
if(box[x]==human)
printf("\nYou won!");
else
printf("\nAI won!");
return TRUE;
}
}
if((((box[0]==box[4])&&(box[4]==box[8]))||((box[2]==box[4])&&(box[4]==box[6])))&&\
(box[4]=='X'||box[4]=='O'))
{
gui();
if(box[4]==human)
printf("\nYou won!");
else
printf("\nAI won!");
return TRUE;
}
for(x=0;x<=8;x++)
{
if(empty[x]==TRUE)
return FALSE;
}
printf("\n Tie!\n");
return TRUE;
}
getinput.c
Code:
#include<stdio.h>
#include "myheader.h"
void getinput()
{
int choice;
extern char human;
extern char box[9];
extern int empty[9];
char ch;
start:
printf("\nEnter your choice(1-9):");
scanf("%d",&choice);
while( (ch = getchar()) != '\n' && ch != EOF);
if(choice>0 && choice<10 && empty[NUMBER]==TRUE)
{
empty[NUMBER]=FALSE;
/*if(p==1)
box[NUMBER]='x';
else
box[NUMBER]='o';*/
box[NUMBER]=human;
}
else
{
printf("\nNope, enter again");
goto start;
}
}
gui.c
Code:
#include<stdio.h>
void gui()
{
extern char box[9];
printf("\n\n\n\n");
printf("\n\t%c\t%c\t%c",box[0],box[1],box[2]);
printf("\n");
printf("\n\t%c\t%c\t%c",box[3],box[4],box[5]);
printf("\n");
printf("\n\t%c\t%c\t%c",box[6],box[7],box[8]);
}
myheader.h
Code:
#ifndef H_MYHEADER
#define H_MYHEADER
#define NUMBER choice-1
#define TRUE 1
#define FALSE 0
void getinput();
int check();
void gui();
void playai();
#endif