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 8 of 8

Thread: Urgent Ideas Needed

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    Kentucky
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Urgent Ideas Needed

    I have this terrible problem I have been working on, we cannot use loops and arrays and such as we have not learned about them. I can use boolean variables but I am teaching myself their functions essentially as my professor hasn't explained them. Anyways, this is due tonight at 11:55 PM eastern time.

    Question: Let b11, b12, b13, b21, b22, b23, b31, b32, b33 represent the 3 x 3 tic tac toe board. Write a Java program that assigns each variable randomly with ' ', 'O', 'X' and determines if O, X, both, or none wins.

    Problem: Cannot get it to correctly print out "Both Wins", haven't bothered to get "No winner" yet because I was going to put that in my ending else statement. Thanks for your help and ideas.

    import java.util.Random;
    public class Lab5 {
        static String getWinner() {
            char b11, b12, b13, 
                 b21, b22, b23, 
                 b31, b32, b33;
            int n;
            Random rand = new Random();
     
            n = rand.nextInt(3);
     
            if (n == 0) 
                b11 = 'O';
            else if (n == 1)
                b11 = 'X';
            else 
                b11 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b12 = 'O';
            else if (n == 1)
                b12 = 'X';
            else 
                b12 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b13 = 'O';
            else if (n == 1)
                b13 = 'X';
            else 
                b13 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b21 = 'O';
            else if (n == 1)
                b21 = 'X';
            else 
                b21 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b22 = 'O';
            else if (n == 1)
                b22 = 'X';
            else 
                b22 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b23 = 'O';
            else if (n == 1)
                b23 = 'X';
            else 
                b23 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b31 = 'O';
            else if (n == 1)
                b31 = 'X';
            else 
                b31 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b32 = 'O';
            else if (n == 1)
                b32 = 'X';
            else 
                b32 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b33 = 'O';
            else if (n == 1)
                b33 = 'X';
            else 
                b33 = ' ';
     
     
            /*Finding the winner */
            String s = " ";
     
     
            if (b11 == b21 && b21 == b31){
                if (b11 == 'X' && )  
                    s = "X wins";    
                else if (b11 == 'O')
                    s = "O wins"; 
     
            } 
            if (b12 == b22 && b22 == b32) {
                if (b12 == 'X')  
                    s = "X wins";    
                else if (b12 == 'O')
                    s = "O wins";                        
            }
            if (b13 == b23 && b23 == b33) {
                if (b13 == 'X')  
                    s = "X wins";    
                else if (b13 == 'O')
                    s = "O wins";                        
            }
            if (b11 == b12 && b12 == b13) {
                if (b11 == 'X')  
                    s = "X wins";    
                else if (b11 == 'O')
                    s = "O wins";                        
            } 
            if (b21 == b22 && b22 == b23) {
                if (b21 == 'X')  
                    s = "X wins";    
                else if (b21 == 'O')
                    s = "O wins";                        
            }  
            if (b31 == b32 && b32== b33) {
                if (b31 == 'X')  
                    s = "X wins";    
                else if (b31 == 'O')
                    s = "O wins";                        
            }   
            if (b11 == b22 && b22 == b33) {
                if (b11 == 'X')  
                    s = "X wins";    
                else if (b11 == 'O')
                    s = "O wins";                        
            } 
            if (b31 == b22 && b22 == b13) {
                if (b31 == 'X')  
                    s = "X wins";    
                else if (b31 == 'O')
                    s = "O wins";                        
            }
            System.out.println(b11+" "+b12+" "+b13);
            System.out.println(b21+" "+b22+" "+b23);
            System.out.println(b31+" "+b32+" "+b33);
     
            // Vertical    
            if ((b11 == b21 && b21 == b31) || (b12 == b22 && b22 == b32) || (b13 == b23 && b23 == b33))      
            {
                if (b11 != b12 && b12 != b13 && b21 != b22 && b22 != b23 && b31 != b32 && b32 != b33) 
                    s = "Both Wins";      
            }
     
            if ((b12 == b22 && b22 == b32) == (b13 == b23 && b23 == b33)) 
                s = "Both Wins"; 
            if ((b11 == b21 && b21 == b31) == (b13 == b23 && b23 == b33)) 
                s = "Both Wins";
            // Horizontal
            if ((b11 == b12 && b12 == b13) == (b21 == b22 && b22 == b23))
                s = "Both Wins";    
            if ((b11 == b12 && b12 == b13) == (b31 == b32 && b32 == b33))  
                s = "Both Wins"; 
            if ((b21 == b22 && b22 == b23) == (b31 == b32 && b32 == b33))  
                s = "Both Wins";
             // Diagonal
            if ((b11 == b22 && b22 == b33) == (b31 == b22 && b22 == b13))
                s = "Both Wins";
            else 
                s = "No Winner";  
     
            return s;      
        }    
     
        public static void main(String[] args) {
           System.out.println(getWinner());
     
        }   
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Urgent Ideas Needed

    I've never played a game of tic-tac-toe where both players win. Do you mean a draw? Or both lose? Has political correctness changed the game?

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Location
    Kentucky
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Urgent Ideas Needed

    This is what the professor asked in the question, I copied and pasted it directly, I had the same thoughts as you.

    --- Update ---

    How can I define two Boolean variables before all of my test cases named “xWins” and “oWins”. Instantiate them both to false. Then in each of my test cases, instead of setting a value for s, set xWins or oWins = true as applicable?? Any clue on how I can do this?

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

    Default Re: Urgent Ideas Needed

    Yes, my cursed knee-jerk reaction got the better of me. I thought about it some more. Since the players are not intelligent, it's possible to have one row or column of 'X' and another of 'Y' iff the determination of which player has won is made after the board is full.

    As for your updated question, I'm not sure why you'd use a Boolean object at all. Why not just a primitive boolean, or is that a part of the assignment I'm not getting.

    I see you have an error at this line (80-something):

    if (b11 == 'X' && )

    Have you fixed that?

    I'll keep looking at your code in an attempt to better understand your question.

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Location
    Kentucky
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Urgent Ideas Needed

    Someone told me that was the easiest way, he is a programmer at my workplace, but we haven't learned boolean variables at all, he hasn't taught anything of this nature. I understand !=, =, ==, &&, || etc. just not putting a new input variable such as "boolean s;" I fixed that error, I was just modifying code and forgot to change that before uploading here, sorry for that, my brain is overloaded.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Urgent Ideas Needed

    No loops or arrays is a significant crutch handicap, but I don't see where you're doing anything wrong. I might start by assigning all of the variables to a space and then randomly changing them to 'X', 'Y', or leaving them as is, but that approach may not comply with the instructions. You probably also do not yet know the ternary operator, but I would probably use that too.

    Post your corrected code, and I recommend adding some comments.

    Think through the possible results. Since you know there could be 4 outcomes and one of the outcomes (both wins) is a set that includes both 'X' wins and 'Y' wins, then you need to modify your logic to determine if both wins is the outcome after one or the other wins. You might need another variable to track the winners, one that is true if 'X' wins, another that is true if 'Y' wins.

  7. #7
    Junior Member
    Join Date
    Aug 2014
    Location
    Kentucky
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Urgent Ideas Needed

    Finished!
    import java.util.Random;
    public class Lab5 {
        static String getWinner() {
            char b11, b12, b13, 
                 b21, b22, b23, 
                 b31, b32, b33;
            int n;
     
            Random rand = new Random();
     
            n = rand.nextInt(3);
     
            if (n == 0) 
                b11 = 'O';
            else if (n == 1)
                b11 = 'X';
            else 
                b11 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b12 = 'O';
            else if (n == 1)
                b12 = 'X';
            else 
                b12 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b13 = 'O';
            else if (n == 1)
                b13 = 'X';
            else 
                b13 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b21 = 'O';
            else if (n == 1)
                b21 = 'X';
            else 
                b21 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b22 = 'O';
            else if (n == 1)
                b22 = 'X';
            else 
                b22 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b23 = 'O';
            else if (n == 1)
                b23 = 'X';
            else 
                b23 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b31 = 'O';
            else if (n == 1)
                b31 = 'X';
            else 
                b31 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b32 = 'O';
            else if (n == 1)
                b32 = 'X';
            else 
                b32 = ' ';
            n = rand.nextInt(3);
            if (n == 0)
                b33 = 'O';
            else if (n == 1)
                b33 = 'X';
            else 
                b33 = ' ';
     
            /*Finding the winner */
     
            boolean xWins = false, oWins = false;
     
            if (b11 == b21 && b21 == b31){
                if (b11 == 'X')  
                    xWins = true;     
                else if (b11 == 'O')
                    oWins = true;
     
            } 
            if (b12 == b22 && b22 == b32) {
                if (b12 == 'X')  
                    xWins = true;     
                else if (b12 == 'O')
                    oWins = true;                        
            }
            if (b13 == b23 && b23 == b33) {
                if (b13 == 'X')  
                    xWins = true;      
                else if (b13 == 'O')
                    oWins = true;                        
            }
            if (b11 == b12 && b12 == b13) {
                if (b11 == 'X')  
                    xWins = true;      
                else if (b11 == 'O')
                    oWins = true;                        
            } 
            if (b21 == b22 && b22 == b23) {
                if (b21 == 'X')  
                    xWins = true;      
                else if (b21 == 'O')
                    oWins = true;                        
            }  
            if (b31 == b32 && b32== b33) {
                if (b31 == 'X')  
                    xWins = true;      
                else if (b31 == 'O')
                    oWins = true;                        
            }   
            if (b11 == b22 && b22 == b33) {
                if (b11 == 'X')  
                    xWins = true;      
                else if (b11 == 'O')
                    oWins = true;                        
            } 
            if (b31 == b22 && b22 == b13) {
                if (b31 == 'X')  
                    xWins = true;     
                else if (b31 == 'O')
                    oWins = true;                        
            }
     
            String s;
     
            if (xWins && oWins) {
            s = "Both Win!";
            }
            else if (xWins && !oWins) {
            s = "X Wins!";
            }
            else if (!xWins && oWins) {
            s = "O Wins!";
            }
            else {
            s = "No one wins";
            }
     
            return s;      
        }    
     
        public static void main(String[] args) {
           System.out.println(getWinner());
     
        }   
    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Urgent Ideas Needed

    Excellent! I added the code to print the resulting board back in and ran it to see that all results looked right. Printing the board with the result was a nice feature that you should have left in and commented out if it was not desired in the final product. Some instructors insist that all test code is left in but commented out when the assignment is turned in.

Similar Threads

  1. help needed (URGENT)
    By sour_bee in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2013, 01:55 PM
  2. Replies: 1
    Last Post: August 5th, 2012, 08:05 PM
  3. urgent help needed
    By nono in forum Member Introductions
    Replies: 2
    Last Post: March 10th, 2011, 11:18 AM
  4. Urgent help needed
    By mohit1007 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 23rd, 2010, 09:34 PM
  5. Urgent code needed
    By subhvi in forum AWT / Java Swing
    Replies: 4
    Last Post: August 27th, 2009, 12:55 AM