#include <stdio.h>
#include<stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include<ctype.h>
// Constants for TTT game board symbols.
#define EMPTY ' '
#define USER 'O'
#define COMPUTER 'X'
// function proto-types (these are function declarations).
// Value returning funcitons tested by the automated driver.
bool ValueInRange(int value, int min, int max);
bool RowIsWinner(char s1, char s2, char s3);
char TTTopponent(char player);
// I/O functions -- manually tested.
int GetIntInput(int min, int max);
void DisplayHorizontalLine();
void DisplayOneRow(char s1, char s2, char s3);
// Utility functions used by the automated test driver:
//bool UserSaysYes(string question);
void Report(int, bool, char[]);
void DisplayTTTBoards();
bool UserSaysYes(char[]);
char getCharInput();
void clearInputStream();
//*****************************************************
//*****************************************************
//**** Test driver / Test oracle for functions *******
//**** DO NOT CHANGE THE CODE IN THE MAIN FUNCTION!!!**
//*****************************************************
//*****************************************************
int main()
{
// Test oracle: this portion will run some automated tests and report the results.
// Automated tests for ValueInRange()
printf("\n Running test oracle for ValueInRange() function...\n");
printf( "These test cases should return true...\n");
Report(1, ValueInRange(5,1,10), "ValueInRange(5,1,10)");
Report(2, ValueInRange(1,1,1), "ValueInRange(1,1,1)");
printf( "These test cases should return false...\n" );
Report(3, ! ValueInRange(-1,1,10), "ValueInRange(-1,1,10)");
Report(4, ! ValueInRange(101,-100,100), "ValueInRange(101,-100,100)");
// Automated tests for RowIsWinner()
printf("\n Running test oracle for RowIsWinner() function...\n" );
printf( "These test cases should return true...\n");
Report(1, RowIsWinner('X', 'X', 'X'), "RowIsWinner('X', 'X', 'X')");
Report(2, RowIsWinner('a', 'a', 'a'), "RowIsWinner('a', 'a', 'a')");
printf( "These test cases should return false...\n");
Report(3, !RowIsWinner(' ', 'X', 'O'), "RowIsWinner(' ', 'X', 'O')");
Report(4, !RowIsWinner(' ', ' ', ' '), "RowIsWinner(' ', ' ', ' ')");
printf( "\n Running test oracle for TTTopponent() function...\n");
Report(1, TTTopponent(COMPUTER) == USER, "TTTopponent(COMPUTER) should return USER");
Report(2, TTTopponent(USER) == COMPUTER, "TTTopponent(USER) should return COMPUTER");
// Interactive Test driver: this portion will allow you to run some custom tests.
int val, min, max;
char s1, s2, s3;
printf( "\n Ready to run interactive tests...\n\n");
while ( UserSaysYes("Do you want to run another test?") )
{
printf( "Testing GetIntInput() function...\n");
printf("Enter a minimum bound (any integer):");
min = GetIntInput(INT_MIN, INT_MAX);
printf("Enter a maximum bound (must be >= %d )", min);
max = GetIntInput(min, INT_MAX);
printf( "Enter a value between %d and %d ", min ,max);
val = GetIntInput(min, max);
printf( "ValueInRange(%d, %d, %d) returned %s \n", val, min ,max,
(ValueInRange(val,min,max)?"true":"false"));
printf( "\n\n Enter values(characters) for three squares from a TTT board:\n ");
s1 = getCharInput();
s2= getCharInput();
s3 = getCharInput();
printf( "That would %s be considered a winning TTT row\n", (RowIsWinner(s1, s2, s3)?" ":" NOT "));
}
printf( "Testing functions to display TTT board...\n" );
DisplayTTTBoards();
system("PAUSE");
return 0;
}
//*****************************************************
//*** Function stubs are provide below for the six ***
//*** functions that you need to write. ***
//*****************************************************
// This function determines if a value is within a range.
// PRE: min <= max and value is defined
// POST: returns true if min <= value <= max, and false otherwise.
bool ValueInRange(int val, int min, int max)
{
return( val>= min && val<= max);
}
// 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 (horizontal, vertical, or diagonal) on
// a tic-tac-toe board containing [s1, s2, s3],
// is a winning row, and false otherwise.
bool RowIsWinner(char s1, char s2, char s3)
{
// Your code goes here
// Note: this function may assume that an empty square
// contains the value EMPTY, but should make no assumptions
// about how the other game pieces (X's and O's) are represented!
return (s1==s2 && s2==s3 && s3!= EMPTY);
}
// This function returns the opponent of the player.
// PRE: player == COMPUTER or player = USER
// POST: returns the opponent of player.
char TTTopponent(char player)
{
// Your code goes here
if(player == COMPUTER)
{
return USER;
}
else
{
return COMPUTER; // dummy return value -- delete this.
}
}
int GetIntInput(int min, int max)
{
// Your code goes here
// Note: you should call your ValueInRange function to validate the user's input!
// This function must also check that the user enters integer data,
// and issue an error message and re-prompt the user if they make a mistake.
int val;
printf("Please enter an integer:\n");
if (val!= // if it's not an integer )
{
printf ( "Invalid input! Please enter your value again:\n" );
scanf ("%i", &val);
}
else
{
if(ValueInRange( val, min, max))
{
return val;
}
else
{
printf("This number is not in range!\n");
}
}
return(0);
}