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?
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();
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 ¤tPlayer)
{
//switches the current player
if (currentPlayer == 'X')
{
currentPlayer = 'O';
}
else
{
currentPlayer = 'X';
}
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];
}
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;