Name:
Anonymous
2012-11-13 1:47
I'm practicing JS by trying to make a rock, paper, scissor game.
I have failed.
Can /prog/ help a nigga out?
http://pastebin.com/2Sq06a0c
Everything is appreciated, just keep in mind I'm new as fuck to programming.
Name:
Anonymous
2012-11-14 3:28
var choices = {"rock" : 1, "paper" : 4, "scissors": 3};
var choicesBackward = {1 : "rock", 4 : "paper", 3 : "scissors"};
var choicesArray = ["rock", "paper", "scissors"];
function player(name, playFunction) {
this.choice = 0;
this.name = name;
this.winCount = 0;
this.play = playFunction;
};
function getWinner(player1, player2) {
var result;
result = player1.choice - player2.choice;
if(player1.choice == player2.choice)
return "tie";
if(result == -2 || result == 3 || result == -1)
{
player1.winCount += 1;
return player1;
}
else
{
player2.winCount += 1;
return player2;
}
}
function playAndGetResults(player1, player2) {
var result;
player1.choice = player1.play();
player2.choice = player2.play();
console.log(player1.name + " chose " + choicesBackward[player1.choice]);
console.log(player2.name + " chose " + choicesBackward[player2.choice]);
result = getWinner(player1, player2);
if(result == "tie")
console.log("tie");
else
console.log("player " + result.name + " won with " + choicesBackward[result.choice]);
}
function botSelectChoice() {
var choicesArrayIndex;
choicesArrayIndex = Math.floor(Math.random() * 10 % 3);
return choices[choicesArray[choicesArrayIndex]];
}
function main() {
var player1 = new player("Alice", botSelectChoice);
var player2 = new player("Bob", botSelectChoice);
playAndGetResults(player1, player2);
}
paste it into your interpreter, and just call main() over and over