Tic Tac Toe win function

closed account (z8q4izwU)
Hello my code works almost 100% i cant figure out how to get my win function to work it finds the winner for x but not for o or no winner. Can anyone help?!

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
129
130
#include <iostream>

using namespace std;

void input (char board [3][3]);
void print (char board [3][3]);
bool findWinner (char board [3][3], char player);

int main()
{
    char board[3][3];
    char again;
    while(true){

        cout << "Enter your board as characters (x, o or .): "<< endl;
        
        input(board);
        
        cout << endl;
        
        cout << "The board is: " << endl;
        
        print(board);
        
        cout << endl;
        
        if(findWinner(board,'x')){

            cout << "X wins!" << endl;

        }else if(findWinner(board,'o')){

            cout << "O wins!" << endl;

        }else{

            cout << "No winner!" << endl;

        }

        cout << endl;

        cout << "Would you like to play another round (y or n)? ";
        cin >> again;

        if(again == 'n'){

            break;

        }
        cout << endl;

    }

    return 0;
}

void input (char board [3][3])
{
    int r,c;
    char line [4];

    for(r = 0 ; r < 3 ; r++){

        cin >> line;

        for(c = 0 ; c < 3 ; c++){

            board[r][c] = line[c];

        }

    }


}

void print (char board [3][3])
{
     for(int r = 0; r < 3; r++){
		for(int c = 0; c < 3; c++){
			cout << board[r][c];
			if(c < 2)
				cout << " | ";
		}
		cout << endl;
		if(r < 2){     
			cout << "_ _ _ _ _" << endl;
		}
	}

}

bool findWinner (char board [3][3], char player)
{
     int r, c;
     int count = 0;
    // Loop through each row 
	
    for (int r = 0; r < 3; r++) 
	{
        if(board[r][c] == player){
        count++; 
}
	// Loop through each column checking if char 
	
    for (int c = 0; c < 3; c++) 
    {
        if (board[r][c] ==  player) 
        {
          count++; 
          } 
	} 
	
	// If we have 3, it means they have the row so they win. 
	
    if (count == 3) 
    { 
      return true; 
    }
    if(board[0][2] == player & board[1][1] == player & board[2][0] == player){
     return true;
	} 
}
}

	

	
You have quite a few problems in your findWinner function:
Line 102: c is uninitialized. You need a second nested loop to check each column within the row.
Line 101: You need to zero count at the top of the loop for each row. You don't want two Xs in the first row and one in the second to trigger a win.
Line 104: You need to check count inside the outter loop before checking the next row.
Line 109: value of r is left over from the previous loop. Again, you need a second nested loop to check each row within the column.
Line 108: Same comment as 101 about zeroing count.
Line 113: Same comment as 104 about checking cout.
Lines 117-120: Doesn't work here. Remove.
Lines 121-123: You're only checking one diagonal.
Line 125: You're not returning false if all the checks for winning combinations fail.

closed account (z8q4izwU)
I think i did what you said...


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
  int count = 0;
    int count1 = 0;
    int count2 = 0;
    int r,c;
    
    for(c = 0 ; c < size ; c++)
        {
            for(r = 0 ; r < size ; r++)
            {        
            if(board[r][c] == player)
            {
                count++;
            }
            if(board[r][c] == player)
            {
                count1++;
            }
            if(r == c & board[c][r] == player)
            {
                count2++;
            }
        }
        if(count == 3 | count1 == 3 | count2 == 3)
        {

            return true;

        }else{

            count = 0;
            count1 = 0;

        }
    }

    if(board[0][2] == player & board[1][1] == player & board[2][0] == player)
    {
        return true;
    }
    return false;
}
Lines 10-13 and lines 14-17 do exactly the same thing.

I think you meant for line 14 to be:
 
 if(board[c][r] == player)

closed account (z8q4izwU)
Hahaha yea didnt mean to do that
Topic archived. No new replies allowed.