Jump to content

HELP Figuring out if it's a TIE game.

Sergio45
Go to solution Solved by Mira Yurizaki,

I'm sure if a certain someone saw this they would rant at you. So I'm going to mention it.

 

Do not mix your GUI code with business logic. That is, you shouldn't be evaluating the board based on the strings the elements you have are using. Use an array with values in it to figure out your results.

 

In any case, I remember doing a Tic Tac Toe game a while back as an exercise. The way I evaluated it was each spot was either empty (0), taken by player one (1) or take by player 2 (-1). It would then add the rows, columns, and diagonals. If anything summed up to 3, player 1 won, if anything summed up to -3, player 2 won. If it ran through all of the combinations without either or, it's a tie.

I just need some help to figuring out how to find if the game is a tie. 

Its a tic tac toe game and what is posted is the method where it checks the board. 

 

Thanks again.

  public void endGame()
        {
                String a,b,c,d,e,f,g,h,i;
            boolean end = false;

            a= One.getText().toString();
            b= Two.getText().toString();
            c= Three.getText().toString();
            d=Four.getText().toString();
            e=Five.getText().toString();
            f=Six.getText().toString();
            g=Seven.getText().toString();
            h=Eight.getText().toString();
            i=Nine.getText().toString();

            if(a.equals("X") && b.equals("X") && c.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
                
            }
            if(a.equals("X") && e.equals("X") && i.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(a.equals("X") && d.equals("X") && g.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(b.equals("X") && e.equals("X") && h.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(c.equals("X") && f.equals("X") && i.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(d.equals("X") && e.equals("X") && f.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }

            if(g.equals("X") && h.equals("X") && i.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            /////////////////////////////////////////////
            if(a.equals("O") && b.equals("O") && c.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(a.equals("O") && e.equals("O") && i.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(a.equals("O") && d.equals("O") && g.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(b.equals("O") && e.equals("O") && h.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(c.equals("O") && f.equals("O") && i.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(d.equals("O") && e.equals("O") && f.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }

            if(g.equals("O") && h.equals("O") && i.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            ///////////////////////////
            if (g.equals("X") && e.equals("X") && c.equals("X")){
            GameMessage.setText("Winner Player X");
            end = true;
        }

            if (g.equals("O") && e.equals("O") && c.equals("O")){
                GameMessage.setText("Winner Player O");
            end = true;
        }
           /* else
            {
                GameMessage.setText("IT'S A TIE!");
                end=true;
            }*/

            if (end)
            {
                One.setEnabled(false);
                Two.setEnabled(false);
                Three.setEnabled(false);
                Four.setEnabled(false);
                Five.setEnabled(false);
                Six.setEnabled(false);
                Seven.setEnabled(false);
                Eight.setEnabled(false);
                Nine.setEnabled(false);

            }
        }

 

Link to comment
Share on other sites

Link to post
Share on other sites

since you are setting end to true if any of those conditions are true, you can just check the state of end after those other if statements. Not that it makes a huge difference, but if your first if statement is true, your code still seems to run through all of the other checks even if it doesn't have to.

if (end == false)

{

GameMessage.setText("IT'S A TIE!");

end=true;

}

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Reno2792 said:

since you are setting end to true if any of those conditions are true, you can just check the state of end after those other if statements. Not that it makes a huge difference, but if your first if statement is true, your code still seems to run through all of the other checks even if it doesn't have to.

if (end == false)

{

GameMessage.setText("IT'S A TIE!");

end=true;

}

Wouldn't that just automatically detect that the first button that is clicked is not true (false) and give me the message on the first try?

Sorry if that is a stupid question.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Sergio45 said:

Wouldn't that just automatically detect that the first button that is clicked is not true (false) and give me the message on the first try?

Sorry if that is a stupid question.

Not a stupid question, you are right. Maybe creating a new variable that counts the amount of non blanks would help. If you get to 9 non blanks and end is not equal to true, then it would be a tie. 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Reno2792 said:

Not a stupid question, you are right. Maybe creating a new variable that counts the amount of non blanks would help. If you get to 9 non blanks and end is not equal to true, then it would be a tie. 

What I did is every time that the button is selected the button is setEnable(false). I will post that part of the code. 

 One.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)

            {
                if (One.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        One.setText("X");
                        One.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        One.setText("O");
                        One.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Two.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Two.setText("X");
                        Two.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Two.setText("O");
                        Two.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Three.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Three.setText("X");
                        Three.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Three.setText("O");
                        Three.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Four.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Four.setText("X");
                        Four.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Four.setText("O");
                        Four.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Five.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Five.setText("X");
                        Five.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Five.setText("O");
                        Five.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Six.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Six.setText("X");
                        Six.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Six.setText("O");
                        Six.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Seven.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Seven.setText("X");
                        Seven.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Seven.setText("O");
                        Seven.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Eight.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Eight.setText("X");
                        Eight.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Eight.setText("O");
                        Eight.setEnabled(false);
                    }

                }
                endGame();
            }
        });

        Nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Nine.getText().toString().equals("")) {
                    if (turn == 1) {


                        turn = 2;
                        Nine.setText("X");
                        Nine.setEnabled(false);
                    } else if (turn == 2) {
                        turn = 1;
                        Nine.setText("O");
                        Nine.setEnabled(false);
                    }

                }
                endGame();
            }
        });

And then I think somewhere in here I can set it to find for a tie game.

I think and thanks for the help man.


        public void endGame()
        {
                String a,b,c,d,e,f,g,h,i;
            boolean end = false;

            a= One.getText().toString();
            b= Two.getText().toString();
            c= Three.getText().toString();
            d=Four.getText().toString();
            e=Five.getText().toString();
            f=Six.getText().toString();
            g=Seven.getText().toString();
            h=Eight.getText().toString();
            i=Nine.getText().toString();

            if(a.equals("X") && b.equals("X") && c.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;



            }


            if(a.equals("X") && e.equals("X") && i.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(a.equals("X") && d.equals("X") && g.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(b.equals("X") && e.equals("X") && h.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(c.equals("X") && f.equals("X") && i.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            if(d.equals("X") && e.equals("X") && f.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }

            if(g.equals("X") && h.equals("X") && i.equals("X"))
            {
                GameMessage.setText("Winner Player X");
                end = true;
            }
            /////////////////////////////////////////////
            if(a.equals("O") && b.equals("O") && c.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(a.equals("O") && e.equals("O") && i.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(a.equals("O") && d.equals("O") && g.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(b.equals("O") && e.equals("O") && h.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(c.equals("O") && f.equals("O") && i.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            if(d.equals("O") && e.equals("O") && f.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }

            if(g.equals("O") && h.equals("O") && i.equals("O"))
            {
                GameMessage.setText("Winner Player O");
                end = true;
            }
            ///////////////////////////
            if (g.equals("X") && e.equals("X") && c.equals("X")){
            GameMessage.setText("Winner Player X");
            end = true;
        }

            if (g.equals("O") && e.equals("O") && c.equals("O")){
                GameMessage.setText("Winner Player O");
            end = true;
        }
          /*  else if (end == false)
            {
                GameMessage.setText("IT'S A TIE!");
                end=true;
            }*/


            if (end)
            {
                One.setEnabled(false);
                Two.setEnabled(false);
                Three.setEnabled(false);
                Four.setEnabled(false);
                Five.setEnabled(false);
                Six.setEnabled(false);
                Seven.setEnabled(false);
                Eight.setEnabled(false);
                Nine.setEnabled(false);
               if(end ==false)
               {
                   GameMessage.setText("Its a tie");
               }
            }
        }

 

Link to comment
Share on other sites

Link to post
Share on other sites

couldn't you put all the strings into one long strong and then check that for a winning pattern?

xoooxooox

 

would make x win left to right slanted. I'm sure you could use regx for this, I'm not that great at it.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

I'm sure if a certain someone saw this they would rant at you. So I'm going to mention it.

 

Do not mix your GUI code with business logic. That is, you shouldn't be evaluating the board based on the strings the elements you have are using. Use an array with values in it to figure out your results.

 

In any case, I remember doing a Tic Tac Toe game a while back as an exercise. The way I evaluated it was each spot was either empty (0), taken by player one (1) or take by player 2 (-1). It would then add the rows, columns, and diagonals. If anything summed up to 3, player 1 won, if anything summed up to -3, player 2 won. If it ran through all of the combinations without either or, it's a tie.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, M.Yurizaki said:

I'm sure if a certain someone saw this they would rant at you. So I'm going to mention it.

 

Do not mix your GUI code with business logic. That is, you shouldn't be evaluating the board based on the strings the elements you have are using. Use an array with values in it to figure out your results.

 

In any case, I remember doing a Tic Tac Toe game a while back as an exercise. The way I evaluated it was each spot was either empty (0), taken by player one (1) or take by player 2 (-1). It would then add the rows, columns, and diagonals. If anything summed up to 3, player 1 won, if anything summed up to -3, player 2 won. If it ran through all of the combinations without either or, it's a tie.

that's not a bad way to do it. +1 on arrays seems nee to this so I didn't suggest it.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

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

×