Here is my assignment.
5. Assignment: Using the code given below develop a solution to the game Tic-Tac-Toe.
a. The game is played between a human user and the computer. The computer’s moves are made randomly. You must get 3-in-a-row to win; getting five of the nine squares is a tie game.
b. Use proper designing strategies. All input and output should be done in a single module (I/O should not be done in strategy modules). Have an introduction screen that explains how to play the game, let the user know the computer’s move at each turn, and have an ending screen announcing the winner.
6. Complete all function stubs and correct the logic of the main() to obtain a working program.

The starting code;

#include <iostream>
#include <ctime>
using namespace std; 
//Purpose: Prints to the screen the an introduction and 
// provides the rules of the game
//Input: None
//Output: None
void Intro()                                
{
}
//Purpose: allows the computer to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the computer's move 
void ComputerMove(char B[][3])     
{ int i=0, turn=0;      
      cout<<"\n IN COMPUTER MOVE \n";
 //while (turn==0)     //Make sure the move is ok
      { 
      i=rand()%9+1;     //randomize a move
      if (i==1)      // make sure the first location is not taken
        {
            if ((B[0][0]!='x')&&(B[0][0]!='o'))
                  {     B[0][0]='o';  //if not taken, then move
                        turn=1;
                  }           
        }
      /*********
      repeat the above check for all locations
      ***********/
      }
}





//Purpose: allows the user to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the human's move 
void HumanMove(char B[][3]) 
{int i=0, turn=0;
cout<<"\n IN HUMAN MOVE \n";
/*************
// the logic is similar to the computer move except that 
// it takes the location from the keyboard
****************/
}
//Purpose: Print out the board
//Input: B[][] a 2 dimensional array that represents the board
//Output: None
void PrintBoard(char B[][3]) 
{     cout<<"\n   "<<B[0][0]<<"|   "<<B[0][1]<<"|   "<<B[0][2];   
      cout<<"\n_________________";
      cout<<"\n   "<<B[1][0]<<"|   "<<B[1][1]<<"|   "<<B[1][2];
      cout<<"\n_________________";
      cout<<"\n   "<<B[2][0]<<"|   "<<B[2][1]<<"|   "<<B[2][2];
      cout<<"\n\n";
}
//Purpose: Check for a winner
//Input: B[][] a 2 dimensional array that represents the board
//Output: win and integer indicating winner status. 
//       0=no winner, 1= human winner, 2= computer winner
int CheckWin(char B[][3])
{     int win = 0;                                    
      cout<<"\n IN CHECK WIN "<<win<<endl;
      return (win);
}
void main ()
{int choice=0, done=0, t=0, w=0;                                              
char Board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
srand(time(0));
Intro();
PrintBoard(Board);
cout<<"Do you want to go first? Enter 1 for yes, 2 for no";
cin>>choice;
      if (choice==1)                                  //if player wishes to go first
      { 
            do{ cout<<"\n t = "<<t<<endl;
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)                           
               cout<<"Youwin!";                                   
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            ComputerMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            t=t+1;
            }while((t!=9)||(w!=0));                                     
      }
      else                    
      {
            do{   cout<<"\n t = "<<t<<endl;
            ComputerMove(Board);
            w=CheckWin(Board);
           if(w==1)                                                              		cout<<"You Win!";
           else if (w==2)
                  cout<<"You lose!";
           else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            t=t+1;
      	}while((t!=9)||(w!=0));                               
      }
}

I took this code did all the hard work below;

//Michelle Stokes CSI130 Lab 13
//Code from hand out Lab14 TTT

