Returning array character to find game winner

I know that the function BOOL WIN returns a value, that being player in order to determine who has won the game.

When I compile and run the program, it outputs a garbage value for player followed by "win!" mine is outputting " u wins! ".

Can anyone shed some insight. I know im missing something very small.
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;


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


int main(int argc, char *argv[])
{
    bool playagain = true;
    while(playagain == true){
    
    char board [3][3];
    char player;
    
    cout << "Enter your board as characters (x,o or .)" << endl;
    cout << endl;
    
    input(board);
    
    
    print(board);
    
    win(board, player);
    cout << endl;
    cout << endl;
    
    
    char playagainchar;
    cout << "Would you like to do another (y or n)? ";
    cin >>playagainchar;
    cout << "\n";
    if(playagainchar != 'y') 
    {
     playagain = false;
     cout << "Thanks for playing!\n\n";
     
    system("PAUSE");
    return EXIT_SUCCESS;  }
    cout << endl;
}
}


void input (char board [3][3])
{
     
     int row;
     int col;
    
     
     for (row=0; row<3; row++)
         for (col=0; col<3; col++)
             cin>>board[row][col];
     
     
     cout << endl;
    
    
     }

void print (char board [3][3])
{
     cout << "The Board is: " << endl;
     cout << endl;
     
     int r, c;

     
     for (r = 0; r < 3; r++)
      {      
             if( board[r][c] == '.' )
             board[r][c]=' ';
             
       for (c = 0; c < 3; c++) 
         {     
               if( board[r][c] == '.' )
               board[r][c]=' ';
              
               cout << " "<< setw(1)<< board [r][c];
         
              if (c<2) cout << " |";
       
                       }
                       
     cout << endl;
     if (r<2)
      {
        cout <<"-----------"<< endl;
                                     }

}

cout << endl;

}
   
     
bool win (char board [3][3], char player)
{



if( player == board[0][0] && player == board[1][1] && player == board[2][2] )
return( true );
cout << player << "wins!";
if( player == board[0][2] && player == board[1][1] && player == board[2][0] )
return( true );
cout << player << "wins!";

if( player == board[0][0] && player == board[0][1] && player == board[0][2] )
return( true );
cout << player << "wins!";
if( player == board[1][0] && player == board[1][1] && player == board[1][2] )
return( true );
cout << player << "wins!";
if( player == board[2][0] && player == board[2][1] && player == board[2][2] )
return( true );
cout << player << "wins!";



if( player == board[0][0] && player == board[1][0] && player == board[2][0] )
return( true );
cout << player << "wins!";
if( player == board[0][1] && player == board[1][1] && player == board[2][1] )
return( true );
cout << player << "wins!";
if( player == board[0][2] && player == board[1][2] && player == board[2][2] )
return( true );
cout << player << "wins!";


return( false );
cout << "No winner!" << endl;
     
     

     
     
     }
Last edited on
Line 19 player is uninitialized.
Line 29 You call win with the uninitialized value of player.
At line 110, you return true if the preceding condition detected a winner.
If you get to line 111, the preceding condition was false, so you don't have a winner (yet), but you output the following:
 
cout << player << "wins!";

Where player is still uninitialized (garbage) and "wins!" is clearly not true.
Ditto for lines 114, 118, 121, 124, 130, 133, 136.

No way to reach the statement at line 140.
I am thankful for being your 666th post haha.

I still can't get it to determine the winner properly
Last edited on
Helps!
Is the print() function printing the right board you put in when you called input()?
Your win function looks correct (except for the cout statements).
Make sure that you have the correct value in player. If that does match what's entered in input(), you'll never detect a win.

BTW, line 29 needs to be something like:
1
2
3
4
5
6
7
if (win(board, 'X')
  cout << "Player X wins" << end;
else
  if win(board, 'O')
    cout << "Player O wins << end;
  else
    cout << "Tie game" << endl; 


You probably should add edits to input() to make sure your entered values are correct and in a single case.

If these suggestions don't help, post your revised code.
Last edited on
Topic archived. No new replies allowed.