Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-18-2012, 03:55 PM   PM User | #16
piemanXD
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
piemanXD can only hope to improve
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
piemanXD is offline   Reply With Quote
Old 12-19-2012, 01:29 AM   PM User | #17
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-19-2012, 02:11 PM   PM User | #18
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is offline   Reply With Quote
Old 12-19-2012, 08:43 PM   PM User | #19
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-19-2012, 08:53 PM   PM User | #20
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-19-2012, 09:36 PM   PM User | #21
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-19-2012, 09:47 PM   PM User | #22
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-20-2012, 01:31 AM   PM User | #23
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-20-2012, 01:46 AM   PM User | #24
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:12 AM.


Advertisement
Log in to turn off these ads.