Need help with a tictactoe game

I need help with this tictactoe game that I have to write up and that is contains 3 files with it 2 .cpp files and 1 .h file.



This is the TicTacToeimp.cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include "TicTacToe.h"

using namespace std;


int winner(char Array[][3], int idx);
void aTie();
void printBoard(char Array[][3], int idx);

int main()
{
	char Array[3][3] = { {'-', '-', '-' },
					     {'-', '-', '-' },
					     {'-', '-', '-' } };
	int idx, player = 1, player_row, player_col, gameOver;
	int temp;
	//board print
	do
	{
	printBoard(Array, 3);
	cout << "Player " << player << " Please enter a row and a column number." << endl;
	cin >> player_row;
	cin >> player_col;
	if(player == 1)
		Array[player_row][player_col] = 'X';
	else 
		Array[player_row][player_col] = 'O';
	player = (player%2 + 1);
	temp = winner(Array,3);
	} while((winner(Array, 3) == 0));
return 0;
}

//returns 1 if winner, 2 if board full, 0 if neither
int winner(char Array[][3], int idx)
{
	for (int row = 0; row < 3; row++)
	{
		if (Array[row][0] ==  Array[row][1] && Array[row][1] == Array[row][2])
		{
			if (Array[row][0] == 'X')
			{
				cout << "That's impossible! Player 1 WON!" << endl;
				return 1;
			}
			if (Array[row][0] == 'O')
			{
				cout << "This can't be real! Player 2 WON!" << endl;
				return 1;
			}
		}
	}

	for (int col = 0; col < 3; col++)
	{
		if(Array[0][col] == Array[1][col] && Array[1][col] == Array[2][col])
		{
			if(Array[0][col] == 'X')
			{
				cout << "That's impossible! Player 1 WON!" << endl;
				return 1;
			}
			if(Array[0][col] == 'O')
			{
				cout << "This can't be real! Player 2 WON!" << endl;
				return 1;
			}
		}
	}

	if (Array[0][0] == Array[1][1] && Array[1][1] == Array[2][2])
	{
		if(Array[1][1] == 'X')
		{
			cout << "That's impossible! Player 1 WON!" << endl;
			return 1;
		}
		if(Array[1][1] == 'O')
		{
			cout << "This can't be real! Player 2 WON!" << endl;
			return 1;
		}
	}
		
	if (Array[0][0] != '-' && Array[0][1] != '-' && Array[0][2] != '-' && Array[1][0] != '-' && Array[1][1] != '-' && Array[1][2] != '-' && Array[2][0] != '-' && Array[2][1] != '-' && Array[2][2] != '-')
	{
		aTie();
		return 2;
	}
	else
		return 0;
	return 0;
}
//no winner/tie
void aTie()
{
	char ans = ' ';
	cout << "A forseen tie! Play again yes or no(y/n)?";
	cin >> ans;
	if (ans == 'y' || ans == 'Y')
		main();
	else
		"Thanks for playing!";
}
void printBoard(char Array[][3], int idx)
{
	for(int row = 0; row < 3 ; row++) 
	{
		cout << endl;
		for (int col = 0; col < 3 ; col++)
			cout << Array[row][col] << "\t";
	}
}






This is the TicTacToe.h file:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Tic-Tac-Toe Header file 

#include <string>

using namespace std;
 
enum status {WIN, DRAW, CONTINUE};

class ticTacToe
{
public:
	void play();
       //Function to start the play.
	


    void displayBoard() const;
       //Function to print the board.

    bool isValidMove(int x, int y) const;
       //Function to determine if a move is valid.

	bool getXOMove(char moveSymbol);
       //Function to get a move for a player

    status gameStatus();
       //Function to determine the current status of the game.

    void reStart();
       //Function to restart the game.

    ticTacToe();
       //Default constructor.
       //Postcondition: Initializes the board to an empty state.

private:
    char board[3][3];
    int noOfMoves; 
};


This is the tictactoe.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "TicTacToe.h"
 
using namespace std;

int main()
{
	 ticTacToe game;

     game.play();

     return 0;
}



what im have trouble with is the tictactoe.h file and i get get what to put in it. coulde anyone help me out.
Thanks
If your project should result in one executable, you should only define exactly one main() function. There's no need for main() in TicTacToe.cpp.

I think your TicTacToe.h isn't a problem. Instead you should think about TicTacToe.cpp. It should implement all those method as declared in TicTacToe.h.

F.e.:

1
2
3
4
5
6
7
8
#include "TicTacToe.h"

void ticTacToe::play()
{
    // Whatever code is needed to play comes here
}

// And so on.... 
Topic archived. No new replies allowed.