Referencing a Function

When I enter the selected rows and columns in my postMove it doesn't show up when building the program? I believe everything is set up right and this function is holding others at a stand still unless, I'm missing a part in another function. Any good debuggers that can assist?



#include <iostream>

using std::cout;
using std::cin;
using std::endl;

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();

system("pause");
}

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;

while (row < 1 || row > 3)
{
cout << "Invalid posiition number" << endl;
cout << "Select 1 for row 1, 2 for row 2, 3 for row 3: ";
cin >> row;
}

cout << "enter your column: ";
cin >> clmn;

while (clmn < 1 || clmn > 3)
{
cout << "Invalid posiition number" << endl;
cout << "Select 1 for column 1, 2 for column 2, 3 for column 3: ";
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
if (currentPlayer == 'X')
{
currentPlayer = 'O';
}
else
{
currentPlayer = 'X';
}


}

void TicTacToe::showBoard() //displays the board
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
theBoard[i][j]= '-';
}
}
cout << "[ " << theBoard[0][0] << " ]" << "[ " << theBoard[0][1] << " ]" << "[ " << theBoard[0][2] << " ]" << endl;
cout << "[ " << theBoard[1][0] << " ]" << "[ " << theBoard[1][1] << " ]" << "[ " << theBoard[1][2] << " ]" << 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 posts it to the board
{
if (theBoard[row -1][col -1] == 'O')
{theBoard[row][col] = 'X';}
else
{ cout << "Space already occupied, please select another." << 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';
}
As I can't post the code here (too long because added comments).
Here, I post the code without comments
If you want the version with comments, you can send an e-mail to clsdennis2007@hotmail.com or add clsdennis2007@hotmail as MSN contact.
I have bolded the changes I made.

-----------------------------------------------------------------------------------------
// Debugged and modified by Dennis.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class TicTacToe{
private:
char theBoard [3][3];
bool excption;


public:
TicTacToe(void);
void playOneGame(void);
void switchPlayer(char&);
void showBoard(void) const;
bool postMove(int, int, char);
char determineWinner(void) const;
};

int main (void){

TicTacToe Game1;
Game1.playOneGame();

system("pause");
}

void TicTacToe::playOneGame(void){

const int MaxMoves = 9;
char currentPlayer = 'O';
int row = 0;
int clmn = 0;
char theWinner = ' ';
int nmbrOfMoves = 0;

do {
switchPlayer(currentPlayer);
showBoard();



cout << "\n\nPlayer " << currentPlayer << endl;
cout << "enter your row: ";
cin >> row;

while (row < 1 || row > 3)
{
cout << "Invalid posiition number" << endl;
cout << "Select 1 for row 1, 2 for row 2, 3 for row 3: ";
cin >> row;
}

cout << "enter your column: ";
cin >> clmn;

while (clmn < 1 || clmn > 3)
{
cout << "Invalid posiition number" << endl;
cout << "Select 1 for column 1, 2 for column 2, 3 for column 3: ";
cin >> clmn;
}

if (!postMove(row, clmn, currentPlayer))
continue;



theWinner = determineWinner();

nmbrOfMoves++;


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


showBoard();


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

TicTacToe::TicTacToe(void){

for (int a=0; a<3; a++)
for (int b=0; b<3; b++)
theBoard[a][b] = '-';

excption = false;



}


void TicTacToe::switchPlayer(char &currentPlayer)
{
if (!excption)
{


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

}
else
{
excption = false;
}

}




void TicTacToe::showBoard() const
{
for (int rw=0; rw<3; rw++){
for (int cl=0; cl<3; cl++){
cout << "[ " << theBoard[rw][cl] << " ]";
}
cout << "\n";
}


}

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

if (theBoard[row -1][col -1] == '-')
{

theBoard[row-1][col-1] = value;

return true;
}

else
{
cout << "Space already occupied, please select another." << endl;
excption = true;
return false;

}

}


char TicTacToe::determineWinner(void) const
{

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


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';
}
Last edited on
Topic archived. No new replies allowed.