hey i am trying to write a program that allows two users to play tic tac toe....right now i am doing the program in pieces i have accomplished setting up the two dimensional array. i am having a problem changing the * thats in a section of array to an X or O. here's my code:

#include <iostream>
using namespace std;

int main()
{
  char game[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};  
  char choice;
  
  int row, column;
  
  cout<<"Lets play a game of TicTacToe! Here's the starting game board: "<<endl;
  
  for(int i=0; i<3; i++)
  {
     for(int j=0; j<3; j++)
	 {
		 cout << game[i][j]<<"  ";
	 }
	 cout<<endl;
  }
 
  cout<<"Are you X or O?  ";
  cin>>choice;
  
  cout<<"Enter the row and column to place your "<<choice<<".  ";
  cin>>row>>column;

  game[row][column]=choice;

  for(int i=0; i<3; i++)
  {
     for(int j=0; j<3; j++)
	 {
		 cout << game[i][j]<<"  ";
	 }
	 cout<<endl;
  }

  return 0;
}

Recommended Answers

All 7 Replies

i found my problem with changing a value in the array...now i need major help...how and what can i use to determine when there is a winner

It seems to work for me, I type: x : 0 : 0
colon representing hitting enter and it puts an x in top left.

EDIT: See you've posted something before this.
The way I have done it in the past, which isn't necessarily a good way, is just having an if statement checking each possible win condition. (8 of them)

It seems to work for me, I type: x : 0 : 0
colon representing hitting enter and it puts an x in top left.

EDIT: See you've posted something before this.
The way I have done it in the past, which isn't necessarily a good way, is just having an if statement checking each possible win condition. (8 of them)

ok then i will try an if statement or if else statement

ok i have written out my statements to justify whether there is a winner or not in the tic tac toe game.....but when there is a winner the game just keep going...any suggestions to changing this code:

#include <iostream>
using namespace std;

