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 ¤tPlayer){
//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

New Topic/Question
Reply




MultiQuote





|