Refining the refined RPS experience!


#1



Ok, so first of all I would like to give credit to Codecademy user Zyphy. Their post ( https://www.codecademy.com/forum_questions/52d30b44548c35f5240040e4) directly inspired/ influenced me to add more functonality to my version of rock, paper, scissors, so a thank you goes out to this user for sharing their work. Also i would like to credit user leonhard.wettengmx.n for some excellent advice earlier today regarding an error I was experiencing ' TypeError: X is not a function'. Your time and wisdom is greatly appreciated.

Below is a list of the functions i've added to the basic game, as well as those I intend to add... I am not finished with this project however I feel it's only right that I share my work, so that others may learn from it. Heres the jsfiddle link for those that want it. https://jsfiddle.net/DDUk/59tbajd1/1/

Extra functions:
Main menu
Single/ Multiplayer
Ability to exit at any time simply by typing 'Exit' into a prompt.
A little 'easter egg'... seemed appropriate :slight_smile:
Best of 'x' (user choice for number of games, with score counter)

Functions to be added:
Ability to substitute player 2 for AI player, should player 2 decide not to play.

If anyone would like to make any suggestions on how I could/ should do things differently to improve my code then I emplore you to do so. Also if anyone spots anything not working correctly please let me know :slight_smile:

I would also like to reference a couple of websites I have used along the way to help me. These are:
JSHint.com, which proved invaluable for finding and correcting many syntax errors i encountered along the way,
and JSBeautifier.org, which i used to tidy my code before writing this post. Hopefully some other users will find these sites of use.

EDIT
Updated to v2.03, changelog below.
v2.00
started changelog
v2.01
converted to named functions
v2.02
fixed incorrect alert regarding incorrect entry for sp/mp
added single player best of function
added multiplayer best of function
added check for best of, so that number of games cannot produce a tie.
v2.03
various fixes for best of feature
rearranged menu/ title to fix unnecessary repeat actions


//***********************************************************/
//************************ MAIN MENU ************************/

function title() {
    console.log("Rock, Paper Scissors! by DDUk\n\nWelcome to RPSv2.03!\n");
    var ready = confirm("Are you ready?")
    if (ready === false) {
        console.log("You have chosen not to play, thanks anyway!");
           alert ("You have chosen not to play, thanks anyway!")
        return;}
    else {
        menu()}
};

function menu(){
    mainmenu = prompt("Would you like a 1 or 2 player game? (To exit the game type 'Exit')");
    if (mainmenu === "1") {
        maxgames = prompt ("How many rounds would you like to play?")
        maxgamescheck ()
        console.log("Game mode selected: Best of " + maxgames + ": Single Player!\n");
        rps()
            } else if (mainmenu === "2") {
            maxgames = prompt ("How many rounds would you like to play?")
            maxgamescheck()
            rps()
            console.log("Game mode selected: Best of " + maxgames + ": Multi Player!\n");
        name2 = prompt("Player 2, What is your name? (To exit the game type 'Exit')");
        multiplayer1()
        if (name2 === "Exit") {
            console.log("You have chosen not to play, thanks anyway!");
            alert ("You have chosen not to play, thanks anyway!")
            return;
        }
    } else if (mainmenu === "Exit") {
        console.log("You have chosen not to play, thanks anyway!");
           alert ("You have chosen not to play, thanks anyway!")
        return;
    } else {
        console.log("You cannot have that many players, please try again!");
           alert ("You cannot have that many players, please try again!")
        menu();
    }
    }
function rps(){
    name = prompt("Player 1, What is your name? (To exit the game type 'Exit')");
    if (name === "Exit") {
        console.log("You have chosen not to play, thanks anyway!");
           alert ("You have chosen not to play, thanks anyway!")
        return;
    }
    else {
    console.log("Ok, thank you " + name + ", Let's Play Rock, Paper Scissors!\n");}
    if (mainmenu === "1"){
        singleplayer()}
    else if (mainmenu === "2"){
        multiplayer1}
                };

title();

/***********************************************************/
//********************** SINGLE PLAYER **********************/
function compareSP(userChoice, choice2) {
    if (name === "Chuck Norris") {
        console.log("Ermm...It doesn't matter, you win, please don't hurt me!");
        alert ("Ermm...It doesn't matter, you win, please don't hurt me!");
    } else if (userChoice === choice2) {
        console.log("The result is a tie, please retry!\n");
        alert ("The result is a tie, please retry!")
        singleplayer();
    } else if (userChoice === "Rock" && choice2 === "Paper" || userChoice === "Paper" && choice2 === "Scissors" ||
        userChoice === "Scissors" && choice2 === "Rock") {
        console.log(choice2 + " beats " + userChoice + "\nComputer Wins!\n");
        alert("Computer Wins. Better luck next time!\n");
        player2WinCount++
    score ()
    } else {
        console.log(userChoice + " beats " + choice2 + "\n" + name + " Wins!\n");
        alert("Congratulations, you win!\n");
        player1WinCount++
        score ()
    }
};

function checkinputsp() {
    if (userChoice === "Rock" || userChoice === "Scissors" || userChoice === "Paper" || name === "Chuck Norris") {
        console.log(name + " chooses: " + userChoice);
        console.log("Computer Chooses: " + choice2 + "\n");
        compareSP(userChoice, choice2);
    } else if (userChoice === "Exit")
        return;
    else {
        alert("Invalid choice, please retry!");
        singleplayer();
    }
};
function singleplayer() {
    userChoice = prompt("Hi " + name + ", Do you choose Rock, Paper or Scissors? (To exit the game type 'Exit')");
    choice2 = Math.random(); {
        if (choice2 < 0.34) {
            choice2 = "Rock";
        } else if (choice2 <= 0.67) {
            choice2 = "Paper";
        } else {
            choice2 = "Scissors";
        }
        checkinputsp();
    }
};

    

//***********************************************************/
//********************** MULTI PLAYER ***********************/

function compareMP(userChoice, choice2) {
    if (userChoice === choice2) {
        console.log("The result is a tie, please retry!\n");
        alert ("The result is a tie, please retry!")
        multiplayer1();
    } else if (userChoice === "Rock" && choice2 === "Paper" || userChoice === "Paper" && choice2 === "Scissors" ||
        userChoice === "Scissors" && choice2 === "Rock") {
        console.log(choice2 + " beats " + userChoice + "\n" + name2 + " Wins!");
        alert("Congratulations " + name2 + ", you win!\n");
        player2WinCount++
        score ()
    } else {
        console.log(userChoice + " beats " + choice2 + "\n" + name + " Wins!");
        alert("Congratulations " + name + ", you win!\n");
        player1WinCount++
        score ()
    }
};
function checkinputmp1() {
    if (userChoice === "Rock" || userChoice === "Scissors" || userChoice === "Paper") {
        console.log(name + " chooses: " + userChoice);
        multiplayer2();
    } else if (userChoice === "Exit")
        return;
    else {
        alert("Invalid choice, please retry!");
        multiplayer1();
    }
};
function checkinputmp2() {
    if (choice2 === "Rock" || choice2 === "Scissors" || choice2 === "Paper") {
        console.log(name2 + " chooses: " + choice2 + "\n");
        compareMP(userChoice, choice2);
    } else if (choice2 === "Exit")
        return;
    else {
        alert("Invalid choice, please retry!");
        multiplayer2();
    }
};
function multiplayer1() {
    userChoice = prompt("Hi " + name + ", Do you choose Rock, Paper or Scissors? (To exit the game type 'Exit')");
    checkinputmp1();
};
function multiplayer2() {
    choice2 = prompt("Hi " + name2 + ", Do you choose Rock, Paper or Scissors? (To exit the game type 'Exit')");
    checkinputmp2();
};


//***********************************************************/
//************************** SCORE **************************/

var winscore = Math.ceil(maxgames/2) 
var maxgames = 0
var gameCount = 0;
var player1WinCount = 0;
var player2WinCount = 0;

function score (){
    if (player1WinCount === winscore) {console.log (name + " Wins " + player1WinCount + " - " + player2WinCount)
    alert ("\n" + name + " Wins " + player1WinCount + " - " + player2WinCount + " Congratulations!")
    return;}
    else if (player2WinCount === winscore && mainmenu === "1") {console.log ("Computer Wins " + player2WinCount + " - " + player1WinCount)
    alert ("\nComputer Wins " + player2WinCount + " - " + player1WinCount + " Better luck next time!")
    return;}  
    else if (player2WinCount === winscore && mainmenu === "2") {console.log (name2 + " Wins " + player2WinCount + " - " + player1WinCount)
    alert (name2 + " Wins " + player2WinCount + " - " + player1WinCount + " Better luck next time!")
    return;}
    else {
        if (mainmenu === "1"){
            singleplayer()}
        else if (mainmenu === "2") {
            multiplayer1()}
         }
}
function maxgamescheck (){
    if ((maxgames%2) === 0){
        alert ("This will result in a tie! Please try again")
        menu()
    }
}


#2

Very good.  


#3

Thank you, i appreciate your comment :slight_smile: