tictactoe

any time it becomes player 1's turn this terminates...why?
main.cpp
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

int main()
{
	cout << "Welcome to Tic-Tac-Toe" << endl<< "----------------------" << endl << endl;

	//create player objects
	player player1, player2;
	//create temp string to store player name
	string tempName;

	//random person to start game
	unsigned seed = time(0);
	srand(seed);
	int starterNumber = 1 + rand() % 2;


	//enter player 1 name, then set name and player number
	cout << "Enter your name, Player 1: ";
	cin >> tempName;
	player1.setValue(tempName, 1);
	tempName = "";

	//enter player 1 name, then set name and player number
	cout << "Enter your name, Player 2: ";
	cin >> tempName;
	player2.setValue(tempName, 2);
	tempName = "";

	//display toss winner
	if(starterNumber == 1)
		cout << player1.getName() << " won the toss."<< endl;
	else if (starterNumber == 2)
		cout << player2.getName() << " won the toss."<< endl;

	//display game board
	tictactoe tictactoe1;

	//input row and column, player1 or player2
	int row, column, playerNumber, playerIndex;

	//playerNumber = starterNumber;
	if(starterNumber == 1)
		playerNumber = 2;
	else
		playerNumber = 1;

	// ALWAYS RETURNING TRUE FIX IT!!!!
	do
	{
		if(playerNumber == 1)
			playerNumber = 2;
		else
			playerNumber = 1;

		if(playerNumber == 1)
		{
			cout << "Make your move " << player1.getName() << ": ";
			cin >> row >> column;
			playerIndex = 1;

			while (tictactoe1.setValue(row, column, playerIndex) != true);
			{
				cout << "Invalid Move, make your move " << player1.getName() << ": ";
				cin >> row >> column;
				playerIndex = 1;
			}
		}
		else
		{
			cout << "Make your move " << player2.getName() << ": ";
			cin >> row >> column;
			playerIndex = 2;

			while (tictactoe1.setValue(row, column, playerIndex) != true);
			{
				cout << "Invalid Move, make your move " << player1.getName() << ": ";
				cin >> row >> column;
				playerIndex = 1;
			}
		}


	}while (tictactoe1.getStatus() == 3);
}


tictactoe.cpp

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
bool tictactoe::setValue(int row, int column, int playerIndex)
{
	bool returnValue;

	if ((row >= 0 && row <= 2) && (column >= 0 && column <= 2) && gameBoard[row][column] == 0)
	{
		if(playerIndex == 1)
		{
			gameBoard[row][column] = 'X';
			returnValue = true;
		}
		else if(playerIndex == 2)
		{
			gameBoard[row][column] = 'O';
			returnValue = true;
		}
	}
	else
		returnValue = false;

	return returnValue;
}


int tictactoe::getStatus()
{
	//return value
	int returnStatus;
		//check if player1 wins
	    if ((gameBoard[0][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[2][2] == 'X'))
	    	returnStatus = 1;

	    else if ((gameBoard[2][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[0][2] == 'X'))
	    	returnStatus = 1;

	    else if ((gameBoard[0][0] == 'X') && (gameBoard[0][1] == 'X') && (gameBoard[0][2] == 'X'))
	    	returnStatus = 1;

	    else if ((gameBoard[0][0] == 'X') && (gameBoard[0][1] == 'X') && (gameBoard[0][2] == 'X'))
	    	returnStatus = 1;

	    else if ((gameBoard[1][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[1][2] == 'X'))
	    	returnStatus = 1;

	    else if ((gameBoard[2][0] == 'X') && (gameBoard[2][1] == 'X') && (gameBoard[2][2] == 'X'))
	    	returnStatus = 1;

	    else if ((gameBoard[1][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[1][2] == 'X'))
	    	returnStatus = 1;

	    else if ((gameBoard[2][0] == 'X') && (gameBoard[2][1] == 'X') && (gameBoard[2][2] == 'X'))
	    	returnStatus = 1;


	    if ((gameBoard[0][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[2][2] == 'O'))
	    	returnStatus = 2;
	    else if ((gameBoard[2][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[0][2] == 'O'))
	    	returnStatus = 2;

   	    else if ((gameBoard[0][0] == 'O') && (gameBoard[0][1] == 'O') && (gameBoard[0][2] == 'O'))
   	    	returnStatus = 2;

   	    else if ((gameBoard[0][0] == 'O') && (gameBoard[0][1] == 'O') && (gameBoard[0][2] == 'O'))
   	    	returnStatus = 2;

   	    else if ((gameBoard[1][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[1][2] == 'O'))
   	    	returnStatus = 2;

   	    else if ((gameBoard[2][0] == 'O') && (gameBoard[2][1] == 'O') && (gameBoard[2][2] == 'O'))
   	    	returnStatus = 2;

   	    else if ((gameBoard[1][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[1][2] == 'O'))
   	    	returnStatus = 2;

   	    else if ((gameBoard[2][0] == 'O') && (gameBoard[2][1] == 'O') && (gameBoard[2][2] == 'O'))
   	    	returnStatus = 2;



	    //game ends in tie
	    else
	    {
	    	returnStatus = 0;
	    }

	    //check if game still playing
	    	    for(int row = 0; row <= 2; row++)
	    	    		for(int column = 0; column <= 2; column++)
	    	    			if (gameBoard[row][column] == 0)
	    	    			{
	    	    					returnStatus = 3;
	    	    			}


	    return returnStatus;
}

any time it becomes player 1's turn this terminates...why?

That doesn't agree with the comment you have at line 47.
 
// ALWAYS RETURNING TRUE FIX IT!!!! 

Looking at line 83
 
}while (tictactoe1.getStatus() == 3);

we can see that the loop will continue as long as getStatus() returns 3.

Looking at getStatus(), we see that it's going though a series of if statements checking for a win, then at line 87 regardless of whther a win was detected, you seach the board for an empty square and if you find one you return a 3. Therefore, your game will continue after a winning move as long as there is an empty position.


That doesn't agree with the comment you have at line 47.

// ALWAYS RETURNING TRUE FIX IT!!!!

this was a previous comment i didn't go back and delete.


Looking at getStatus(), we see that it's going though a series of if statements checking for a win, then at line 87 regardless of whther a win was detected, you seach the board for an empty square and if you find one you return a 3. Therefore, your game will continue after a winning move as long as there is an empty position.


I fixed this.
But basically the program will not allow player1 to ever enter a move, and if player to is chosen to start the game then they enter a move and the program terminates and i don't see what could be causing this. Any ideas?
Topic archived. No new replies allowed.