help with tic tac toe winner

hello i need help finding the winner in my tic tac toe program.
Program below
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

void InitaializeBoard (char board [][3]);
void DisplayBoard (char board [][3]);
void GetBoardConfiguration (char board [][3]);
char FindRowWin (char board [][3]);
void DisplayStatistics (char board [][3]);
void GenerateReports (char board [][3]);
using namespace std;

int main()
{ 
	char play;
	char gameBoard[3][3];
	InitaializeBoard(gameBoard);

	cout << "Would you like to play the game? Enter 'y' for yes and 'n' for no." << endl;
	cin >> play;

	while (play == 'y')
	{
		GetBoardConfiguration(gameBoard);
		DisplayBoard(gameBoard);
		char FindRowWin;
		

		cout << "Would you like to play the game? Enter 'y' for yes and 'n' for no." << endl;
		cin >> play;
	}

	system("pause");
	return 0;
}
//******************************************************************************
void InitaializeBoard (char board [][3])
{
	for(int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			board[i][j] = ' ';
		}
	}
	return;
}
//******************************************************************************
void DisplayBoard (char board [][3])
{
	//Displays game configuration 
		for (int row = 0; row < 3; row++)
		{
			for (int col = 0; col < 3; col++)
				cout << board[row][col] << " ";
			cout << endl;
		}
		return;
}
//******************************************************************************
void GetBoardConfiguration (char board [][3])
{
		char extra; //to get rid of extra characters in input stream
		cout << "Enter 9 characters; either blan, x, or o, and then press enter" << endl;
		cin.get(extra);

	//reads in game configuration
		for (int row = 0; row < 3; row++)
		{
			for (int col = 0; col < 3; col++)
			{
				cin.get(board[row][col]);
			}
		}
		return;
}
//*****************************************************************************
char FindRowWin (char board [][3])
{
	int row0;
	int row1;
	int row2;

	if( board[0][0] == board[0][1] == board[0][2])
		return row0;
	if( board[1][0] == board[1][1] == board[1][2])
		return row1; 
	if( board[2][0] == board[2][1] == board[2][2])
		return row2; 

	

}

stuck down here at the bottom
Last edited on
Several problems with FindRowWin ().

Lines 85, 87, 89: You can;t do conparisons like that.

C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Lines 81-83, 86, 88, 98: row0, row1, row2 are all uninitialized variables. You're returning garbage.

Line 93: If none of the if statements are true, you don't return anything.

Lines 79,86,88,90: You have a type mismatch between the function return type (char) and what you're trying to return (int).

Here's my favorite function for checking a winner on a 2D tic-tac-toe game.
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
struct Combo
{   int row;
    int col;
};
    
bool check_winner (char board[3][3], char who)
{  const Combo combo[8][3] = 
   {  0,0, 0,1, 0,2,    //  Row 0
      1,0, 1,1, 1,2,    //  Row 1
      2,0, 2,1, 2,2,    //  Row 2
      0,0, 1,0, 2,0,    //  Col 0
      1,0, 1,1, 2,1,    //  Col 1
      2,0, 2,1, 2,2,    //  Col 2
      0,0, 1,1, 2,2,    //  LR Diagonal
      0,2, 1,1, 2,0,    //  RL Diagonal
    };
    
    for (int i=0; i<8; i++) //  Check each combo
    {   int count = 0;
        for (int j=0; j<3; j++)     // Check row and col
        {   if (board[combo[i][j].row][combo[i][j].col] == who)
                count++;  //  One cell matched
        }
        if (count == 3)
            return true;    //  All three cells matched
    }
    return false;   //  No combo's matched
}                

thanks for the help!
Topic archived. No new replies allowed.