Click here to Skip to main content
15,868,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I am doing a project on tictactoe using java and instead of using arrays or anything else that would make the job more simple, we are required to list all the possibilities. Although I managed to do that but there is one part of code that wouldn't work and I have tried my all on that. In tictactoe sometimes there are blank spaces left near the end, essentially that would mean "No Winner", but my code counts that as a win too. For instance, b b b (b for no spaces) would equal a win for me and I don't want that. it should equal "No Winner". How would I fix my code, accordingly?

**Check my code where I start to list the "b" possibilities which is near the middle



import java.util.Scanner;
public class tictactoe{
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
int option = keyboard.nextInt();
String TL = keyboard.next();
String TM = keyboard.next();
String TR = keyboard.next();
String ML = keyboard.next();
String MM = keyboard.next();
String MR = keyboard.next();
String BL = keyboard.next();
String BM = keyboard.next();
String BR = keyboard.next();
if (option == 1){
System.out.print(TL);
System.out.print(TM);
System.out.print(TR);
System.out.print("\n"+ML);
System.out.print(MM);
System.out.print(MR);
System.out.print("\n"+BL);
System.out.print(BM);
System.out.print(BR);
}
else if (option == 2){
System.out.print(TL);
System.out.print(TM);
System.out.print(TR);
System.out.print("\n"+ML);
System.out.print(MM);
System.out.print(MR);
System.out.print("\n"+BL);
System.out.print(BM);
System.out.print(BR);
if (TL.equals(TL) && TL.equals(ML) && TL.equals(BL)){
System.out.println("\n"+TL +" "+"wins");
}
else if (TL.equals(TL) && TL.equals(MM) && TL.equals(BR)){
System.out.println("\n"+TL +" "+"wins");
}
else if (TL.equals(TL) && TR.equals(TM) && TR.equals(TR)){
System.out.println("\n"+TL +" "+"wins");
}
else if (TM.equals(TM) && TM.equals(MM) && TM.equals(BM)){
System.out.println("\n"+TM +" "+"wins");
}
else if (TM.equals(TM) && TM.equals(TL) && TM.equals(TR)){
System.out.println("\n"+TM +" "+"wins");
}
else if (TR.equals(TR) && TR.equals(MR) && TR.equals(BR)){
System.out.println("\n"+TR +" "+"wins");
}
else if (TR.equals(TR) && TR.equals(MM) && TR.equals(BL)){
System.out.println("\n"+TR +" "+"wins");
}
else if (TR.equals(TR) && TR.equals(TM) && TR.equals(TL)){
System.out.println("\n"+TR +" "+"wins");
}
else if (ML.equals(ML) && ML.equals(TL) && ML.equals(BL)){
System.out.println("\n"+ML +" "+"wins");
}
else if (ML.equals(ML) && ML.equals(MM) && ML.equals(MR)){
System.out.println("\n"+ML +" "+"wins");
}
else if (MM.equals(MM) && MM.equals(ML) && MM.equals(MR)){
System.out.println("\n"+MM +" "+"wins");
}
else if (MM.equals(MM) && MM.equals(BL) && MM.equals(TR)){
System.out.println("\n"+MM +" "+"wins");
}
else if (MM.equals(MM) && MM.equals(TM) && MM.equals(BM)){
System.out.println("\n"+MM +" "+"wins");
}
else if (MR.equals(MR) && MR.equals(TR) && MR.equals(BR)){
System.out.println("\n"+MR +" "+"wins");
}
else if (MR.equals(MR) && MR.equals(MM) && MR.equals(ML)){
System.out.println("\n"+MR +" "+"wins");
}
else if (BL.equals(BL) && BL.equals(ML) && BL.equals(TL)){
System.out.println("\n"+BL +" "+"wins");
}
else if (BL.equals(BL) && BL.equals(MM) && BL.equals(TR)){
System.out.println("\n"+BL +" "+"wins");
}
else if (BL.equals(BL) && BL.equals(BM) && BL.equals(BR)){
System.out.println("\n"+BL +" "+"wins");
}
else if (BM.equals(BM) && BM.equals(MM) && BM.equals(TM)){
System.out.println("\n"+BM +" "+"wins");
}
else if (BM.equals(BM) && BM.equals(BL) && BM.equals(BR)){
System.out.println("\n"+BM +" "+"wins");
}
else if (BR.equals(BR) && BR.equals(MR) && BR.equals(TR)){
System.out.println("\n"+BR +" "+"wins");
}
else if (BR.equals(BR) && BR.equals(MM) && BR.equals(TL)){
System.out.println("\n"+BR +" "+"wins");
}
else if (BR.equals(BR) && BR.equals(BM) && BR.equals(BL)){
System.out.println("\n"+BR +" "+"wins");
}
else if (TL.equals(TL.equals("b")) && TL.equals(ML.equals("b")) && TL.equals(BL.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (TL.equals(TL.equals("b")) && TL.equals(TM.equals("b")) && TL.equals(TR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (TL.equals(TL.equals("b")) && TL.equals(MM.equals("b")) && TL.equals(BR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (TM.equals(TM.equals("b")) && TM.equals(TL.equals("b")) && TM.equals(TR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (TM.equals(TM.equals("b")) && TM.equals(TM.equals("b")) && TM.equals(BM.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (TR.equals(TR.equals("b")) && TR.equals(MR.equals("b")) && TR.equals(BR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (TR.equals(TR.equals("b")) && TR.equals(MM.equals("b")) && TR.equals(BL.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (TR.equals(TL.equals("b")) && TR.equals(TM.equals("b")) && TR.equals(TL.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (ML.equals(ML.equals("b")) && ML.equals(TL.equals("b")) && ML.equals(BL.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (ML.equals(ML.equals("b")) && ML.equals(MM.equals("b")) && ML.equals(MR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (MM.equals(MM.equals("b")) && MM.equals(ML.equals("b")) && MM.equals(MR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (MM.equals(MM.equals("b")) && MM.equals(TL.equals("b")) && MM.equals(BR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (MM.equals(MM.equals("b")) && MM.equals(TM.equals("b")) && MM.equals(TM.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (MR.equals(MR.equals("b")) && MR.equals(TR.equals("b")) && MR.equals(BR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (MR.equals(MR.equals("b")) && MR.equals(MM.equals("b")) && MR.equals(ML.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BL.equals(BL.equals("b")) && BL.equals(MM.equals("b")) && BL.equals(TR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BL.equals(BL.equals("b")) && BL.equals(ML.equals("b")) && BL.equals(TL.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BL.equals(BL.equals("b")) && BL.equals(BM.equals("b")) && BL.equals(BR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BM.equals(BM.equals("b")) && BM.equals(BL.equals("b")) && BM.equals(BR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BM.equals(BM.equals("b")) && BM.equals(MM.equals("b")) && BM.equals(TM.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BR.equals(BR.equals("b")) && BR.equals(MM.equals("b")) && BR.equals(TL.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BR.equals(BR.equals("b")) && BR.equals(MR.equals("b")) && BR.equals(TR.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else if (BR.equals(BR.equals("b")) && BR.equals(BM.equals("b")) && BR.equals(BL.equals("b"))){ //restriction
System.out.println("\n"+"No Winner");
}
else{
System.out.println("\n"+"No Winner");
}

}
else if (option == 3){
boolean go = true;
while (go){
TL = keyboard.next();
TM = keyboard.next();
TR = keyboard.next();
ML = keyboard.next();
MM = keyboard.next();
MR = keyboard.next();
BL = keyboard.next();
BM = keyboard.next();
BR = keyboard.next();
}
}
}
}

What I have tried:

The above code is what i tried
Posted
Updated 10-Apr-20 10:51am

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Your code have numerous flaws:
Java
if (TL.equals(TL) && TL.equals(ML) && TL.equals(BL)){
System.out.println("\n"+TL +" "+"wins");
}

TL.equals(TL) is always true, no matter what is in TL.
So, if TL= ML= BL= "b", "b" is a winner, and so on every where.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900