Jane Doe
Jane Doe

Reputation: 9

Check for Winner Tic Tac Toe game Java

my check for winner method doesn't detect tie games. I don't understand where the problem is with the tie game, and searching through a column, so if anyone could help that would be great!.The code is below

public static boolean CheckForWinner(int player)
            {
                //Row and Column Check
                if (GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player ||
                    GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player ||
                    GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player ||
                    GameBoard[0][0] == player && GameBoard[1][0] == player && GameBoard[2][0] == player ||
                    GameBoard[0][1] == player && GameBoard[1][1] == player && GameBoard[2][1] == player ||
                    GameBoard[0][2] == player && GameBoard[1][2] == player && GameBoard[2][2] == player)
                    {
                        finalwinner=true;
                        return true;
                    }

                //diagonal checks
                if (GameBoard[0][0]==player && GameBoard[1][1]==player && GameBoard[2][2]==player)
                {
                finalwinner=true;
                return true;
                }

                if (GameBoard[2][0]==player && GameBoard[1][1]==player && GameBoard[0][2]==player)
                {
                finalwinner=true;
                return true;
                }

                //Tie Game Check
                if (GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player &&
                    GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player &&
                    GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player)
                    {
                tieGame=true;
                    return true;
                }
                return false;
            }

This is the main method:

       public static void main (String[] args)
                {
                System.out.println("Tic Tac Toe Game");
                System.out.println("Player 1 is 1, Computer is 2");

                Draw();
                currentplayer=1;
                while (winner!= true)
                {
                    if(currentplayer==1)
                        {
                            System.out.println("Your Turn");
                            Player1Turn(currentplayer);
                        }
                    else
                        ComputerTurn(currentplayer);

                    finalwinner=CheckForWinner(currentplayer);

                    if(finalwinner==true && tieGame==false)
                    {
                        System.out.println("Winner is Player" +currentplayer+ "Congrats Champion!");
                        System.out.println("Beginning New Game! Press Cancel then Enter to Exit");
                        Reset();
                    }

                    if(tieGame==true)
                    {
                        System.out.println("It's a Tie! Play Again.");
                        System.out.println("Beginning New Game! Press Cancel then Enter to Exit");
                        Reset();
                    }

                    if(currentplayer==1)
                        currentplayer=2;
                    else if(currentplayer==2)
                        currentplayer=1;
                }

                }

Upvotes: 0

Views: 1017

Answers (6)

Jack Stafford
Jack Stafford

Reputation: 86

The error is in the main code/structure of the function. Your function needs to return two variables, finalwinner and tie game. Right now your function stops at the first return statement, and sets that equal to finalwinner in the main.

Try something like this:

public static boolean[] CheckForWinner(int player)
        {
            //Row and Column Check
            if (GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player ||
                GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player ||
                GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player ||
                GameBoard[0][0] == player && GameBoard[1][0] == player && GameBoard[2][0] == player ||
                GameBoard[0][1] == player && GameBoard[1][1] == player && GameBoard[2][1] == player ||
                GameBoard[0][2] == player && GameBoard[1][2] == player && GameBoard[2][2] == player)
                {
                    boolean finalwinner=true;
                }

            //diagonal checks
            if (GameBoard[0][0]==player && GameBoard[1][1]==player && GameBoard[2][2]==player)
            {
                boolean finalwinner=true;
            }

            if (GameBoard[2][0]==player && GameBoard[1][1]==player && GameBoard[0][2]==player)
            {
                boolean finalwinner=true;
            }

            //Tie Game Check
            if (GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player &&
                GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player &&
                GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player)
                {
                boolean tieGame=true;
            }
            return new boolean [] {finalwinner, tiegame};
        }

public static void main (String[] args)
                {
                System.out.println("Tic Tac Toe Game");
                System.out.println("Player 1 is 1, Computer is 2");
            Draw();
            currentplayer=1;
            while (winner!= true)
            {
                if(currentplayer==1)
                    {
                        System.out.println("Your Turn");
                        Player1Turn(currentplayer);
                    }
                else
                    ComputerTurn(currentplayer);

                boolean result[] = CheckForWinner(currentplayer);

                if(result[0]==true && result[1]==false)
                {
                    System.out.println("Winner is Player" +currentplayer+ "Congrats Champion!");
                    System.out.println("Beginning New Game! Press Cancel then Enter to Exit");
                    Reset();
                }

                if(result[1]==true)
                {
                    System.out.println("It's a Tie! Play Again.");
                    System.out.println("Beginning New Game! Press Cancel then Enter to Exit");
                    Reset();
                }

                if(currentplayer==1)
                    currentplayer=2;
                else if(currentplayer==2)
                    currentplayer=1;
            }

            }

Upvotes: 0

Yev
Yev

Reputation: 321

First of all, in your main method you say finalwinner=CheckForWinner(currentplayer); so you don't have to set finalwinner=true; every time after checking row, column and diagonal. return true; is enough.

Your problem is, you check for a Tie Game and if it is the case, you also return true, but it is wrong, because neither player nor computer wins! So your checkForWinner method always returns true.

You don't really have to check extra for Tie Game. Just check win for player, check win for computer, if both are false and last move done - its a tie game.

Upvotes: 0

SQL Hacks
SQL Hacks

Reputation: 1332

Change GameBoard[0][0] == player to GameBoard[0][0] != 0 for all 9 conditions.

Upvotes: 0

Andrei-Marius Longhin
Andrei-Marius Longhin

Reputation: 563

EDIT: Info on enums

This is probably not what you'd prefer, but I strongly suggest you go for a different move/board representation:

HOW

Firstly, I suggest you implement your board cells as enums (since there are only 9 of them).

Then, implement the board as a set of 9 enums, as such:

public enum Move {
A0, A1, A2, B0, B1, B2, C0, C1, C2; }

Also, implement winning patterns (e.g. a whole line, a whole diagonal) as sets of 3 enums (i.e. a subset of the board set).

After these changes, it gets a lot easier to check whether a player (be it you or computer) has won.

I recommend you add the subsets and the method that checks if a player has won inside the Move class as well.

A subset can be created like this: e.g.

static private final EnumSet<Move> lineA = EnumSet.of(A0,A1,A2);

And a check from your won method (note you need to check for all subsets) could possibly be:

moves.containsAll(lineA)

where moves is the set of moves made by player (when a player makes a move you add the enum to it's moves set).

WHY

This design is more concise, straightforward and elegant. You should not experience the problems you described any longer, if you use this approach.

Upvotes: 0

Jack Allan
Jack Allan

Reputation: 15004

The problem is this expression in the if statement:

GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player &&
                    GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player &&
                    GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player

You're checking to see if the player is in every board position.

You want to determine that there wasn't a winner. That means checking that none of the the rows, columns and diagonals have the same player.

Upvotes: 1

You are not checking if the "computer" won, if the computer didnt won, and player either, then you can turn the tieGame flag true

Upvotes: 0

Related Questions