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,716
    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 01:54 PM.
    signature under construction

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.33579 seconds
  • Memory Usage 2,892KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates