Tic Tac Toe: How do I print who wins?!

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
#include<iostream>
#include<string>
using namespace std;

void main()
{
	int t=1,i,j;
	int box[3][3]={4,4,4,4,4,4,4,4,4};

	for(t=1;t<=10;t++)
	{
		cout<<"\n";
		for(i=0;i<3;i++)
		{
			for(j=0;j<3;j++)
			{
				if(box[i][j]!=0 && box[i][j]!=1)
				cout<<"   ";
				if(box[i][j]==1) cout<<" 1 ";
				if(box[i][j]==0) cout<<" 0 ";
				if(j<2)cout<<"|";
				if(j==2)cout<<"\n";
			}
			if(i<2)cout<<"-----------\n";
		}
		
		if(t<10)
		{
			if (t%2==0)cout<<"\n\nPlayer 1's turn:\n";
			if (t%2==1)cout<<"\n\nPlayer 0's turn:\n";

			cout<<"Enter the row number: ";
			cin>>i;
		
			cout<<"Enter the column number: ";
			cin>>j;
		
			i--;
			j--;

			if (t%2==0)
				box[i][j]=1;
			if (t%2==1)
				box[i][j]=0;
		}

	}
}


Can there be a generalized way to compare the elements of a 2D array?
closed account (3pj6b7Xj)
std::cout<<"you win!!"<<std::endl;

lol.
I think he wants to terminate the program together with the confirmation of winning if a player win.
Also I noticed that I can overwrite the previous move of the other player.
First, change
void main()
to
int main()
because void returns nothing.

Then, at the end, put
1
2
3
4
5
6
7
std::cout<<"you win!!"<<std::endl;

// don't forget to #include <limits> for this!
std::cout << "Press ENTER to end the program..." << endl;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;


You will, of course, have to change "you win" to "you lose" if you lost.


Topic archived. No new replies allowed.