/    Sign up×
Community /Pin to ProfileBookmark

I am working on a game and need to find a way to have the game run 3 times and print out the score for each round and declare a winner after three rounds. I have a good start but need some help. Here is the Java Script code: Please help.

$(document).ready(function(e){

var com, choice;
//this should only allow the process to run 3 times

//this will make the computer pick their weapon

function comPick() {

var pick = parseInt(Math.random()*6);

if ( pick == 1) {
com = “rock”;
}
else if ( pick == 2) {
com = “paper”;
}
else if ( pick == 3){
com = “scissors”;
}
else if ( pick == 4){
com = “lizzard”;
}
else if ( pick == 5){
com = “spock”;
}
}
//this will let the player pick their weapon

$(‘#rock’).click(function(e){
choice = “rock”;
comPick();
Winner();
return false;
});
$(‘#paper’).click(function(e){
choice = “paper”;
comPick();
Winner();
return false;
});

$(‘#scissors’).click(function(e){
choice = “scissors”;
comPick();
Winner();
return false;
});
$(‘#lizzard’).click(function(e){
choice = “lizzard”;
comPick();
Winner();
return false;
});
$(‘#spock’).click(function(e){
choice = “spock”;
comPick();
Winner();
return false;
});

//this will find the winner and print that out along with points added to total
function Winner(){
var line1, line2, winner, p1 = 0, p2 = 0;

if (choice == “rock” && com == “paper”){
winner = “You loose.”;
$(‘#line1’).html(“You picked rock”);
$(‘#line2’).html (“The computer picked paper, ” + winner);
p2 = p2 + 1;
}
else if (choice == “paper” && com == “scissors”){
winner = “You loose.”;
$(‘#line1’).html(“You picked paper.” );
$(‘#line2’).html(“The computer picked scissors, ” + winner);
p2 = p2 + 1;
}
else if (choice == “scissors” && com == “rock”){
winner = “You loose.”;
$(‘#line1’).html(“You picked scissors.” );
$(‘#line1’).html(” The computer picked rock, ” + winner);
p2 = p2 + 1;
}
else if (choice == “lizzard” && com == “rock”){
winner = “You loose.”;
$(‘#line1’).html(“You picked lizzard.”);
$(‘#line2’).html(“The computer picked rock, ” + winner);
p2 = p2 + 1;
}
else if (choice == “spock” && com == “lizzard”){
winner = “You loose.”;
$(‘#line1’).html(“You picked Spock.”);
$(‘#line2’).html(“The computer picked lizzard, ” + winner);
p2 = p2 + 1;
}
else if (choice == “scissors” && com == “spock”){
winner = “You loose.”;
$(‘#line1’).html(“You picked scissors.”);
$(‘#line2’).html(“The computer picked Spock, ” + winner);
p2 = p2 + 1;
}
else if (choice == “lizzard” && com == “scissors”){
winner = “You loose.”;
$(‘#line1’).html(“You picked lizzard.”);
$(‘#line2’).html(“The computer picked scissors, ” + winner);
p2 = p2 + 1;
}
else if (choice == “paper” && com == “lizzard”){
winner = “You loose.”;
$(‘#line1’).html(“You picked paper.”);
$(‘#line2’).html(“The computer picked lizzard, ” + winner);
p2 = p2 + 1;
$(‘#p2’).val(p2);
}
else if (choice == “spock” && com == “paper”){
winner = “You loose.”;
$(‘#line1’).html(“You picked Spock.”);
$(‘#line2’).html(“The computer picked paper, ” + winner);
p2 = p2 + 1;
}
else if (choice == “rock” && com == “spock”){
winner = “You loose.”;
$(‘#line1’).html(“You picked rock.”);
$(‘#line2’).html(“The computer picked Spock, ” + winner);
p2 = p2 + 1;
}
else if (choice == “rock” && com == “scissors”){
winner = “You win.”;
$(‘#line1’).html(“You picked rock.”) ;
$(‘#line2’).html(“The computer picked scissors, ” + winner);
p1 = p1 + 1;
}
else if (choice == “paper” && com == “rock”){
winner =”You win.”;
$(‘#line1’).html(“You picked paper.” );
$(‘#line2’).html(“The computer picked rock, ” + winner);
p1 = p1 + 1;
}
else if (choice == “scissors” && com == “paper”){
winner = “You win.”;
$(‘#line1’).html (“You picked scissors.” );
$(‘#line2’).html(“The computer picked paper, ” + winner);
p1 = p1 + 1;
}
else if (choice == “rock” && com == “lizzard”){
winner= “You win.”;
$(‘#line1’).html(“You picked rock.”);
$(‘#line2’).html(“The computer picked lizzard, ” + winner);
p1 = p1 + 1;
}
else if (choice == “lizzard” && com == “spock”){
winner= “You win.”;
$(‘#line1’).html(“You picked lizzard.”);
$(‘#line2’).html(“The computer picked Spock, ” + winner);
p1 = p1 + 1;
}
else if (choice == “spock” && com == “scissors”){
winner= “You win.”;
$(‘#line1’).html(“You picked Spock.”);
$(‘#line2’).html(“The computer picked scissors, ” + winner);
p1 = p1 + 1;
}
else if (choice == “scissors” && com == “lizzard”){
winner= “You win.”;
$(‘#line1’).html(“You picked scissors.”);
$(‘#line2’).html(“The computer picked lizzard, ” + winner);
p1 = p1 + 1;
}
else if (choice == “lizzard” && com == “paper”){
winner= “You win.”;
$(‘#line1’).html(“You picked lizzard.”);
$(‘#line2’).html(“The computer picked paper, ” + winner);
p1 = p1 + 1;
}
else if (choice == “paper” && com == “spock”){
winner= “You win.”;
$(‘#line1’).html(“You picked paper.”);
$(‘#line2’).html(“The computer picked Spock, ” + winner);
p1 = p1 + 1;
}
else if (choice == “spock” && com == “rock”){
winner= “You win.”;
$(‘#line1’).html(“You picked Spock.”);
$(‘#line2’).html(“The computer picked rock, ” + winner);
p1 = p1 + 1;
}
else{
winner = “it’s a tie”;
$(‘#line1’).html(“You picked ” + choice + “.”);
$(‘#line2’).html(“The computer picked ” + com + “, ” + winner);
}
}

function update(){
$(‘#p1’).html(p1);
$(‘#p2’).html(p2);
setTimeOut(‘update()’,1000)

}

});

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@PadonakNov 02.2013 — try this

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;title&gt;R | S | P&lt;/title&gt;
&lt;style&gt;
body{text-align:center;padding-top:150px;}
a:active,a:link,a:visited{text-decoration:none;color:Navy;}
a:hover{text-decoration:none;color:Crimson;}
#buttons{margin-bottom:50px;}
#buttons a{font-weight:bold;margin-left:10px;margin-right:10px;}
#drop{cursor:pointer;display:block;margin-top:15px;}
#rnd{position:absolute;top:20px;right:30px;}
#rnd span{color:Crimson;}
#score{padding:20px 20px;}
#score span{margin-left:25px;margin-right:25px;font-weight:bold;letter-spacing:1px;}
#total{position:absolute;top:20px;left:30px;}
#total span{margin-left:10px;margin-right:10px;}
&lt;/style&gt;
&lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function(e){
var com,choice,rounds=0,p1=0,p2=0,you=0,comp=0;
$('#buttons a').each(function(){
//this will set the id attribute for each button according to its text
$(this).attr('id',$(this).text());
//this will let the player pick their weapon
$(this).click(function(){
//this should only allow the process to run 3 times
if(rounds &lt; 3){rounds++;choice=$(this).text();comPick();Winner();}
return false;
});
});
//this will make the computer pick their weapon
function comPick() {
var pick=parseInt(Math.random()*6);
switch(pick){
case 1:com="rock";break;
case 2:com="paper";break;
case 3:com="scissors";break;
case 4:com="lizzard";break;
case 5:com="spock";break;
}
}
//this will find the winner and print that out along with points added to total
function Winner(){
var winner;
if(choice == "rock" &amp;&amp; com == "paper"){winner="You loose.";p2++;}
if(choice == "paper" &amp;&amp; com == "scissors"){winner="You loose.";p2++;}
if(choice == "scissors" &amp;&amp; com == "rock"){winner="You loose.";p2++;}
if(choice == "lizzard" &amp;&amp; com == "rock"){winner="You loose.";p2++;}
if(choice == "spock" &amp;&amp; com == "lizzard"){winner="You loose.";p2++;}
if(choice == "scissors" &amp;&amp; com == "spock"){winner="You loose.";p2++;}
if(choice == "lizzard" &amp;&amp; com == "scissors"){winner="You loose.";p2++;}
if(choice == "paper" &amp;&amp; com == "lizzard"){winner="You loose.";p2++;}
if(choice == "spock" &amp;&amp; com == "paper"){winner="You loose.";p2++;}
if(choice == "rock" &amp;&amp; com == "spock"){winner="You loose.";p2++;}
if(choice == "rock" &amp;&amp; com == "scissors"){winner="You win.";p1++;}
if(choice == "paper" &amp;&amp; com == "rock"){winner="You win.";p1++;}
if(choice == "scissors" &amp;&amp; com == "paper"){winner="You win.";p1++;}
if(choice == "rock" &amp;&amp; com == "lizzard"){winner="You win.";p1++;}
if(choice == "lizzard" &amp;&amp; com == "spock"){winner="You win.";p1++;}
if(choice == "spock" &amp;&amp; com == "scissors"){winner="You win.";p1++;}
if(choice == "scissors" &amp;&amp; com == "lizzard"){winner="You win.";p1++;}
if(choice == "lizzard" &amp;&amp; com == "paper"){winner="You win.";p1++;}
if(choice == "paper" &amp;&amp; com == "spock"){winner="You win.";p1++;}
if(choice == "spock" &amp;&amp; com == "rock"){winner="You win.";p1++;}
else{winner="it's a tie";}
$('#line1').html('You picked &lt;b&gt;'+choice+'&lt;/b&gt;.');
$('#line2').html('The computer picked &lt;b&gt;'+com+'&lt;/b&gt;. '+winner);
upd(p1,p2);
}

function dropScore(){
$('#score').append('&lt;input id="drop" type="button" value="Play again" /&gt;');
$('#drop').click(function(){rounds=0;$('#line1').empty();$('#line2').empty();p1=0;p2=0;upd(p1,p2);$(this).fadeOut('normal',function(){$(this).remove();});});
}

function upd(p1,p2){
$('#score').html('').html('&lt;span id="p1"&gt;&lt;/span&gt;&lt;span id="p2"&gt;&lt;/span&gt;');
$('#p1').html('You: '+p1);
$('#p2').html('Computer: '+p2);
$('#rnd').html('&lt;h2&gt;Rounds played: &lt;span&gt;'+rounds+'&lt;/span&gt;&lt;/h2&gt;');
if(rounds == 3){
var who='';
if(p1&gt;p2){who='You win!';you++;}
if(p1&lt;p2){who='Computer wins!';comp++;}
if(p1==p2){who='Fidel Castro wins!';}
$('#score').append('&lt;h1&gt;'+who+'&lt;/h1&gt;');
$('#total').html('&lt;h4&gt;Total score:&lt;/h4&gt;&lt;span&gt;&lt;b&gt;You&lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt;Computer&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;b&gt;['+you+']&lt;/span&gt;:&lt;span&gt;['+comp+']&lt;/b&gt;&lt;/span&gt;');
var sp1=$('#total span:first'),sp2=sp1.next();
if(you&gt;comp){sp1.css('color','Green');sp2.css('color','Crimson');}
if(you&lt;comp){sp1.css('color','Crimson');sp2.css('color','Green');}
if(you==comp){sp1.css('color','Darkorange');sp2.css('color','Darkorange');}
dropScore();
}
}

upd(p1,p2);
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;div id="total"&gt;&lt;/div&gt;
&lt;div id="rnd"&gt;&lt;/div&gt;
&lt;div id="buttons"&gt;&lt;a href=""&gt;rock&lt;/a&gt; | &lt;a href=""&gt;paper&lt;/a&gt; | &lt;a href=""&gt;scissors&lt;/a&gt; | &lt;a href=""&gt;lizzard&lt;/a&gt; | &lt;a href=""&gt;spock&lt;/a&gt;&lt;/div&gt;
&lt;div id="line1"&gt;&lt;/div&gt;
&lt;div id="line2"&gt;&lt;/div&gt;
&lt;div id="score"&gt;&lt;/div&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @bziingler spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 10.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @ddiebold17,
tipped: article
amount: 1000 SATS,

tipper: @Mqlinka19,
tipped: live stream
amount: 4900 SATS,

tipper: @Mqlinka19,
tipped: article
amount: 10 SATS,
)...