javascript - Values not returning correctly when playing Rock-Paper-Scissors type game -


this question has answer here:

i trying duplicate script wrote in python in javascript, however, i'm having issues. in both scripts set random range between zero(0) , five(5). find modular value. if value equals 1 of 2 numbers, message displayed. if value equals 1 of 2 other numbers, different message displayed. if value zero(0) message displayed. python script works way should. issue javascript version. i'm getting negative numbers when shouldn't. can explain why scripts aren't working same way?

here part 2 scripts should running same aren't.

py

def rpsls(player_choice):     player_number = name_to_number(player_choice)     print ""     print "player chooses", number_to_name(player_number)      comp_number = random.randrange(0, 5)       print "computer chooses", number_to_name(comp_number)      difference = (comp_number - player_number) % 5      if((difference == 3) or  (difference == 4)):         print "player wins!"     elif((difference == 1) or (difference == 2)):         print "computer wins!"     else:         print "it tie!" 

js

function numbertoname(computer_throw){                 if(computer_throw === 0){                     computer_throw = "rock";                 }                  else if(computer_throw === 1){                     computer_throw = "paper";                 }                  else if(computer_throw === 2){                     computer_throw = "scissors";                 }                  else if(computer_throw === 3){                     computer_throw = "lizard";                 }                  else{                     computer_throw = "spock";                 }                  return computer_throw;             }              function nametonumber(user_throw){                 if(user_throw == "rock"){                     user_throw = 0;                 }                  else if(user_throw == "paper"){                     user_throw = 1;                 }                  else if(user_throw == "scissors"){                     user_throw = 2;                 }                  else if(user_throw == "lizard"){                     user_throw = 3;                 }                  else if(user_throw == "spock"){                     user_throw = 4;                 }                  else{                     console.log("please pick throw.");                 }                  return user_throw;             }              function game(thrw){                 var computer_throw = math.floor((math.random() * 5));                 thrw = nametonumber(thrw);                 var difference = math.abs((computer_throw - thrw) % 5);                 if((difference === 3) || (difference === 4)){                     console.log("player wins!");                 }                 else if((difference === 1) || (difference === 2)){                     console.log("computer wins!");                 }                 else{                     console.log("it tie!");                 }                  $("#result").append("<p>player <strong>" +numbertoname(thrw) +"</strong></p>");                 $("#result").append("<p>comp <strong>" +numbertoname(computer_throw) +"</strong></p>");                  console.log(difference);             }              $("#start").on("click", function(){                 $("#result").children().remove();                 var user_throw = $("#throw").val();                 //game("rock");                 //game("paper");                 //game("scissors");                 //game("lizard");                 //game("spock"); 

as can see, 2 functions identical , should working same way, they're not. have ideas why? these links working versions.

python script

javascript script

as joe frambach pointed out in comments answer, have had winning cases backwards, turns out wasn't either. here's what's happening. if set player throw rock, following cases , values.

difference-player v computer win respectively

player throws rock: rvr: 0-t, rvp: 1-c, rvs: 2-p, rvl: 3-p, rvspk: 4-c

if change user throw else, following results.

player throws paper: pvr: 1-p, pvp: 0-t, pvs: 1-c, pvl: 2-c, pvspk: 3-p

player throws scissors: svr: 2-c, svp: 1-p, svs: 0-t, svl: 1-p, svspk: 2-c

player throws lizard: lvr: 3-c, lvp: 2-p, lvs: 1-c, lvl: 0-t, lvspk: 1-p

player throws spock: spkvr: 4-p, spkvp: 3-c, spkvs: 2-p, spkvl: 1-c, spkvspk: 0-t

as can see, values aren't matching correctly , 1 of numbers being replaced. when user plays paper, same value set vs. rock , vs. scissors. cause conflict in if statement. value of 1 has both players winning, whereas in logic, value of 1 should have computer winning. holds true of other throws. can tell me how fix this

a simple hack not negative numbers:

(5 + computer_throw - thrw) % 5; 

if computer_throw - thrw @ least -5, adding 5 wrap around 0. in modular arithmetic equivalent.


why not math.abs?

consider ring:

'rock', 'paper', 'scissors', 'lizard', 'spock', 'rock', 'paper', 'scissors', 'lizard', 'spock' 

and consider user , rng pick these:

'rock', 'paper', 'scissors', 'lizard', 'spock', 'rock', 'paper', 'scissors', 'lizard', 'spock'                     ^ rng                         ^user 

if abs difference, we'll 3.

now consider user , rng pick these:

'rock', 'paper', 'scissors', 'lizard', 'spock', 'rock', 'paper', 'scissors', 'lizard', 'spock'                     ^ user                         ^rng 

if abs difference, we'll 3 again, not correct answer! should thought of this:

'rock', 'paper', 'scissors', 'lizard', 'spock', 'rock', 'paper', 'scissors', 'lizard', 'spock'   ^ rng             ^ user 

where correct difference 2. follow if-else logic, , see 2 corresponds rng winning.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -