dimanche 4 avril 2021

Set paper rock scissors win rules without ton of if statments

I was working on a paper rock scissors challenge, and when I needed to set a function that decides the winner, I felt like writing a lot of if statements that includes every potential case is not practical at all. But after I run a small search, all the codes I had examined used if statements. Isn’t there any better way to do that ?

I will share the code I have below :

 //after the user clickes on scissor or paper or rock mymove function runs.
function mymove(choice) {
     let humanchoice = choice.id 
    //a while loop that runs till the computer choice is not the same as the user choice
    while (true){
         let pcnum = Math.random();
         console.log(pcnum);
         let pcchoie = 'paper';

        
         if(pcnum <= 0.33){
         pcchoie = "paper";
      
    } else if(pcnum <= 0.66){
         pcchoie = "scissor"; 
    }else {
         pcchoie = "rock"; 
       
    }
    console.log(pcchoie); 

    
    if(humanchoice == pcchoie){ console.log ("lag");}
    else if(humanchoice != pcchoie){ break;}
}

}

Now i need a way to set the rules to make the computer decide who is the winner.

Aucun commentaire:

Enregistrer un commentaire