/    Sign up×
Community /Pin to ProfileBookmark

Rock,Paper,Scissors Help :)

Hello all, I have been assigned with a class ICT project to create a basic game using javascript.
I have decided to do a Rock, Paper, Scissors game. I need to include IF statements, a WHILE loop and 8 prompts and 8 alerts.

I just wanted to see if anyone could start me in the right direction with some basic code or if anyone has any good links to tutorials for a basic javascript game (Year 9)

Thankyou

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@mityaNov 12.2009 — Seems odd that you have been dictated to regarding what code you have to use. Why would anyone need 8 prompts - or even 8 alerts - for rock, paper, scissors? They'd just be extraneous.

Here 'ya go - wrote this for you as I'm off work ill today. Buy me a pint some day.

[CODE]//declare valid options, each with an array detailing what beats it and loses to it
var options = [
{item: 'rock', losesTo: 'paper', beats: 'scissors'},
{item: 'paper', losesTo: 'scissosrs', beats: 'rock'},
{item: 'scissors', losesTo: 'rock', beats: 'paper'}
]

//prepare function to ask for user's choice
function doPrompt(badChoice) {
var msg = '';
if (badChoice) msg += "'"+badChoice+"' is not a valid choice.n";
msg += "Choose an option - rock, paper or scissors";
return prompt(msg, "").toLowerCase();
}

//ask the user for his choice, ensuring it's a legal choice (keep prompting if not)
var choice, isValidChoice;
do {
choice = doPrompt(isValidChoice == false ? choice : null);
for (var i in options) {
if (choice == options[i].item) { isValidChoice = true; break; }
}
if (isValidChoice != true) isValidChoice = false;
} while(isValidChoice != true);

//randomise a choice for the computer player
var computerChoice = options[Math.floor(Math.random()*options.length)].item;

//now compare. Look at the row in our options array to decipher how the two choices relate to each other
var result;
for (var u in options) {
if (options[u].item == choice) {
if (options[u].losesTo == computerChoice)
result = 'you lose';
else if (options[u].beats == computerChoice)
result = 'you win';
else
result = 'draw';
break;
}
}

//report back
alert("Result: "+result+"nnYour choice: "+choice+"nvs.nComputer choice: "+computerChoice);[/CODE]
Copy linkTweet thisAlerts:
@wicked765authorNov 12.2009 — Ah! Didn't expect a reply so soon ?

Thankyou very much Mitya!This will really help me, i really appreciate it ? You must be good to throw code like that together in such a short amount of time, thanks again.

Oh and yes I do owe you a pint

Thanks again for your time ?
Copy linkTweet thisAlerts:
@mityaNov 12.2009 — No probs.
Copy linkTweet thisAlerts:
@dorishallAug 08.2019 — Awesome links. Thanks. But I'm kind of understanding that changing paper type isn't something you can gain control of, except for landscape and portrait...
Copy linkTweet thisAlerts:
@VITSUSAAug 08.2019 — @wicked765#1047436 I agree with Mitya, but you should have basic knowledge about basic and Advance Java as well as proper documentation is very important to create any game which help you to go in right direction.
×

Success!

Help @wicked765 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...