Next Steps (9)


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661

hey there,
eventhough this task is not a must but i do want to know how to input the step three:
"In this version, if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?"


var userChoice = prompt("Do you choose rock, paper, scissors, or rope?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
    return "The result is a tie!";
    
} 
else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return "rock wins";    
    } 
    else {
        return "paper wins";
    }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins"; 
        }
        else {
            return "scissors wins";
        }
        } 
        else if (choice1 === "scissors") {
            if (choice2 === "rock") {
                return "rock wins";
            } 
            else {
                return "scissors wins";
            }
        }
else {
    return "There's no such option sir!";
}
};

compare(userChoice, computerChoice);


#2
function Player(name, isComputer) {
    this.name = name;
    this.isComputer = isComputer;
}
Player.prototype.cast = function () {
    if (this.isComputer) {
        this.choice = choices[Math.floor(Math.random() * 3)];
    } else {
        this.choice = choice();
    }
};
var game = function (human, computer) {
    choices = ['rock', 'paper', 'scissors'];
    players = [];    
    choice = function () {
        var u;
        do {
            u = prompt(choices);
            if (u.match(/rock|paper|scissors/)) {
                return u;
            }
        } while (u !== null);
    };
    players.push(new Player(computer || "ET", true));
    players.push(new Player(human || "Me", false));
    while (true) {
        break;
    }
    console.log("Bye");
};

This is the,

sketch of a running game
function Player(name, isComputer) {
    this.name = name;
    this.isComputer = isComputer;
}
Player.prototype.cast = function () {
    if (this.isComputer) {
        this.choice = choices[Math.floor(Math.random() * 3)];
    } else {
        this.choice = choice();
    }
};

var game = function (human, computer) {
    choices = ['rock', 'paper', 'scissors'];
    players = [];    
    choice = function () {
        var u;
        do {
            u = prompt(choices);
            if (u.match(/rock|paper|scissors/)) {
                return u;
            }
        } while (u !== null);
    };
    players.push(new Player(computer || "ET", true));
    players.push(new Player(human || "Me", false));
    while (true) {
        break;
    }
    console.log("Bye");
};

with many pieces still missing. The real missing pieces are the concepts that are introduced in later topics. We really cannot go further on this if it interferes with your completion of the track, but when you have completed it definitely come back to this challenge.

 > 
=> [Function]
 > game()
 Bye
 > players[0].cast()
 > players[1].cast()
 > players
=> [ { name: 'ET', isComputer: true, choice: 'scissors' },
  { name: 'Me', isComputer: false, choice: 'paper' } ]
    

Hopefully this only intrigues you, and doesn’t come off as some condescending BS. It is meant to point you in a direction that you might not understand, just now, but will when you return.

 > game('Roy','A10')
 Bye
 > players[0].cast()
 > players[1].cast()
 > players
=> [ { name: 'A10', isComputer: true, choice: 'rock' },
  { name: 'Roy', isComputer: false, choice: 'rock' } ]    

#3

what a great length of code lol :joy:
will keep it until then, thx btw for your time mtf


#4

For you enjoyment, here is a working proof of concept

function Player(name, isComputer) {
    this.name = name;
    this.isComputer = isComputer;
}
Player.prototype.cast = function () {
    if (this.isComputer) {
        this.choice = choices[Math.floor(Math.random() * 3)];
    } else {
        this.choice = choice();
    }
};
var game = function (human, computer) {
    choices = ['rock', 'paper', 'scissors'];
    players = [];    
    choice = function () {
        var re = /^rock|^paper|^scissors/;
        var u;
        do {
            u = prompt(choices);
            if (u === null) return u;
            if (re.test(u)) return u;
            else u = "";
        } while (!u);
    };
    var compare = function (p) {
        var C = "Computer wins.";
        var H = "Human wins.";
        var c = p[0].choice;
        var h = p[1].choice;
        console.log("\nHuman:    " + h + "\nComputer: " + c);
        if (h === c) return "It's a tie!";
        if (h === 'rock') return c  === 'scissors' ? H : C;
        if (h === 'paper') return c === 'rock' ? H : C;
        if (h === 'scissors') return c === 'paper' ? H : C;
    };

    players.push(new Player(computer || "ET", true));
    players.push(new Player(human || "Me", false));
    while (true) {
        players[0].cast();
        players[1].cast();
        if (!players[1].choice) break;
        console.log(compare(players));
    }
    console.log("Bye");
};

To play, start by invoking the game() function with or without name parameters. Use Cancel or Esc to end the game.

When you are done the track and are ready to talk this over, we can analyze it critically for weaknesses, flaws, best practice, and so forth. It is a proof of concept only, at this stage and it never hurts to tear things down and rebuild a better version.


#5

Thanks for the masterpiece mtf,
hopefully soon i'll be able to reach your level of coding lol
(maybe not so soon) :grin:


#6

Revised ever so slightly to force constraints.

Code
// Rock, Paper, Scissors
// Repeated play proof of concept by Roy
// 

function Player(name, isComputer) {
    this.name = name;
    this.isComputer = isComputer;
}
Player.prototype.cast = function () {
    if (this.isComputer) {
        this.choice = choices[Math.floor(Math.random() * 3)];
    } else {
        this.choice = choice();
    }
};

var game = function (human, computer) {
    choices = ['rock', 'paper', 'scissors'];
    players = [];    
    choice = function () {
        var re = /^rock|^paper|^scissors/;
        var u;
        do {
            u = prompt(choices);
            if (u === null) return u;
            if (re.test(u)) return u;
            else u = "";
        } while (!u);
    };
    var compare = function (p) {
        var C = "Computer wins.";
        var H = "Human wins.";
        var c = p[0].choice;
        var h = p[1].choice;
        console.log("\nHuman:    " + h + "\nComputer: " + c);
        if (h === c) return "It's a tie!";
        if (h === 'rock') return c  === 'scissors' ? H : C;
        if (h === 'paper') return c === 'rock' ? H : C;
        if (h === 'scissors') return c === 'paper' ? H : C;
    };

    players.push(new Player(computer || "ET", true));
    players.push(new Player(human || "Me", false));
    while (true) {
        players[0].cast();
        players[1].cast();
        if (!players[1].choice) break;
        console.log(compare(players));
    }
    console.log("Bye");
};
game();

When we come back to this we will want to look at making a Game class that contains the presently global functions and variables. That’s a big flaw, right off the hop. No closure. Something to think on when the bridge comes up.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.