4 Replies - 558 Views - Last Post: 21 August 2009 - 10:12 PM Rate Topic: -----

#1 sp11765  Icon User is offline

  • New D.I.C Head

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

Tic Tac Toe Class Problems displaying my board

Posted 21 August 2009 - 06:21 PM

I'm working on my Tic Tac Toe problem and I am having problems... when I run the code the board is full of junk, so I think that I'm not initializing it properly. I had it last night clear, but it wasn't displaying the x and o's. can someone help?

I saw the program you guys put out, but I couldn't make the changes to my program to make it work to display the x and o's.

Any additional help is appreciated. Thanks!!

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

using namespace std;

class TicTacToe{
	private:
		char theBoard[3][3]; //variable
	
		
		
		
	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
	int i = 0;
	int j = 0;
	theBoard[3][3]=' ';
	for(int i = 0; i < 3;i++)
        for (int j = 0; j < 3; j++);
		theBoard[i][j]=0;
		theBoard[i][j]=' ';

		

	
}


void TicTacToe::switchPlayer(char &currentPlayer){
	//switchs the current player
	
	currentPlayer = ' ';
		
	if (currentPlayer =='X')
	{
		
		currentPlayer = 'O';
		
	}
	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 != 0 &&  row != 1&& row != 2)
		{// should I say??
			cout << "Invalid choice" << endl;
		}
	
		for(col = 0; col <3; col++)
		{
			if(col != 0 && col != 1 && col != 2)
			{
				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';

}

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


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe Class Problems displaying my board

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Tic Tac Toe Class Problems displaying my board

Posted 21 August 2009 - 06:29 PM

TicTacToe::TicTacToe(void){
        //intialize the array contents
        int i = 0;
        int j = 0;
        theBoard[3][3]=' '; // Exceeds the bounds of the arrays
        for(int i = 0; i < 3;i++)
        for (int j = 0; j < 3; j++); // <-- Get rid of the ;
                theBoard[i][j]=0;   // Don't think you want this
                theBoard[i][j]=' ';
        
}

Was This Post Helpful? 1
  • +
  • -

#3 sp11765  Icon User is offline

  • New D.I.C Head

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

Re: Tic Tac Toe Class Problems displaying my board

Posted 21 August 2009 - 07:37 PM

I fixed most of my problems... except one I can't change the x's and o's can you help? Thanks Again!!!

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

using namespace std;

class TicTacToe{
	private:
		char theBoard[3][3]; //variable
			
		
	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 << "\nEnter your row (01, 02, 03): ";
		cin >> row;
		cout << "\nEnter your column (01, 02, 03): ";
		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" << endl;
}

TicTacToe::TicTacToe(void){
	//intialize the array contents
	
	for(int row= 0; row< 3; row ++){
		for (int clmn=0; clmn<3; clmn++){
		theBoard[row][clmn]=' ';
		}
	}
}


void TicTacToe::switchPlayer(char &currentPlayer){
	//switchs the current player
	
	currentPlayer = ' ';
		
	if (currentPlayer ='X')
	{		
		(currentPlayer = 'O');
		cout << "\nIt's player 1 turn" << endl;
		
	}
	else
		(currentPlayer = 'X');		
		
}
	

void TicTacToe::showBoard(){
	//displays the board
	cout << "	 Tic Tac Toe!" << endl << endl;
	cout << "	 1	 2	 3" << endl << endl;
	cout << "1	" << theBoard[0][0] << "  |  " << theBoard[0][1] << "  |  " << theBoard[0][2] << endl;
	cout << "   -----------------" << endl;
	cout << "2	" << theBoard[1][0] << "  |  " << theBoard[1][1] << "  |  " << theBoard[1][2] << endl;
	cout << "   -----------------" << endl;
	cout << "3	" << theBoard[2][0] << "  |  " << theBoard[2][1] << "  |  " << theBoard[2][2] << endl << endl;
}

	

void TicTacToe::postMove(int row, int col, char value){
	//gets the users move and post it to the board//showXO
	char currentPlayer = ' ';
	theBoard[row-1][col-1]= value;
	if (row!=0 && row != 1 && row != 2)
	{
		cout << "\nInvalid move, try Again" << endl;
	}
	else if (col != 0 && col != 1 && col != 2)
	{
		cout << "\nInvalid move, try Again" << endl;;
	}
	else if(theBoard[row][col]== currentPlayer)
	{
		cout << "\nAlready Taken, Try Again" << endl;;
	}

	
}



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';

}


Was This Post Helpful? 0
  • +
  • -

#4 sp11765  Icon User is offline

  • New D.I.C Head

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

Re: Tic Tac Toe Class Problems displaying my board

Posted 21 August 2009 - 08:33 PM

I also want to know why it's asking me to enter the numbers 01, 02, 03 and so on because the array is supposed to start with 0, 1, 2... so I had to change it. I thought that was strange.
Was This Post Helpful? 0
  • +
  • -

#5 sp11765  Icon User is offline

  • New D.I.C Head

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

Re: Tic Tac Toe Class Problems displaying my board

Posted 21 August 2009 - 10:12 PM

was anyone able to figure this out?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1