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()); } }