Tic Tac Toe program

Creating a tic tac toe program using arrays and pointers.

Page 1 of 1

8 Replies - 21699 Views - Last Post: 21 August 2009 - 05:17 PM Rate Topic: -----

#1 drakenathaniel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 09

Tic Tac Toe program

Posted 12 April 2009 - 04:18 PM

I have been working on this program for hours... this is the instructions:

Create a C++ console application that allows two players to participate in a tic-tac-toe game. The program will be responsible for deciding who wins the game.

The program should include the following class, properties and methods:
	class TicTacToe{
			 private:
					char theBoard [3][3];
			 public:
					TicTacToe(void);
					void playOneGame(void);
					void switchPlayer(char &);
					void showBoard(void);
					void postMove(int, int, char);
					char determineWinner(void);
	};

	int main (void){
			//test the class by playing one game
			TicTacToe Game1;
			Game1.playOneGame();
	}

	void TicTacToe::playOneGame(void){
		  //start a game and play until someone wins or a draw occurs...
		  const int MaxMoves = 9;
		  char currentPlayer = 'O';
		  int row = 0;
		  int clmn = 0;
		  char theWinner = ' ';
		  int nmbrOfMoves = 0; //keep track of the number of moves max is 9

		  do {
				switchPlayer(currentPlayer); //change player from x to o or vice versa				
				showBoard();		   

				cout << "\n\nPlayer " << currentPlayer << endl; //get the players move 
				cout << "enter your row: ";
				cin >> row;
				cout << "enter your column: ";
				cin >> clmn; 

				postMove(row, clmn, currentPlayer); //post the move to the board	  
				theWinner = determineWinner();  //see if anyone won the game
				nmbrOfMoves++;  //keep track of the number of moves

		  } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

		   showBoard(); //show the ending board

		   if (theWinner != 'D')  //declare a winner
				cout << "\n\nThe Winner is player " << theWinner;
		   else
				cout << "\n\nThe Game was a Draw";
	}

	TicTacToe::TicTacToe(void){
			  //intialize the array contents
	}

	void TicTacToe::switchPlayer(char &currentPlayer){
			 //switches the current player
	}

	void TicTacToe::showBoard(){
			//displays the board
	}

	void TicTacToe::postMove(int row, int col, char value){
		   //gets the users move and posts it to the board
	}

	char TicTacToe::determineWinner(void){
		  //analyzes the board to see if there is a winner
		  //returns a X, O indicating the winner
		  //if the game is a draw then D is returned

		  //check the rows
		  for (int i = 0; i < 3; i++){
				if (theBoard[i][0] == theBoard[i][1]
					 && theBoard[i][1] == theBoard[i][2]
					 && theBoard[i][0] != ' '){
					 return theBoard[i][0];
				}
		  }

		  //check the clmns
		  for (int i = 0; i < 3; i++){
				 if (theBoard[0][i] == theBoard[1][i]
					   && theBoard[1][i] == theBoard[2][i]
					   && theBoard[0][i] != ' '){
					   return theBoard[0][i];
				 }
		   }

		   //check the diagnals
		   if (theBoard[0][0] == theBoard[1][1]
				  && theBoard[1][1] == theBoard[2][2]
				  && theBoard[0][0] != ' ') {
				  return theBoard[0][0];
		   }

		   if (theBoard[2][0] == theBoard[1][1]
				   && theBoard[1][1] == theBoard[0][2]
				   && theBoard[2][0] != ' ') {
				   return theBoard[2][0];
		   }

		   return 'D';
	}




Ok so I have my base structure. I have used all the knowledge I have from previous labs and lesson to apply it to this lab. I came up with this:




#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
class  TicTacToe{
		 private:
				char theBoard [3][3];
		 public:
				TicTacToe(void);
				void playOneGame(void);
				void switchPlayer(char &);
				void showBoard(void);
				void postMove(int, int, char);
				char determineWinner(void);
};

int main (void){
		//test the class by playing one game
		TicTacToe Game1;
		Game1.playOneGame();
}

void TicTacToe::playOneGame(void){
	  //start a game and play until someone wins or a draw occurs...
	  const int MaxMoves = 9;
	  char currentPlayer = 'O';
	  int row = 0;
	  int clmn = 0;
	  char theWinner = ' ';
	  int nmbrOfMoves = 0; //keep track of the number of moves max is 9

	  do {
			switchPlayer(currentPlayer); //change player from x to o or vice versa				
			showBoard();		   

			cout << "\n\nPlayer " << currentPlayer << endl; //get the players move 
			cout << "enter your row: ";
			cin >> row;
			cout << "enter your column: ";
			cin >> clmn; 

			postMove(row, clmn, currentPlayer); //post the move to the board	  
			theWinner = determineWinner();  //see if anyone won the game
			nmbrOfMoves++;  //keep track of the number of moves

	  } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

	   showBoard(); //show the ending board

	   if (theWinner != 'D')  //declare a winner
			cout << "\n\nThe Winner is player " << theWinner;
	   else
			cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void){
		  //intialize the array contents
	int grid [3][3];
	for(int i = 0; i < 3;i++)
		for (int j = 0; j < 3; j++)
			cout <<	grid [i][j] = 0;
}

void TicTacToe::switchPlayer(char &currentPlayer){
		 //switches the current player
	if currentPlayer == 'O'
		cout << "Player 2 its your turn" << endl;
	else currentPlayer == 'X'
		cout << "Player 1 its your turn" << endl;

}

void TicTacToe::showBoard(){
	cout << "[ ] [ ] [ ] \n";
	cout << "[ ] [ ] [ ] \n";
	cout << "[ ] [ ] [ ] \n";//displays the board

	for(int i = 0; i < 3;i++)
		for (int j = 0; j < 3; j++)
			cout <<	grid [i][j] = 0;
}

void TicTacToe::postMove(int row, int col, char value){
	   //gets the users move and posts it to the board
	cout << "Player 1 whats your move:" << postMove;
	cin >> >> postMove;
	cout << "Player 2 whats your move:" << postMove;

}

char TicTacToe::determineWinner(void){
	  //analyzes the board to see if there is a winner
	  //returns a X, O indicating the winner
	  //if the game is a draw then D is returned

	  //check the rows
	  for (int i = 0; i < 3; i++){
			if (theBoard[i][0] == theBoard[i][1]
				 && theBoard[i][1] == theBoard[i][2]
				 && theBoard[i][0] != ' '){
				 return theBoard[i][0];
			}
	  }

	  //check the clmns
	  for (int i = 0; i < 3; i++){
			 if (theBoard[0][i] == theBoard[1][i]
				   && theBoard[1][i] == theBoard[2][i]
				   && theBoard[0][i] != ' '){
				   return theBoard[0][i];
			 }
	   }

	   //check the diagnals
	   if (theBoard[0][0] == theBoard[1][1]
			  && theBoard[1][1] == theBoard[2][2]
			  && theBoard[0][0] != ' ') {
			  return theBoard[0][0];
	   }

	   if (theBoard[2][0] == theBoard[1][1]
			   && theBoard[1][1] == theBoard[0][2]
			   && theBoard[2][0] != ' ') {
			   return theBoard[2][0];
	   }

	   return 'D';
}




So now I'm stuck with errors and whatever else is wrong with it... I need to know why my code wont work and what do I need to do to fix this.

This post has been edited by drakenathaniel: 12 April 2009 - 07:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe program

#2 drakenathaniel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 09

Re: Tic Tac Toe program

Posted 12 April 2009 - 04:25 PM

I have been working on this program for hours... this is the instructions:

Create a C++ console application that allows two players to participate in a tic-tac-toe game. The program will be responsible for deciding who wins the game.

The program should include the following class, properties and methods:
	class TicTacToe{
			 private:
					char theBoard [3][3];
			 public:
					TicTacToe(void);
					void playOneGame(void);
					void switchPlayer(char &);
					void showBoard(void);
					void postMove(int, int, char);
					char determineWinner(void);
	};

	int main (void){
			//test the class by playing one game
			TicTacToe Game1;
			Game1.playOneGame();
	}

	void TicTacToe::playOneGame(void){
		  //start a game and play until someone wins or a draw occurs...
		  const int MaxMoves = 9;
		  char currentPlayer = 'O';
		  int row = 0;
		  int clmn = 0;
		  char theWinner = ' ';
		  int nmbrOfMoves = 0; //keep track of the number of moves max is 9

		  do {
				switchPlayer(currentPlayer); //change player from x to o or vice versa				
				showBoard();		   

				cout << "\n\nPlayer " << currentPlayer << endl; //get the players move 
				cout << "enter your row: ";
				cin >> row;
				cout << "enter your column: ";
				cin >> clmn; 

				postMove(row, clmn, currentPlayer); //post the move to the board	  
				theWinner = determineWinner();  //see if anyone won the game
				nmbrOfMoves++;  //keep track of the number of moves

		  } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

		   showBoard(); //show the ending board

		   if (theWinner != 'D')  //declare a winner
				cout << "\n\nThe Winner is player " << theWinner;
		   else
				cout << "\n\nThe Game was a Draw";
	}

	TicTacToe::TicTacToe(void){
			  //intialize the array contents
	}

	void TicTacToe::switchPlayer(char &currentPlayer){
			 //switches the current player
	}

	void TicTacToe::showBoard(){
			//displays the board
	}

	void TicTacToe::postMove(int row, int col, char value){
		   //gets the users move and posts it to the board
	}

	char TicTacToe::determineWinner(void){
		  //analyzes the board to see if there is a winner
		  //returns a X, O indicating the winner
		  //if the game is a draw then D is returned

		  //check the rows
		  for (int i = 0; i < 3; i++){
				if (theBoard[i][0] == theBoard[i][1]
					 && theBoard[i][1] == theBoard[i][2]
					 && theBoard[i][0] != ' '){
					 return theBoard[i][0];
				}
		  }

		  //check the clmns
		  for (int i = 0; i < 3; i++){
				 if (theBoard[0][i] == theBoard[1][i]
					   && theBoard[1][i] == theBoard[2][i]
					   && theBoard[0][i] != ' '){
					   return theBoard[0][i];
				 }
		   }

		   //check the diagnals
		   if (theBoard[0][0] == theBoard[1][1]
				  && theBoard[1][1] == theBoard[2][2]
				  && theBoard[0][0] != ' ') {
				  return theBoard[0][0];
		   }

		   if (theBoard[2][0] == theBoard[1][1]
				   && theBoard[1][1] == theBoard[0][2]
				   && theBoard[2][0] != ' ') {
				   return theBoard[2][0];
		   }

		   return 'D';
	}




Ok so I have my base structure. I have used all the knowledge I have from previous labs and lesson to apply it to this lab. I came up with this:



#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
class  TicTacToe{
		 private:
				char theBoard [3][3];
		 public:
				TicTacToe(void);
				void playOneGame(void);
				void switchPlayer(char &);
				void showBoard(void);
				void postMove(int, int, char);
				char determineWinner(void);
};

int main (void){
		//test the class by playing one game
		TicTacToe Game1;
		Game1.playOneGame();
}

void TicTacToe::playOneGame(void){
	  //start a game and play until someone wins or a draw occurs...
	  const int MaxMoves = 9;
	  char currentPlayer = 'O';
	  int row = 0;
	  int clmn = 0;
	  char theWinner = ' ';
	  int nmbrOfMoves = 0; //keep track of the number of moves max is 9

	  do {
			switchPlayer(currentPlayer); //change player from x to o or vice versa				
			showBoard();		   

			cout << "\n\nPlayer " << currentPlayer << endl; //get the players move 
			cout << "enter your row: ";
			cin >> row;
			cout << "enter your column: ";
			cin >> clmn; 

			postMove(row, clmn, currentPlayer); //post the move to the board	  
			theWinner = determineWinner();  //see if anyone won the game
			nmbrOfMoves++;  //keep track of the number of moves

	  } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

	   showBoard(); //show the ending board

	   if (theWinner != 'D')  //declare a winner
			cout << "\n\nThe Winner is player " << theWinner;
	   else
			cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void){
		  //intialize the array contents
	int grid [3][3];
	for(int i = 0; i < 3;i++)
		for (int j = 0; j < 3; j++)
			cout <<	grid [i][j] = 0;
}

void TicTacToe::switchPlayer(char &currentPlayer){
		 //switches the current player
	if currentPlayer == 'O'
		cout << "Player 2 its your turn" << endl;
	else currentPlayer == 'X'
		cout << "Player 1 its your turn" << endl;

}

void TicTacToe::showBoard(){
	cout << "[ ] [ ] [ ] \n";
	cout << "[ ] [ ] [ ] \n";
	cout << "[ ] [ ] [ ] \n";//displays the board

	for(int i = 0; i < 3;i++)
		for (int j = 0; j < 3; j++)
			cout <<	grid [i][j] = 0;
}

void TicTacToe::postMove(int row, int col, char value){
	   //gets the users move and posts it to the board
	cout << "Player 1 whats your move:" << postMove;
	cin >> >> postMove;
	cout << "Player 2 whats your move:" << postMove;

}

char TicTacToe::determineWinner(void){
	  //analyzes the board to see if there is a winner
	  //returns a X, O indicating the winner
	  //if the game is a draw then D is returned

	  //check the rows
	  for (int i = 0; i < 3; i++){
			if (theBoard[i][0] == theBoard[i][1]
				 && theBoard[i][1] == theBoard[i][2]
				 && theBoard[i][0] != ' '){
				 return theBoard[i][0];
			}
	  }

	  //check the clmns
	  for (int i = 0; i < 3; i++){
			 if (theBoard[0][i] == theBoard[1][i]
				   && theBoard[1][i] == theBoard[2][i]
				   && theBoard[0][i] != ' '){
				   return theBoard[0][i];
			 }
	   }

	   //check the diagnals
	   if (theBoard[0][0] == theBoard[1][1]
			  && theBoard[1][1] == theBoard[2][2]
			  && theBoard[0][0] != ' ') {
			  return theBoard[0][0];
	   }

	   if (theBoard[2][0] == theBoard[1][1]
			   && theBoard[1][1] == theBoard[0][2]
			   && theBoard[2][0] != ' ') {
			   return theBoard[2][0];
	   }

	   return 'D';
}




So now I'm stuck with errors and whatever else is wrong with it... I need to know why my code wont work and what do I need to do to fix this.

This post has been edited by drakenathaniel: 12 April 2009 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: Tic Tac Toe program

Posted 12 April 2009 - 04:30 PM

Please don't post threads on the same topic in different forums. Merged.
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Tic Tac Toe program

Posted 12 April 2009 - 04:55 PM

What errors are you getting? Show us your ouput? Plz.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Tic Tac Toe program

Posted 12 April 2009 - 06:01 PM

Wow, a quick Google shows you've posted this all over the Internet, and in not a single place have you said what is the exact problem. We need that info to help you. A craftier person might have realized that this problem comes up every semester at DeVry (I think?) and actually searched for info before plastering it all over the Internet with your name on it.

Anyway, because I'm waiting for my own code to compile, I figured I'd try to compile it, which I guess is what you wanted us all to do.

What's this do?
 cout <<    grid [i][j] = 0;

cout is for printing to the console. Not what you're looking for.

Go back to your book and/or notes and read up the syntax for control statements like if/else, because this:
if currentPlayer == 'O'
        cout << "Player 2 its your turn" << endl;
    else currentPlayer == 'X'
        cout << "Player 1 its your turn" << endl;


isn't right. And you're supposed to be changing the value of currentPlayer in this function as well as printing.

And your goal here?
//gets the users move and posts it to the board
cout << "Player 1 whats your move:" << postMove;
cin >> >> postMove;
cout << "Player 2 whats your move:" << postMove;

Just throwing the name of the function everywhere isn't going to help you at all. Although the comment says so, you're not supposed to GET the user's move in here, as it's being read in the main do-while loop and passed to this function in the row and col arguments; you're supposed to put it in the array here, then show the board.
Was This Post Helpful? 0
  • +
  • -

#6 drakenathaniel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 09

Re: Tic Tac Toe program

Posted 12 April 2009 - 07:36 PM

View PostBetaWar, on 12 Apr, 2009 - 03:30 PM, said:

Please don't post threads on the same topic in different forums. Merged.


Oh ok im sorry. Im new to this I thought it would help me get more help faster.
Was This Post Helpful? 0
  • +
  • -

#7 drakenathaniel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 09

Re: Tic Tac Toe program

Posted 12 April 2009 - 07:54 PM

Ok I did post it all over looking for some helpful advice. My first semester online. Really I don't like it though. What I was trying to figure out is why my program wasn't working. Now my program executes (did it on my own too :-) ). One problem I have now is trying to figure out how to get the X's and O's inside the grid I made.... I don't want any hand outs but to be shown the errors and and idea of where to begin looking for a solution. Here is my new code:



#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
class  TicTacToe{
		 private:
				char theBoard [3][3];
		 public:
				TicTacToe(void);
				void playOneGame(void);
				void switchPlayer(char &);
				void showBoard(void);
				void postMove(int, int, char);
				char determineWinner(void);
};

int main (void){
		//test the class by playing one game
		TicTacToe Game1;
		Game1.playOneGame();
}

void TicTacToe::playOneGame(void){
	  //start a game and play until someone wins or a draw occurs...
	  const int MaxMoves = 9;
	  char currentPlayer = 'O';
	  int row = 0;
	  int clmn = 0;
	  char theWinner = ' ';
	  int nmbrOfMoves = 0; //keep track of the number of moves max is 9

	  do {
			switchPlayer(currentPlayer); //change player from x to o or vice versa				
			showBoard();		   

			cout << "\n\nPlayer " << currentPlayer << endl; //get the players move 
			cout << "enter your row: ";
			cin >> row;
			cout << "enter your column: ";
			cin >> clmn; 

			postMove(row, clmn, currentPlayer); //post the move to the board	  
			theWinner = determineWinner();  //see if anyone won the game
			nmbrOfMoves++;  //keep track of the number of moves

	  } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

	   showBoard(); //show the ending board

	   if (theWinner != 'D')  //declare a winner
			cout << "\n\nThe Winner is player " << theWinner;
	   else
			cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void){
		  //intialize the array contents
	int grid  [3][3];
	for(int i = 0; i < 3;i++)
		for (int j = 0; j < 3; j++);
			
}

void TicTacToe::switchPlayer(char &currentPlayer){
		 //switches the current player

	if (currentPlayer == 'X')
		currentPlayer = 'O';
	else
		currentPlayer = 'X';


}

void TicTacToe::showBoard(){
	//displays the board
		cout << "[ ] [ ] [ ] \n";
		cout << "[ ] [ ] [ ] \n";
		cout << "[ ] [ ] [ ] \n";

}

void TicTacToe::postMove(int row, int col, char value){
	   //gets the users move and posts it to the board
	theBoard[row-1][col-1] = value;

}

char TicTacToe::determineWinner(void){
	  //analyzes the board to see if there is a winner
	  //returns a X, O indicating the winner
	  //if the game is a draw then D is returned

	  //check the rows
	  for (int i = 0; i < 3; i++){
			if (theBoard[i][0] == theBoard[i][1]
				 && theBoard[i][1] == theBoard[i][2]
				 && theBoard[i][0] != ' '){
				 return theBoard[i][0];
			}
	  }

	  //check the clmns
	  for (int i = 0; i < 3; i++){
			 if (theBoard[0][i] == theBoard[1][i]
				   && theBoard[1][i] == theBoard[2][i]
				   && theBoard[0][i] != ' '){
				   return theBoard[0][i];
			 }
	   }

	   //check the diagnals
	   if (theBoard[0][0] == theBoard[1][1]
			  && theBoard[1][1] == theBoard[2][2]
			  && theBoard[0][0] != ' ') {
			  return theBoard[0][0];
	   }

	   if (theBoard[2][0] == theBoard[1][1]
			   && theBoard[1][1] == theBoard[0][2]
			   && theBoard[2][0] != ' ') {
			   return theBoard[2][0];
	   }

	   return 'D';
}




So the question I have is ( to be very clear) is what process do I need to do to get the X's and O's inside the grid.
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Tic Tac Toe program

Posted 12 April 2009 - 09:17 PM

A while ago I posted the code, as per below, for someone at DIC ... It may give some ideas and a working start ...

Shalom,
David
http://developers-he.../index.p...opic,127.0.html

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

char m[9]; // using a global array to keep function calls simple ...

char a_winner() // returns winner 'x' or 'o' ... or, if NO winner, returns ' '
{
	if( m[0]==m[1] && m[1] == m[2] ) return m[0];
	if( m[3]==m[4] && m[4] == m[5] ) return m[3];
	if( m[6]==m[7] && m[7] == m[8] ) return m[6];
	
	if( m[0]==m[3] && m[3] == m[6] ) return m[0];
	if( m[1]==m[4] && m[4] == m[7] ) return m[1];
	if( m[2]==m[5] && m[5] == m[8] ) return m[2];
	
	if( m[0]==m[4] && m[4] == m[8] ) return m[0];
	if( m[2]==m[4] && m[4] == m[6] ) return m[2];

	return ' ';
}

