### 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?!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````#include 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...

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` `````` 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.