# Tic Tac Toe problem

• 03-05-2014, 05:01 AM
perrinormal
Tic Tac Toe problem
In my code I'm coding, Rock Paper Scissors, I ran into a problem and i don't know how to fix it, the problem is that the IF statements i made doesn't exist, it always goes straight to the ELSE statement. So if you do can fix it, can you write a 1 to 2 sentence explaining how you fixed it, so i can learn for the future.

CODE:
• 03-05-2014, 03:36 PM
alykins
Why do you have the for loop in there at the beginning? I am assuming you are going to make that more than a one-time loop in the future?

Also this line:
Code:

`c1 = number.nextInt(20);`
Code:

`c1 = number.nextInt(3);`
If you are saying it never hits this block
*by the way on this block, with the online if's, you don't need the braces- also should be using else-if or case statements
Code:

```if(P1c == "R" || P1c == "P" || P1c == "S") {     if(P1c.equals("R"))        f1 = 1;     if(P1c.equals("P"))        f1 = 2;     if(P1c.equals("S")) f1 = 3;              }```
Throw a breakpoint on that if statement and see what the value of P1c is. See if it is some bogus character or something...

If you want to use case statement for first part, it would look something like this (my syntax might be a little off)
Code:

```switch(p1.toUpperCase()) {   // note we can omit P1c entirely here   default:       System.out.println ("Sorry, you didn't pick Rock, Paper, or Scissors.");       break;   case 'R' :       f1 = 1;       break;   case 'P' :       f1 = 2;       break;   case 'S' :       f1 = 3;       break; }```
But anyways- if you could capture that value through a break point that would be great info for us to help u :)
• 03-05-2014, 04:09 PM
alykins
and here it is in C# for you to see the logic working (you can copy the logic fairly well too to Java)

Code:

``` class Program     {         enum rpc { R = 1, P = 2, S = 3};         static void Main(string[] args)         {             Random r = new Random();             int computer;             int player;             Console.WriteLine("Please select R (rock), P (paper), S (scissors)");             player = Convert.ToInt32(Enum.Parse(typeof(rpc), Console.ReadLine().ToUpper()));             computer = r.Next(1, 3);             switch (player)             {                 default:                     // in this case the default is not really needed because an invalid input will                     // result in an error on the enum conversion                     Console.WriteLine();                     break;                 case 1:                     if (computer == 1) Console.WriteLine("tie");                     else if (computer == 2) Console.WriteLine("lose");                     else Console.WriteLine("win");                     break;                 case 2:                     if (computer == 1) Console.WriteLine("win");                     else if (computer == 2) Console.WriteLine("tie");                     else Console.WriteLine("lose");                     break;                 case 3:                     if (computer == 1) Console.WriteLine("lose");                     else if (computer == 2) Console.WriteLine("win");                     else Console.WriteLine("tie");                     break;             }             Console.WriteLine("fin");             Console.ReadLine();         }     }```
• 03-05-2014, 04:22 PM
Fou-Lu
You cannot compare strings in java using a `var == "L"`. Strings are immutable, you need to use `var.equals("L")` for example.
I don't know why you did it correctly within the if branch, but not within the surrounding if.
As pointed out, c1 can simply be assigned number.nextInt(3). The loop though is for allowing the game to continue, but I don't see an exit condition; you should have a do/while instead, its easier to handle the exit condition without requiring the break.

Also, the second provided code could be converted to java, but the first could not. Java doesn't allow a switch statement on a string since its not a primitive. I stand by the recommendation for an enum; java requires a bit more to assign numerical values to an enumerated value, but that said you never need to make a numerical comparison since you can simply compare an enum to another enum.
• 03-05-2014, 04:27 PM
alykins
I keep forgetting you can't compare strings like that in Java :P
• 03-05-2014, 04:44 PM
Fou-Lu
Yep, strings are a pain in Java. Although since this is all based on a single character, casting to a char would actually let you get away with switches and comparisons since it is primitive.