void showXOs()
{
	cout<< m[0]<<" | "<<m[1]<<" | "<<m[2]<<endl
		<< "---------"<<endl
		<< m[3]<<" | "<<m[4]<<" | "<<m[5]<<endl
		<< "---------"<<endl
		<< m[6]<<" | "<<m[7]<<" | "<<m[8]<<endl
		<< endl;
}


int main ()
{
	srand( time(NULL) );

	for( int i=0; i<9; ++i ) m[i] = '1'+i;
	int pos, tmp;
	char player, computer, winner = ' ';
	string name;
	cout << "Please enter your name : " << flush;
	getline( cin, name );

	int places = 9;
	for(;; )
	{
		if(places==9) cout << "Welcome " << name
						   << " to Computer Random Guess X and O's ...\n\n";
		showXOs();
	
		if(places<=4)
		{
			if( (winner = a_winner()) != ' ' ) break;
		}
		
		if(!places) break;
		
		if( places == 9 ) // i.e. the first pass only ...
		{
			 // initial all to ' ' ... a space char ... before first pass
			for( int i=0; i<9; ++i ) m[i] = ' ';
			cout<<name<<", your choice, 'x' or 'o' ? ";
			cin >> player;
			cin.sync();

			if(player == 'x')
			{
				computer = 'o';
				for(;; )
				{
					cout<<name<<" goes first ... ";
					cout<<"Now "<<name<<", choose your position 1..9 ? ";
					cin>>pos;
					if( !cin.good() || pos<1 || pos >9 )
					{
						cin.clear();
						cin.sync();
						cout << "Invalid ...\n";
					}
					else
					{
						cin.sync();
						break; // out of inner for loop
					}
				}
				m[pos-1] = player;
				--places;
				while( m[(tmp = rand()%9)] != ' ' ); // keep getting tmp until ok
				m[tmp] = computer;
				--places;
			}
			else if(player == 'o')
			{
				computer = 'x';
				cout<<name<<", you go second ..."<<endl;
				m[0] = computer;
				--places;

			}
			else
			{
				cout<<"Invalid ...\n";
				continue; // from top of outer for loop right now ...
			}

		}
		
		else // places < 9 ... i.e. all passes after first pass ...
		{
			for(;; )
			{
				cout<<name<<" '"<< player<<"', choose your position : ";
				cin>>pos;
				if( !cin.good() || pos<1 || pos >9 || m[pos-1] != ' ')
				{
					cin.clear();
					cin.sync();
					cout << "Invalid ...\n";
				}
				else
				{
					cin.sync();
					break; // out of this inner for loop ...
				}
			}
			m[pos-1] = player;
			--places;

			if(places<=4)
			{
				if( (winner = a_winner()) != ' ' )
				{
					showXOs();
					break;
				}
			}


			if(!places) continue;//will exit, but first show last time X and O's

			while( m[(tmp = rand()%9)] != ' ' );
			m[tmp] = computer;
			--places;
		}

	}
	
	// report winner ... or draw ...
	
	if( winner != ' ' )
	{
		cout << "The winner was '" << winner << "'";
		if(winner == player) cout<<", i.e. " << name << endl;
		else cout << ", i.e. the Computer" << endl;
	}
	else cout << "There was no winner ... it was a draw." << endl;
	
	cout << "\nPress 'Enter' to exit ... " << flush;
	cin.get();
}

Was This Post Helpful? 1
  • +
  • -

#9 sp11765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: Tic Tac Toe program

Posted 21 August 2009 - 05:17 PM

I'm working on my tic tac toe project... and I'm having problems also... can someone help??? I think that I didn't initialize the array properly, I know how to put a value to any array like int theBoard[0][0] = '1'; that space will = 1 and so on... I'm having problems clearing my board... here's my code any help would be greatly appreciated!!! Thanks Again!!!

#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class TicTacToe{
	private:
		char theBoard[3][3]; //variable
		int board[3][3];
		
		
		
	public:
		TicTacToe(void);//constructor
		void playOneGame(void);//member function
		void switchPlayer(char &);//member function
		void showBoard(void);// member function 
		void postMove(int, int, char);// member function
		char determineWinner(void);// member function
		

};

int main(void){
	//test the class by playing one game
	TicTacToe Game1;
	Game1.playOneGame();
}//end main

		
	

void TicTacToe::playOneGame(void){
	//start a game and play until someone wins or a draw occurs
	const int MaxMoves = 9;
	char currentPlayer = 'O';
	int row = 0;
	int clmn = 0;
	char theWinner = ' ';
	int nmbrOfMoves = 0; //kep track of the number

	do{
		switchPlayer(currentPlayer);//change player from x to o or vice versa
		showBoard();
		cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
		cout << "Enter your row (0, 1, 2): ";
		cin >> row;
		cout << "Enter your column (0, 1, 2): ";
		cin >> clmn;

		postMove(row, clmn, currentPlayer); //post the move to the board

		theWinner = determineWinner(); //see if anyone won the game

		nmbrOfMoves++;//keeptrack of the number of moves
	}while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));

	showBoard();//show the ending board

	if(theWinner != 'D') //declare a winner
		cout << "\n\nTheWinner is player " << theWinner;
	else
		cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void){
	//intialize the array contents
	theBoard[3][3]=' ';
	
	
}


void TicTacToe::switchPlayer(char ¤tPlayer){
	//switchs the current player
	
	currentPlayer = ' ';
		
	if (currentPlayer =='X')
	{
		cout << "\nPlayer 1: Make your move " ;
		currentPlayer = 1;
		
	}
	else
		(currentPlayer == 'X');
		

		
		

		
}
	


void TicTacToe::showBoard(){
	//displays the board
	cout << "\n|" << theBoard[0][0] << "|" << theBoard[0][1] << "|" << theBoard[0][2] << endl;
	cout << "--------------------" << endl;
	cout << "|" << theBoard[1][0] << "|" << theBoard[1][1] << "|" << theBoard[1][2] << endl;
	cout << "--------------------" << endl;
	cout << "|" << theBoard[2][0] << "|" << theBoard[2][1] << "|" << theBoard[2][2] << endl;

}

void TicTacToe::postMove(int row, int col, char value){
	//gets the users move and post it to the board//showXO
	char currentPlayer = ' ';
	//it's not doing what I want... 
	// the spaces are all full.. 
	
	if (theBoard[row][col]== 1|| theBoard[row][col]==2)
	{
		cout << "Space already used. Try again. " << endl;
	}
	else 
	{
		theBoard[row][col]=currentPlayer;
	}
 
	for(row = 0; row <3; row ++)
	{
		if(row > 3)
		{// should I say(row != 0) (row != 1) (row != 2)??
			cout << "Invalid choice" << endl;
		}
	
		for(col = 0; col <3; col++)
		{
			if(col > 3)
			{
				cout << "Invalid choice" << endl;
			}
		
			if(theBoard[row][col]==0)
			{
				theBoard[row][col]=' ';
			}
			if (theBoard[row][col]== 1)
			{
				theBoard[row][col] = 'X';
			}
			if (theBoard[row][col]== 2)
			{
				theBoard[row][col] = 'O';
			}

		}
	}
}








char TicTacToe::determineWinner(void){
      //analyzes the board to see if there is a winner
      //returns a X, O indicating the winner
      //if the game is a draw then D is returned

      //check the rows
      for (int i = 0; i < 3; i++){
            if (theBoard[i][0] == theBoard[i][1]
                 && theBoard[i][1] == theBoard[i][2]
                 && theBoard[i][0] != ' '){
                 return theBoard[i][0];
            }
      }

      //check the clmns
      for (int i = 0; i < 3; i++){
             if (theBoard[0][i] == theBoard[1][i]
                   && theBoard[1][i] == theBoard[2][i]
                   && theBoard[0][i] != ' '){
                   return theBoard[0][i];
             }
       }

       //check the diagnals
       if (theBoard[0][0] == theBoard[1][1]
              && theBoard[1][1] == theBoard[2][2]
              && theBoard[0][0] != ' ') {
              return theBoard[0][0];
       }

       if (theBoard[2][0] == theBoard[1][1]
               && theBoard[1][1] == theBoard[0][2]
               && theBoard[2][0] != ' ') {
               return theBoard[2][0];
       }

       return 'D';

}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 21 August 2009 - 06:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1