Hey there. I am taking a programming class using C++ and my assignment is to create "Tictactoe" using ObjectOrientedProgramming... The problem is I have no idea what I am doing...
I need to have a "tictactoe" class file and a "player" class file - each with their own header files and one driver file.
"You will need to have two classes: a TicTacToe class and a Player class. The TicTacToe class should contain the above mentioned 2D array for representing the game board, i.e., this class should have a 2D array of characters as its data member. You should have a zero-argument constructor in the TicTacToe class that will generate the initial game state (all the 2D array elements initialized to '\0'). You should overload the << operator to display the game board at any point. The game board should be displayed along with the grid as shown in the sample executions. There should also be a member function called SetValue that will have three arguments (a row index, a column index, and an integer representing the player index) and a boolean return type. This function will set the specified cell of the 2D array to an appropriate character depending on the player index. This function should also make sure that a player makes its move to a blank and valid (inside the array bounds) position. In case a player wants to make a move to an invalid or already occupied position, it should return false. In case of a valid move, it should return true. There should be another member function called GetStatus that will return an integer (0-3) representing the game status; 0 representing a tie, 1 representing a win by Player1, 2 representing a win by Player2, and 3 otherwise.
The Player class will be relatively simple and will have information such as the player’s name and an integer representing whether he/she is Player1 or Player2. It should have a two-argument constructor to initialize the data members, a member function called GetIndex to return an integer value (1 or 2) depending on whether it’s Player1 or Player2, and a member function called GetName to return the name of the player. There should be a member function called NextMove that will allow a Player object to make a move. This function should accept a TicTacToe object by reference, prompt the player to enter his move, accept the move in the form of the row and column numbers of the position where he wants to make the move (look at the sample execution), and make changes to the 2D array of the TicTacToe object. Note that this function will be calling the SetValue function of the TicTacToe class and will keep on re-prompting the player (as shown in the sample executions) to make another move as long as the SetValue function returns false. Note that the Player class should not have the TicTacToe object as its data member."
So far, all I have is:
tictactoe.cpp
#include <iostream>
using namespace std;
#include "tictactoe.h"
Tictactoe::Tictactoe()
{
}
void Tictactoe::displayBoard()
{
cout << endl << endl;
cout << " " << s[1] << " | " << s[2] << " | " << s[3] << endl;
cout << " -----------" << endl;
cout << " " << s[4] << " | " << s[5] << " | " << s[6] << endl;
cout << " -----------" << endl;
cout << " " << s[7] << " | " << s[8] << " | " << s[9] << endl << endl;
}
void Tictactoe::placement()
{
cout << "Which row please? " << endl;
cin >> row;
cout << "Which column please? " << endl;
cin >> col;
if (row == 1)
{
if (col == 1)
{
s[1] = 'X';
}
else if (col == 2)
{
s[2] = 'X';
}
else if (col == 3)
{
s[3] = 'X';
}
}
else if (row == 2)
{
if (col == 1)
{
s[4] = 'X';
}
else if (col == 2)
{
s[5] = 'X';
}
else if (col == 3)
{
s[6] = 'X';
}
}
else if (row == 3)
{
if (col == 1)
{
s[7] = 'X';
}
else if (col == 2)
{
s[8] = 'X';
}
else if (col == 3)
{
s[9] = 'X';
}
}
}
tictactoe.h
class Tictactoe
{
private:
int s[9];
int row, col;
public:
Tictactoe();
void displayBoard();
void placement();
};
player.cpp
#include <iostream>
using namespace std;
#include "player.h"
void Player::getName(string name)
{
playerName = name;
}
int Player::choose(int player)
{
int random_integer = rand();
if (random_integer%2)
{
cout << playerName << " goes first." << endl;
}
else
{
cout << playerName << " goes first." << endl;
}
}
player.h
class Player
{
private:
string playerName;
public:
void getName(string name);
int choose(int player);
};
and the driver file
#include <iostream>
using namespace std;
#include "tictactoe.h"
int main()
{
Tictactoe game;
return 0;
}
Any help is greatly appreciated -- mainly I'm not quite sure what is it I need to add to make this work: not looking for code but description of what I'm missing.
Thanks