Help with a function

This game is mostly working but I think I have an issue with my checkwinner function, it is returning true way too early. Any insight is appreciated!

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
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Write a program that allows twO players to playa game of tic-rae-roc. Use a two dimensional
char array with three rows and three columns as the game board. Each
element of the array should be initialized with an asterisk (*). The program should fll in a
loop that:
		Displays the contents of the board array
		Allows player 1 to select a location on the board for an X. The program should
			ask the user to enter the row and column number.
		Allows player 2 LO select a location on the board for an O. The program should
			ask the user to enter the row and column number.


		Determines whether a player has won, or a tic has occurred. If a player has won,
			the program should declare that player the winner and end. If a tie has occurred,
			the program should say so and end.
		Player 1 wins when there are three XS in a rowan the game board. The XS can appear
			in a row, in a column, or diagonally across the board. A tic occurs when all of the
			locations on the board are full, but there is no winner.
*/



#include <iostream>
#include <string>

using namespace std;
void displayboard ( char [][3], int);
void getmoveO ( char [][3], int); 
void getmovex (char [][3], int);
bool checkwinner(char [][3], int);
int main ()
{
	char ticboard [3][3] = {{'*','*','*'}, 
							{'*', '*', '*'} , 
							{'*', '*', '*'}};
	bool win = 0;

	while (win == false)
	{
		displayboard (ticboard, 3);
		getmoveO (ticboard, 3);
		displayboard (ticboard, 3);
		getmovex (ticboard, 3);
		win = checkwinner (ticboard, 3);
	}



	int z;
	cin >>z;
	return 0; 

}

void displayboard ( char array [][3], int rows)
{
	for ( int r =0; r<rows;r++)
	{
		cout<<endl;
		for (int c =0; c<3; c++)
		{
			cout << array [r][c];
		}
	}

}

void getmoveO ( char array [][3], int rows)
{
	cout <<endl<<endl<<"Enter a row number for O: ";
	int row0;
	cin >>row0;
	cout <<endl<<"Enter a column number for O: ";
	int column0;
	cin >>column0;
	array [row0-1][column0-1] = 'O';
}

void getmoveX ( char array [][3], int rows)
{
	cout <<endl<<endl<<"Enter a row number for X: ";
	int rowx;
	cin >>rowx;
	cout <<endl<<"Enter a column number for X: ";
	int columnx;
	cin >>columnx;
	array [rowx-1][columnx-1] = 'X';
}

bool checkwinner (char array [][3], int rows )
{
	bool checkwinner; 
	char winner = 'T';

// Checks for:
//     X X X
for (int i = 0; i < 3; i++)
    if (array[i][0] == array[i][1] && array[i][1] == array[i][2])
        winner = array[i][0];

// Checks for:
// X
// X
// X
for (int i = 0; i < 3; i++)
    if (array[0][i] == array[1][i] && array[1][i] == array[2][i])
        winner = array[0][1];

// Checks for:
// X                 X
//   X      or     X
//     X         X 
if ((array[0][0] == array[1][1] && array[1][1] == array[2][2]) || 
    (array[0][2] == array[1][1] && array[1][1] == array[2][0]))
    winner = array[1][1];

// checking the result:
switch (winner) {
    case 'T': /* tie */  checkwinner = true; break;
    case 'X': /* X won */  checkwinner = true; break;
    case 'O': /* O won */  checkwinner =true; break;
}

return checkwinner; 

}

Last edited on
Check the spelling on your getmovex (or getmoveX) function - it's not consistent.
wow. I'm new to programming but I can't believe I missed that thanks!
I'm still working on the bool function, for some reason It keeps returning true too quickly
line 107 - do you mean that to be an i instead of a 1?
yes, sorry for the typo. I think that either my bool function is somehow flawed logically or the execution in main is. (I fixed the typo but am still getting a bad result)
line 119 - case 'T' - if no one's won the game yet, don't you want to return false instead of true? So your while loop at line 38 loops again?
Last edited on
Topic archived. No new replies allowed.