www.webdeveloper.com
Results 1 to 2 of 2

Thread: bzingler

  1. #1
    Join Date
    Nov 2013
    Posts
    1

    bzingler

    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)

    }

    });

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,653
    try this

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>R | S | P</title>
    <style>
    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;}
    </style>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
    $(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 < 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" && com == "paper"){winner="You loose.";p2++;}
    if(choice == "paper" && com == "scissors"){winner="You loose.";p2++;}
    if(choice == "scissors" && com == "rock"){winner="You loose.";p2++;}
    if(choice == "lizzard" && com == "rock"){winner="You loose.";p2++;}
    if(choice == "spock" && com == "lizzard"){winner="You loose.";p2++;}
    if(choice == "scissors" && com == "spock"){winner="You loose.";p2++;}
    if(choice == "lizzard" && com == "scissors"){winner="You loose.";p2++;}
    if(choice == "paper" && com == "lizzard"){winner="You loose.";p2++;}
    if(choice == "spock" && com == "paper"){winner="You loose.";p2++;}
    if(choice == "rock" && com == "spock"){winner="You loose.";p2++;}
    if(choice == "rock" && com == "scissors"){winner="You win.";p1++;}
    if(choice == "paper" && com == "rock"){winner="You win.";p1++;}
    if(choice == "scissors" && com == "paper"){winner="You win.";p1++;}
    if(choice == "rock" && com == "lizzard"){winner="You win.";p1++;}
    if(choice == "lizzard" && com == "spock"){winner="You win.";p1++;}
    if(choice == "spock" && com == "scissors"){winner="You win.";p1++;}
    if(choice == "scissors" && com == "lizzard"){winner="You win.";p1++;}
    if(choice == "lizzard" && com == "paper"){winner="You win.";p1++;}
    if(choice == "paper" && com == "spock"){winner="You win.";p1++;}
    if(choice == "spock" && com == "rock"){winner="You win.";p1++;}
    else{winner="it's a tie";}
    $('#line1').html('You picked <b>'+choice+'</b>.');
    $('#line2').html('The computer picked <b>'+com+'</b>. '+winner);
    upd(p1,p2);
    }
    
    function dropScore(){
    $('#score').append('<input id="drop" type="button" value="Play again" />');
    $('#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('<span id="p1"></span><span id="p2"></span>');
    $('#p1').html('You: '+p1);
    $('#p2').html('Computer: '+p2);
    $('#rnd').html('<h2>Rounds played: <span>'+rounds+'</span></h2>');
    if(rounds == 3){
    var who='';
    if(p1>p2){who='You win!';you++;}
    if(p1<p2){who='Computer wins!';comp++;}
    if(p1==p2){who='Fidel Castro wins!';}
    $('#score').append('<h1>'+who+'</h1>');
    $('#total').html('<h4>Total score:</h4><span><b>You</b></span><span><b>Computer</b></span><br /><span><b>['+you+']</span>:<span>['+comp+']</b></span>');
    var sp1=$('#total span:first'),sp2=sp1.next();
    if(you>comp){sp1.css('color','Green');sp2.css('color','Crimson');}
    if(you<comp){sp1.css('color','Crimson');sp2.css('color','Green');}
    if(you==comp){sp1.css('color','Darkorange');sp2.css('color','Darkorange');}
    dropScore();
    }
    }
    
    upd(p1,p2);
    });
    </script>
    </head>
    <body>
    <center>
    <div id="total"></div>
    <div id="rnd"></div>
    <div id="buttons"><a href="">rock</a> | <a href="">paper</a> | <a href="">scissors</a> | <a href="">lizzard</a> | <a href="">spock</a></div>
    <div id="line1"></div>
    <div id="line2"></div>
    <div id="score"></div>
    </center>
    </body>
    </html>
    Last edited by Padonak; 11-02-2013 at 02:54 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles