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

Thread: Urgent Ideas Needed

1. 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());

}
}  Reply With Quote

3. 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?  Reply With Quote

4. 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?  Reply With Quote

5. 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.  Reply With Quote

6. 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.  Reply With Quote

7. 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.  Reply With Quote

8. 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());

}
}  Reply With Quote

9. 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.  Reply With Quote