Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ticTacToe(){
- function game(wantToPlay){
- if (wantToPlay=="Yes"||wantToPlay=="yes"){
- var userChoice = prompt("Do you choose rock, paper or scissors?");
- //gets a random number
- var computerChoice = Math.random();
- //assigns that random number to be either rock, paper or scissors based on the number
- if (computerChoice < .34) {
- computerChoice = "rock";}
- else if(computerChoice <= .66) {
- computerChoice = "paper";}
- else {
- computerChoice = "scissors";
- }
- console.log("computerChoice: " + computerChoice);
- console.log("userChoice: " + userChoice);
- //compares the computer choice to the user choice and then prints out the outcome on the page.
- function compare(choice1,choice2){
- console.log(choice1 + "<-----User");
- console.log(choice2 + "<-----Computer");
- if (choice1==choice2){
- alert("It was a tie!");
- game("yes");
- }
- if (choice1=="rock"){
- if (choice2=="scissors"){
- document.getElementById("messages").innerHTML="";
- document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
- document.getElementById("loss").innerHTML="";
- console.log("wins " + wins);
- }
- if (choice2 =="paper"){
- document.getElementById("messages").innerHTML="";
- document.getElementById("win").innerHTML="";
- document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
- console.log("losses " + loss);
- }
- /**else{
- document.getElementById("messages").innerHTML="";
- document.getElementById("loss").innerHTML="You lose. Rock crushes scissors";
- document.getElementById("win").innerHTML="";
- }**/
- }
- else if (choice1=="paper"){
- if (choice2=="rock"){
- document.getElementById("messages").innerHTML="";
- document.getElementById("win").innerHTML="You win. Paper smothers rock.";
- document.getElementById("loss").innerHTML="";
- }
- if (choice2 =="scissors"){
- document.getElementById("messages").innerHTML="";
- document.getElementById("win").innerHTML="";
- document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
- }
- /** else{
- document.getElementById("messages").innerHTML="";
- document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
- document.getElementById("win").innerHTML="";
- }**/
- }
- else if (choice1=="scissors"){
- if (choice2=="paper"){
- document.getElementById("messages").innerHTML="";
- document.getElementById("win").innerHTML="You win. Scissors cut paper.";
- document.getElementById("loss").innerHTML="";
- }
- if (choice2 =="rock"){
- document.getElementById("messages").innerHTML="";
- document.getElementById("win").innerHTML="";
- document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
- }
- /** else{
- document.getElementById("messages").innerHTML="";
- document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
- document.getElementById("win").innerHTML="";
- }**/
- }
- else{
- alert("Very funny. Read the help menu and do it right.");
- game("yes");
- }
- };
- compare(userChoice,computerChoice);
- }
- //this only runs if the user does not type yes
- else{
- document.getElementById("messages").innerHTML="Well alrighty then.";
- document.getElementById("loss").innerHTML="";
- document.getElementById("win").innerHTML="";
- }
- }
- //promts the start of the game and loops x amount of times based on user input.
- var start = prompt ("Do you want to play?","Yes");
- var i = prompt("How many times do you want to play?", 5);
- for (n = 0; n < i; n ++){
- game(start);
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement