May 4, 2016 at 2:54pm UTC
I made a Tic-Tac-Toe program using 2-D array but it does'nt display the winner. Plz help me.
Here is my coding
#include <iostream>
using namespace std;
int const ROW = 3;
int const COL =3;
char square[ROW][COL] = { {'*','*','*'},
{'*','*','*'},
{'*','*','*'} };
int checkwin();
void board();
int main()
{
int player = 1,i,choice1,choice2;
char mark;
for (int l = 0; l <9 ; l++)
{
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin >> choice1;
cin>>choice2;
mark=(player == 1) ? 'X' : 'O';
// fiiling the positions of astrisks
if ((choice1 == 1&& choice2 ==1) &&( square[0][0] == '*') )
square[0][0] = mark;
else if ((choice1 == 1&& choice2 ==2)&&( square[0][1] == '*') )
square[0][1] = mark;
else if( (choice1 == 1&& choice2 ==3) &&( square[0][2] == '*'))
square[0][2] = mark;
else if ((choice1 == 2&& choice2 ==1)&&( square[1][0] == '*') )
square[1][0] = mark;
else if( (choice1 == 2&& choice2 ==2)&&( square[1][1] == '*') )
square[1][1] = mark;
else if( (choice1 == 2&& choice2 ==3) &&( square[1][2] == '*') )
square[1][2] = mark;
else if ((choice1 == 3&& choice2 ==1) &&( square[2][0] == '*'))
square[2][0] = mark;
else if ((choice1 == 3&& choice2 ==2)&&( square[2][1] == '*') )
square[2][1] = mark;
else if ((choice1 == 3&& choice2 ==3) &&( square[2][2] == '*'))
square[2][2] = mark;
i=checkwin();
player++;
}
board();
if(i==1)
{
cout<<"Player "<<--player<<" has won ";
}
return 0;
}
int checkwin()
{
// rows
if ( (square[0][0] == square[0][1] ) && (square[0][1] == square[0][2] ))
{
return 1;
}
if ( (square[1][0] == square[1][1] ) && (square[1][1] == square[1][2] ))
{
return 1;
}
if ( (square[2][0] == square[2][1] ) && (square[2][1] == square[2][2] ))
{
return 1;
}
// columns
if ( (square[0][0] == square[1][0] ) && (square[1][0] == square[2][0] ))
{
return 1;
}
if ( (square[0][1] == square[1][1] ) && (square[1][1] == square[2][1] ))
{
return 1;
}
if ( (square[0][2] == square[1][2] ) && (square[1][2] == square[2][2] ))
{
return 1;
}
// diagonally
if ( (square[0][0]==mark)&&(square[0][0] == square[1][1] ) && (square[1][1] == square[2][2] ) )
{
return 1;
}
if ( (square[2][0]==mark)&&(square[2][0] == square[1][1] ) && (square[1][1] == square[0][2] ))
{
return 1;
}
else if (square[0][0] != '*' && square[0][1] != '*' && square[0][2] != '*' &&
square[1][0] != '*' && square[1][1] != '*' && square[1][2] != '*' &&
square[2][0] != '*' && square[2][1] != '*' && square[2][2] != '*')
return 0;
else
return -1;
}
void board()
{
cout <<square[0][0] << square[0][1] << square[0][2] << endl;
cout << square[1][0] << square[1][1] << square[1][2] << endl;
cout << square[2][0] << square[2][1] << square[2][2] << endl;
}
May 4, 2016 at 5:05pm UTC
I can't read the code that well since I'm reading it off my crap phone. But where do you declare i. If you declare i inside the for loop, then the problem is that you are checking i outside the loop. i only lives inside the scope of the for loop if it's declared there. You can try moving your if (i == 1) statement into the for loop. Also. Since its a for loop. Doesn't it iterate 9 times even if the player has won?