TicTacToe with classes

I have this TicTacToe program that needs finishing. All the code is finished, but I'm getting runtime errors. It is printing out junk values from my array when they should be empty. I would love help please. This is due in a day and I've had 4 people working with me for 9 hours trying to get it finished.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Player.h
#include <string>
#include "TTT.h"

using namespace std;

class Player {
private:
	string name;
	int playerNumber;
public:
	Player(string tempName, int tempPlayerNumber);
	int getIndex();
	string getName();
	void nextMove(TTT &object);
};


1
2
3
4
5
6
7
8
9
10
11
12
//TTT.h
using namespace std;

class TTT {
private:
	char board[3][3];
public:	
	TTT();
	void displayBoard();
	bool setValue(int row, int column, int playerNumber);
	int getStatus();
};


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
//Player.cpp
#include <iostream>
#include <string>
#include "Player.h"

using namespace std;

Player::Player(string tempName, int tempPlayerNumber) {
	name = tempName;
	playerNumber = tempPlayerNumber;
}

int Player::getIndex() {
	return playerNumber;
}

string Player::getName() {
	return name;
}

void Player::nextMove(TTT &game) {
	int row, column;
	cout << ", make your move(Ex. 1(space)2): ";
	cin >> row >> column;
	while(row > 2 || row < 0 || column > 2 || column < 0) {
		cout << "Invalid entry. Please try again: ";
		cin >> row >> column;
	}
	while(game.setValue(row, column, playerNumber) != '\0') {
		cout << "That space has already been taken. Please make another selection: ";
	}
	game.setValue(row, column, playerNumber);
}


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
TTT.cpp
#include <iostream>
#include "TTT.h"

using namespace std;

TTT::TTT() {
	const int SIZE = 3;
	int board[SIZE][SIZE] = {};
	//for(int i = 0; i < 3; i++) {
	//	for(int k = 0; k < 3; k++) {
	//		board[i][k] = 0;
	//	}
	//}
}

void TTT::displayBoard() {
	cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl
		 << "-----------" << endl
		 << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl
		 << "-----------" << endl
		 << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
}

bool TTT::setValue(int row, int column, int playerNumber) {
	char i;
	if(playerNumber == 1) {
		i = 'X';
	}
	else if(playerNumber == 2) {
		i = 'O';
	}
	if(board[row][column] == '\0') {
		board[row][column] = i;
		return true;
	}
	else if(board[row][column] != '\0') {
		return false;
	}
}

int TTT::getStatus() {
	for(int i = 0; i < 3; i++) {
		for(int j = 0; j < 3; j++) {
			if(board[i][j] == '\0') {
				return 0;
			}
		}
	}
	
	if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' && 
	   board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' && 
	   board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' &&
	   board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' && 
	   board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' && 
	   board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' &&
	   board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' && 
	   board[0][2] == 'X' && board[1][1] == 'X' && board[0][2] == 'X') {
		return 1;
	}
	else if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' && 
			board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' && 
			board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' &&
			board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' && 
			board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' && 
			board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' &&
			board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' && 
			board[0][2] == 'O' && board[1][1] == 'O' && board[0][2] == 'O') {
			return 2;
		}
	else {
		return 3;
	}
}


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
//driver.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Player.h"

using namespace std;

int main(void) {
	string name1, name2;
	TTT game;
	TTT();
	
	cout << "Welcome to Tic-Tac-Toe" << endl
		 << "----------------------" << endl
		 << "First player, enter your name: ";
	getline(cin, name1);
	Player player1(name1, 1);
	cout << endl
		 << "Second player, enter your name: ";
	getline(cin, name2);
	Player player2(name2, 2);
	
	//random toss
	unsigned seed = time(0);
	srand(seed);
	
	int x = rand() % 100 + 1;
	
	int index = 0;
	if(x > 50) {
		cout << player2.getName() << " won the toss." << endl;
		game.displayBoard();
		do {
			index = player2.getIndex();
			cout << player2.getName();
			player2.nextMove(game);
			game.displayBoard();
			index = player1.getIndex();
			cout << player1.getName();
			player1.nextMove(game);
		}while(game.getStatus() == 3);
	}
	else if(x <= 50) {
		cout << player1.getName() << " won the toss." << endl;
		game.displayBoard();
		do {
			index = player1.getIndex();
			cout << player1.getName();
			player1.nextMove(game);
			game.displayBoard();
			index = player2.getIndex();
			cout << player2.getName();
			player2.nextMove(game);
		}while(game.getStatus() == 3);
	}
	if(game.getStatus() == 0) {
		cout << "The game is a tie!";
	}
	else if(game.getStatus() == 1) {
		cout << player1.getName() << " has won the game! Congratulations!";
	}
	else if(game.getStatus() == 2) {
		cout << player2.getName() << " has won the game! Congratulations!";
	}
	else {
		cout << "Unknown error. Programmer needs to learn how to code.";
	}
}


//This is a screenshot of the output.
http://prntscr.com/t1vkr
By far the easiest way to discover run time errors is to use the debugger. It should be easy if your are using an IDE.

Create a watch list of variables, step through the code 1 line at a time, see where the values go wrong, then deduce what the problem is.

This is much easier than staring at 205 LOC for hours.

HTH
In TTT.cpp on line 9 you declare a variable with the same name as a private variable (board.) It would appear you're actually trying to initialize the data member, but you are not actually doing so.
Right. I was testing different ways, trying to make every element = NULL or empty. The program keeps printing junk letters.
Ok, you have to change TTT
TTT::TTT() {
const int SIZE = 3;
int board[SIZE][SIZE] = {};
//for(int i = 0; i < 3; i++) {
// for(int k = 0; k < 3; k++) {
// board[i][k] = 0;
// }
//}
}

to

TTT::TTT() {
row = 0;
column = 0;
int playerNumber = '\0';
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < 3; k++)
{
board[i][k] = playerNumber;
}
}
}


Okay good. Now it's printing out the blank board like it's supposed to. However, it's telling me "That space has already been taken." error.

http://prntscr.com/t45hq

I can't seem to figure this one out...
Topic archived. No new replies allowed.