#include <iostream>
#include <ctime>
using namespace std; 
//Purpose: Prints to the screen the an introduction and 
// provides the rules of the game
//Input: None
//Output: None
void Intro()                                
{
	cout << "This game is played between a human user and the computer. \n" <<  "The computers moves are made randomly. \n" <<  "You must get 3-in-a-row to win; getting five of the nine squares is a tie game. \n";
	cout << "You player will be lower case 'x'.  You will enter the number of the space you would like to play. \n";
}
//Purpose: allows the computer to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the computer's move 
void ComputerMove(char B[][3])     
{ int i=0, turn=0;      
      cout<<"\n IN COMPUTER MOVE \n";
 //while (turn==0)     //Make sure the move is ok
      { 
      i=rand()%9+1;     //randomize a move
      if (i==1)      // make sure the first location is not taken
        {
            if ((B[0][0]!='x')&&(B[0][0]!='o'))
                  {     B[0][0]='o';  //if not taken, then move
                        turn=1;
                  } 
		}
			else if (i==2)
			{	
			if ((B[0][1]!='x')&&(B[0][1]!='o'))
                  {     B[0][1]='o';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==3)
			{				
				if ((B[0][2]!='x')&&(B[0][2]!='o'))
                  {     B[0][2]='o';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==4)
			{			
				if ((B[1][0]!='x')&&(B[1][0]!='o'))
                  {     B[1][0]='o';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==5)
			{
				if ((B[1][1]!='x')&&(B[1][1]!='o'))
                  {     B[1][1]='o';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==6)
			{
				if ((B[1][2]!='x')&&(B[1][2]!='o'))
                  {     B[1][2]='o';  //if not taken, then move
                        turn=1;
                  } 
			}
			else if (i==7)
			{
				if ((B[2][0]!='x')&&(B[2][0]!='o'))
                  {     B[2][0]='o';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==8)
			{
				if ((B[2][1]!='x')&&(B[2][1]!='o'))
                  {     B[2][1]='o';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==9)
			{
				if ((B[2][2]!='x')&&(B[2][2]!='o'))
                  {     B[2][2]='o';  //if not taken, then move
                        turn=1;
                  }
			}
		}
      }


//Purpose: allows the user to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the human's move 
void HumanMove(char B[][3]) 
{int i=0, turn=0;
cout<<"\n IN HUMAN MOVE \n";
//while (turn==0)     //Make sure the move is ok
      { 
		  cout << "Please enter the number of the space you would like to move to: ";
		  cin >> i;     
      if (i==1)      // make sure the first location is not taken
        {
            if ((B[0][0]!='x')&&(B[0][0]!='o'))
                  {     B[0][0]='x';  //if not taken, then move
                        turn=1;
                  } 
		}
			else if (i==2)
			{
				if ((B[0][1]!='x')&&(B[0][1]!='o'))
                  {     B[0][1]='x';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==3)
			{
				if ((B[0][2]!='x')&&(B[0][2]!='o'))
                  {     B[0][2]='x';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==4)
			{
				if ((B[1][0]!='x')&&(B[1][0]!='o'))
                  {     B[1][0]='x';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==5)
			{
				if ((B[1][1]!='x')&&(B[1][1]!='o'))
                  {     B[1][1]='x';  //if not taken, then move
                        turn=1;
                  } 
			}
			else if (i==6)
			{
				if ((B[1][2]!='x')&&(B[1][2]!='o'))
                  {     B[1][2]='x';  //if not taken, then move
                        turn=1;
                  } 
			}
			else if (i==7)
			{
				if ((B[2][0]!='x')&&(B[2][0]!='o'))
                  {     B[2][0]='x';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==8)
			{
				if ((B[2][1]!='x')&&(B[2][1]!='o'))
                  {     B[2][1]='x';  //if not taken, then move
                        turn=1;
                  }
			}
			else if (i==9)
			{
				if ((B[2][2]!='x')&&(B[2][2]!='o'))
                  {     B[2][2]='x';  //if not taken, then move
                        turn=1;
                  }
			}
		}
	  }

//Purpose: Print out the board
//Input: B[][] a 2 dimensional array that represents the board
//Output: None
void PrintBoard(char B[][3]) 
{     cout<<"\n   "<<B[0][0]<<"|   "<<B[0][1]<<"|   "<<B[0][2];   
      cout<<"\n_________________";
      cout<<"\n   "<<B[1][0]<<"|   "<<B[1][1]<<"|   "<<B[1][2];
      cout<<"\n_________________";
      cout<<"\n   "<<B[2][0]<<"|   "<<B[2][1]<<"|   "<<B[2][2];
      cout<<"\n\n";
}
//Purpose: Check for a winner
//Input: B[][] a 2 dimensional array that represents the board
//Output: win and integer indicating winner status. 
//       0=no winner, 1= human winner, 2= computer winner
int CheckWin(char B[][3])
{     int win = 0;                                    
    if((B[0][0] == B[0][1] && B[0][1] == B[0][2]))
	{
		if (B[0][0]=='x')
			win=1;
		else if (B[0][0]=='o')
			win=2;
		if (B[0][1]=='x')
			win=1;
		else if (B[0][1]=='o')
			win=2;
		if (B[0][2]=='x')
			win=1;
		else if (B[0][2]=='o')
			win=2;
	}
	if ((B[1][0] == B[1][1] && B[1][1] == B[1][2]))
	{
		if (B[1][0]=='x')
			win=1;
		else if (B[1][0]=='o')
			win=2;
		if (B[1][1]=='x')
			win=1;
		else if (B[1][1]=='o')
			win=2;
		if (B[1][2]=='x')
			win=1;
		else if (B[1][2]=='o')
			win=2;
	}
	if ((B[2][0] == B[2][1] && B[2][1] == B[2][2]))
	{
		if (B[2][0]=='x')
			win=1;
		else if (B[2][0]=='o')
			win=2;
		if (B[2][1]=='x')
			win=1;
		else if (B[2][1]=='o')
			win=2;
		if (B[2][2]=='x')
			win=1;
		else if (B[2][2]=='o')
			win=2;
	}
	if ((B[0][0] == B[1][1] && B[1][1] == B[2][2]))
	{
		if (B[0][0]=='x')
			win=1;
		else if (B[0][0]=='o')
			win=2;
		if (B[1][1]=='x')
			win=1;
		else if (B[1][1]=='o')
			win=2;
		if (B[2][2]=='x')
			win=1;
		else if (B[2][2]=='o')
			win=2;	
	}
	if ((B[0][2] == B[1][1] && B[1][1] == B[2][0]))
	{
		if (B[0][2]=='x')
			win=1;
		else if (B[0][2]=='o')
			win=2;
		if (B[1][1]=='x')
			win=1;
		else if (B[1][1]=='o')
			win=2;
		if (B[2][0]=='x')
			win=1;
		else if (B[2][0]=='o')
			win=2;	
	}
	if ((B[0][0] == B[1][0] && B[1][0] == B[2][0]))
	{
		if (B[0][0]=='x')
			win=1;
		else if (B[0][0]=='o')
			win=2;
		if (B[1][0]=='x')
			win=1;
		else if (B[1][0]=='o')
			win=2;
		if (B[2][0]=='x')
			win=1;
		else if (B[2][0]=='o')
			win=2;	
	}
	if ((B[0][1] == B[1][1] && B[1][1] == B[2][1]))
	{
		if (B[0][1]=='x')
			win=1;
		else if (B[0][1]=='o')
			win=2;
		if (B[1][1]=='x')
			win=1;
		else if (B[1][1]=='o')
			win=2;
		if (B[2][1]=='x')
			win=1;
		else if (B[2][1]=='o')
			win=2;	
	}
	if ((B[0][2] == B[1][2] && B[1][2] == B[2][2]))
	{
		if (B[0][2]=='x')
			win=1;
		else if (B[0][2]=='o')
			win=2;
		if (B[1][2]=='x')
			win=1;
		else if (B[1][2]=='o')
			win=2;
		if (B[2][2]=='x')
			win=1;
		else if (B[2][2]=='o')
			win=2;	
	}
	cout<<"\n IN CHECK WIN "<<win<<endl;
      return (win);
}
void main ()
{int choice=0, done=0, t=0, w=0;                                              
char Board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
srand(time(0));
Intro();
PrintBoard(Board);
cout<<"Do you want to go first? Enter 1 for yes, 2 for no: ";
cin>>choice;
      if (choice==1)                                  //if player wishes to go first
      { 
            do{ cout<<"\n turn = "<<t<<endl;
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)                           
               cout<<"Youwin!";                                   
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            ComputerMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
			PrintBoard(Board);
            t=t+1;
            }while((t!=9)||(w!=0));                                     
      }
      else                   
      {
            do{   cout<<"\n turn = "<<t<<endl;
            ComputerMove(Board);
            w=CheckWin(Board);
           if(w==1)
			     cout<<"You Win!";
           else if (w==2)
                 cout<<"You lose!";
           else if (t==9&&w==0)
                 cout<<"Tie Game";
            PrintBoard(Board);
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            t=t+1;
      	}while((t!=9)||(w!=0));                               
	  }
}

While still in the lab I was able to get the program going. However I can't get it to stop after someone wins, nor can I get the tie to work (might be related).

Please help! The program has to be in this format as he gave us the guide line and told us something in main was broken.... :)

This minor change made it work when I move first

if (choice==1)                                  //if player wishes to go first
      { 
            bool done = false;
            do{ 
                cout<<"\n turn = "<<t<<endl;
                HumanMove(Board);
                w=CheckWin(Board);
                if (w==1)
                {
                    cout<<"Youwin!";
                    done = true;
                }
                else if (w==2)
                {
                  cout<<"You lose!";
                  done = true;
                }
                else if (t==9&&w==0)
                {
                    cout<<"Tie Game";
                    done = true;
                }
                if( done == false)
                {
                    PrintBoard(Board);
                    ComputerMove(Board);
                    w=CheckWin(Board);
			        PrintBoard(Board);
                    t=t+1;
                }
            }while(done == false);
      }

thanks, yes that works for the human first. now i need to solve for the pc first and why when I block a move the pc never makes another guess..

You can solve for pc first in a similar manner that I posted. I think the problem in ComputerMove() is that if a square is already eith 'x' or 'o' you need to generate another random number and try again. At some point the computer is going to generate a random number for a square that has already been used. When that happes it needs to generate another random number. You already had such a loop coded but you commented it out.

Edited 7 Years Ago by Ancient Dragon: n/a

After looking at your checkWin function you can change it from

if((B[0][0] == B[0][1] && B[0][1] == B[0][2]))
{
	if (B[0][0]=='x')
		win=1;
	else if (B[0][0]=='o')
		win=2;
	if (B[0][1]=='x')
		win=1;
	else if (B[0][1]=='o')
		win=2;
	if (B[0][2]=='x')
		win=1;
	else if (B[0][2]=='o')
		win=2;
}

to this

if( B[0][0] == B[0][1] && B[0][1] == B[0][2] )
{
	if ( B[0][0] == 'x' )
		win = 1;
	else if ( B[0][0] == 'o' )
		win = 2;
}

for each of them because you already know that B[0][x] where x is 0 1 2 are all the same.
Also I would use else if for each row/col/diag check because a win is a win and you do not need to check to see if you won in 2 different ways so there is no point in checking more than one way if you do not have to.

I did some work with your project. I know this is not what you're asking for, but I was bored waiting to see if someone could help with my question, and I decided to make something extra with your project. Hope You like it:

// The headers are still the same
//Michelle Stokes CSI130 Lab 13
//Code from hand out Lab14 TTT
#include <iostream>
#include <ctime>

using namespace std;

Here you will notice that i changed your functions

// Function prototypes
void intro(void);
void printBoard(char [3][3]);
int move(char [3][3], int, int [9], int); // Now i only use 1 function to control the moves of computer and human player
int checkWinner(char [3][3], int); // This function checks if there is a winner on the game
bool checkLine(char [3]); // This other function checks if the elements of a char string are all the same
bool checkPosition(char [3][3], int, int); // This function checks if a given position is free

int main(void){
	int user = 0, winner = 0, step = 0;
	// Now i will use an array to control the random function
	int positions[9] = {1, 2, 3, 4, 5, 6 , 7, 8, 9}; // I made this array to control the random function, because i noticed that the computer was taking too long to find a random free spot 
	int available = 9; // This is the new limit for random, it will decrease with every move played
	// And this is my matrix
	char board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};

	// Show the intro
	intro();
	printBoard(board);

	while(user != 1 && user != 2){		// Ask who will go first until user gives a valid answer
		cout
			<< "Who will go first? 1 for human player, 2 for computer: ";
		cin
			>> user;
	}

	while(winner == 0){							// Play the game until we have a winner or a tie
		user = move(board, user, positions, available);
		winner = checkWinner(board, step);
		step++;									// Increase the number of moves played
		available--;							// Decrease the available places
		printBoard(board);
// This piece of code is just to see if the control array was working good
		for(int a = 0; a < 9; a++)
			cout
				<< " " << positions[a];
		cout << endl;
	}
	if(winner != 3){
		cout
			<< "The winner was ";
		switch(winner){
			case 1:
				cout
					<< "the human player";
				break;
			case 2:
				cout
					<< "the computer";
				break;
		}
		cout
			<< endl << "The game lasted " << step << " moves." << endl;
	}
	else{
		cout
			<< "The game is a tie, no possible winner" << endl;
	}
	cin.get();
	return 0;
}

// Functions
void intro(void){
	cout
		<< "This game is played between a human user and the computer." << endl
		<<  "The computers moves are made randomly." << endl
		<<  "You must get 3-in-a-row to win; getting five of the nine squares is a tie game." << endl;

	cout
		<< "You player will be lower case 'x'. You will enter the number of the space you would like to play.";
}

int move(char board[3][3], int user, int nums[9], int avail){
	int i, j;				// The position of the move
	int input = 0;			// The input of the human user
	int next;				// The next player to move
	char letter;			// The sign of the person who will move
	int valid = 0;			// checks if the move is valid

	switch(user){
		case 1:
			next = 2;
			letter = 'x';
			cout
				<< "Please enter the number of the space you would like to move to: ";
			while(valid == 0){
				while(input > 10 || input < 1)
					cin >> input;
				input --;
// Check this, you can obtain the i,j positions with an easy math calculation (trust me it works)
				i = input / 3;
				j = input % 3;
				valid = checkPosition(board, i, j);
			}
			int pos;
			for(int a = 0; a < 9; a++){
				if(nums[a] == (input + 1)){ pos = a; }
			}
			input = pos;
			break;
		case 2:
			next = 1;
			letter = 'o';
			// Get a random position in the board
			srand((unsigned) time(NULL));
			input = (rand() % avail);		// Choose one of the available cells
			i = (nums[input] - 1) / 3;
			j = (nums[input] - 1) % 3;
			cout
				<< endl 
				<< "IN COMPUTER MOVE: " << input << endl;
			break;
	}
	// Remove the available space from the array
	for(int cont = input; (cont < 9) && (nums[cont] != 0); cont++){
		if(cont < 8)
			nums[cont] = nums[cont + 1];
		else
			nums[cont] = 0;
	}

	// Add the right letter to the matrix
	board[i][j] = letter;
	return next;
}

void printBoard(char board[3][3]){
	cout
		<< endl
		<< "\t-------------" << endl;
	for(int i = 0; i < 3; i++){
		cout
			<< "\t";
		for(int j = 0; j < 3; j++){
			cout
				<< "| " << board[i][j] << " ";
		}
		cout
			<< "|" << endl
			<< "\t-------------" << endl;
	}
}

bool checkPosition(char board[3][3], int i, int j){
	if(i < 3 && j < 3)
		if((board[i][j] != 'x') && (board[i][j] != 'o')) return true;
	return false;
}

int checkWinner(char board[3][3], int step){
	char line[3];
	char winner = ' ';
	if(step < 4){ return 0; }
	else{
		// Check horizontal lines
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 3; j++){ line[j] = board[i][j]; }
			if(checkLine(line)){ winner = line[0]; }
		}
		// Check vertical lines
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 3; j++){ line[j] = board[j][i]; }
			if(checkLine(line)){ winner = line[0]; }
		}
		// Check diagonals
		for(int i = 0; i < 3; i ++){ line[i] = board[i][i]; }
		if(checkLine(line)){ winner = line[0]; }
		for(int i = 0, j = 2; i < 3; i++, j--){ line[i] = board[j][i]; }
		if(checkLine(line)){ winner = line[0]; }
	}
	if((step >= 8) && winner == ' ') return 3;	// It's a tie
	else if(winner == 'x') return 1;			// Winner is human player
	else if(winner == 'o') return 2;			// Winner is computer
	return 0;									// No winner yet
}

bool checkLine(char line[3]){
	if((line[0] == line[1]) && (line[0] == line[2]))
		return true;
	return false;
}

Well, I hope You can understand the changes i made (and also hope that you don't get too upset because i changed a lot of your code), if you need anything, message me.

This article has been dead for over six months. Start a new discussion instead.