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.
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ever have an extended period of really stressful times? Well look, I'm not normally a dick like that but I'm just stressed out, and I know that doesn't give me the right to say anything like I did but hey, that's why I did it. I want to apologize to the guy who wrote out the new code for me only to have me throw it back in his face - so I'm sorry! I also want to apologize to the community at large but I guess I couldn't really have gotten off on a worst foot. Anyone who actually wants to see what I said can see it quoted in one of the posts above... once again, sorry guys and especially sorry to oldpedant.

    TLDR; I'm a hot headed idiot and I apologize for that fact

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    LOL, at least you edited your posts.

    As I said, you almost surely can't show my code to your instructor.

    (1) He/she won't believe you wrote it.
    (2) He/she quite possibly won't even understand it.

    If you have the typical college instructor who only knows what is in the book and has never actually programmed for a living, he/she won't even realize that the book is 10 years or more out of date.

    *********

    Now, having said all the above...

    You could *still* use the CONCEPT that I showed in that code in your own code.

    Look carefully at how I decided who had won. All very simple. Just one big set of && and || conditions. Yes, you can use those same conditions in your own code.

    Given that you apparently *must* use ugly alert( )s to output your messages, consider something like this:
    Code:
         var msg = "Player plays " + userInput + ", Computer plays " + cpuInput;
         if ( userInput == cpuInput ) { msg += ", result is a DRAW"; }
         else if ( .... see my post for the concept ... ) { msg += ", PLAYER wins"; }
         else { msg += ", COMPUTER wins; }
         alert( msg );
    See the idea? Don't try to output the message all in one go. Build it up from pieces.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    another approach that you probably can't use but you might want to look at:
    Code:
    <script>
    var user=true;
    
    var cpuInput = ["rock","paper","scissors"][Math.floor(Math.random()*3)];
    var opts={
    rock:{rock:"draw",paper:"win",scissors:"lose"},
    paper:{rock:"lose",paper:"draw",scissors:"win"},
    scissors:{rock:"win",paper:"lose",scissors:"draw"}
    }
    
    while(user){
    user=prompt("please choose rock, paper or scissors","");
    var txt=user?"Computer chose "+cpuInput+", you chose "+user+". You "+opts[cpuInput][user]:"game over";
    alert(txt) 
    }
    </script>

  4. #19
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    LOL! I almost gave him that, Xelawho, in my first post But I decided that it would confuse him even worse than what I did show.

    *** BUT ***

    But you have a HUGE goof in that code!

    As written, the computer will make ONE CHOICE FOR ALL TIME.

    So it's pretty easy for the player to win all games except the first one.

    You need to move the var cpuInput = ... line *inside* the while loop.

    Also, you need to provide for the case when the user types in *something* in response to the prompt() but not a valid answer.

    So I would turn the opts around and have the player as the "outer" set of names, to make it easy to check to see if the choice is valid.

    Maybe something like this:
    Code:
    <script>
    var opts={
        rock: /* player chose rock */
              { rock :    "tied",  /* computer chose rock */
                paper:    "lose",  /* computer chose paper */
                scissors: "win"    /* computer chose scissors */
              },
        paper: 
              { rock:     "win",
                paper:    "tied",
                scissors: "lose" 
              },
        scissors:
             {  rock:     "lose",
                paper:    "win",
                scissors: "tied"
             }
    };
    
    while( true )
    {
        var user = prompt("please choose rock, paper or scissors","");
        if ( user == "" ) break;
        if ( opts[user] == null ) 
        {
            alert("invalid choice");
        } else {
            var cpuInput = ["rock","paper","scissors"][Math.floor(Math.random()*3)];
            alert( "Computer chose "+cpuInput+", you chose "+user+". You "+opts[user][cpuInput] );
        }
    }
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  5. #20
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    If you want to make it more compact and unreadable:
    Code:
    <script>
    var opts={rock:{rock:"tied",paper:"lose",scissors: "win"},
              paper:{rock:"win",paper:"tied",scissors: "lose"},
              scissors:{rock:"lose",paper:"win",scissors: "tied"}
        };
    while( true )
    {
        if ( (u = prompt("please choose rock, paper or scissors",""))=="") break;
        alert( opts[u] ? "Computer chose "+(c=["rock","paper","scissors"][Math.floor(Math.random()*3)])
                         +", you chose "+u+". You "+opts[u][c]                
                       : "invalid choice"
             );
    }
    </script>
    (Could have made it even more unreadable, of course, but this seems bad enough.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  6. #21
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by Old Pedant View Post
    If you want to make it more compact and unreadable:
    Code:
    <script>
    var opts={rock:{rock:"tied",paper:"lose",scissors: "win"},
              paper:{rock:"win",paper:"tied",scissors: "lose"},
              scissors:{rock:"lose",paper:"win",scissors: "tied"}
        };
    while( true )
    {
        if ( (u = prompt("please choose rock, paper or scissors",""))=="") break;
        alert( opts[u] ? "Computer chose "+(c=["rock","paper","scissors"][Math.floor(Math.random()*3)])
                         +", you chose "+u+". You "+opts[u][c]                
                       : "invalid choice"
             );
    }
    </script>
    (Could have made it even more unreadable, of course, but this seems bad enough.)
    That doesn't look unreadable to me. It looks like concise properly written JavaScript - except that in real JavaScript the prompt an alert calls would be replaced by a form and an innerHTML call respectively - which would also do away with the while loop as each "loop" would be triggered by submitting the form.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  7. #22
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...Felgall, where do you see var in that code?

    To me, that makes it ugly.

    And then I really think that the alert( opts[u] ? ... ); is too long and ugly. I think there is a time and place for the ternary operator, but this place just makes the code ugly. And probably unreadable for novices.

    And even if it's not unreadable, it's ugly. My opinion, only.

    Anyway, Xelawho and I are just having fun now. I gave an answer I think you'd approve of earlier. in post #8.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  8. #23
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...Felgall, where do you see var in that code?
    In the part that would still be there once you got rid of the prompt and alert.

    With what I said needed to be replaced to convert it to real Javascript all that would be left of the code would be the one statement starting var opts = ... and that the rest of the code will have been completely replaced in getting rid of the prompt and alert.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  9. #24
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Oh, is *THAT* what you meant!

    Then you aren I are in violent agreement! <grin/>

    Sorry for misunderstanding you.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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