Homework assignment I dont completely understand

Create a C++ console application that allows two players to participate in a tic-tac-toe game. The program will be responsible for deciding who wins the game.

The program should include the following class, properties and methods:

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
}

void TicTacToe::switchPlayer(char &currentPlayer){
//switches the current player
}

void TicTacToe::showBoard(){
//displays the board
}

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

The output of the program should be similar to this:
[ ] [ ] [ ]
[ ] [O] [ ]
[ ] [ ] [X]

Player X:
Enter row of move (1, 2, 3): 3
Enter column of move (1, 2, 3): 1
Ok I went back and revised some...

Anyone want to help me and explain why?



// Nathaniel Drake
// Lab 6 Excercise 2
// This lab creates and allows two users to play a friendly game of tic-tac-toe.
// Tic-tac-toe.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

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 grid [3][3];
for(int i = 0; i < 3;i++)
for (int j = 0; j < 3; j++)
cout << grid [i][j] = 0;
}

void TicTacToe::switchPlayer(char &currentPlayer){
//switches the current player
if currentPlayer == 'O'
cout << "Player 2 its your turn" << endl;
else currentPlayer == 'X'
cout << "Player 1 its your turn" << endl;

}

void TicTacToe::showBoard(){
cout << "[ ] [ ] [ ] \n";
cout << "[ ] [ ] [ ] \n";
cout << "[ ] [ ] [ ] \n";//displays the board

for(int i = 0; i < 3;i++)
for (int j = 0; j < 3; j++)
cout << grid [i][j] = 0;
}

void TicTacToe::postMove(int row, int col, char value){
//gets the users move and posts it to the board
cout << "Player 1 whats your move:" << postMove;
cin >> >> postMove;
cout << "Player 2 whats your move:" << postMove;

}

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