This is the .h part:
#include<string>
using namespace std;
#ifndef GAME_H
#define GAME_H
struct Player
{
string name;
char symbol;
int currentScore;
};
class TicTacToe
{
private:
Player player1, player2;
int currentWinner;
char board[9];
//bool player;
public:
TicTacToe();
void Reset();
void playerName(string, string);
void DisplayBoard(char [9]);
void ChoosePosition(char [9], bool);
bool CheckforWinner(char [9]);
bool isLegal(char [9], int);
void DisplayScore();
};
#endif
the c++ part:
#include "game.h"
#include<iostream>
TicTacToe::TicTacToe()
{
player1.name="P1";
player2.name="P2";
player1.symbol='X';
player2.symbol='0';
player1.currentScore=0;
player2.currentScore=0;
currentWinner=3;
}
void TicTacToe::playerName(string pl1, string pl2)
{
player1.name = pl1;
player2.name = pl2;
player1.symbol = 'X';
player2.symbol = 'O';
player1.currentScore = 0;
player2.currentScore = 0;
currentWinner = 3;
}
void TicTacToe::DisplayBoard(char boardReset[9])
{
cout << "\n " << boardReset[0] << " | " << boardReset[1] << " | " << boardReset[2] << endl
<< " ---------" << endl
<< " " << boardReset[3] << " | " << boardReset[4] << " | " << boardReset[5] << endl
<< " ---------" << endl
<< " " << boardReset[6] << " | " << boardReset[7] << " | " << boardReset[8] << endl;
}
bool TicTacToe::CheckforWinner(char boardcheck[9])
{
if(boardcheck[0] == boardcheck[1] && boardcheck[2] == boardcheck[0] )
return true;
else if(boardcheck[3] == boardcheck[4] && boardcheck[5] == boardcheck[3])
return true;
else if(boardcheck[6] == boardcheck[7] && boardcheck[8] == boardcheck[6])
return true;
else if(boardcheck[0] == boardcheck[3] && boardcheck[6] == boardcheck[0])
return true;
else if(boardcheck[1] == boardcheck[4] && boardcheck[7] == boardcheck[1])
return true;
else if(boardcheck[2] == boardcheck[5] && boardcheck[8] == boardcheck[2])
return true;
else if(boardcheck[0] == boardcheck[4] && boardcheck[8] == boardcheck[0])
return true;
else if(boardcheck[2] == boardcheck[4] && boardcheck[6] == boardcheck[2])
return true;
else
return false;
}
void TicTacToe::ChoosePosition(char boardchoose[9], bool pl)
{
int spot;
if(pl == true)
cout << "\nEnter your move Player 1: ";
else
cout << "\nEnter your move Player 2: ";
cin >> spot;
if(isLegal(spot))
{
if(pl == true)
boardchoose[spot-1] = 'X';
else
boardchoose[spot-1] = 'O';
}
else
ChoosePosition(pl);
DisplayBoard();
}
bool TicTacToe::isLegal(char boardlegal[9], int spt)
{
if(boardlegal[spt-1] == 'X' || boardlegal[spt-1] == 'O')
return false;
else
return true;
}
and the main part:
#include "game.cpp"
#include<iostream>
using namespace::std;
#include <stdlib.h>
int main()
{
TicTacToe game1;
string name1, name2;
bool player = false; // true = X false = O
char array[9]={'1','2','3','4','5','6','7','8','9'};
//int count = 1;
//int winOrLoose;
cout<<"Welcome to TicTacToe version C++\n";
cout<<"Player1 please enter your name here: ";
getline(cin, name1);
cout<<"Player2 please enter your name here: ";
getline(cin, name2);
game1.playerName(name1, name2);
game1.DisplayBoard(array);
while(!checkforWinner(array))
{
if(player == true)
player = false;
else
player = true;
ChoosePosition(player);
}
if(player == true)
cout << "Player 1 WINS" << endl;
else
cout << "Player 2 WINS" << endl;
return 0;
}
please need some hints!!!!!
You need to make sure your functions are spelled properly and if you are using class functions, you need to use a class object to execute them.