Jump to content

Tic tac toe null condition game in neat beans, how to?

TheMidnightNarwhal

Alright so basically I am doing a Tic tac toe game with buttons and shit and I am trying to do a "null" condition when no one wins. I currently did a condition where the X and O wins. So how can I do this? Here is my code for the Xs and Os win conditions.  I used fucntions to do this.
 

public void gameover()    {                                                                if (bt1.equals ("X")  &  bt2.equals  ("X")  &  bt3.equals ("X"))        {      cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);       cmd1.setBackground(Color.red);      cmd2.setBackground(Color.red);      cmd3.setBackground(Color.red);            lblWinner.setText("X Win");                    }                if (bt1.equals ("O")  &  bt2.equals  ("O")  &  bt3.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);           cmd1.setBackground(Color.red);      cmd2.setBackground(Color.red);      cmd3.setBackground(Color.red);            lblWinner.setText("O Win");        }                                                                                                        if (bt4.equals ("X")  &  bt5.equals  ("X")  &  bt6.equals ("X"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);           cmd4.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd6.setBackground(Color.red);            lblWinner.setText("X Win");        }                if (bt4.equals ("O")  &  bt5.equals  ("O")  &  bt6.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);           cmd4.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd6.setBackground(Color.red);            lblWinner.setText("O Win");        }                                                                                                                                                                       if (bt7.equals ("X")  &  bt8.equals  ("X")  &  bt9.equals ("X"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);           cmd7.setBackground(Color.red);      cmd8.setBackground(Color.red);      cmd9.setBackground(Color.red);            lblWinner.setText("X Win");        }                if (bt7.equals ("O")  &  bt8.equals  ("O")  &  bt9.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);           cmd7.setBackground(Color.red);      cmd8.setBackground(Color.red);      cmd9.setBackground(Color.red);            lblWinner.setText("O Win");        }                                                                                                                if (bt1.equals ("X")  &  bt4.equals  ("X")  &  bt7.equals ("X"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);           cmd1.setBackground(Color.red);      cmd4.setBackground(Color.red);      cmd7.setBackground(Color.red);            lblWinner.setText("X Win");        }                if (bt1.equals ("O")  &  bt4.equals  ("O")  &  bt7.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                 cmd1.setBackground(Color.red);      cmd4.setBackground(Color.red);      cmd7.setBackground(Color.red);            lblWinner.setText("O Win");        }                                                                                                                                                                if (bt2.equals ("X")  &  bt5.equals  ("X")  &  bt8.equals ("X"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                cmd2.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd8.setBackground(Color.red);            lblWinner.setText("X Win");        }                if (bt2.equals ("O")  &  bt5.equals  ("O")  &  bt8.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                       cmd2.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd8.setBackground(Color.red);            lblWinner.setText("O Win");        }                                                                                                                                                                                                if (bt3.equals ("X")  &  bt6.equals  ("X")  &  bt9.equals ("X"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                       cmd3.setBackground(Color.red);      cmd6.setBackground(Color.red);      cmd9.setBackground(Color.red);            lblWinner.setText("X Win");        }                if (bt3.equals ("O")  &  bt6.equals  ("O")  &  bt9.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                             cmd3.setBackground(Color.red);      cmd6.setBackground(Color.red);      cmd9.setBackground(Color.red);            lblWinner.setText("O Win");        }                                                                                                                                                                                                                if (bt1.equals ("X")  &  bt5.equals  ("X")  &  bt9.equals ("X"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                             cmd1.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd9.setBackground(Color.red);            lblWinner.setText("X Win");        }                if (bt1.equals ("O")  &  bt5.equals  ("O")  &  bt9.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                                   cmd1.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd9.setBackground(Color.red);            lblWinner.setText("O Win");        }                                                                                                                                                                                              if (bt7.equals ("X")  &  bt5.equals  ("X")  &  bt3.equals ("X"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                                   cmd7.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd3.setBackground(Color.red);            lblWinner.setText("X Win");        }                if (bt7.equals ("O")  &  bt5.equals  ("O")  &  bt3.equals ("O"))        {                              cmd1.setEnabled(false);      cmd2.setEnabled(false);      cmd3.setEnabled(false);      cmd4.setEnabled(false);      cmd5.setEnabled(false);      cmd6.setEnabled(false);      cmd7.setEnabled(false);      cmd8.setEnabled(false);      cmd9.setEnabled(false);                                   cmd7.setBackground(Color.red);      cmd5.setBackground(Color.red);      cmd3.setBackground(Color.red);            lblWinner.setText("O Win");        }
Link to comment
Share on other sites

Link to post
Share on other sites

If you find a way to detect if there was a winner, any winner, then you will automatically have solved the alternate condition.

 

Remember, a function should do one thing and do it well.

Link to comment
Share on other sites

Link to post
Share on other sites

If you find a way to detect if there was a winner, any winner, then you will automatically have solved the alternate condition.

 

Remember, a function should do one thing and do it well.

 

Yeah, my winner conditions works fine. Either 0s or Xs, the thing is the null, if no one wins. I can't just do my  Win function false because it's false at th ebegining.

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, my winner conditions works fine. Either 0s or Xs, the thing is the null, if no one wins. I can't just do my  Win function false because it's false at th ebegining.

 

What?

Link to comment
Share on other sites

Link to post
Share on other sites

What?

 

So the code I posted is a function basically, that is where all my winning conditions are for my tic tac toe game. How do I do a function or condition identifying no winner/tie game?

Link to comment
Share on other sites

Link to post
Share on other sites

So the code I posted is a function basically, that is where all my winning conditions are for my tic tac toe game. How do I do a function or condition identifying no winner/tie game?

If those are all wining conditions for teams in that function, then think logically that all others conditions are then no one wins. And not sure how game play works as far as i know you put your mark until you get 3. Then games stop. From you functions it seems that you check who wins on end, when all 9 squares been filled. So you kinda need to see whose got more wins if both has 0 wins tie. But not sure how you intended to do. More info would be great.

Link to comment
Share on other sites

Link to post
Share on other sites

What SSL means is that  by checking for all the possible winning combinations you have basically already done what you are asking. There are multiple ways of checking if any of your IF statements are true like using if else to cycle through them all with an else at the end to denote a draw, checking the text in lblWinner or to make it simple add a new variable that changes if any are matched like this: (code might not work but should give you an idea)

	public void reset() {		cmd1.setEnabled(false);		cmd2.setEnabled(false);		cmd3.setEnabled(false);		cmd4.setEnabled(false);		cmd5.setEnabled(false);		cmd6.setEnabled(false);		cmd7.setEnabled(false);		cmd8.setEnabled(false);		cmd9.setEnabled(false); 	}public void gameover(){	var winner = 0;	if (bt1.equals ("X")  &  bt2.equals  ("X")  &  bt3.equals ("X"))	{		reset();		cmd1.setBackground(Color.red);		cmd2.setBackground(Color.red);		cmd3.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}	if (bt1.equals ("O")  &  bt2.equals  ("O")  &  bt3.equals ("O"))	{		reset();		cmd1.setBackground(Color.red);		cmd2.setBackground(Color.red);		cmd3.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}	if (bt4.equals ("X")  &  bt5.equals  ("X")  &  bt6.equals ("X"))	{		reset();		cmd4.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd6.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}	if (bt4.equals ("O")  &  bt5.equals  ("O")  &  bt6.equals ("O"))	{		reset();		cmd4.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd6.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}	if (bt7.equals ("X")  &  bt8.equals  ("X")  &  bt9.equals ("X"))	{ 		reset();		cmd7.setBackground(Color.red);		cmd8.setBackground(Color.red);		cmd9.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}	if (bt7.equals ("O")  &  bt8.equals  ("O")  &  bt9.equals ("O"))	{		reset();		cmd7.setBackground(Color.red);		cmd8.setBackground(Color.red);		cmd9.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}	if (bt1.equals ("X")  &  bt4.equals  ("X")  &  bt7.equals ("X"))	{		reset();		cmd1.setBackground(Color.red);		cmd4.setBackground(Color.red);		cmd7.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}		if (bt1.equals ("O")  &  bt4.equals  ("O")  &  bt7.equals ("O"))	{		reset();		cmd1.setBackground(Color.red);		cmd4.setBackground(Color.red);		cmd7.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}	if (bt2.equals ("X")  &  bt5.equals  ("X")  &  bt8.equals ("X"))	{		reset();		cmd2.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd8.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}	if (bt2.equals ("O")  &  bt5.equals  ("O")  &  bt8.equals ("O"))	{		reset();		cmd2.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd8.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}	if (bt3.equals ("X")  &  bt6.equals  ("X")  &  bt9.equals ("X"))	{ 		reset();		cmd3.setBackground(Color.red);		cmd6.setBackground(Color.red);		cmd9.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}	if (bt3.equals ("O")  &  bt6.equals  ("O")  &  bt9.equals ("O"))	{		reset();		cmd3.setBackground(Color.red);		cmd6.setBackground(Color.red);		cmd9.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}	if (bt1.equals ("X")  &  bt5.equals  ("X")  &  bt9.equals ("X"))	{		reset();		cmd1.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd9.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}	if (bt1.equals ("O")  &  bt5.equals  ("O")  &  bt9.equals ("O"))	{ 		reset();		cmd1.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd9.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}	if (bt7.equals ("X")  &  bt5.equals  ("X")  &  bt3.equals ("X"))	{ 		reset();		cmd7.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd3.setBackground(Color.red);		lblWinner.setText("X Win");		winner = 1;	}	if (bt7.equals ("O")  &  bt5.equals  ("O")  &  bt3.equals ("O"))	{		reset();		cmd7.setBackground(Color.red);		cmd5.setBackground(Color.red);		cmd3.setBackground(Color.red);		lblWinner.setText("O Win");		winner = 1;	}		if (winner == 0)	{		lblWinner.setText("You're Both Losers!");	}}

Also note, repeating the same 9 lines of code in every IF statement is a waste of space and just makes it harder to read and maintain code. Move it out and just call it when needed. 144 lines reduced down to 11.

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

Better, but still far, far more duplication than is needed. Think of all the control and data structures that are at your disposal. This is doable on the order of 20-30 lines or less.

Link to comment
Share on other sites

Link to post
Share on other sites

Better, but still far, far more duplication than is needed. Think of all the control and data structures that are at your disposal. This is doable on the order of 20-30 lines or less.

 

Yes it could be better but the aim was to answer the question, not rewrite their code so they learn nothing and dont understand it

Link to comment
Share on other sites

Link to post
Share on other sites

Yes it could be better but the aim was to answer the question, not rewrite their code so they learn nothing and dont understand it

 

I know, I'm pointing it out for the benefit of the OP, and thus why I did not provide any code at all.

Link to comment
Share on other sites

Link to post
Share on other sites

I know, I'm pointing it out for the benefit of the OP, and thus why I did not provide any code at all.

I guess he can use array as there are 9 tiles only and that is a static number. And he could use loops to minimize code

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×