Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Tic Tac Toe Program compile error

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe Program compile error

    I am creating a Tic Tac Toe program, but I am getting a compile error that i can't figure out why..I am sure its a dumb reason. Any help will be greatly appreciated. If you figure out the reason for the compile error, could you also check the rest to make sure I did it right. Please Help. Assignment due soon. I put a comment next to the code with the error.


    Here is my Class:

    public class PlayTicTacToe
    {
        //instance variables
        private String[][] p;//2d array for the board
        private String player;
        //Constructor    
        public PlayTicTacToe()
        {
            String p[][]=new String [3][3];
            for (int i=0;i<3;i++)
            {
                for (int j=0;j<3;j++)
                {
                    p[i][j]=" ";
                }
            }
        }
        // Accessor methods for the data
        public String getPlayer()
        {
            return player;
        }
        private int wonDiagonal()
        {   
     
            return 1;
        }
     
        private int wonStraightLines()
        {
            for (int i=0;i<3;i++)
            {
     
                int play1=0;
                int play2=0;
                for (int j=0;j<3;j++)
                {
                    if (p[i][j].equals(""))
                    {
                    }
                    else if (p[i][j].equalsIgnoreCase("X"))
                    {
                        play1++;
                    }
                    else
                    {
                        play2++;
                    }
                }
                if(play1==3)
                {
                    return 1;
                }
                else if (play2==3)
                {
                    return 2;
                }
                else             
                {
                }
            }
            for (int j=0;j<3;j++)
            {
                int play1=0;
                int play2=0;
                for (int i=0;i<3;i++)
                {
                    if (p[j][i].equals(""))
                    {
                    }
                    else if (p[j][i].equalsIgnoreCase("X"))
                    {
                        play1++;
                    }
                    else
                    {
                        play2++;
                    }
                }
                if(play1==3)
                {
                    return 1;
                }
                else if (play2==3)
                {
                    return 2;
                }
                else             
                {
                }
            }
            return 0;
        }
     
        public int win()
        {
            if (wonDiagonal()|| wonStraightLines())           //Compile error is here!!!! Says: bad operand types for binary operator '||', first type: int;  second type: int
            {
                return true;
            }
            else
            {
                return false;
            }
        }
     
        public void drawBoard()
        {
            System.out.println("|-----|");
            System.out.println("|"+p[0][0]+"|"+p[0][1]+"|"+p[0][2]+"|");
            System.out.println("|-----|");
            System.out.println("|"+p[1][0]+"|"+p[1][1]+"|"+p[1][2]+"|");
            System.out.println("|-----|");
            System.out.println("|"+p[2][0]+"|"+p[2][1]+"|"+p[2][2]+"|");
            System.out.println("|-----|");
        }
     
        public void play()
        {
            Scanner scan=new Scanner(System.in);
            System.out.println("Player 1 choose a row and column.");
            int p1row=scan.nextInt();
            int p1col=scan.nextInt();
            p[p1row][p1col]="X";
            System.out.println("Player 2 enter your move.");
            int p2row=scan.nextInt();
            int p2col=scan.nextInt();
            p[p2row][p2col]="O";
        }
     
    }
     
     
     
    import java.util.Scanner;                                                  //This is my Runner. 
    public class Runner 
    { 
        public static void main(String[] args) 
        { 
            Scanner in = new Scanner(System.in);
     
            PlayTicTacToe p = new PlayTicTacToe(); 
     
            p.drawBoard();
            while (!p.win()) 
            { 
                p.play(); 
                p.drawBoard(); 
            } 
    System.out.println("Player " + p.getPlayer() + " wins!"); 
        }
    }
    Last edited by fireman3780; September 24th, 2014 at 04:01 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tic Tac Toe Program compile error

    bad operand types for binary operator '||', first type: int; second type: int
    The boolean operator || is used to connect two boolean expressions. The compiler see int values where there should be boolean expressions. Change the code by replacing the int values with boolean expressions.
    For example: x < 1 is a boolean expression.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Program compile error

    I am still not understanding it. Why is it seeing int values.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Tic Tac Toe Program compile error

    Why is it seeing int values.
    ?

    if (wonDiagonal()|| wonStraightLines())

    What do those two methods return?

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Program compile error

    I am understanding that I need a Boolean expression, but am having a hard time figuring out what I need to change my code to. For some reason i had it working at school (with help from my tutor), but when I loaded the code up at home I must have changed something.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tic Tac Toe Program compile error

    When do you want that if statement to be true? What do the values returned by those methods need to be compared against to give the true or false values you want?

    This is another example of where comments in the code would help anyone (including and especially the author) looking at the code understand what is happening. What values does a method return? What do each of the possible values that a method returns mean?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] tic tac toe java program error
    By JSF001 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 22nd, 2013, 05:14 PM
  2. Tic Tac Toe Program
    By shodai in forum AWT / Java Swing
    Replies: 5
    Last Post: May 5th, 2013, 10:29 AM
  3. Tic Tac Toe Program, Can someone help me out????
    By KVM in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 24th, 2012, 08:11 PM
  4. Tic Tac Toe Program, Can someone help me out????
    By KVM in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2012, 04:47 PM
  5. [SOLVED] Tic-Tac-Toe program
    By Actinistia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2011, 11:18 PM

Tags for this Thread