Objective
Code a game allowing two human players to play tictactoe.
Create 2 classes:
-Create a 3 x 3, 2-D array board class to play the game.
-Player; has a private string name data member and a method that reads the players’ row and column selections from the keyboard.
Create 2 player objects from this class. Name the players Orestes and Xerxes.
Think carefully about board and player classes responsibilities and how they interaction with one another. The players do not collaborate with one another but they collaborate with the board.
My Problem
The program compiles with the header file, but it the displaying is wrong as you will see when you enter your row and column.
Please look into this problem and thank you for your time. I know the project isn't completed because this problem is stalling me.
Sources
Header file:
Code:
#include <iostream>
#include <string>
using namespace std;
class Board
{
public:
void display(char Z[][3], int row, int col);
};
class Player1
{
public:
Player1()
{
name = "Orestes";
};
string name;
};
class Player2
{
public:
Player2()
{
name = "Xerxes";
};
string name;
};
void Board::display(char Z[][3], int row, int col)
{
cout << " _________________\n";
cout << "| | | | \n";
cout << "| " << Z[0][0] << " | " << Z[0][1] << " | " << Z[0][2] <<
" |\n";
cout << "|_____|_____|_____|\n";
cout << "| | | |\n";
cout << "| " << Z[1][0] << " | " << Z[1][1] << " | " << Z[1][2] <<
" |\n";
cout << "|_____|_____|_____|\n";
cout << "| | | |\n";
cout << "| " << Z[2][0] << " | " << Z[2][1] << " | " << Z[2][2] <<
" |\n";
cout << "|_____|_____|_____|\n";
}
Main file:
Code:
#include "TTT.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
char X[][3] = {{'X','X','X'},{'X','X','X'},{'X','X','X'}};
char O[][3] = {{'O','O','O'},{'O','O','O'},{'O','O','O'}};
int row, col;
Board board;
Player1 player1;
//board.display(NULL, NULL, NULL);
cout << endl << player1.name;
cout << "Enter row: ";
cin >> row;
cout << "Enter column: ";
cin >> col;
board.display(O, row, col);
}