int main()
{
  char game[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};  
  char choice;
  
  int row, column, count=0, winner=0;
  
  cout<<"Lets play a game of TicTacToe! Here's the starting game board: "<<endl;
  
  for(int i=0; i<3; i++)
  {
     for(int j=0; j<3; j++)
	 {
		 cout << game[i][j]<<"  ";
	 }
	 cout<<endl;
  }
 
  do
  {
	cout<<"Are you X or O? Please enter a captial X or O:  ";
    cin>>choice;

	cout<<"Enter the row and column to place your "<<choice<<".  ";
	cin>>row>>column;
	
	game[row][column]=choice;

	count+=1;

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			cout << game[i][j]<<"  ";
		}
		
		cout<<endl;
	}

	if(game[0][0]=='X' && game[0][1]=='X' && game[0][2]=='X')
	{
		winner=1;

		cout<<game[0][0]<<" wins!!"<<endl;
	}
	
	else if(game[1][0]=='X' && game[1][1]=='X' && game[1][2]=='X')
	{
		winner=1;
		 
		cout<<game[1][0]<<" wins!!"<<endl;
	}

	else if(game[2][0]='X' && game[2][1]=='X' && game[2][2]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='X' && game[1][0]=='X' && game[2][0]=='X')
	{
		winner=1;

		cout <<game[0][0]<<" wins!!"<<endl;
	}

	else if(game[0][1]=='X' && game[1][1]=='X' && game[2][1]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}
	
	else if(game[0][2]=='X' && game[1][2]=='X' && game[2][2]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='X' && game[1][1]=='X' && game[2][2]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

    else if(game[0][2]=='X' && game[1][1]=='X' && game[2][0]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	 else if(game[0][0]=='O' && game[0][1]=='O' && game[0][2]=='O')
	{
		winner=1;

		cout<<game[0][0]<<" wins!!"<<endl;
	}
	
	else if(game[1][0]=='O' && game[1][1]=='O' && game[1][2]=='O')
	{
		winner=1;
		 
		cout<<game[1][0]<<" wins!!"<<endl;
	}

	else if(game[2][0]=='O' && game[2][1]=='O' && game[2][2]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='O' && game[1][0]=='O' && game[2][0]=='O')
	{
		winner=1;

		cout <<game[0][0]<<" wins!!"<<endl;
	}

	else if(game[0][1]=='O' && game[1][1]=='O' && game[2][1]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}
	
	else if(game[0][2]=='O' && game[1][2]=='O' && game[2][2]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='O' && game[1][1]=='O' && game[2][2]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

    else if(game[0][2]=='O' && game[1][1]=='O' && game[2][0]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

  }while(count<=8 &&  winner==0);

  if(winner=0)
	  cout<<"There is no winner";

  return 0;
}

Fix to :

if(winner == 0)

Also make use of functions.

I made that correction but it still does not work...gotta figure it out befor e 11:59 tomorrow...but i'm still searching for the error.

my program now runs the way it should but the output seems disappear in certain areas.here's my code:

#include <iostream>
using namespace std;

int main()
{
  char game[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};  
  char choice;
  
  int row, column, count=0, winner=0;
  
  cout<<"Lets play a game of TicTacToe! Here's the starting game board: "<<endl;
  
  for(int i=0; i<3; i++)
  {
     for(int j=0; j<3; j++)
	 {
		 cout << game[i][j]<<"  ";
	 }
	 cout<<endl;
  }
 
  do
  {
	cout<<"Are you X or O? Please enter a captial X or O:  ";
    cin>>choice;

	cout<<"Enter the row and column to place your "<<choice<<".  ";
	cin>>row>>column;
	
	game[row][column]=choice;

	count+=1;

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			cout << game[i][j]<<"  ";
		}
		
		cout<<endl;
	}

	if(game[0][0]=='X' && game[0][1]=='X' && game[0][2]=='X')
	{
		winner=1;

		cout<<game[0][0]<<" wins!!"<<endl;
	}
	
	else if(game[1][0]=='X' && game[1][1]=='X' && game[1][2]=='X')
	{
		winner=1;
		 
		cout<<game[1][0]<<" wins!!"<<endl;
	}

	else if(game[2][0]='X' && game[2][1]=='X' && game[2][2]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='X' && game[1][0]=='X' && game[2][0]=='X')
	{
		winner=1;

		cout <<game[0][0]<<" wins!!"<<endl;
	}

	else if(game[0][1]=='X' && game[1][1]=='X' && game[2][1]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}
	
	else if(game[0][2]=='X' && game[1][2]=='X' && game[2][2]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='X' && game[1][1]=='X' && game[2][2]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][2]=='X' && game[1][1]=='X' && game[2][0]=='X')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	 else if(game[0][0]=='O' && game[0][1]=='O' && game[0][2]=='O')
	{
		winner=1;

		cout<<game[0][0]<<" wins!!"<<endl;
	}
	
	else if(game[1][0]=='O' && game[1][1]=='O' && game[1][2]=='O')
	{
		winner=1;
		 
		cout<<game[1][0]<<" wins!!"<<endl;
	}

	else if(game[2][0]=='O' && game[2][1]=='O' && game[2][2]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='O' && game[1][0]=='O' && game[2][0]=='O')
	{
		winner=1;

		cout <<game[0][0]<<" wins!!"<<endl;
	}

	else if(game[0][1]=='O' && game[1][1]=='O' && game[2][1]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}
	
	else if(game[0][2]=='O' && game[1][2]=='O' && game[2][2]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][0]=='O' && game[1][1]=='O' && game[2][2]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}

	else if(game[0][2]=='O' && game[1][1]=='O' && game[2][0]=='O')
	{
		winner=1;

		cout <<game[2][0]<<" wins!!"<<endl;
	}
	
	else if(count>=8)
	  cout<<"There is no winner";

  }while(winner==0 && count<8);

  
  return 0;
}

here's my recent output:

Lets play a game of TicTacToe! Here's the starting game board:
* * *
* * *
* * *
Are you X or O? Please enter a captial X or O: X
Enter the row and column to place your X. 0
1
* X *
* * *
* * *
Are you X or O? Please enter a captial X or O: O
Enter the row and column to place your O. 0
0
O X *
* * *
* *
Are you X or O? Please enter a captial X or O: X
Enter the row and column to place your X. 2
0
O X *
* * *
X * *
Are you X or O? Please enter a captial X or O: O
Enter the row and column to place your O. 2
1
O X *
* * *
O *
Are you X or O? Please enter a captial X or O: X
Enter the row and column to place your X. 1
0
O X *
X * *
O *
Are you X or O? Please enter a captial X or O: O
Enter the row and column to place your O. 2
2
O X *
X * *
O O
Are you X or O? Please enter a captial X or O: X
Enter the row and column to place your X. 0
2
O X X
X * *
O O
Are you X or O? Please enter a captial X or O: O
Enter the row and column to place your O. 1
1
O X X
X O *
O O
wins!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.