Tic tac Toe game, checking for winner not working

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2365 Views - Last Post: 05 March 2012 - 09:07 PM Rate Topic: -----

#1 maxpain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-March 12

Tic tac Toe game, checking for winner not working

Posted 04 March 2012 - 09:28 PM

I got the program running but cant find the winner or tie. I used these if statements but they do not seem to work. This is a tic tac toe game. If a player get 3 in a row, it should say that that person won, or if no one wins, it should say tie and the program should continue as long as there are no winners or tie. I have all that but the if statements do not work. I am trying to do this program for like 4 days. its due in 2 days.Thanks to anyone who helps



#include <iostream>
using namespace std;

void displayBoard();

const char player1='X';
const char player2='O';
const char empty='*';
const int row=3;
const int col=3;
int row1, col1, row2, col2, num=0;
char board[row][col];

int main()
{
	char input;
	cout << "**************************************************" << endl;
	cout << "*****************   Tic-Tac-Toe   ****************" << endl;
	cout << "**************************************************" << endl << endl;

	do
	{
		for(int a=0; a<row; a++)
		{
			for(int b=0; b<col; b++)
			{
				board[a][b]=empty;
			}
		}
		displayBoard();

		cout << "Player 1: X\nPlayer 2: O" << endl;

		do
		{

				cout << "Player 1 enter your row and column number(with space in between): ";
				cin >> row1 >> col1;
				board[row1-1][col1-1]='X';
				displayBoard();
				cout << "Player 2 enter your row and column number(with space in between): ";
				cin >> row2 >> col2;
				board[row2-1][col2-1]='O';
				displayBoard();

				if(board[0][0]==board[0][1]&&board[0][1]==board[0][2])
				{
					if(board[0][0]=='X')
						num=1;
					else
						num=2;
				}
				else if(board[1][0]==board[1][1]&&board[1][1]==board[1][2])
				{
					if(board[1][0]=='X')
						num=1;
					else if(board[1][0]=='O')
						num=2;
				}
				else if(board[2][0]==board[2][1]&&board[2][1]==board[2][2])
				{
					if(board[2][0]=='X')
						num=1;
					else if(board[2][0]=='O')
						num=2;
				}
				else if(board[0][0]==board[1][0]&&board[1][0]==board[2][0])
				{
					if(board[0][0]=='X')
						num=1;
					else if(board[0][0]=='O')
						num=2;
				}
				else if(board[0][1]==board[1][1]&&board[1][1]==board[2][1])
				{
					if(board[1][1]=='X')
						num=1;
					else if(board[1][1]=='O')
						num=2;
				}
				else if(board[0][2]==board[1][2]&&board[1][2]==board[2][2])
				{
					if(board[1][2]=='X')
						num=1;
					else if(board[1][2]=='O')
						num=2;
				}
				else if(board[0][0]==board[1][1]&&board[1][1]==board[2][2])
				{
					if(board[0][0]=='X')
						num=1;
					else if(board[0][0]=='O')
						num=2;
				}
				else if(board[2][0]==board[1][1]&&board[1][1]==board[0][2])
				{
					if(board[2][0]=='X')
						num=1;
					else if(board[2][0]=='O')
						num=2;
				}
				for(int j=0; j<row; j++)
				{
					for(int jj=0; j<col; j++)
					{
						if(board[j][jj]!='*')
							num=3;
					
					}
				}


			}while(num==0);
			if(num==1)
				cout << "Player 1 wins" << endl;
			else if(num==2)
				cout << "Player 2 wins" << endl;
			else if(num==3)
				cout << "Its a tie" << endl;
		













		cout << "Do you want to play another game?: ";
		cin >> input;
	}while(input=='y'||input=='Y');

	cout << "Thank You" << endl;

	system("pause");
	return 0;

}
void displayBoard()
{
	for(int x=0; x<row; x++)
	{
		for (int y=0; y<col; y++)
		{
			cout << board[x][y];
		}
		cout << endl;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tic tac Toe game, checking for winner not working

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,169
  • Joined: 30-May 10

Re: Tic tac Toe game, checking for winner not working

Posted 04 March 2012 - 09:51 PM

Why don't you create some more functions, like say

checkWin(), which checks a whole board for a winner.

Which in turn calls these functions to check specific aspects of the board
checkWinRow()
checkWinCol()
checkWinDiagonal()
Was This Post Helpful? 0
  • +
  • -

#3 maxpain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-March 12

Re: Tic tac Toe game, checking for winner not working

Posted 04 March 2012 - 09:57 PM

View PostSalem_c, on 04 March 2012 - 09:51 PM, said:

Why don't you create some more functions, like say

checkWin(), which checks a whole board for a winner.

Which in turn calls these functions to check specific aspects of the board
checkWinRow()
checkWinCol()
checkWinDiagonal()



Ya I was going to do that but after I get the if statements right. If I get the if statements working and I get the winner or tie, I will put them in functions.
Was This Post Helpful? 0
  • +
  • -

#4 maxpain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-March 12

Re: Tic tac Toe game, checking for winner not working

Posted 04 March 2012 - 10:04 PM

View PostSalem_c, on 04 March 2012 - 09:51 PM, said:

Why don't you create some more functions, like say

checkWin(), which checks a whole board for a winner.

Which in turn calls these functions to check specific aspects of the board
checkWinRow()
checkWinCol()
checkWinDiagonal()


my main question is that why are my if statements not doing what they are supposed to do. they should tell me the winner but thats not happening. what should i do to find the winner or if its a tie. can someone fix it.
Was This Post Helpful? 0
  • +
  • -

#5 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Tic tac Toe game, checking for winner not working

Posted 04 March 2012 - 10:17 PM

It looks like you are trying to do way to much at once. Try to break up your code a little more. Make if statements only for checking if there are three "X" in a row. Then make if statements checking if there are three "O" in a row.

Then like you said, break those if statements into methods. Methods make reading and writing code a lot easier.

Hope this helps.

Jack
Was This Post Helpful? 0
  • +
  • -

#6 maxpain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-March 12

Re: Tic tac Toe game, checking for winner not working

Posted 04 March 2012 - 10:24 PM

View Postjdavi134, on 04 March 2012 - 10:17 PM, said:

It looks like you are trying to do way to much at once. Try to break up your code a little more. Make if statements only for checking if there are three "X" in a row. Then make if statements checking if there are three "O" in a row.

Then like you said, break those if statements into methods. Methods make reading and writing code a lot easier.

Hope this helps.

Jack

i tried doing it for only the 'X' and it still woulnt work. can you give me an example
Was This Post Helpful? 0
  • +
  • -

#7 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Tic tac Toe game, checking for winner not working

Posted 04 March 2012 - 10:29 PM

View Postmaxpain, on 04 March 2012 - 10:24 PM, said:

View Postjdavi134, on 04 March 2012 - 10:17 PM, said:

It looks like you are trying to do way to much at once. Try to break up your code a little more. Make if statements only for checking if there are three "X" in a row. Then make if statements checking if there are three "O" in a row.

Then like you said, break those if statements into methods. Methods make reading and writing code a lot easier.

Hope this helps.

Jack

i tried doing it for only the 'X' and it still woulnt work. can you give me an example


Sure.

First if statement would look like this, then continue through.

if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')



Keep doing this for all row and column possibilities, including the "O". That should get you there.

Jack

This post has been edited by jdavi134: 04 March 2012 - 10:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,169
  • Joined: 30-May 10

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 10:01 AM

> Ya I was going to do that but after I get the if statements right.
> If I get the if statements working and I get the winner or tie, I will put them in functions.
Which is exactly why you're failing at the moment.

You've got a 100+ line main() which is still growing, and you can't see the changes you're making in context of everything else.

If you had the 4 functions I proposed, you would at least be able to say (with some confidence) that say horizontal and vertical lines work OK, but it doesn't detect diagonal wins properly.
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,600
  • Joined: 19-February 09

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 10:19 AM

Without knowing what happens when it's run. You don't check whether the row, that is all equal, is all empty.
Was This Post Helpful? 0
  • +
  • -

#10 maxpain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-March 12

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 10:36 AM

View PostSalem_c, on 05 March 2012 - 10:01 AM, said:

> Ya I was going to do that but after I get the if statements right.
> If I get the if statements working and I get the winner or tie, I will put them in functions.
Which is exactly why you're failing at the moment.

You've got a 100+ line main() which is still growing, and you can't see the changes you're making in context of everything else.

If you had the 4 functions I proposed, you would at least be able to say (with some confidence) that say horizontal and vertical lines work OK, but it doesn't detect diagonal wins properly.


So I made one function to try if player 1 has won but it still does not work. If it works, than I would make another functions but this does not even start to work. I ran the program and put three X in a row but it still does not show Player 1 as a winner. You can try it. This is the code



#include <iostream>
using namespace std;

void displayBoard();
int checkWinP1();

const char player1='X';
const char player2='O';
const char empty='*';
const int row=3;
const int col=3;
int row1, col1, row2, col2, num=0;
char board[row][col];

int main()
{
	char input;
	cout << "**************************************************" << endl;
	cout << "*****************   Tic-Tac-Toe   ****************" << endl;
	cout << "**************************************************" << endl << endl;

	do
	{
		for(int a=0; a<row; a++)
		{
			for(int b=0; b<col; b++)
			{
				board[a][b]=empty;
			}
		}
		displayBoard();

		cout << "Player 1: X\nPlayer 2: O" << endl;

		do
		{

				cout << "Player 1 enter your row and column number(with space in between): ";
				cin >> row1 >> col1;
				board[row1-1][col1-1]='X';
				displayBoard();
				cout << "Player 2 enter your row and column number(with space in between): ";
				cin >> row2 >> col2;
				board[row2-1][col2-1]='O';
				displayBoard();
				num=checkWinP1();
				

			}while(num==0);
			if(num==1)
				cout << "Player 1 wins" << endl;
			else if(num==2)
				cout << "Player 2 wins" << endl;
			else if(num==3)
				cout << "Its a tie" << endl;
		













		cout << "Do you want to play another game?: ";
		cin >> input;
	}while(input=='y'||input=='Y');

	cout << "Thank You" << endl;

	system("pause");
	return 0;

}
void displayBoard()
{
	for(int x=0; x<row; x++)
	{
		for (int y=0; y<col; y++)
		{
			cout << board[x][y];
		}
		cout << endl;
	}
}
int checkWinP1()
{
	int win=0;
	if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
		win=1;
	if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
		win=1;
	if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
		win=1;
	if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
		win=1;	
	if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
		win=1;
	if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
		win=1;
	if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
		win=1;
	if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
		win=1;
	return win;
}


Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,600
  • Joined: 19-February 09

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 11:06 AM

It seems to work if you also enter player two's turn.
Was This Post Helpful? 0
  • +
  • -

#12 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,169
  • Joined: 30-May 10

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 01:12 PM

So now make it
int checkWin(char player);

So you can do
win = checkWin(player1);
or
win = checkWin(player2);
Was This Post Helpful? 0
  • +
  • -

#13 maxpain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-March 12

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 02:23 PM

View PostSalem_c, on 05 March 2012 - 01:12 PM, said:

So now make it
int checkWin(char player);

So you can do
win = checkWin(player1);
or
win = checkWin(player2);


I did what you told me but still no luck. I changed it to int checkWinP1(int player) but no luck. Is somebody outthere, this is due in couple of days. I know the mistake is minor and anyone can fix it but i cant. if anyone can copy it in their compiler and try it out. when the person gets 3 in a row, it should say that person won and end the loop otherwise keep going and if no one wins, say tie. Heres the code.

#include <iostream>
using namespace std;

void displayBoard();
int checkWinP1(char);

char player1='X';
char player2='O';
char empty='*';
const int row=3;
const int col=3;
int row1, col1, row2, col2, num=0;
char board[row][col];

int main()
{
	char input;
	cout << "**************************************************" << endl;
	cout << "*****************   Tic-Tac-Toe   ****************" << endl;
	cout << "**************************************************" << endl << endl;

	do
	{
		for(int a=0; a<row; a++)
		{
			for(int b=0; b<col; b++)
			{
				board[a][b]=empty;
			}
		}
		displayBoard();

		cout << "Player 1: X\nPlayer 2: O" << endl;

		do
		{

				cout << "Player 1 enter your row and column number(with space in between): ";
				cin >> row1 >> col1;
				board[row1-1][col1-1]='X';
				displayBoard();
				cout << "Player 2 enter your row and column number(with space in between): ";
				cin >> row2 >> col2;
				board[row2-1][col2-1]='O';
				displayBoard();
				num=checkWinP1(player1);
				for(int y=0; y<3; y++)
				{
					int x=0, c=1, d=2;
					if(board[x][y] == 'X' && board[x][c] == 'X' && board[x][d] == 'X')
						cout << "You won" << endl;
					x++;
				}

				

			}while(num==0);
			if(num==1)
				cout << "Player 1 wins" << endl;
			else if(num==2)
				cout << "Player 2 wins" << endl;
			else if(num==3)
				cout << "Its a tie" << endl;

		cout << "Do you want to play another game?: ";
		cin >> input;
	}while(input=='y'||input=='Y');

	cout << "Thank You" << endl;

	system("pause");
	return 0;

}
void displayBoard()
{
	for(int x=0; x<row; x++)
	{
		for (int y=0; y<col; y++)
		{
			cout << board[x][y];
		}
		cout << endl;
	}
}
int checkWinP1(char player)
{
	player='X';
	int win=0;
	if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
		win=1;
	if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
		win=1;
	if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
		win=1;
	if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
		win=1;	
	if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
		win=1;
	if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
		win=1;
	if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
		win=1;
	if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
		win=1;
	return win;
}


Was This Post Helpful? 0
  • +
  • -

#14 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 06:09 PM

Ok finished your program,not posting the code, here are my tips:
1 - Create a general function to check the win, it receives the board and can receive one of the two players.
As both players win by the same criteria you can make the paramater received be named player and just check if that player won.
Like:
int checkWin(char player,char board[][col])
{
   //code to check if he won
}


you can call it by:
checkWin(player2,board)//for player 2
checkWin(player1,board)//for player 1


2 - Everytime someone makes a move you need to check if he won
3 - Make a while loop to not let both players play in the same space, you can do this by checking if the place the player selected is empty or not.
4 - Create a function to check if the board is full, so you can know when to call it a tie
You can do that by using your loop that empty the board, but instead of making it empty just check if its not empty, if every space is not empty you have a full board.
5 - Make your loop that empty the board a function
6 - After checking if a player won and the result is a fail, check if the game ended in a tie
7 - Make a flag variable to see if the game is over or not, and run a while(!flag) loop.

I think that is it.

This post has been edited by shurd: 05 March 2012 - 06:52 PM

Was This Post Helpful? -1
  • +
  • -

#15 maxpain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-March 12

Re: Tic tac Toe game, checking for winner not working

Posted 05 March 2012 - 06:53 PM

View Postshurd, on 05 March 2012 - 06:09 PM, said:

Ok finished your program,not posting the code, here are my tips:
1 - Create a general function to check the win, it receives the board and can receive one of the two players.
As both players win by the same criteria you can make the paramater received be named player and just check if that player won.
Like:
int checkWin(char player,char board[][col])
{
   //code to check if he won
}


you can call it by:
checkWin(player2,board)//for player 2
checkWin(player1,board)//for player 1


2 - Everytime someone makes a move you need to check if he won
3 - Make a while loop to not let both players play in the same space, you can do this by checking if the place the player selected is empty or not.
4 - Create a function to check if the board is full, so you can know when to call it a tie
You can do that by using your loop that empty the board, but instead of making it empty just check if its not empty, if every space is not empty you have a full board.
5 - Make your loop that empty the board a function
6 - After checking if a player won and the result is a fail, check if the game ended in a tie
7 - Make a flag variable to see if the game is over or not, and run a while(!flag) loop.

I think that is it.


let me try, thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2