Roping rocks and snipping paper with Codecadamy

So for the last two weeks or so I have been monkeying about with injecting some coding knowledge into my skill set (or something…). This has primarily been through the Stanford CS106A (Programming Methodology) course and lectures on Udemy/YouTube, and resuming my long-abandoned and meagre progress on Codecademy. I spent an inordinate amount of time fiddling with the Rock, Paper, Scissors exercise on the latter’s JavaScript track, and because I want to feel like it was worthwhile I’m dropping it here, after the jump.

roping-rocks-and-snipping-paper-with-codecadamy

Right then. I’ve no clue if this stuff depends on any particular libraries being present on the system running it or anything like that, I’m just copying the code right out of the Codecademy editor and sticking it here. Hurrah! If anyone reading this knows a thing or two about JavaScript or programming in general (or even if you know nothing at all, it’s cool), do let me know what you think of my scratchings. ^^


// declare global user and computer choice vars
var userChoice = null;
var computerChoice = null;

// make your picks
var makeChoices = function () {
    userChoice = prompt("Do you choose rock, paper or scissors? Or perhaps you'll hang yourself with a rope...").toLowerCase();
    computerChoice = Math.random();
    if (computerChoice <0.34) {
	    computerChoice = "rock";
    } else if (computerChoice <=0.67) {
	    computerChoice = "paper";
    } else if (computerChoice <=0.99) {
	    computerChoice = "scissors";
    } else {
        computerChoice = "rope";
    }
    console.log("You picked: " + userChoice);
    console.log("Computer picked: " + computerChoice);
};

// was the user a derp?
var validateUserChoice = function () {
    if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "rope") {
        console.log("User made an invalid choice! What a derp! Retrying...");
       return playGame();
    }
};

// user wasn't a derp, so compare the picks...
var compare = function (choice1, choice2) {
    if (choice2 === "rope") {
        if (choice1 === "rope") {
            return "rope"
        }
    }
   
    if (choice1 === choice2) {
        return "draw"
    }
    
	if (choice1 === "rock") {
		if (choice2 === "scissors") {
			return "Rock wins!";
			} else if (choice2 === "paper") {
				return "Paper wins!";
			}
	}
	
	if (choice1 === "paper") {
	    if (choice2 === "rock") {
	        return "Paper wins!"
	    } else if (choice2 === "scissors") {
	        return "Scissors wins!"
	    }
	}
	
	if (choice1 === "scissors") {
	    if (choice2 === "rock") {
	        return "Rock wins!"
	    } else if (choice2 === "paper") {
	        return "Scissors wins!"
	    }
	}
	
	if (choice1 === "rope") {
	    return choice2.substring(0,1).toUpperCase() + choice2.substring(1) + " wins. The opposition hung themselves!" 
	}
	
	if (choice2 === "rope") {
	    return choice1.substring(0,1).toUpperCase() + choice1.substring(1) + " wins. The opposition hung themselves!"
	}
};

var playGame = function () {
    makeChoices();
    validateUserChoice();
    
    if (compare(userChoice, computerChoice) === "draw") {
        console.log("It's a draw!");
        playAgain();
    } else if (compare(userChoice, computerChoice) === "rope") { 
    return "Everyone's been hung, and there's no one left to play...";
    console.log("Game over.")
    } else {
        console.log(compare(userChoice, computerChoice));
        playAgain();
    }
};

var playAgain = function () {
    if (confirm("Play another round?") == true) {
        return (playGame());
    } else {
        console.log("Game over.");
        return;
    }
}

playGame();

The game seems to play fine, as far as I've tested. It does feel a little nuts to have written 100 lines of code for something so simple, but I guess in the grand scheme of things that's pretty tiny. Not to mention all the extra space that all the best-practice spacing takes up.

If you'd actually like to try the code out, this Codecademy link will take you straight to the relevant exercise page. Once there, just copy my code above and stick it into the editor.

css.php