Hello,
Been trying to work on this tic tac toe game and I can't seem to figure out how to post the moves to the board. Everything as I have it now seems ok with the compilier but I just need help with that part. Let me know . . thanks!!

/* Specification:
This program is a tic tac toe game*/

#include <iostream>
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 row = 0;
		int col = 0;
		for (int i = 0; i < row; i++)
		{
			for (int j =0; j < col; j++)
				cout << theBoard [i][j];
			cout << endl;
		}
			
}

void TicTacToe::switchPlayer(char &currentPlayer){
         //switches the current player
	  //If currentPlayer is equal to X
		if (currentPlayer == 'X')
               //Set currentPlayer to zero
			currentPlayer = 'O';
      //otherwise
		else
                //Set currentPlayer to X
			currentPlayer = 'X';
}

void TicTacToe::showBoard(){
        //displays the board
int grid[3][3];
	for(int i = 0; i< 3; i++)
		for(int j = 0; j< 3; j++)
			grid[i][j] = 0;
	for(int i = 0; i< 3; i++)
	{
		for(int j = 0; j< 3; j++)
			cout << " [ "<< grid[i][j] << " ] ";
		cout << endl;
	}

}

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

Recommended Answers

All 4 Replies

I suggest that you create a new char array,initialize the array with 0i.e. '0', fill in the co-ordinates of the user's input, then display the array,using a for loop.

would this be anything close to what you need...?

void TicTacToe::postMove(int row, int col, char value)
{
     theboard[row][col] = value;
     showBoard();
}

would this be anything close to what you need...?

void TicTacToe::postMove(int row, int col, char value)
{
     theboard[row][col] = value;
     showBoard();
}

Keep in mind that I am really new at this . . so it's confusing to me, lol . . this does not seem to work but I can see where you are going with this. I know that I need to -1 from the row and column because of the array. Like if the user enters 3 it would really be 2 in the array. I dont really know what to do to do that. Probably is something way more simple then I would think it would be.

then how about this..??

void TicTacToe::postMove(int row, int col, char value)
{
     theboard[row-1][col-1] = value; 
     showBoard();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.