Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post 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:
    Attached Files Attached Files

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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);
    for your case should be
    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

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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();
    
            }
        }

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I keep forgetting you can't compare strings like that in Java :P

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •