tic tac toe

Hi, I am a beginner and I have made this tic tac toe program with some help of others.I want to if you have any notices or if there are improvements.


#include <iostream>
using namespace std;
int main()
{

const int rows=3;
const int columns=3;
bool gameover;
char board[rows][columns]= { {'1','2','3'},
{'4','5','6'},
{'7','8','9'}};

for ( int i=0; i<rows; ++i)
{
for(int j=0; j<columns; ++j)
{
cout << board[i][j];
}
cout << endl;
}
int playerturn;

bool invalidmove;

do {

cout << "player 1's turn" << endl;
cin >> playerturn;
if ( playerturn ==1) { board[0][0]='O';}
else if ( playerturn ==2) { board[0][1]='O';}
else if ( playerturn ==3) { board[0][2]='O';}
else if ( playerturn ==4) { board[1][0]='O';}
else if ( playerturn ==5) { board[1][1]='O';}
else if ( playerturn ==6) { board[1][2]='O';}
else if ( playerturn ==7) { board[2][0]='O';}
else if ( playerturn ==8) { board[2][1]='O';}
else { board[2][2]='O';}




for ( int i=0; i<rows; ++i)
{
for(int j=0; j<columns; ++j)
{
cout << board[i][j];
}
cout << endl;
}





if ( board[0][0]==board[0][1] && board[0][1]==board[0][2]
|| board[0][0]==board[1][0] && board[1][0]==board[2][0]
|| board[2][0]==board[2][1] && board[2][1]==board[2][2]
|| board[1][0]==board[1][1] && board[1][1]==board[1][2]
|| board[0][1]==board[1][1] && board[1][1]==board[2][1]
|| board[0][2]==board[1][2] && board[1][2]==board[2][2]
|| board[0][0]==board[1][1] && board[1][1]==board[2][2]
|| board[0][2]==board[1][1] && board[1][1]==board[2][0] )
{
cout << "player 1 is the winner" << endl;
system("pause");
return 0;}

cout << "player 2's turn" << endl;
cin >> playerturn;
if ( playerturn ==1) { board[0][0] ='X';}
else if ( playerturn ==2) { board[0][1]='X';}
else if ( playerturn ==3) { board[0][2]='X';}
else if ( playerturn ==4) { board[1][0]='X';}
else if ( playerturn ==5) { board[1][1]='X';}
else if ( playerturn ==6) { board[1][2]='X';}
else if ( playerturn ==7) { board[2][0]='X';}
else if ( playerturn ==8) { board[2][1]='X';}
else { board[2][2]='X';}




for ( int i=0; i<rows; ++i)
{
for(int j=0; j<columns; ++j)
{
cout << board[i][j];
}
cout << endl;
}

if ( board[0][0]==board[0][1] && board[0][1]==board[0][2]
|| board[0][0]==board[1][0] && board[1][0]==board[2][0]
|| board[2][0]==board[2][1] && board[2][1]==board[2][2]
|| board[1][0]==board[1][1] && board[1][1]==board[1][2]
|| board[0][1]==board[1][1] && board[1][1]==board[2][1]
|| board[0][2]==board[1][2] && board[1][2]==board[2][2]
|| board[0][0]==board[1][1] && board[1][1]==board[2][2]
|| board[0][2]==board[1][1] && board[1][1]==board[2][0] )
{
cout << "player 2 is the winner" << endl;
system("pause");
return 0;
}
}
while ( true );

system ("pause");
return 0;
}
Two things I see:

1. You are using sytem("pause") alot. Use something else like std::cin.get() instead.
2. You could benefit alot from using functions.

Edit: Here are some links:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
http://www.cplusplus.com/forum/beginner/1988/
Last edited on
Topic archived. No new replies allowed.