Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <time.h>
/* Constants for TTT game board symbols. */
#define EMPTY ' '
#define USER 'O'
#define COMPUTER 'X'
#define TRUE 1
#define FALSE 0
#define MIN 1
#define MAX 9
/* function proto-types (these are function declarations)*/
int ValueInRange(int value, int min, int max);
int RowIsWinner(char s1, char s2, char s3);
char TTTopponent(char player);
int GetIntInput();
void DisplayHorizontalLine();
void DisplayOneRow(char s1, char s2, char s3);
void writetoboard( char TTTboard[], int squarenumber, char player);
void DisplayTTTBoards(char TTTboard[]);
int UserSaysYes(char[]);
char getCharInput();
void clearInputStream();
void userturn(char TTTboard[]);
void computerturn(char TTTboard[]);
int randIntEven(int, int);
int CheckPossRow(char TTTboard[]);
int BoardFull(char TTTboard[],int max);
void DisplayOneRowInt(int a, int b, int c);
int randInt(int, int);
int main()
{
char TTTboard[]={EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY};
printf("\t\tWELCOME TO TIC-TAC-TOE!!\n\n");
while ( UserSaysYes("WOULD YOU LIKE TO PLAY A GAME OF TIC TAC TOE WITH ME?"))
{
char currentplayer=USER;
DisplayOneRowInt(1,2,3);
DisplayHorizontalLine();
DisplayOneRowInt(4,5,6);
DisplayHorizontalLine();
DisplayOneRowInt(7,8,9);
do
{
userturn(TTTboard);
currentplayer=TTTopponent(currentplayer);
computerturn(TTTboard);
}while(CheckPossRow(TTTboard)==FALSE||BoardFull(TTTboard,MAX)==FALSE);
}
printf("\tThank you for playing TIC-TAC-TOE! See you soon!\n\n");
system("PAUSE");
return 0;
}
/**********************************************************************
This function uses two output functions display the actual array TTT board
PRE: none.
POST: three TTT boards have been drawn on the output stream.
*******************************************************************************/
void DisplayTTTBoards(char TTTboard[])
{
DisplayOneRow(TTTboard[0],TTTboard[1],TTTboard[3]);
DisplayHorizontalLine();
DisplayOneRow(TTTboard[3],TTTboard[4], TTTboard[5]);
DisplayHorizontalLine();
DisplayOneRow(TTTboard[6],TTTboard[7],TTTboard[8]);
}
/*******************************************************************************
********************************************************************************
This function gets a valid integer input from the user.
PRE: User has been prompted to enter an integer and min <= max.
POST: an integer value, i, entered by user is returned, where min <= i <= max.
*/
int GetIntInput()
{
int i;
do
{
printf("\n\nPlease enter number of the square you would like to place your 'O' in:");
while(scanf("%i", &i) != 1)
/* get rid of the characters that were entered */
{
clearInputStream();
printf("\nThat is not valid. Please enter a valid integer: ");
}
/* clear the input stream again, in case extra characters after the integer
were entered */
clearInputStream();
}while(ValueInRange(i,MIN,MAX)==FALSE);
return i;
}
/*******************************************************************************
********************************************************************************
This function takes the users choice of square and enters the appropriate character ('o' or 'x')
PRE:
POST:
*******************************************************************************/
void writetoboard( char TTTboard[], int squarenumber, char player)
{
if (player==USER)
{
squarenumber=(squarenumber-1);
TTTboard[squarenumber]='O';
}
else
{
TTTboard[squarenumber]='X';
}
}
/**********************************************************************
This function is for the users turn
*******************************************************************************/
void userturn(char TTTboard[])
{
int SquareNumber;
do
{SquareNumber=GetIntInput();
}while(SquareNumber=='X'||SquareNumber=='O');
writetoboard(TTTboard, SquareNumber,USER);
DisplayTTTBoards(TTTboard);
}
/*******************************************************************************
********************************************************************************
******************************************************************************
********************************************************************************
This function determines if a row of tic-tac-toe squares is a winning row.
PRE: s1, s2, and s3 are defined
POST: returns true if a row on
a tic-tac-toe board containing [s1, s2, s3],
is a winning row, and false otherwise.
*******************************************************************************/
int RowIsWinner(char s1, char s2, char s3)
{
if (s1==s2 && s2==s3 && s1!= EMPTY)
return TRUE;
else
return FALSE;
}
/*******************************************************************************
********************************************************************************
This Function checks for winning rows with the help of RowIsWinner
*******************************************************************************/
int CheckPossRow(char TTTboard[])
{
char Sq1=TTTboard[0];
char Sq2=TTTboard[1];
char Sq3=TTTboard[2];
char Sq4=TTTboard[3];
char Sq5=TTTboard[4];
char Sq6=TTTboard[5];
char Sq7=TTTboard[6];
char Sq8=TTTboard[7];
char Sq9=TTTboard[8];
if(RowIsWinner(Sq1,Sq2,Sq3)==TRUE)
return TRUE;
else if(RowIsWinner(Sq4,Sq5,Sq6)==TRUE)
return TRUE;
else if(RowIsWinner(Sq7,Sq8,Sq9)==TRUE)
return TRUE;
else if(RowIsWinner(Sq1,Sq4,Sq7)==TRUE)
return TRUE;
else if(RowIsWinner(Sq2,Sq5,Sq8)==TRUE)
return TRUE;
else if (RowIsWinner(Sq3,Sq6,Sq9)==TRUE)
return TRUE;
else if(RowIsWinner(Sq1,Sq5,Sq9)==TRUE)
return TRUE;
else if(RowIsWinner(Sq3,Sq5,Sq7)==TRUE)
return TRUE;
else
return FALSE;
}
/*******************************************************************************
********************************************************************************
This function checks to see if the board (array) is full or has empty spaces free
*******************************************************************************/
int BoardFull(char TTTboard[],int max)
{
int i;
for (i=0; i < max; i++)
{
if (TTTboard[i] == EMPTY)
{
return(FALSE); /* it was found */
}
}
return(TRUE); /* if it was not found */
}